Skip to content

Commit

Permalink
Merge pull request #23 from bobanetwork/deploy
Browse files Browse the repository at this point in the history
improve contract deploy
  • Loading branch information
kitounliu committed Aug 26, 2024
2 parents b5ae0f0 + 21409c8 commit eddddb3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .env.example.deploy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ ADMIN_ADDRESS=<admin account 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
Expand Down
92 changes: 79 additions & 13 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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 ?? ''
Expand All @@ -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
}

Expand Down

0 comments on commit eddddb3

Please sign in to comment.