Skip to content

Commit

Permalink
refactor: change deploy script output file format (#295)
Browse files Browse the repository at this point in the history
* refactor: change deploy script output file format

* chore: remove contract addresses

* build: remove contracts.json files from core package

* fix: use correct `ProdExecutor` when deploying to production env

* fix: pass correct --env argument to verify script
  • Loading branch information
dohaki authored Feb 1, 2022
1 parent 7650d07 commit b92c9db
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ node_modules
# Test Coverage
coverage*

/addresses
13 changes: 8 additions & 5 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ const lazyImport = async (module) => {
}

task("deploy", "Deploy contracts on a provided network")
.addOptionalParam("env", "Which environment is going to be used for contract deployment. Choose between prod, demo, dev or empty for local deployment", "hardhat")
.addOptionalParam("env", "(Optional) Provide additional context on which environment the contracts are deployed to: production, staging or testing", "")
.setAction( async ({env}) => {
const { deploy } = await lazyImport('./scripts/deploy')
await deploy(env);
})

task("contracts-verify", "Verify already deployed contracts. Bear in mind that at least couple of blocks should be mined before execution!")
.addOptionalParam("env", "Which environment is going to be used for contract deployment. Choose between prod, demo & dev", "dev")
.addOptionalParam("env", "(Optional) Provide additional context on which environment the contracts are deployed to: production, staging or testing", "")
.setAction(async ({env}) => {
const { verifyContracts } = await lazyImport('./scripts/verify')
await verifyContracts(env);
Expand All @@ -54,16 +54,19 @@ const config: HardhatUserConfig = {
},
rinkeby: {
url: `https://rinkeby.infura.io/v3/${INFURA_KEY}`,
accounts: ACCOUNTS
accounts: ACCOUNTS,
chainId: 4
},
ropsten: {
url: `https://ropsten.infura.io/v3/${INFURA_KEY}`,
accounts: ACCOUNTS
accounts: ACCOUNTS,
chainId: 3
},
mainnet: {
url: `https://mainnet.infura.io/v3/${INFURA_KEY}`,
accounts: ACCOUNTS,
initialBaseFeePerGas: 0
initialBaseFeePerGas: 0,
chainId: 1
},
},
etherscan: {
Expand Down
25 changes: 11 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
"artifacts/contracts/VoucherKernel.sol/VoucherKernel.json",
"abi/contracts/VoucherKernel.sol/VoucherKernel.json",
"artifacts/contracts/interfaces/**/*.json",
"!artifacts/contracts/interfaces/**/*.dbg.json",
"scripts/contracts-demo.json",
"scripts/contracts-dev.json",
"scripts/contracts-prod.json"
"!artifacts/contracts/interfaces/**/*.dbg.json"
],
"scripts": {
"contracts:run": "npm run contracts:clean && npm run contracts:compile && hardhat deploy",
Expand All @@ -49,17 +46,17 @@
"contracts:lint-fix": "solhint contracts/**/*.sol -w 0 --fix",
"contracts:format": "prettier --list-different contracts/**/*.sol",
"contracts:format-fix": "prettier --write contracts/**/*.sol",
"contracts:migrate:dev": "npm run contracts:clean && npm run contracts:compile && hardhat deploy --network rinkeby --env dev",
"contracts:migrate:dev:ropsten": "npm run contracts:clean && npm run contracts:compile && hardhat deploy --network ropsten --env dev",
"contracts:migrate:demo": "npm run contracts:clean && npm run contracts:compile && hardhat deploy --network rinkeby --env demo",
"contracts:migrate:demo:ropsten": "npm run contracts:clean && npm run contracts:compile && hardhat deploy --network ropsten --env demo",
"contracts:migrate:prod": "npm run contracts:clean && npm run contracts:compile && hardhat deploy --network mainnet --env prod",
"contracts:migrate:testing:rinkeby": "npm run contracts:clean && npm run contracts:compile && hardhat deploy --network rinkeby --env testing",
"contracts:migrate:testing": "npm run contracts:clean && npm run contracts:compile && hardhat deploy --network ropsten --env testing",
"contracts:migrate:staging:rinkeby": "npm run contracts:clean && npm run contracts:compile && hardhat deploy --network rinkeby --env staging",
"contracts:migrate:staging": "npm run contracts:clean && npm run contracts:compile && hardhat deploy --network ropsten --env staging",
"contracts:migrate:production": "npm run contracts:clean && npm run contracts:compile && hardhat deploy --network mainnet --env production",
"contracts:size": "hardhat size-contracts",
"contracts:verify:dev": "hardhat contracts-verify --network rinkeby --env dev",
"contracts:verify:dev:ropsten": "hardhat contracts-verify --network ropsten --env dev",
"contracts:verify:demo": "hardhat contracts-verify --network rinkeby --env demo",
"contracts:verify:demo:ropsten": "hardhat contracts-verify --network ropsten --env demo",
"contracts:verify:prod": "hardhat contracts-verify --network mainnet --env prod",
"contracts:verify:testing:rinkeby": "hardhat contracts-verify --network rinkeby --env testing",
"contracts:verify:testing": "hardhat contracts-verify --network ropsten --env testing",
"contracts:verify:staging:rinkeby": "hardhat contracts-verify --network rinkeby --env staging",
"contracts:verify:staging": "hardhat contracts-verify --network ropsten --env staging",
"contracts:verify:production": "hardhat contracts-verify --network mainnet --env production",
"tests:lint": "eslint test/**/*.ts testHelpers/**/*.ts",
"tests:lint-fix": "eslint --fix test/**/*.ts testHelpers/**/*.ts scripts/**/*.ts",
"tests:format": "prettier --list-different test/**/*.ts testHelpers/**/*.ts scripts/**/*.ts",
Expand Down
13 changes: 0 additions & 13 deletions scripts/contracts-demo.json

This file was deleted.

13 changes: 0 additions & 13 deletions scripts/contracts-dev.json

This file was deleted.

12 changes: 0 additions & 12 deletions scripts/contracts-hardhat.json

This file was deleted.

13 changes: 0 additions & 13 deletions scripts/contracts-prod.json

This file was deleted.

28 changes: 19 additions & 9 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import hre from 'hardhat';
import fs from 'fs';
import {isValidEnv} from './env-validator';
import {isValidEnv, addressesDirPath, getAddressesFilePath} from './utils';
import {calculateDeploymentAddresses} from '../testHelpers/contractAddress';
import {TOKEN_TYPE} from '../testHelpers/constants';
import constants from '../testHelpers/constants';
import packageFile from '../package.json';

const {TOKEN_TYPE} = constants;
const ethers = hre.ethers;

/**
Expand Down Expand Up @@ -182,7 +184,9 @@ class DeploymentExecutor {
}

async deployContracts() {
let [primaryDeployer, ccTokenDeployer] = await ethers.getSigners();
const signers = await ethers.getSigners();
let [, ccTokenDeployer] = signers;
const [primaryDeployer] = signers;

if (
process.env.PROTOCOL_DEPLOYER_PRIVATE_KEY ==
Expand Down Expand Up @@ -384,21 +388,28 @@ class DeploymentExecutor {
}

writeContracts() {
if (!fs.existsSync(addressesDirPath)) {
fs.mkdirSync(addressesDirPath);
}

fs.writeFileSync(
`scripts/contracts-${this.env.toLowerCase()}.json`,
getAddressesFilePath(hre.network.config.chainId, this.env),
JSON.stringify(
{
network: hre.network.name,
chainId: hre.network.config.chainId,
env: this.env || '',
protocolVersion: packageFile.version,
tokenRegistry: this.tokenRegistry.address,
voucherSets: this.voucherSets.address,
vouchers: this.vouchers.address,
voucherKernel: this.voucherKernel.address,
cashier: this.cashier.address,
br: this.br.address,
bosonRouter: this.br.address,
daiTokenWrapper: this.daiTokenWrapper.address,
gate: this.gate.address,
erc1155NonTransferable: this.erc1155NonTransferable.address,
daiTokenUsed: this.dai_token,
daiToken: this.dai_token,
bosonToken: this.boson_token,
},
null,
2
Expand All @@ -415,7 +426,6 @@ class DeploymentExecutor {
class ProdExecutor extends DeploymentExecutor {
constructor() {
super();
this.env = 'prod';
this.boson_token = process.env.BOSON_TOKEN;
}

Expand Down Expand Up @@ -481,7 +491,7 @@ export async function deploy(_env: string): Promise<void> {
}

const executor =
env == 'prod' ? new ProdExecutor() : new NonProdExecutor(env);
env == 'production' ? new ProdExecutor() : new NonProdExecutor(env);

await executor.deployContracts();
await executor.deployMockToken(); //only deploys mock locally
Expand Down
5 changes: 0 additions & 5 deletions scripts/env-validator.ts

This file was deleted.

19 changes: 19 additions & 0 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const addressesDirPath = __dirname + `/../addresses`;

const availableEnvironments = [
'testing',
'staging',
'production',
'hardhat',
'',
];

export function isValidEnv(env: string): boolean {
return availableEnvironments.some((e) => e == env);
}

export function getAddressesFilePath(chainId: number, env?: string): string {
return `${addressesDirPath}/${chainId}${
env ? `-${env.toLowerCase()}` : ''
}.json`;
}
13 changes: 8 additions & 5 deletions scripts/verify.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {isValidEnv} from './env-validator';
import hre from 'hardhat';
import {isValidEnv, getAddressesFilePath} from './utils';
import fs from 'fs';
import hre from 'hardhat';

export async function verifyContracts(env: string): Promise<void> {
const contracts = JSON.parse(
fs.readFileSync(`./scripts/contracts-${env}.json`, 'utf-8')
fs.readFileSync(
getAddressesFilePath(hre.network.config.chainId, env),
'utf-8'
)
);

if (contracts.network != hre.network.name) {
if (contracts.chainId != hre.network.config.chainId) {
throw new Error(
'Contracts are not deployed on the same network, that you are trying to verify!'
);
Expand Down Expand Up @@ -89,7 +92,7 @@ export async function verifyContracts(env: string): Promise<void> {
//verify BosonRouter
try {
await hre.run('verify:verify', {
address: contracts.br,
address: contracts.bosonRouter,
constructorArguments: [
contracts.voucherKernel,
contracts.tokenRegistry,
Expand Down

0 comments on commit b92c9db

Please sign in to comment.