Skip to content

Commit

Permalink
refactor(cli): move deploy vk registry and testing commands to sdk
Browse files Browse the repository at this point in the history
- [x] Move deploy vk registry
- [x] Move testing commands (timeTravel, fundWallet)
- [x] Improve contract storage functions for cli
  • Loading branch information
0xmad committed Feb 28, 2025
1 parent aaaf9be commit bcd8d12
Show file tree
Hide file tree
Showing 36 changed files with 357 additions and 366 deletions.
2 changes: 1 addition & 1 deletion apps/relayer/hardhat.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ module.exports = {
},
paths: {
sources: path.resolve(__dirname, parentDir, "./node_modules/maci-contracts/contracts"),
artifacts: path.resolve(__dirname, parentDir, "./node_modules/maci-contracts/build/artifacts"),
artifacts: path.resolve(__dirname, parentDir, "./node_modules/maci-contracts/artifacts"),
},
};
28 changes: 26 additions & 2 deletions apps/relayer/tests/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hardhat from "hardhat";
import { deploy, deployVkRegistryContract } from "maci-cli";
import { deploy } from "maci-cli";
import { deployPoseidonContracts } from "maci-contracts";
import { Keypair } from "maci-domainobjs";
import {
EMode,
Expand All @@ -9,6 +10,7 @@ import {
signup,
joinPoll,
deployPoll,
deployVkRegistryContract,
} from "maci-sdk";

import {
Expand Down Expand Up @@ -93,7 +95,29 @@ export class TestDeploy {
signer,
});

const maciAddresses = await deploy({ stateTreeDepth: 10, signer });
const { PoseidonT3Contract, PoseidonT4Contract, PoseidonT5Contract, PoseidonT6Contract } =
await deployPoseidonContracts(signer, {});

const poseidonAddrs = await Promise.all([
PoseidonT3Contract.getAddress(),
PoseidonT4Contract.getAddress(),
PoseidonT5Contract.getAddress(),
PoseidonT6Contract.getAddress(),
]).then(([poseidonT3, poseidonT4, poseidonT5, poseidonT6]) => ({
poseidonT3,
poseidonT4,
poseidonT5,
poseidonT6,
}));

const maciAddresses = await deploy({
stateTreeDepth: 10,
poseidonT3Address: poseidonAddrs.poseidonT3,
poseidonT4Address: poseidonAddrs.poseidonT4,
poseidonT5Address: poseidonAddrs.poseidonT5,
poseidonT6Address: poseidonAddrs.poseidonT6,
signer,
});

const startDate = Math.floor(Date.now() / 1000) + 30;

Expand Down
4 changes: 3 additions & 1 deletion packages/cli/tests/ceremony-params/ceremonyParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import {
publish,
deployPoll,
generateProofs,
deployVkRegistryContract,
timeTravel,
type IGenerateProofsArgs,
} from "maci-sdk";

import type { Signer } from "ethers";

import { deploy, deployVkRegistryContract, timeTravel } from "../../ts/commands";
import { deploy } from "../../ts/commands";
import { DEFAULT_SG_DATA, DeployArgs, DeployedContracts } from "../../ts/utils";
import {
coordinatorPrivKey,
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import {
type ITallyData,
type IMergeSignupsArgs,
type IVerifyArgs,
IProveOnChainArgs,
type IProveOnChainArgs,
type ITimeTravelArgs,
} from "maci-sdk";

import { homedir } from "os";

import type { Signer } from "ethers";

import { DeployArgs, TimeTravelArgs, readJSONFile } from "../ts/utils";
import { DeployArgs, readJSONFile } from "../ts/utils";
import { DEFAULT_INITIAL_VOICE_CREDITS, DEFAULT_VOTE_OPTIONS } from "../ts/utils/defaults";

export const STATE_TREE_DEPTH = 10;
Expand Down Expand Up @@ -110,7 +111,7 @@ export const checkVerifyingKeysArgs: Omit<ICheckVerifyingKeysArgs, "vkRegistry"
tallyVotesZkeyPath: tallyVotesTestZkeyPath,
};

export const timeTravelArgs: Omit<TimeTravelArgs, "signer"> = {
export const timeTravelArgs: Omit<ITimeTravelArgs, "signer"> = {
seconds: pollDuration,
};

Expand Down
4 changes: 3 additions & 1 deletion packages/cli/tests/e2e/e2e.nonQv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import {
publish,
deployPoll,
generateProofs,
deployVkRegistryContract,
timeTravel,
type IGenerateProofsArgs,
} from "maci-sdk";

import type { Signer } from "ethers";

import { deploy, deployVkRegistryContract, timeTravel } from "../../ts/commands";
import { deploy } from "../../ts/commands";
import { DEFAULT_SG_DATA, DeployedContracts } from "../../ts/utils";
import {
deployPollArgs,
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/tests/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import {
publish,
deployPoll,
generateProofs,
deployVkRegistryContract,
timeTravel,
type IGenerateProofsArgs,
} from "maci-sdk";

import fs from "fs";

import type { Signer } from "ethers";

import { deploy, deployVkRegistryContract, timeTravel } from "../../ts/commands";
import { deploy } from "../../ts/commands";
import { DEFAULT_IVCP_DATA, DEFAULT_SG_DATA, DeployedContracts } from "../../ts/utils";
import {
deployPollArgs,
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/tests/e2e/keyChange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ import {
proveOnChain,
publish,
generateProofs,
deployPoll,
deployVkRegistryContract,
timeTravel,
type IGenerateProofsArgs,
type ITallyData,
deployPoll,
} from "maci-sdk";

import fs from "fs";

import type { Signer } from "ethers";

import { DeployedContracts } from "../../ts";
import { deploy, deployVkRegistryContract, timeTravel } from "../../ts/commands";
import { deploy } from "../../ts/commands";
import { DEFAULT_IVCP_DATA, DEFAULT_SG_DATA } from "../../ts/utils";
import {
coordinatorPrivKey,
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/tests/unit/joinPoll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import {
setVerifyingKeys,
signup,
deployPoll,
deployVkRegistryContract,
} from "maci-sdk";

import { deploy, DeployedContracts, deployVkRegistryContract } from "../../ts";
import { deploy, DeployedContracts } from "../../ts";
import { DEFAULT_IVCP_DATA, DEFAULT_SG_DATA } from "../../ts/utils";
import {
deployArgs,
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/tests/unit/poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
mergeSignups,
setVerifyingKeys,
deployPoll,
IPollContractsData,
deployVkRegistryContract,
timeTravel,
type IPollContractsData,
} from "maci-sdk";

import type { Signer } from "ethers";

import { deploy, deployVkRegistryContract, timeTravel } from "../../ts/commands";
import { deploy } from "../../ts/commands";
import { DeployedContracts } from "../../ts/utils";
import { deployPollArgs, deployArgs, pollDuration, verifyingKeysArgs } from "../constants";
import { clean } from "../utils";
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/tests/unit/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import {
setVerifyingKeys,
signup,
publishBatch,
deployPoll,
deployVkRegistryContract,
type IPublishMessage,
type IPublishBatchArgs,
deployPoll,
IPollContractsData,
type IPollContractsData,
} from "maci-sdk";

import type { Signer } from "ethers";

import { deploy, deployVkRegistryContract } from "../../ts/commands";
import { deploy } from "../../ts/commands";
import { DEFAULT_SG_DATA, DeployedContracts } from "../../ts/utils";
import { deployPollArgs, deployArgs, pollDuration, verifyingKeysArgs } from "../constants";

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/tests/unit/signup.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { expect } from "chai";
import { Signer } from "ethers";
import { Keypair } from "maci-domainobjs";
import { getDefaultSigner, getSignedupUserData, setVerifyingKeys, signup } from "maci-sdk";
import { getDefaultSigner, getSignedupUserData, setVerifyingKeys, signup, deployVkRegistryContract } from "maci-sdk";

import { deploy, DeployedContracts, deployVkRegistryContract } from "../../ts";
import { deploy, DeployedContracts } from "../../ts";
import { DEFAULT_SG_DATA } from "../../ts/utils";
import { deployArgs, verifyingKeysArgs } from "../constants";

Expand Down
16 changes: 0 additions & 16 deletions packages/cli/tests/unit/timeTravel.test.ts

This file was deleted.

77 changes: 47 additions & 30 deletions packages/cli/ts/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
logError,
logGreen,
success,
readContractAddress,
storeContractAddress,
readContractAddresses,
storeContractAddresses,
DEFAULT_INITIAL_VOICE_CREDITS,
type DeployedContracts,
type DeployArgs,
Expand Down Expand Up @@ -42,32 +42,44 @@ export const deploy = async ({

const network = await signer.provider?.getNetwork();

const poseidonT3 = poseidonT3Address || (await readContractAddress("PoseidonT3", network?.name));
const poseidonT4 = poseidonT4Address || (await readContractAddress("PoseidonT4", network?.name));
const poseidonT5 = poseidonT5Address || (await readContractAddress("PoseidonT5", network?.name));
const poseidonT6 = poseidonT6Address || (await readContractAddress("PoseidonT6", network?.name));

// if we did not deploy it before, then deploy it now
let initialVoiceCreditProxyContractAddress: string | undefined =
initialVoiceCreditsProxyAddress || (await readContractAddress("InitialVoiceCreditProxy", network?.name));

if (!initialVoiceCreditsProxyAddress) {
const [
poseidonT3,
poseidonT4,
poseidonT5,
poseidonT6,
initialVoiceCreditProxyContractAddress,
signupGatekeeperContractAddress,
] = await readContractAddresses(
["PoseidonT3", "PoseidonT4", "PoseidonT5", "PoseidonT6", "InitialVoiceCreditProxy", "SignUpGatekeeper"],
network?.name,
[
poseidonT3Address,
poseidonT4Address,
poseidonT5Address,
poseidonT6Address,
initialVoiceCreditsProxyAddress,
signupGatekeeperAddress,
],
);

let initialVoiceCreditsProxyContractAddress = initialVoiceCreditProxyContractAddress;

if (!initialVoiceCreditsProxyContractAddress) {
const contract = await deployConstantInitialVoiceCreditProxy(
initialVoiceCredits || DEFAULT_INITIAL_VOICE_CREDITS,
signer,
true,
);

initialVoiceCreditProxyContractAddress = await contract.getAddress();
initialVoiceCreditsProxyContractAddress = await contract.getAddress();
}

// check if we have a signupGatekeeper already deployed or passed as arg
let signupGatekeeperContractAddress =
signupGatekeeperAddress || (await readContractAddress("SignUpGatekeeper", network?.name));
let gatekeeperContractAddress = signupGatekeeperContractAddress;

if (!signupGatekeeperContractAddress) {
if (!gatekeeperContractAddress) {
const contract = await deployFreeForAllSignUpGatekeeper(signer, true);
signupGatekeeperContractAddress = await contract.getAddress();

gatekeeperContractAddress = await contract.getAddress();
}

// deploy a verifier contract
Expand All @@ -77,7 +89,7 @@ export const deploy = async ({

// deploy MACI, PollFactory and poseidon
const { maciContract, pollFactoryContract, poseidonAddrs } = await deployMaci({
signUpTokenGatekeeperContractAddress: signupGatekeeperContractAddress,
signUpTokenGatekeeperContractAddress: gatekeeperContractAddress,
poseidonAddresses: {
poseidonT3,
poseidonT4,
Expand All @@ -95,15 +107,20 @@ export const deploy = async ({
]);

// save to the JSON File
await storeContractAddress("InitialVoiceCreditProxy", initialVoiceCreditProxyContractAddress, network?.name);
await storeContractAddress("SignUpGatekeeper", signupGatekeeperContractAddress, network?.name);
await storeContractAddress("Verifier", verifierContractAddress, network?.name);
await storeContractAddress("MACI", maciContractAddress, network?.name);
await storeContractAddress("PollFactory", pollFactoryContractAddress, network?.name);
await storeContractAddress("PoseidonT3", poseidonAddrs.poseidonT3, network?.name);
await storeContractAddress("PoseidonT4", poseidonAddrs.poseidonT4, network?.name);
await storeContractAddress("PoseidonT5", poseidonAddrs.poseidonT5, network?.name);
await storeContractAddress("PoseidonT6", poseidonAddrs.poseidonT6, network?.name);
await storeContractAddresses(
{
InitialVoiceCreditProxy: initialVoiceCreditsProxyContractAddress,
SignUpGatekeeper: gatekeeperContractAddress,
Verifier: verifierContractAddress,
MACI: maciContractAddress,
PollFactory: pollFactoryContractAddress,
PoseidonT3: poseidonAddrs.poseidonT3,
PoseidonT4: poseidonAddrs.poseidonT4,
PoseidonT5: poseidonAddrs.poseidonT5,
PoseidonT6: poseidonAddrs.poseidonT6,
},
network?.name,
);

logGreen(quiet, success(`MACI deployed at: ${maciContractAddress}`));

Expand All @@ -116,7 +133,7 @@ export const deploy = async ({
poseidonT4Address: poseidonAddrs.poseidonT4,
poseidonT5Address: poseidonAddrs.poseidonT5,
poseidonT6Address: poseidonAddrs.poseidonT6,
signUpGatekeeperAddress: signupGatekeeperContractAddress,
initialVoiceCreditProxyAddress: initialVoiceCreditProxyContractAddress,
signUpGatekeeperAddress: gatekeeperContractAddress,
initialVoiceCreditProxyAddress: initialVoiceCreditsProxyContractAddress,
};
};
38 changes: 0 additions & 38 deletions packages/cli/ts/commands/deployVkRegistry.ts

This file was deleted.

Loading

0 comments on commit bcd8d12

Please sign in to comment.