Skip to content

Commit

Permalink
Improve Validator deploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriianChestnykh committed Oct 1, 2024
1 parent 07a6b0c commit b3d4498
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 82 deletions.
21 changes: 11 additions & 10 deletions helpers/DeployHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
Groth16VerifierMTPWrapperModule,
Groth16VerifierSigWrapperModule,
Groth16VerifierV3WrapperModule,
CredentialAtomicQueryMTPV2ValidatorModule,
CredentialAtomicQuerySigV2ValidatorModule,
CredentialAtomicQueryV3ValidatorModule,
VerifierLibModule,
VCPaymentModule,
StateProxyModule,
IdentityTreeStoreProxyModule,
CredentialAtomicQueryMTPV2ValidatorProxyModule,
CredentialAtomicQuerySigV2ValidatorProxyModule,
CredentialAtomicQueryV3ValidatorProxyModule,
} from "../ignition";
import { chainIdInfoMap } from "./constants";
import { waitNotToInterfereWithHardhatIgnition } from "./helperUtils";
Expand Down Expand Up @@ -504,18 +504,20 @@ export class DeployHelper {
switch (validatorType) {
case "mtpV2":
g16VerifierWrapperModule = Groth16VerifierMTPWrapperModule;
validatorModule = CredentialAtomicQueryMTPV2ValidatorModule;
validatorModule = CredentialAtomicQueryMTPV2ValidatorProxyModule;
break;
case "sigV2":
g16VerifierWrapperModule = Groth16VerifierSigWrapperModule;
validatorModule = CredentialAtomicQuerySigV2ValidatorModule;
validatorModule = CredentialAtomicQuerySigV2ValidatorProxyModule;
break;
case "v3":
g16VerifierWrapperModule = Groth16VerifierV3WrapperModule;
validatorModule = CredentialAtomicQueryV3ValidatorModule;
validatorModule = CredentialAtomicQueryV3ValidatorProxyModule;
break;
}

await waitNotToInterfereWithHardhatIgnition(undefined);

groth16VerifierWrapper = (
await ignition.deploy(g16VerifierWrapperModule, {
strategy: deployStrategy,
Expand All @@ -527,15 +529,14 @@ export class DeployHelper {
`${g16VerifierContractWrapperName} Wrapper deployed to: ${await groth16VerifierWrapper.getAddress()}`,
);

// Deploying Validator contract to predictable address but with dummy implementation
const tx = await groth16VerifierWrapper.deploymentTransaction();
await waitNotToInterfereWithHardhatIgnition(tx);
await waitNotToInterfereWithHardhatIgnition(await groth16VerifierWrapper.deploymentTransaction());

// Deploying Validator contract to predictable address but with dummy implementation
validator = (
await ignition.deploy(validatorModule, {
strategy: deployStrategy,
})
).validator;
).proxy;
await validator.waitForDeployment();

// Upgrading Validator contract to the first real implementation
Expand Down
35 changes: 27 additions & 8 deletions helpers/helperUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ContractTransactionResponse } from "ethers";
import hre, { network } from "hardhat";
import { boolean } from "hardhat/internal/core/params/argumentTypes";

export function getConfig() {
return {
Expand All @@ -16,14 +17,32 @@ export function getConfig() {
}

export async function waitNotToInterfereWithHardhatIgnition(
tx: ContractTransactionResponse,
tx: ContractTransactionResponse | undefined,
): Promise<void> {
const confirmationsNeeded = hre.config.ignition?.requiredConfirmations ?? 1;
const waitConfirmations = ["localhost", "hardhat"].includes(network.name)
const isLocalNetwork = ["localhost", "hardhat"].includes(network.name);
const confirmationsNeeded = isLocalNetwork
? 1
: confirmationsNeeded;
console.log(
`Waiting for ${waitConfirmations} confirmations to not interfere with Hardhat Ignition`,
);
await tx.wait(waitConfirmations);
: hre.config.ignition?.requiredConfirmations ?? 1;

if (tx) {
console.log(
`Waiting for ${confirmationsNeeded} confirmations to not interfere with Hardhat Ignition`,
);
await tx.wait(confirmationsNeeded);
} else if (isLocalNetwork) {
console.log(
`Mining ${confirmationsNeeded} blocks not to interfere with Hardhat Ignition`,
);
for (const _ of Array.from({ length: confirmationsNeeded })) {
await hre.ethers.provider.send("evm_mine");
}
} else {
const blockNumberDeployed = await hre.ethers.provider.getBlockNumber();
let blockNumber = blockNumberDeployed;
console.log("Waiting some blocks to expect at least 5 confirmations for ignition...");
while (blockNumber < blockNumberDeployed + 10) {
await new Promise((resolve) => setTimeout(resolve, 5000));
blockNumber = await hre.ethers.provider.getBlockNumber();
}
}
}
19 changes: 1 addition & 18 deletions ignition/modules/credentialAtomicQueryMTPV2Validator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
import { create2AddressesInfo } from "../../helpers/constants";

const CredentialAtomicQueryMTPV2ValidatorProxyModule = buildModule(
export const CredentialAtomicQueryMTPV2ValidatorProxyModule = buildModule(
"CredentialAtomicQueryMTPV2ValidatorProxyModule",
(m) => {
const proxyAdminOwner = m.getAccount(0);
Expand All @@ -25,20 +25,3 @@ const CredentialAtomicQueryMTPV2ValidatorProxyModule = buildModule(
return { proxyAdmin, proxy };
},
);

export const CredentialAtomicQueryMTPV2ValidatorModule = buildModule(
"CredentialAtomicQueryMTPV2ValidatorModule",
(m) => {
const { proxy, proxyAdmin } = m.useModule(CredentialAtomicQueryMTPV2ValidatorProxyModule);

// Here we're using m.contractAt(...) a bit differently than we did above.
// While we're still using it to create a contract instance, we're now telling Hardhat Ignition
// to treat the contract at the proxy address as an instance of the Demo contract.
// This allows us to interact with the underlying Demo contract via the proxy from within tests and scripts.
const CredentialAtomicQueryMTPV2Validator = m.contractAt(
"CredentialAtomicQueryMTPV2Validator",
proxy,
);
return { validator: CredentialAtomicQueryMTPV2Validator, proxy, proxyAdmin };
},
);
20 changes: 1 addition & 19 deletions ignition/modules/credentialAtomicQuerySigV2Validator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
import { create2AddressesInfo } from "../../helpers/constants";

const CredentialAtomicQuerySigV2ValidatorProxyModule = buildModule(
export const CredentialAtomicQuerySigV2ValidatorProxyModule = buildModule(
"CredentialAtomicQuerySigV2ValidatorProxyModule",
(m) => {
const proxyAdminOwner = m.getAccount(0);
Expand All @@ -25,21 +25,3 @@ const CredentialAtomicQuerySigV2ValidatorProxyModule = buildModule(
return { proxyAdmin, proxy };
},
);

export const CredentialAtomicQuerySigV2ValidatorModule = buildModule(
"CredentialAtomicQuerySigV2ValidatorModule",
(m) => {
const { proxy, proxyAdmin } = m.useModule(CredentialAtomicQuerySigV2ValidatorProxyModule);

// Here we're using m.contractAt(...) a bit differently than we did above.
// While we're still using it to create a contract instance, we're now telling Hardhat Ignition
// to treat the contract at the proxy address as an instance of the Demo contract.
// This allows us to interact with the underlying Demo contract via the proxy from within tests and scripts.
const CredentialAtomicQuerySigV2Validator = m.contractAt(
"CredentialAtomicQuerySigV2Validator",
proxy,
);

return { validator: CredentialAtomicQuerySigV2Validator, proxy, proxyAdmin };
},
);
20 changes: 1 addition & 19 deletions ignition/modules/credentialAtomicQueryV3Validator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
import { create2AddressesInfo } from "../../helpers/constants";

const CredentialAtomicQueryV3ValidatorProxyModule = buildModule(
export const CredentialAtomicQueryV3ValidatorProxyModule = buildModule(
"CredentialAtomicQueryV3ValidatorProxyModule",
(m) => {
const proxyAdminOwner = m.getAccount(0);
Expand All @@ -23,21 +23,3 @@ const CredentialAtomicQueryV3ValidatorProxyModule = buildModule(
return { proxyAdmin, proxy };
},
);

export const CredentialAtomicQueryV3ValidatorModule = buildModule(
"CredentialAtomicQueryV3ValidatorModule",
(m) => {
const { proxy, proxyAdmin } = m.useModule(CredentialAtomicQueryV3ValidatorProxyModule);

// Here we're using m.contractAt(...) a bit differently than we did above.
// While we're still using it to create a contract instance, we're now telling Hardhat Ignition
// to treat the contract at the proxy address as an instance of the Demo contract.
// This allows us to interact with the underlying Demo contract via the proxy from within tests and scripts.
const CredentialAtomicQueryV3Validator = m.contractAt(
"CredentialAtomicQueryV3Validator",
proxy,
);

return { validator: CredentialAtomicQueryV3Validator, proxy, proxyAdmin };
},
);
9 changes: 1 addition & 8 deletions scripts/deployValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs";
import path from "path";
import { DeployHelper } from "../helpers/DeployHelper";
import hre, { ethers, network } from "hardhat";
import { getConfig } from "../helpers/helperUtils";
import { getConfig, waitNotToInterfereWithHardhatIgnition } from "../helpers/helperUtils";

async function main() {
const config = getConfig();
Expand Down Expand Up @@ -30,13 +30,6 @@ async function main() {
validator: await validator.getAddress(),
groth16verifier: await groth16VerifierWrapper.getAddress(),
});
const blockNumberDeployed = await hre.ethers.provider.getBlockNumber();
let blockNumber = blockNumberDeployed;
console.log("Waiting some blocks to expect at least 5 confirmations for ignition...");
while (blockNumber < blockNumberDeployed + 10) {
await new Promise((resolve) => setTimeout(resolve, 5000));
blockNumber = await hre.ethers.provider.getBlockNumber();
}
}

const chainId = parseInt(await network.provider.send("eth_chainId"), 16);
Expand Down

0 comments on commit b3d4498

Please sign in to comment.