Skip to content

Commit

Permalink
feat: hardhat-deploy (#797)
Browse files Browse the repository at this point in the history
* feat(hardhatdeploy): poc hardhat-deploy

integrate hardhat-deploy plugin to deploy test contract

* feat(hardhat-deploy): integrate hardhat-deploy plugin

deploy all Razor contracts using hardhat-deploy, update deploy.sh script

* fix(hardhat-deploy): integrate hardhat-deploy plugin

hotfix remove test contract

* fix(hardhat-deploy): integrate hardhat-deploy plugin

hotfix remove unnecessary changes

* fix(hardhat-deploy): integrate hardhat-deploy plugin

hotfix remove unnecessary changes

* fix(hardhat-deploy): fix current post deployment

wait for contract deployment file to updated

* fix(hardhat-deploy): migrate contracts setup

create shared folder for contracts initialisation and grant role

* feat(hardhat-deploy): post deployment test

add post deployment script test, 1 staker to vote for 1 epoch

* feat(hardhat-deploy): clean up

hotfix

* fix: lint fix and yarn.lock

* feat(hardhat-deploy): add deployment fixtures for tests

* fix(hardhat-deploy): fix circleci errors

* feat(hardhat-deploy): remove postdeployment test and deployment fixtures. Release hardhat-deploy

* feat(hardhat-deploy): use common deploy func, code modularisation

* feat(hardhat-deploy): circleci hotfix

* fix: clean up functions,rename deploy_all

* fix: postdeployment setup code clean up and fixes for failing init contract + same contract instance

* fix: update grant role to promise

* fix: add deploy readme

* fix: move to root folder

* fix: update content
  • Loading branch information
yohanelly95 committed May 25, 2022
1 parent 25d0cd1 commit bf8c93d
Show file tree
Hide file tree
Showing 22 changed files with 2,122 additions and 1,779 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ deployed/local
deployments
gasReporterOutput.json
.vscode/
docs
docs
package-lock.json
73 changes: 73 additions & 0 deletions DEPLOY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## hardhat-deploy in a nutshell

**hardhat-deploy** allows you to write `deploy scripts` in the `deploy` folder. Each of these files that look as follows will be executed in turn when you execute the following task: `hardhat --network <networkName> deploy`

Note: `hre.deployments.deploy` function will by default only deploy a contract if the contract code has changed, making it easier to write idempotent scripts.

### An example of a deploy script :

```
module.exports = async ({
getNamedAccounts,
deployments,
getChainId,
getUnnamedAccounts,
}) => {
const {deploy} = deployments;
const {deployer} = await getNamedAccounts();
// the following will only deploy "GenericMetaTxProcessor" if the contract was never deployed or if the code changed since last deployment
await deploy('GenericMetaTxProcessor', {
from: deployer,
gasLimit: 4000000,
args: [],
});
};
```

As you can see the HRE passed in has 4 new fields :

- `getNamedAccounts` is a function that returns a promise to an object whose keys are names and values are addresses. It is parsed from the `namedAccounts` configuration.
- `getUnnamedAccounts` is a function that return a promise to an array of accounts which were not named (see `namedAccounts` ). It is useful for tests where you want to be sure that the account has no speicifc role in the system (no token given, no admin access, etc...).
- `getChainId` is a function which return a promise for the chainId, as convenience
- `deployments` is an object which contains functions to access past deployments or to save new ones, as well as helpers functions.

`deployments` contains for example the `deploy` function that allows you to deploy contract and save them.

### **1. namedAccounts (ability to name addresses)**

---

This plugin extends the `HardhatConfig`'s object with an optional `namedAccounts` field.

`namedAccounts` allows you to associate names to addresses and have them configured per chain.
This allows you to have meaningful names in your tests.

```
{
namedAccounts: {
deployer: {
default: 0, // here this will by default take the first account as deployer
1: 0, // similarly on mainnet it will take the first account as deployer. Note though that depending on how hardhat network are configured, the account 0 on one network can be different than on another
4: '0xA296a3d5F026953e17F472B497eC29a5631FB51B', // but for rinkeby it will be a specific address
"goerli": '0x84b9514E013710b9dD0811c9Fe46b837a4A0d8E0', //it can also specify a specific netwotk name (specified in hardhat.config.js)
},
feeCollector:{
default: 1, // here this will by default take the second account as feeCollector (so in the test this will be a different account than the deployer)
1: '0xa5610E1f289DbDe94F3428A9df22E8B518f65751', // on the mainnet the feeCollector could be a multi sig
4: '0xa250ac77360d4e837a13628bC828a2aDf7BabfB3', // on rinkeby it could be another account
}
}
}
```

---

Furthermore you can also ensure these scripts are executed in test’s too by calling `await deployments.fixture(['MyContract'])` in your test.
This is optimised, so if multiple tests use the same contract, the deployment will be executed once and each test will start with the exact same state.

This is a huge benefit for testing since you are not required to replicate the deployment procedure in your tests.

You can even group deploy scripts in different sub folder and ensure they are executed in their logical order.
10 changes: 10 additions & 0 deletions deploy/001_deploy_governance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { deployContractHH } = require('../migrations/migrationHelpers');

const deployGovernance = async () => {
await deployContractHH('Governance');
};

module.exports = async () => {
await deployGovernance();
};
module.exports.tags = ['Governance'];
10 changes: 10 additions & 0 deletions deploy/002_deploy_block_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { deployContractHH } = require('../migrations/migrationHelpers');

const deployBlockManager = async () => {
await deployContractHH('BlockManager');
};

module.exports = async () => {
await deployBlockManager();
};
module.exports.tags = ['BlockManager'];
10 changes: 10 additions & 0 deletions deploy/003_deploy_collection_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { deployContractHH } = require('../migrations/migrationHelpers');

const deployCollectionManager = async () => {
await deployContractHH('CollectionManager');
};

module.exports = async () => {
await deployCollectionManager();
};
module.exports.tags = ['CollectionManager'];
10 changes: 10 additions & 0 deletions deploy/004_deploy_stake_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { deployContractHH } = require('../migrations/migrationHelpers');

const deployStakeManager = async () => {
await deployContractHH('StakeManager');
};

module.exports = async () => {
await deployStakeManager();
};
module.exports.tags = ['StakeManager'];
10 changes: 10 additions & 0 deletions deploy/005_deploy_reward_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { deployContractHH } = require('../migrations/migrationHelpers');

const deployRewardManager = async () => {
await deployContractHH('RewardManager');
};

module.exports = async () => {
await deployRewardManager();
};
module.exports.tags = ['RewardManager'];
10 changes: 10 additions & 0 deletions deploy/006_deploy_vote_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { deployContractHH } = require('../migrations/migrationHelpers');

const deployVoteManager = async () => {
await deployContractHH('VoteManager');
};

module.exports = async () => {
await deployVoteManager();
};
module.exports.tags = ['VoteManager'];
23 changes: 23 additions & 0 deletions deploy/007_deploy_delegator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { deployContractHH, readOldDeploymentFile, appendDeploymentFile } = require('../migrations/migrationHelpers');

const { DELEGATOR_ADDRESS } = process.env;

const deployDelegator = async () => {
if (DELEGATOR_ADDRESS === '' || !DELEGATOR_ADDRESS) {
await deployContractHH('Delegator');
} else {
const { Delegator } = await readOldDeploymentFile();

if (DELEGATOR_ADDRESS !== Delegator) {
throw Error('Delegator instance address is different than what was deployed previously');
}
// eslint-disable-next-line no-console
console.log('Re-using Delegator instance deployed at', Delegator);
await appendDeploymentFile({ Delegator });
}
};

module.exports = async () => {
await deployDelegator();
};
module.exports.tags = ['Delegator'];
26 changes: 26 additions & 0 deletions deploy/008_deploy_razor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { deployContractHH, readOldDeploymentFile, appendDeploymentFile } = require('../migrations/migrationHelpers');

const { NETWORK, RAZOR_ADDRESS } = process.env;
const { BigNumber } = ethers;
const initialSupply = (BigNumber.from(10).pow(BigNumber.from(27)));

const deployRAZOR = async () => {
if (NETWORK !== 'mainnet' && RAZOR_ADDRESS === '') {
await deployContractHH('RAZOR', [initialSupply]);
} else {
const { RAZOR } = await readOldDeploymentFile();

if (RAZOR !== RAZOR_ADDRESS) {
throw Error('Razor instance address is different than that is deployed previously');
}

// eslint-disable-next-line no-console
console.log('Re-using Razor instance deployed at', RAZOR);
await appendDeploymentFile({ RAZOR });
}
};

module.exports = async () => {
await deployRAZOR();
};
module.exports.tags = ['RAZOR'];
10 changes: 10 additions & 0 deletions deploy/009_deploy_staked_token_factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { deployContractHH } = require('../migrations/migrationHelpers');

const deployStakedTokenFactory = async () => {
await deployContractHH('StakedTokenFactory');
};

module.exports = async () => {
await deployStakedTokenFactory();
};
module.exports.tags = ['StakedTokenFactory'];
10 changes: 10 additions & 0 deletions deploy/010_deploy_random_no_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { deployContractHH } = require('../migrations/migrationHelpers');

const deployRandomNoManager = async () => {
await deployContractHH('RandomNoManager');
};

module.exports = async () => {
await deployRandomNoManager();
};
module.exports.tags = ['RandomNoManager'];
12 changes: 0 additions & 12 deletions deployed/mumbai/addresses.json

This file was deleted.

5 changes: 3 additions & 2 deletions docker/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ fi

cp .env.$ENV .env

npm run compile
npx hardhat run migrations/deploy_all.js --network $ENV
echo "Deploying contracts on network $ENV"
npx hardhat --network $ENV deploy
npx hardhat run migrations/postDeployment.js --network $ENV

mkdir -p deployed/$ENV
cp -r artifacts deployed/$ENV/abi
Expand Down
8 changes: 8 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-undef */
const dotenv = require('dotenv');
require('hardhat-deploy');

const dotenvResult = dotenv.config();

Expand Down Expand Up @@ -109,4 +110,11 @@ module.exports = {
dodoc: {
runOnCompile: false,
},
namedAccounts: {
deployer: 0,
tokenOwner: 1,
},
paths: {
sources: './contracts',
},
};
34 changes: 0 additions & 34 deletions migrations/deploy_all.js

This file was deleted.

Loading

0 comments on commit bf8c93d

Please sign in to comment.