Skip to content

Commit

Permalink
feat(contracts): use contract names for deployment service
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmad committed Oct 31, 2024
1 parent ba83ea2 commit 935fa23
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
34 changes: 28 additions & 6 deletions packages/contracts/tasks/helpers/Deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ export class Deployment {
*/
private storage: ContractStorage;

/**
* Contracts name mapping
*/
private contractNames: Record<EContracts, string>;

/**
* Initialize class properties only once
*/
private constructor(hre?: HardhatRuntimeEnvironment) {
private constructor(contractNames: Record<EContracts, string>, hre?: HardhatRuntimeEnvironment) {
this.stepCatalog = new Map([
["full", {}],
["poll", {}],
]);
this.hre = hre;
this.contractNames = contractNames;
this.config = low(
typeof window !== "undefined"
? new LocalStorageSync<TConfig>("deploy-config")
Expand All @@ -78,9 +84,12 @@ export class Deployment {
*
* @returns {ContractStorage} singleton object
*/
static getInstance(hre?: HardhatRuntimeEnvironment): Deployment {
static getInstance({
contractNames = EContracts,
hre = undefined,
}: { contractNames?: Record<EContracts, string>; hre?: HardhatRuntimeEnvironment } = {}): Deployment {
if (!Deployment.INSTANCE) {
Deployment.INSTANCE = new Deployment(hre);
Deployment.INSTANCE = new Deployment(contractNames, hre);
}

return Deployment.INSTANCE;
Expand Down Expand Up @@ -227,6 +236,15 @@ export class Deployment {
this.hre = hre;
}

/**
* Set contract names
*
* @param contractNames - contract names
*/
setContractNames(contractNames: Record<EContracts, string>): void {
this.contractNames = contractNames;
}

/**
* Check if hardhat runtime environment is set
*
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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}`);
Expand All @@ -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();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/tasks/runner/benchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/tasks/runner/deployFull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/tasks/runner/deployPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/tasks/runner/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions packages/contracts/ts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { log } from "./utils";
*/
export const createContractFactory = async (abi: TAbi, bytecode: string, signer?: Signer): Promise<ContractFactory> => {
const hre = await import("hardhat");
const deployment = Deployment.getInstance(hre);
const deployment = Deployment.getInstance({ hre });
deployment.setHre(hre);
const deployer = signer || (await deployment.getDeployer());

Expand All @@ -69,7 +69,7 @@ export const deployContract = async <T extends BaseContract>(
): Promise<T> => {
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);
Expand Down Expand Up @@ -246,7 +246,7 @@ export const deployContractWithLinkedLibraries = async <T extends BaseContract>(
...args: unknown[]
): Promise<T> => {
const hre = await import("hardhat");
const deployment = Deployment.getInstance(hre);
const deployment = Deployment.getInstance({ hre });
deployment.setHre(hre);

return deployment.deployContractWithLinkedLibraries({ contractFactory }, ...args);
Expand Down

0 comments on commit 935fa23

Please sign in to comment.