Skip to content

Commit eddddb3

Browse files
authored
Merge pull request #23 from bobanetwork/deploy
improve contract deploy
2 parents b5ae0f0 + 21409c8 commit eddddb3

File tree

2 files changed

+81
-14
lines changed

2 files changed

+81
-14
lines changed

.env.example.deploy

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ ADMIN_ADDRESS=<admin account address>
44
THRESHOLD=3
55
NUMBER_OF_MEMBERS=5
66
DEGREE=18
7-
DEPLOY_NO_HELPERS=false # optional
7+
# 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
8+
DEPLOY_PARTIAL=false # optional
89
HALO2VK=0x63311f167b6B07fd0D3d83310c16512701B4Cb2d # optional
910
HALO2V=0x26Aa5a7c4CA7D0F81943ea9CbDf97D80c560D6Fa # optional
1011
GPP=0xbB9a8f4c3662b6EF4b512E5f358289d1Db63fc81 # optional

scripts/deploy.ts

+79-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
import hre, {artifacts, ethers} from "hardhat";
22
import {Contract, ContractFactory, providers, utils, Wallet} from "ethers";
33

4+
async function isContractDeployed(
5+
contractAddress: string,
6+
provider: ethers.providers.Provider
7+
): Promise<boolean> {
8+
let isDeployed = false;
9+
10+
try {
11+
// Attempt to get the bytecode of the contract
12+
const bytecode = await provider.getCode(contractAddress);
13+
14+
// If the bytecode is '0x', then the contract is not deployed
15+
if (bytecode !== "0x") {
16+
isDeployed = true;
17+
}
18+
} catch (error) {
19+
// Log the error for debugging purposes
20+
console.warn("Error fetching contract code:", error);
21+
// Set isDeployed to false if there is an error
22+
isDeployed = false;
23+
}
24+
25+
return isDeployed;
26+
}
27+
428
async function main() {
529
const netprovider = new providers.JsonRpcProvider(process.env.L2_NODE_WEB3_URL)
630
const accPrivateKey = process.env.DEPLOYER_PRIVATE_KEY ?? ''
@@ -10,46 +34,88 @@ async function main() {
1034
const numberOfMembers = process.env.NUMBER_OF_MEMBERS
1135
const degree = process.env.DEGREE
1236
const minDeposit = process.env.MIN_DEPOSIT ?? '0'
13-
const deployNoHelpers = process.env.DEPLOY_NO_HELPERS === 'true'
37+
const deployPartial = process.env.DEPLOY_PARTIAL === 'true'
1438

15-
let halo2VerifierAddress
1639
let halo2VerifyingKeyAddress
40+
let halo2VerifierAddress
1741
let globalPublicParamsAddress
1842
let pseudoRandAddress
1943

20-
if (deployNoHelpers) {
21-
halo2VerifierAddress = process.env.HALO2V
44+
if (deployPartial) {
45+
// use contracts that are already deployed
46+
// if any contract address is not provided, the contract will be re-deployed
2247
halo2VerifyingKeyAddress = process.env.HALO2VK
48+
halo2VerifierAddress = process.env.HALO2V
2349
globalPublicParamsAddress = process.env.GPP
2450
pseudoRandAddress = process.env.PSRAND
25-
} else {
51+
52+
// check if contracts are deployed; if not, the contracts will be re-deployed
53+
if (halo2VerifyingKeyAddress) {
54+
const isDeployed = await isContractDeployed(halo2VerifyingKeyAddress, netprovider)
55+
if (!isDeployed) {
56+
console.warn("Halo2VerifyingKey contract address provided but contract is not deployed or invalid.");
57+
halo2VerifyingKeyAddress = null
58+
}
59+
}
60+
61+
if (halo2VerifierAddress) {
62+
const isDeployed = await isContractDeployed(halo2VerifierAddress, netprovider)
63+
if (!isDeployed) {
64+
console.warn("Halo2Verifier contract address provided but contract is not deployed or invalid.");
65+
halo2VerifierAddress = null
66+
}
67+
}
68+
69+
if (globalPublicParamsAddress) {
70+
const isDeployed = await isContractDeployed(globalPublicParamsAddress, netprovider)
71+
if (!isDeployed) {
72+
console.warn("GlobalPublicParams contract address provided but contract is not deployed or invalid.");
73+
globalPublicParamsAddress = null
74+
}
75+
}
76+
77+
if (pseudoRandAddress) {
78+
const isDeployed = await isContractDeployed(pseudoRandAddress, netprovider)
79+
if (!isDeployed) {
80+
console.warn("PseudoRand contract address provided but contract is not deployed or invalid.");
81+
pseudoRandAddress = null
82+
}
83+
}
84+
}
85+
86+
if (!halo2VerifyingKeyAddress) {
2687
const Halo2VerifyingKey = await ethers.getContractFactory(`contracts/Halo2VerifyingKey-${threshold}-${numberOfMembers}-${degree}-g2.sol:Halo2VerifyingKey`)
2788
const halo2VerifyingKey = await Halo2VerifyingKey.connect(deployerWallet).deploy()
2889
await halo2VerifyingKey.deployed()
2990

30-
console.log("Halo2VerifyingKey deployed at", halo2VerifyingKey.address)
91+
console.log("Halo2VerifyingKey (HALO2VK) deployed at", halo2VerifyingKey.address)
92+
halo2VerifyingKeyAddress = halo2VerifyingKey.address
93+
}
3194

95+
if (!halo2VerifierAddress) {
3296
const Halo2Verifier = await ethers.getContractFactory('contracts/Halo2Verifier.sol:Halo2Verifier')
3397
const halo2Verifier = await Halo2Verifier.connect(deployerWallet).deploy()
3498
await halo2Verifier.deployed()
3599

36-
console.log("Halo2Verifier deployed at", halo2Verifier.address)
100+
console.log("Halo2Verifier (HALO2V) deployed at", halo2Verifier.address)
101+
halo2VerifierAddress = halo2Verifier.address
102+
}
37103

104+
if (!globalPublicParamsAddress) {
38105
const GlobalPublicParams = await ethers.getContractFactory('GlobalPublicParams')
39106
const globalPublicParams = await GlobalPublicParams.connect(deployerWallet).deploy()
40107
await globalPublicParams.deployed()
41108

42-
console.log("GlobalPublicParams deployed at", globalPublicParams.address)
109+
console.log("GlobalPublicParams (GPP) deployed at", globalPublicParams.address)
110+
globalPublicParamsAddress = globalPublicParams.address
111+
}
43112

113+
if (!pseudoRandAddress) {
44114
const PseudoRand = await ethers.getContractFactory('PseudoRand')
45115
const pseudoRand = await PseudoRand.connect(deployerWallet).deploy()
46116
await pseudoRand.deployed()
47117

48-
console.log("PseudoRand deployed at", pseudoRand.address)
49-
50-
halo2VerifyingKeyAddress = halo2VerifyingKey.address
51-
halo2VerifierAddress = halo2Verifier.address
52-
globalPublicParamsAddress = globalPublicParams.address
118+
console.log("PseudoRand (PSRAND) deployed at", pseudoRand.address)
53119
pseudoRandAddress = pseudoRand.address
54120
}
55121

0 commit comments

Comments
 (0)