From 18da1306b9bdc10535fd2dba38d5dbe5dfd24494 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Fri, 15 Jul 2022 13:18:15 +0200 Subject: [PATCH 1/2] Refactor deployment script, add error handling --- scripts/create-proxy.js | 124 ++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 61 deletions(-) diff --git a/scripts/create-proxy.js b/scripts/create-proxy.js index f4ea114..10bff88 100644 --- a/scripts/create-proxy.js +++ b/scripts/create-proxy.js @@ -1,72 +1,74 @@ -const { ethers, upgrades } = require("hardhat"); -const path = require("path"); -const fileInject = require("./helpers/file_inject.js"); +const { ethers, upgrades } = require("hardhat"); const path = require("path"); const fileInject = require("./helpers/file_inject.js"); + +function handleError(error) { + console.error(error.message); + process.exit(1); +} async function main() { const network = await hre.ethers.provider.getNetwork(); const networkId = network.chainId; console.log(`Deploying to network #${networkId}`); - const Contributor = await ethers.getContractFactory("Contributor"); - const Contribution = await ethers.getContractFactory("Contribution"); - const Token = await ethers.getContractFactory("Token"); - const Reimbursement = await ethers.getContractFactory("Reimbursement"); - - const contributor = await upgrades.deployProxy(Contributor, []); - await contributor.deployed(); - console.log("Contributor deployed to:", contributor.address); - console.log("...waiting for 1 confirmation"); - await contributor.deployTransaction.wait(); - - const blocksToWait = 40320; // 7 days; 15 seconds block time - const contribution = await upgrades.deployProxy(Contribution, [blocksToWait]); - await contribution.deployed(); - console.log("Contribution deployed to:", contribution.address); - console.log("...waiting for 1 confirmation"); - await contribution.deployTransaction.wait(); - - const token = await upgrades.deployProxy(Token, []); - await token.deployed(); - console.log("Token deployed to:", token.address); - console.log("...waiting for 1 confirmation"); - await token.deployTransaction.wait(); - - const reimbursement = await upgrades.deployProxy(Reimbursement, []); - await reimbursement.deployed(); - console.log("Reimbursement deployed to:", reimbursement.address); - console.log("...waiting for 1 confirmation"); - await reimbursement.deployTransaction.wait(); - - await contributor - .setTokenContract(token.address) - .then((response) => response.wait()); - await contributor - .setContributionContract(contribution.address) - .then((response) => response.wait()); - - await contribution - .setTokenContract(token.address) - .then((response) => response.wait()); - await contribution - .setContributorContract(contributor.address) - .then((response) => response.wait()); - - await token - .setContributionContract(contribution.address) - .then((response) => response.wait()); - await token - .setContributorContract(contributor.address) - .then((response) => response.wait()); - - await reimbursement - .setContributorContract(contributor.address) - .then((response) => response.wait()); + const contractFactories = {}; + const contracts = {}; + + contractFactories.Contributor = await ethers.getContractFactory("Contributor"); + contractFactories.Contribution = await ethers.getContractFactory("Contribution"); + contractFactories.Token = await ethers.getContractFactory("Token"); + contractFactories.Reimbursement = await ethers.getContractFactory("Reimbursement"); + + async function deployContractProxy (contractName, params=[]) { + let contract = await upgrades.deployProxy(contractFactories[contractName], params) + .catch(handleError); + + contracts[contractName.toLowerCase()] = contract; + + await contract.deployed().then(() => { + console.log(`${contractName} deployed to:`, contract.address); + console.log("...waiting for 1 confirmation"); + }).catch(handleError); + + await contract.deployTransaction.wait().catch(handleError); + } + + const blocksVetoPeriod = 40320; // 7 days; 15 seconds block time + + await deployContractProxy('Contributor'); + await deployContractProxy('Contribution', [ blocksVetoPeriod ]); + await deployContractProxy('Token'); + await deployContractProxy('Reimbursement'); + + await contracts.Contributor + .setTokenContract(contracts.Token.address) + .then(res => res.wait()).catch(handleError); + await contracts.Contributor + .setContributionContract(contracts.Contribution.address) + .then(res => res.wait()).catch(handleError); + + await contracts.Contribution + .setTokenContract(contracts.Token.address) + .then(res => res.wait()).catch(handleError); + await contracts.Contribution + .setContributorContract(contracts.Contributor.address) + .then(res => res.wait()).catch(handleError); + + await contracts.Token + .setContributionContract(contracts.Contribution.address) + .then(res => res.wait()).catch(handleError); + await contracts.Token + .setContributorContract(contracts.Contributor.address) + .then(res => res.wait()).catch(handleError); + + await contracts.Reimbursement + .setContributorContract(contracts.Contributor.address) + .then(res => res.wait()).catch(handleError); const addresses = { - Contributor: contributor.address, - Contribution: contribution.address, - Token: token.address, - Reimbursement: reimbursement.address, + Contributor: contracts.Contributor.address, + Contribution: contracts.Contribution.address, + Token: contracts.Token.address, + Reimbursement: contracts.Reimbursement.address, }; console.log("Writing addresses.json"); From 12326ce73f6ff452d624954ba6259eea55efa610 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Fri, 15 Jul 2022 18:19:06 +0200 Subject: [PATCH 2/2] Fix typo and add more logs --- scripts/create-proxy.js | 63 +++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/scripts/create-proxy.js b/scripts/create-proxy.js index 10bff88..7aa40d7 100644 --- a/scripts/create-proxy.js +++ b/scripts/create-proxy.js @@ -22,7 +22,7 @@ async function main() { let contract = await upgrades.deployProxy(contractFactories[contractName], params) .catch(handleError); - contracts[contractName.toLowerCase()] = contract; + contracts[contractName] = contract; await contract.deployed().then(() => { console.log(`${contractName} deployed to:`, contract.address); @@ -39,30 +39,63 @@ async function main() { await deployContractProxy('Token'); await deployContractProxy('Reimbursement'); - await contracts.Contributor + console.log('Calling Contributor#setTokenContract') + await contracts.Contributor.functions .setTokenContract(contracts.Token.address) - .then(res => res.wait()).catch(handleError); - await contracts.Contributor + .then(res => { + console.log(`...transaction published: ${res.hash}`); + return res.wait(); + }).catch(handleError); + + console.log('Calling Contributor#setContributionContract') + await contracts.Contributor.functions .setContributionContract(contracts.Contribution.address) - .then(res => res.wait()).catch(handleError); + .then(res => { + console.log(`...transaction published: ${res.hash}`); + return res.wait(); + }).catch(handleError); - await contracts.Contribution + + console.log('Calling Contribution#setTokenContract') + await contracts.Contribution.functions .setTokenContract(contracts.Token.address) - .then(res => res.wait()).catch(handleError); - await contracts.Contribution + .then(res => { + console.log(`...transaction published: ${res.hash}`); + return res.wait(); + }).catch(handleError); + + + console.log('Calling Contribution#setContributorContract') + await contracts.Contribution.functions .setContributorContract(contracts.Contributor.address) - .then(res => res.wait()).catch(handleError); + .then(res => { + console.log(`...transaction published: ${res.hash}`); + return res.wait(); + }).catch(handleError); - await contracts.Token + console.log('Calling Token#setContributionContract') + await contracts.Token.functions .setContributionContract(contracts.Contribution.address) - .then(res => res.wait()).catch(handleError); - await contracts.Token + .then(res => { + console.log(`...transaction published: ${res.hash}`); + return res.wait(); + }).catch(handleError); + + console.log('Calling Token#setContributorContract') + await contracts.Token.functions .setContributorContract(contracts.Contributor.address) - .then(res => res.wait()).catch(handleError); + .then(res => { + console.log(`...transaction published: ${res.hash}`); + return res.wait(); + }).catch(handleError); - await contracts.Reimbursement + console.log('Calling Reimbursement#setContributorContract') + await contracts.Reimbursement.functions .setContributorContract(contracts.Contributor.address) - .then(res => res.wait()).catch(handleError); + .then(res => { + console.log(`...transaction published: ${res.hash}`); + return res.wait(); + }).catch(handleError); const addresses = { Contributor: contracts.Contributor.address,