From 21409c8badb80c73591691d43bbb4be8101fab2a Mon Sep 17 00:00:00 2001 From: Jia Liu Date: Mon, 19 Aug 2024 15:34:29 +0100 Subject: [PATCH] improve contract deploy --- .env.example.deploy | 3 +- scripts/deploy.ts | 92 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 81 insertions(+), 14 deletions(-) diff --git a/.env.example.deploy b/.env.example.deploy index 1075b32..6a241af 100644 --- a/.env.example.deploy +++ b/.env.example.deploy @@ -4,7 +4,8 @@ ADMIN_ADDRESS= THRESHOLD=3 NUMBER_OF_MEMBERS=5 DEGREE=18 -DEPLOY_NO_HELPERS=false # optional +# Deploy all contracts or just some of them. If set to true, it will deploy with some existing contracts provided below; if any contract address is not provided or invalid, it will redeploy the contract +DEPLOY_PARTIAL=false # optional HALO2VK=0x63311f167b6B07fd0D3d83310c16512701B4Cb2d # optional HALO2V=0x26Aa5a7c4CA7D0F81943ea9CbDf97D80c560D6Fa # optional GPP=0xbB9a8f4c3662b6EF4b512E5f358289d1Db63fc81 # optional diff --git a/scripts/deploy.ts b/scripts/deploy.ts index df33420..697f09e 100644 --- a/scripts/deploy.ts +++ b/scripts/deploy.ts @@ -1,6 +1,30 @@ import hre, {artifacts, ethers} from "hardhat"; import {Contract, ContractFactory, providers, utils, Wallet} from "ethers"; +async function isContractDeployed( + contractAddress: string, + provider: ethers.providers.Provider +): Promise { + let isDeployed = false; + + try { + // Attempt to get the bytecode of the contract + const bytecode = await provider.getCode(contractAddress); + + // If the bytecode is '0x', then the contract is not deployed + if (bytecode !== "0x") { + isDeployed = true; + } + } catch (error) { + // Log the error for debugging purposes + console.warn("Error fetching contract code:", error); + // Set isDeployed to false if there is an error + isDeployed = false; + } + + return isDeployed; +} + async function main() { const netprovider = new providers.JsonRpcProvider(process.env.L2_NODE_WEB3_URL) const accPrivateKey = process.env.DEPLOYER_PRIVATE_KEY ?? '' @@ -10,46 +34,88 @@ async function main() { const numberOfMembers = process.env.NUMBER_OF_MEMBERS const degree = process.env.DEGREE const minDeposit = process.env.MIN_DEPOSIT ?? '0' - const deployNoHelpers = process.env.DEPLOY_NO_HELPERS === 'true' + const deployPartial = process.env.DEPLOY_PARTIAL === 'true' - let halo2VerifierAddress let halo2VerifyingKeyAddress + let halo2VerifierAddress let globalPublicParamsAddress let pseudoRandAddress - if (deployNoHelpers) { - halo2VerifierAddress = process.env.HALO2V + if (deployPartial) { + // use contracts that are already deployed + // if any contract address is not provided, the contract will be re-deployed halo2VerifyingKeyAddress = process.env.HALO2VK + halo2VerifierAddress = process.env.HALO2V globalPublicParamsAddress = process.env.GPP pseudoRandAddress = process.env.PSRAND - } else { + + // check if contracts are deployed; if not, the contracts will be re-deployed + if (halo2VerifyingKeyAddress) { + const isDeployed = await isContractDeployed(halo2VerifyingKeyAddress, netprovider) + if (!isDeployed) { + console.warn("Halo2VerifyingKey contract address provided but contract is not deployed or invalid."); + halo2VerifyingKeyAddress = null + } + } + + if (halo2VerifierAddress) { + const isDeployed = await isContractDeployed(halo2VerifierAddress, netprovider) + if (!isDeployed) { + console.warn("Halo2Verifier contract address provided but contract is not deployed or invalid."); + halo2VerifierAddress = null + } + } + + if (globalPublicParamsAddress) { + const isDeployed = await isContractDeployed(globalPublicParamsAddress, netprovider) + if (!isDeployed) { + console.warn("GlobalPublicParams contract address provided but contract is not deployed or invalid."); + globalPublicParamsAddress = null + } + } + + if (pseudoRandAddress) { + const isDeployed = await isContractDeployed(pseudoRandAddress, netprovider) + if (!isDeployed) { + console.warn("PseudoRand contract address provided but contract is not deployed or invalid."); + pseudoRandAddress = null + } + } + } + + if (!halo2VerifyingKeyAddress) { const Halo2VerifyingKey = await ethers.getContractFactory(`contracts/Halo2VerifyingKey-${threshold}-${numberOfMembers}-${degree}-g2.sol:Halo2VerifyingKey`) const halo2VerifyingKey = await Halo2VerifyingKey.connect(deployerWallet).deploy() await halo2VerifyingKey.deployed() - console.log("Halo2VerifyingKey deployed at", halo2VerifyingKey.address) + console.log("Halo2VerifyingKey (HALO2VK) deployed at", halo2VerifyingKey.address) + halo2VerifyingKeyAddress = halo2VerifyingKey.address + } + if (!halo2VerifierAddress) { const Halo2Verifier = await ethers.getContractFactory('contracts/Halo2Verifier.sol:Halo2Verifier') const halo2Verifier = await Halo2Verifier.connect(deployerWallet).deploy() await halo2Verifier.deployed() - console.log("Halo2Verifier deployed at", halo2Verifier.address) + console.log("Halo2Verifier (HALO2V) deployed at", halo2Verifier.address) + halo2VerifierAddress = halo2Verifier.address + } + if (!globalPublicParamsAddress) { const GlobalPublicParams = await ethers.getContractFactory('GlobalPublicParams') const globalPublicParams = await GlobalPublicParams.connect(deployerWallet).deploy() await globalPublicParams.deployed() - console.log("GlobalPublicParams deployed at", globalPublicParams.address) + console.log("GlobalPublicParams (GPP) deployed at", globalPublicParams.address) + globalPublicParamsAddress = globalPublicParams.address + } + if (!pseudoRandAddress) { const PseudoRand = await ethers.getContractFactory('PseudoRand') const pseudoRand = await PseudoRand.connect(deployerWallet).deploy() await pseudoRand.deployed() - console.log("PseudoRand deployed at", pseudoRand.address) - - halo2VerifyingKeyAddress = halo2VerifyingKey.address - halo2VerifierAddress = halo2Verifier.address - globalPublicParamsAddress = globalPublicParams.address + console.log("PseudoRand (PSRAND) deployed at", pseudoRand.address) pseudoRandAddress = pseudoRand.address }