-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1224 from privacy-scaling-explorations/feature/cl…
…i-poll feat(cli): add get poll cli command
- Loading branch information
Showing
11 changed files
with
268 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { expect } from "chai"; | ||
import { getDefaultSigner } from "maci-contracts"; | ||
|
||
import type { Signer } from "ethers"; | ||
|
||
import { | ||
deploy, | ||
deployPoll, | ||
deployVkRegistryContract, | ||
setVerifyingKeys, | ||
getPoll, | ||
timeTravel, | ||
mergeMessages, | ||
mergeSignups, | ||
} from "../../ts/commands"; | ||
import { DeployedContracts, PollContracts } from "../../ts/utils"; | ||
import { deployPollArgs, setVerifyingKeysArgs, deployArgs } from "../constants"; | ||
import { cleanVanilla } from "../utils"; | ||
|
||
describe("poll", () => { | ||
let maciAddresses: DeployedContracts; | ||
let pollAddresses: PollContracts; | ||
let signer: Signer; | ||
|
||
// before all tests we deploy the vk registry contract and set the verifying keys | ||
before(async () => { | ||
signer = await getDefaultSigner(); | ||
|
||
// we deploy the vk registry contract | ||
await deployVkRegistryContract({ signer }); | ||
// we set the verifying keys | ||
await setVerifyingKeys({ ...setVerifyingKeysArgs, signer }); | ||
}); | ||
|
||
describe("check deploy and get poll", () => { | ||
after(() => { | ||
cleanVanilla(); | ||
}); | ||
|
||
before(async () => { | ||
// deploy the smart contracts | ||
maciAddresses = await deploy({ ...deployArgs, signer }); | ||
// deploy a poll contract | ||
pollAddresses = await deployPoll({ ...deployPollArgs, signer }); | ||
}); | ||
|
||
it("should get current poll properly", async () => { | ||
const pollData = await getPoll({ maciAddress: maciAddresses.maciAddress, signer }); | ||
const samePollData = await getPoll({ maciAddress: maciAddresses.maciAddress, pollId: pollData.id, signer }); | ||
|
||
expect(pollData.address).to.eq(pollAddresses.poll); | ||
expect(pollData).to.deep.eq(samePollData); | ||
}); | ||
|
||
it("should get finished poll properly", async () => { | ||
const pollData = await getPoll({ maciAddress: maciAddresses.maciAddress, signer }); | ||
|
||
await timeTravel({ seconds: Number(pollData.duration), signer }); | ||
await mergeMessages({ pollId: BigInt(pollData.id), signer }); | ||
await mergeSignups({ pollId: BigInt(pollData.id), signer }); | ||
|
||
const finishedPollData = await getPoll({ maciAddress: maciAddresses.maciAddress, signer }); | ||
|
||
expect(pollData.id).to.eq(finishedPollData.id); | ||
expect(pollData.address).to.eq(finishedPollData.address); | ||
expect(finishedPollData.isStateAqMerged).to.eq(true); | ||
}); | ||
|
||
it("should throw error if current poll id is invalid", async () => { | ||
await expect(getPoll({ maciAddress: maciAddresses.maciAddress, pollId: -1n, signer })).eventually.rejectedWith( | ||
"Invalid poll id -1", | ||
); | ||
}); | ||
|
||
it("should throw error if current poll is not deployed", async () => { | ||
await expect(getPoll({ maciAddress: maciAddresses.maciAddress, pollId: 9000n, signer })).eventually.rejectedWith( | ||
"MACI contract doesn't have any deployed poll 9000", | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ZeroAddress } from "ethers"; | ||
import { MACI__factory as MACIFactory, Poll__factory as PollFactory } from "maci-contracts/typechain-types"; | ||
|
||
import type { IGetPollArgs, IGetPollData } from "../utils/interfaces"; | ||
|
||
import { banner } from "../utils/banner"; | ||
import { logError, logGreen, success } from "../utils/theme"; | ||
|
||
/** | ||
* Get deployed poll from MACI contract | ||
* @param {IGetPollArgs} args - The arguments for the get poll command | ||
* @returns {IGetPollData} poll data | ||
*/ | ||
export const getPoll = async ({ maciAddress, signer, pollId, quiet = true }: IGetPollArgs): Promise<IGetPollData> => { | ||
banner(quiet); | ||
|
||
const maciContract = MACIFactory.connect(maciAddress, signer); | ||
const id = | ||
pollId === undefined ? await maciContract.nextPollId().then((nextPollId) => nextPollId - 1n) : BigInt(pollId); | ||
|
||
if (id < 0n) { | ||
logError(`Invalid poll id ${id}`); | ||
} | ||
|
||
const pollAddress = await maciContract.polls(id); | ||
|
||
if (pollAddress === ZeroAddress) { | ||
logError(`MACI contract doesn't have any deployed poll ${id}`); | ||
} | ||
|
||
const pollContract = PollFactory.connect(pollAddress, signer); | ||
|
||
const [[deployTime, duration], isStateAqMerged] = await Promise.all([ | ||
pollContract.getDeployTimeAndDuration(), | ||
pollContract.stateAqMerged(), | ||
]); | ||
|
||
const numSignups = await (isStateAqMerged ? pollContract.numSignups() : maciContract.numSignUps()); | ||
|
||
logGreen( | ||
quiet, | ||
success( | ||
[ | ||
`ID: ${id}`, | ||
`Deploy time: ${new Date(Number(deployTime) * 1000).toString()}`, | ||
`End time: ${new Date(Number(deployTime + duration) * 1000).toString()}`, | ||
`Number of signups ${numSignups}`, | ||
`State Aq merged: ${isStateAqMerged}`, | ||
].join("\n"), | ||
), | ||
); | ||
|
||
return { | ||
id, | ||
address: pollAddress, | ||
deployTime, | ||
duration, | ||
numSignups, | ||
isStateAqMerged, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
import { genKeyPair } from "../commands/genKeyPair"; | ||
import { genMaciPubKey } from "../commands/genPubKey"; | ||
import { getPoll } from "../commands/poll"; | ||
import { publish } from "../commands/publish"; | ||
import { signup, isRegisteredUser } from "../commands/signup"; | ||
import { verify } from "../commands/verify"; | ||
|
||
export { genKeyPair, genMaciPubKey, publish, signup, isRegisteredUser, verify }; | ||
export { genKeyPair, genMaciPubKey, publish, signup, isRegisteredUser, verify, getPoll }; | ||
|
||
export type { Signer } from "ethers"; | ||
|
||
export type { | ||
DeployedContracts, | ||
PollContracts, | ||
TallyData, | ||
SubsidyData, | ||
PublishArgs, | ||
SignupArgs, | ||
ISignupData, | ||
VerifyArgs, | ||
IGetPollArgs, | ||
IGetPollData, | ||
IRegisteredUserArgs, | ||
} from "../utils"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.