Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve contract deploy #23

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading