From 935fa23149e554a937439bec7506beba5bb9a23e Mon Sep 17 00:00:00 2001 From: Anton <14254374+0xmad@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:32:11 -0500 Subject: [PATCH] feat(contracts): use contract names for deployment service --- .../contracts/tasks/helpers/Deployment.ts | 34 +++++++++++++++---- packages/contracts/tasks/runner/benchmarks.ts | 2 +- packages/contracts/tasks/runner/deployFull.ts | 2 +- packages/contracts/tasks/runner/deployPoll.ts | 2 +- packages/contracts/tasks/runner/merge.ts | 2 +- packages/contracts/ts/deploy.ts | 6 ++-- 6 files changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/contracts/tasks/helpers/Deployment.ts b/packages/contracts/tasks/helpers/Deployment.ts index e5b8607d64..aa3d7683c5 100644 --- a/packages/contracts/tasks/helpers/Deployment.ts +++ b/packages/contracts/tasks/helpers/Deployment.ts @@ -56,15 +56,21 @@ export class Deployment { */ private storage: ContractStorage; + /** + * Contracts name mapping + */ + private contractNames: Record; + /** * Initialize class properties only once */ - private constructor(hre?: HardhatRuntimeEnvironment) { + private constructor(contractNames: Record, hre?: HardhatRuntimeEnvironment) { this.stepCatalog = new Map([ ["full", {}], ["poll", {}], ]); this.hre = hre; + this.contractNames = contractNames; this.config = low( typeof window !== "undefined" ? new LocalStorageSync("deploy-config") @@ -78,9 +84,12 @@ export class Deployment { * * @returns {ContractStorage} singleton object */ - static getInstance(hre?: HardhatRuntimeEnvironment): Deployment { + static getInstance({ + contractNames = EContracts, + hre = undefined, + }: { contractNames?: Record; hre?: HardhatRuntimeEnvironment } = {}): Deployment { if (!Deployment.INSTANCE) { - Deployment.INSTANCE = new Deployment(hre); + Deployment.INSTANCE = new Deployment(contractNames, hre); } return Deployment.INSTANCE; @@ -227,6 +236,15 @@ export class Deployment { this.hre = hre; } + /** + * Set contract names + * + * @param contractNames - contract names + */ + setContractNames(contractNames: Record): void { + this.contractNames = contractNames; + } + /** * Check if hardhat runtime environment is set * @@ -323,7 +341,9 @@ export class Deployment { const contractFactory = abi && bytecode ? new ContractFactory(abi, bytecode, deployer) - : await import("hardhat").then(({ ethers }) => ethers.getContractFactory(String(name), deployer)); + : await import("hardhat").then(({ ethers }) => + ethers.getContractFactory(this.contractNames[name as EContracts] || (name as EContracts), deployer), + ); const feeData = await deployer.provider?.getFeeData(); const contract = await contractFactory.deploy(...args, { @@ -387,7 +407,9 @@ export class Deployment { ): T { this.checkHre(); - const value = this.config.get(`${this.hre!.network.name}.${id}.${field}`).value() as T; + const value = this.config + .get(`${this.hre!.network.name}.${this.contractNames[id as EContracts]}.${field}`) + .value() as T; if (mustGet && (value === null || value === undefined)) { throw new Error(`Can't find ${this.hre!.network.name}.${id}.${field}`); @@ -409,7 +431,7 @@ export class Deployment { ): void { this.checkHre(); - this.config.set(`${this.hre!.network.name}.${id}.${field}`, value).write(); + this.config.set(`${this.hre!.network.name}.${this.contractNames[id as EContracts]}.${field}`, value).write(); } /** diff --git a/packages/contracts/tasks/runner/benchmarks.ts b/packages/contracts/tasks/runner/benchmarks.ts index db98b2f576..02a71e9ce0 100644 --- a/packages/contracts/tasks/runner/benchmarks.ts +++ b/packages/contracts/tasks/runner/benchmarks.ts @@ -6,7 +6,7 @@ import { Deployment } from "../helpers/Deployment"; import { EContracts } from "../helpers/types"; task("benchmark", "Run benchmarks").setAction(async (_, hre) => { - const deployment = Deployment.getInstance(hre); + const deployment = Deployment.getInstance({ hre }); deployment.setHre(hre); const deployer = await deployment.getDeployer(); diff --git a/packages/contracts/tasks/runner/deployFull.ts b/packages/contracts/tasks/runner/deployFull.ts index f900ae95c6..f196dc904e 100644 --- a/packages/contracts/tasks/runner/deployFull.ts +++ b/packages/contracts/tasks/runner/deployFull.ts @@ -15,7 +15,7 @@ task("deploy-full", "Deploy environment") .addFlag("verify", "Verify contracts at Etherscan") .addOptionalParam("skip", "Skip steps with less or equal index", 0, types.int) .setAction(async ({ incremental, strict, verify, skip = 0 }: IDeployParams, hre) => { - const deployment = Deployment.getInstance(hre); + const deployment = Deployment.getInstance({ hre }); deployment.setHre(hre); diff --git a/packages/contracts/tasks/runner/deployPoll.ts b/packages/contracts/tasks/runner/deployPoll.ts index 3985fcd392..3fc5e60888 100644 --- a/packages/contracts/tasks/runner/deployPoll.ts +++ b/packages/contracts/tasks/runner/deployPoll.ts @@ -15,7 +15,7 @@ task("deploy-poll", "Deploy poll") .addFlag("verify", "Verify contracts at Etherscan") .addOptionalParam("skip", "Skip steps with less or equal index", 0, types.int) .setAction(async ({ strict, verify, skip = 0 }: IDeployParams, hre) => { - const deployment = Deployment.getInstance(hre); + const deployment = Deployment.getInstance({ hre }); deployment.setHre(hre); diff --git a/packages/contracts/tasks/runner/merge.ts b/packages/contracts/tasks/runner/merge.ts index d4007a9c10..55d3c95ea3 100644 --- a/packages/contracts/tasks/runner/merge.ts +++ b/packages/contracts/tasks/runner/merge.ts @@ -18,7 +18,7 @@ task("merge", "Merge signups and messages") .addOptionalParam("queueOps", "The number of queue operations to perform", DEFAULT_SR_QUEUE_OPS, types.int) .addOptionalParam("prove", "Run prove command after merging", false, types.boolean) .setAction(async ({ poll, prove, queueOps = DEFAULT_SR_QUEUE_OPS }: IMergeParams, hre) => { - const deployment = Deployment.getInstance(hre); + const deployment = Deployment.getInstance({ hre }); deployment.setHre(hre); diff --git a/packages/contracts/ts/deploy.ts b/packages/contracts/ts/deploy.ts index c92cf880e5..7b652912ff 100644 --- a/packages/contracts/ts/deploy.ts +++ b/packages/contracts/ts/deploy.ts @@ -47,7 +47,7 @@ import { log } from "./utils"; */ export const createContractFactory = async (abi: TAbi, bytecode: string, signer?: Signer): Promise => { const hre = await import("hardhat"); - const deployment = Deployment.getInstance(hre); + const deployment = Deployment.getInstance({ hre }); deployment.setHre(hre); const deployer = signer || (await deployment.getDeployer()); @@ -69,7 +69,7 @@ export const deployContract = async ( ): Promise => { log(`Deploying ${contractName}`, quiet); const hre = await import("hardhat"); - const deployment = Deployment.getInstance(hre); + const deployment = Deployment.getInstance({ hre }); deployment.setHre(hre); return deployment.deployContract({ name: contractName as EContracts, signer }, ...args); @@ -246,7 +246,7 @@ export const deployContractWithLinkedLibraries = async ( ...args: unknown[] ): Promise => { const hre = await import("hardhat"); - const deployment = Deployment.getInstance(hre); + const deployment = Deployment.getInstance({ hre }); deployment.setHre(hre); return deployment.deployContractWithLinkedLibraries({ contractFactory }, ...args);