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

chore(contracts): use deployment helper for deploy commands #1383

Merged
merged 1 commit into from
Apr 17, 2024
Merged
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
99 changes: 38 additions & 61 deletions contracts/ts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { type ContractFactory, type Signer, BaseContract } from "ethers";

import type { IDeployMaciArgs, IDeployedMaci, IDeployedPoseidonContracts } from "./types";

import { Deployment } from "../tasks/helpers/Deployment";
import { EContracts } from "../tasks/helpers/types";
import {
ConstantInitialVoiceCreditProxy,
FreeForAllGatekeeper,
Expand All @@ -26,7 +28,7 @@ import {
AccQueueQuinaryMaci__factory as AccQueueQuinaryMaciFactory,
} from "../typechain-types";

import { getDefaultSigner, getFeeData, log } from "./utils";
import { getDefaultSigner, log } from "./utils";

/**
* Link Poseidon libraries to a Smart Contract
Expand All @@ -49,19 +51,19 @@ export const linkPoseidonLibraries = async (
quiet = false,
): Promise<ContractFactory> => {
log(`Linking Poseidon libraries to ${solFileToLink}`, quiet);
const { ethers } = await import("hardhat");

const contractFactory = await ethers.getContractFactory(solFileToLink, {
signer: signer || (await getDefaultSigner()),
libraries: {
PoseidonT3: poseidonT3Address,
PoseidonT4: poseidonT4Address,
PoseidonT5: poseidonT5Address,
PoseidonT6: poseidonT6Address,
},
});

return contractFactory;
const hre = await import("hardhat");
const deployment = Deployment.getInstance(hre);
deployment.setHre(hre);
const deployer = signer || (await deployment.getDeployer());

return deployment.linkPoseidonLibraries(
solFileToLink as EContracts,
poseidonT3Address,
poseidonT4Address,
poseidonT5Address,
poseidonT6Address,
deployer,
);
};

/**
Expand All @@ -78,17 +80,11 @@ export const deployContract = async <T extends BaseContract>(
...args: unknown[]
): Promise<T> => {
log(`Deploying ${contractName}`, quiet);
const { ethers } = await import("hardhat");

const contractFactory = await ethers.getContractFactory(contractName, signer || (await getDefaultSigner()));
const feeData = await getFeeData();
const contract = await contractFactory.deploy(...args, {
maxFeePerGas: feeData?.maxFeePerGas,
maxPriorityFeePerGas: feeData?.maxPriorityFeePerGas,
});
await contract.deploymentTransaction()!.wait();
const hre = await import("hardhat");
const deployment = Deployment.getInstance(hre);
deployment.setHre(hre);

return contract as unknown as T;
return deployment.deployContract(contractName as EContracts, signer, ...args);
};

/**
Expand Down Expand Up @@ -209,19 +205,13 @@ export const deployPoseidonContracts = async (
*/
export const deployContractWithLinkedLibraries = async <T extends BaseContract>(
contractFactory: ContractFactory,
name: string,
quiet = false,
...args: unknown[]
): Promise<T> => {
log(`Deploying ${name}`, quiet);
const feeData = await getFeeData();
const contract = await contractFactory.deploy(...args, {
maxFeePerGas: feeData?.maxFeePerGas,
maxPriorityFeePerGas: feeData?.maxPriorityFeePerGas,
});
await contract.deploymentTransaction()!.wait();

return contract as T;
const hre = await import("hardhat");
const deployment = Deployment.getInstance(hre);
deployment.setHre(hre);

return deployment.deployContractWithLinkedLibraries(contractFactory, ...args);
};

/**
Expand All @@ -238,6 +228,7 @@ export const deployPollFactory = async (signer: Signer, quiet = false): Promise<
poseidonContracts.PoseidonT5Contract.getAddress(),
poseidonContracts.PoseidonT6Contract.getAddress(),
]);

const contractFactory = await linkPoseidonLibraries(
"PollFactory",
poseidonT3Contract,
Expand All @@ -247,7 +238,8 @@ export const deployPollFactory = async (signer: Signer, quiet = false): Promise<
signer,
quiet,
);
return deployContractWithLinkedLibraries(contractFactory, "PollFactory", quiet);

return deployContractWithLinkedLibraries(contractFactory);
};

/**
Expand Down Expand Up @@ -299,47 +291,32 @@ export const deployMaci = async ({
const [maciContractFactory, pollFactoryContractFactory, messageProcessorFactory, tallyFactory] =
await Promise.all(linkedContractFactories);

const pollFactoryContract = await deployContractWithLinkedLibraries<PollFactory>(
pollFactoryContractFactory,
"PollFactory",
quiet,
);
const pollFactoryContract = await deployContractWithLinkedLibraries<PollFactory>(pollFactoryContractFactory);

const messageProcessorFactoryContract = await deployContractWithLinkedLibraries<MessageProcessorFactory>(
messageProcessorFactory,
"MessageProcessorFactory",
quiet,
);
const messageProcessorFactoryContract =
await deployContractWithLinkedLibraries<MessageProcessorFactory>(messageProcessorFactory);

// deploy either the qv or non qv tally factory - they both implement the same interface
// so as long as maci is concerned, they are interchangeable
const tallyFactoryContract = await deployContractWithLinkedLibraries<TallyFactory>(
tallyFactory,
"TallyFactory",
quiet,
);
const tallyFactoryContract = await deployContractWithLinkedLibraries<TallyFactory>(tallyFactory);

const [pollAddr, mpAddr, tallyAddr] = await Promise.all([
const [pollAddress, messageProcessorAddress, tallyAddress] = await Promise.all([
pollFactoryContract.getAddress(),
messageProcessorFactoryContract.getAddress(),
tallyFactoryContract.getAddress(),
]);

const maciContract = await deployContractWithLinkedLibraries<MACI>(
maciContractFactory,
"MACI",
quiet,
pollAddr,
mpAddr,
tallyAddr,
pollAddress,
messageProcessorAddress,
tallyAddress,
signUpTokenGatekeeperContractAddress,
initialVoiceCreditBalanceAddress,
topupCreditContractAddress,
stateTreeDepth,
);

const stateAqContractAddress = await maciContract.stateAq();
const stateAqContract = AccQueueQuinaryMaciFactory.connect(stateAqContractAddress, await getDefaultSigner());
const [stateAqContractAddress, deployer] = await Promise.all([maciContract.stateAq(), getDefaultSigner()]);
const stateAqContract = AccQueueQuinaryMaciFactory.connect(stateAqContractAddress, signer || deployer);

return {
maciContract,
Expand Down
Loading