From 82bb87c5c91349fb40c80b720c590c8ae8e9012b Mon Sep 17 00:00:00 2001 From: prateek105 Date: Fri, 4 Aug 2023 14:00:17 +0530 Subject: [PATCH 1/4] Add deployer contract to deploy grantfund, fund treasury and startNewDistribution --- src/grants/Deployer.sol | 26 ++++++++++++++++++++++++++ test/unit/Deployer.t.sol | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/grants/Deployer.sol create mode 100644 test/unit/Deployer.t.sol diff --git a/src/grants/Deployer.sol b/src/grants/Deployer.sol new file mode 100644 index 0000000..891c06f --- /dev/null +++ b/src/grants/Deployer.sol @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: MIT + +pragma solidity 0.8.18; + +import { IERC20 } from "@oz/token/ERC20/IERC20.sol"; + +import { GrantFund } from "./GrantFund.sol"; + +contract Deployer { + + GrantFund public grantFund; + + function deployGrantFund(address ajnaToken_, uint256 treasury_) public returns (GrantFund) { + + IERC20(ajnaToken_).transferFrom(msg.sender, address(this), treasury_); + + grantFund = new GrantFund(ajnaToken_); + + IERC20(ajnaToken_).approve(address(grantFund), treasury_); + + grantFund.fundTreasury(treasury_); + + grantFund.startNewDistributionPeriod(); + return grantFund; + } +} \ No newline at end of file diff --git a/test/unit/Deployer.t.sol b/test/unit/Deployer.t.sol new file mode 100644 index 0000000..346e70c --- /dev/null +++ b/test/unit/Deployer.t.sol @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.18; + +import { Test } from "@std/Test.sol"; + +import { Deployer } from "../../src/grants/Deployer.sol"; +import { GrantFund } from "../../src/grants/GrantFund.sol"; +import { TestAjnaToken } from "../utils/harness/TestAjnaToken.sol"; + +contract DeployerTest is Test { + + function testGrantFundDeployment() external { + address owner = makeAddr("owner"); + vm.startPrank(owner); + + uint256 treasury = 50_000_000 * 1e18; + + TestAjnaToken ajnaToken = new TestAjnaToken(); + ajnaToken.mint(owner, treasury); + + Deployer deployer = new Deployer(); + ajnaToken.approve(address(deployer), treasury); + + GrantFund grantFund = deployer.deployGrantFund(address(ajnaToken), treasury); + + assertEq(grantFund.getDistributionId(), 1); + + (,,,uint256 fundAvailable,,) = grantFund.getDistributionPeriodInfo(1); + + assertEq(grantFund.treasury(), treasury - fundAvailable); + } +} \ No newline at end of file From 9732ea2db5d52b8cddc6e3e5448ad2a04ead4d51 Mon Sep 17 00:00:00 2001 From: prateek105 Date: Mon, 7 Aug 2023 18:59:21 +0530 Subject: [PATCH 2/4] Add readme for deployer contract --- Makefile | 2 ++ README.md | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Makefile b/Makefile index dd21549..f43697c 100644 --- a/Makefile +++ b/Makefile @@ -34,3 +34,5 @@ deploy-grantfund: eval AJNA_TOKEN=${ajna} forge script script/GrantFund.s.sol:DeployGrantFund \ --rpc-url ${ETH_RPC_URL} --sender ${DEPLOY_ADDRESS} --keystore ${DEPLOY_KEY} --broadcast -vvv --verify +deploy-grantfund-deployer: + forge create --rpc-url ${ETH_RPC_URL} --sender ${DEPLOY_ADDRESS} --keystore ${DEPLOY_KEY} --verify src/grants/Deployer.sol:Deployer diff --git a/README.md b/README.md index b8b1d0b..94c84c9 100644 --- a/README.md +++ b/README.md @@ -77,3 +77,17 @@ make deploy-grantfund ajna= ``` See [GRANT_FUND.md](src/grants/GRANT_FUND.md#deployment) for next steps. + +#### Grant Fund deployer +Deployer contract can be used to deploy grant fund, fund treasury and start distribution in a single call to avoid someone starting a distribution without treasury. + +Steps to use Deployer contract to deploy grant Fund: +1. Deploy `Deployer` contract. +2. Approve `` `AJNA ` to `Deployer` contract from `treasury` address. +3. Call `deployGrantFund(address ajnaToken_, uint256 treasury_)` from `treasury` address to deploy grant fund and start distribution with treasury amount. +4. GrantFund can be verified using remix contract verification plugin, foundry or hardhat. + +To deploy Deployer contract, run: +``` +make deploy-grantfund-deployer +``` From d50335b997d90ad87f8b49378463e319d724628d Mon Sep 17 00:00:00 2001 From: prateek105 Date: Wed, 9 Aug 2023 19:34:15 +0530 Subject: [PATCH 3/4] PR feedback --- src/grants/Deployer.sol | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/grants/Deployer.sol b/src/grants/Deployer.sol index 891c06f..796c011 100644 --- a/src/grants/Deployer.sol +++ b/src/grants/Deployer.sol @@ -8,19 +8,33 @@ import { GrantFund } from "./GrantFund.sol"; contract Deployer { + error IncorrectTreasuryBalance(); + + error DistributionNotStarted(); + GrantFund public grantFund; - function deployGrantFund(address ajnaToken_, uint256 treasury_) public returns (GrantFund) { + function deployGrantFund(address ajnaToken_, uint256 treasury_) public returns (GrantFund grantFund_) { + + // deploy grant Fund + grantFund_ = new GrantFund(ajnaToken_); + + // Approve ajna token to fund treasury + IERC20(ajnaToken_).approve(address(grantFund_), treasury_); + // Transfer treasury ajna tokens to Deployer contract IERC20(ajnaToken_).transferFrom(msg.sender, address(this), treasury_); - grantFund = new GrantFund(ajnaToken_); + // Fund treasury and start new distribution + grantFund_.fundTreasury(treasury_); + grantFund_.startNewDistributionPeriod(); - IERC20(ajnaToken_).approve(address(grantFund), treasury_); + // check treasury balance is correct + if(IERC20(ajnaToken_).balanceOf(address(grantFund_)) != treasury_) revert IncorrectTreasuryBalance(); - grantFund.fundTreasury(treasury_); + // check new distribution started + if(grantFund_.getDistributionId() != 1) revert DistributionNotStarted(); - grantFund.startNewDistributionPeriod(); - return grantFund; + grantFund = grantFund_; } } \ No newline at end of file From 92880a9bc73e48402a279719fda0152e324565c7 Mon Sep 17 00:00:00 2001 From: prateek105 Date: Fri, 18 Aug 2023 14:41:37 +0530 Subject: [PATCH 4/4] PR feedback --- test/unit/Deployer.t.sol | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/unit/Deployer.t.sol b/test/unit/Deployer.t.sol index 346e70c..6d0cd6f 100644 --- a/test/unit/Deployer.t.sol +++ b/test/unit/Deployer.t.sol @@ -28,5 +28,7 @@ contract DeployerTest is Test { (,,,uint256 fundAvailable,,) = grantFund.getDistributionPeriodInfo(1); assertEq(grantFund.treasury(), treasury - fundAvailable); + + assertEq(fundAvailable, treasury * 3 / 100); } } \ No newline at end of file