Skip to content

Commit

Permalink
feat(cli): add initial voice credits for poll sdk method
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmad committed Mar 14, 2024
1 parent e98881c commit d8f3e0c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
15 changes: 12 additions & 3 deletions cli/tests/unit/poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
mergeMessages,
mergeSignups,
} from "../../ts/commands";
import { DeployedContracts, PollContracts } from "../../ts/utils";
import { DEFAULT_INITIAL_VOICE_CREDITS, DEFAULT_IVCP_DATA, DeployedContracts, PollContracts } from "../../ts/utils";
import { deployPollArgs, setVerifyingKeysArgs, deployArgs } from "../constants";
import { cleanVanilla } from "../utils";

Expand Down Expand Up @@ -53,16 +53,25 @@ describe("poll", () => {
});

it("should get finished poll properly", async () => {
const pollData = await getPoll({ maciAddress: maciAddresses.maciAddress, signer });
const pollData = await getPoll({
maciAddress: maciAddresses.maciAddress,
ivcpDataArg: DEFAULT_IVCP_DATA,
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 });
const finishedPollData = await getPoll({
maciAddress: maciAddresses.maciAddress,
ivcpDataArg: DEFAULT_IVCP_DATA,
signer,
});

expect(pollData.id).to.eq(finishedPollData.id);
expect(pollData.address).to.eq(finishedPollData.address);
expect(pollData.voiceCredits).to.eq(BigInt(DEFAULT_INITIAL_VOICE_CREDITS));
expect(finishedPollData.isStateAqMerged).to.eq(true);
});

Expand Down
31 changes: 27 additions & 4 deletions cli/ts/commands/poll.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ZeroAddress } from "ethers";
import { MACI__factory as MACIFactory, Poll__factory as PollFactory } from "maci-contracts/typechain-types";
import {
InitialVoiceCreditProxy__factory as InitialVoiceCreditProxyFactory,
MACI__factory as MACIFactory,
Poll__factory as PollFactory,
} from "maci-contracts/typechain-types";

import type { IGetPollArgs, IGetPollData } from "../utils/interfaces";

import { DEFAULT_IVCP_DATA } from "../utils";
import { banner } from "../utils/banner";
import { logError, logGreen, success } from "../utils/theme";

Expand All @@ -11,28 +16,44 @@ import { logError, logGreen, success } from "../utils/theme";
* @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> => {
export const getPoll = async ({
maciAddress,
signer,
pollId,
ivcpDataArg,
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);
const ivcpData = ivcpDataArg || DEFAULT_IVCP_DATA;

if (id < 0n) {
logError(`Invalid poll id ${id}`);
}

const pollAddress = await maciContract.polls(id);
const [pollAddress, initialVoiceCreditProxyAddress, signerAddress] = await Promise.all([
maciContract.polls(id),
maciContract.initialVoiceCreditProxy(),
signer.getAddress(),
]);

if (pollAddress === ZeroAddress) {
logError(`MACI contract doesn't have any deployed poll ${id}`);
}

const pollContract = PollFactory.connect(pollAddress, signer);
const initialVoiceCreditProxyContract = InitialVoiceCreditProxyFactory.connect(
initialVoiceCreditProxyAddress,
signer,
);

const [[deployTime, duration], isStateAqMerged] = await Promise.all([
const [[deployTime, duration], isStateAqMerged, voiceCredits] = await Promise.all([
pollContract.getDeployTimeAndDuration(),
pollContract.stateAqMerged(),
initialVoiceCreditProxyContract.getVoiceCredits(signerAddress, ivcpData),
]);

const numSignups = await (isStateAqMerged ? pollContract.numSignups() : maciContract.numSignUps());
Expand All @@ -46,6 +67,7 @@ export const getPoll = async ({ maciAddress, signer, pollId, quiet = true }: IGe
`End time: ${new Date(Number(deployTime + duration) * 1000).toString()}`,
`Number of signups ${numSignups}`,
`State Aq merged: ${isStateAqMerged}`,
`Initial voice credits: ${voiceCredits}`,
].join("\n"),
),
);
Expand All @@ -57,5 +79,6 @@ export const getPoll = async ({ maciAddress, signer, pollId, quiet = true }: IGe
duration,
numSignups,
isStateAqMerged,
voiceCredits,
};
};
10 changes: 10 additions & 0 deletions cli/ts/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,11 @@ export interface IGetPollArgs {
*/
pollId?: BigNumberish;

/**
* The initial voice credit proxy data
*/
ivcpDataArg?: string;

/**
* Whether to log the output
*/
Expand Down Expand Up @@ -1024,6 +1029,11 @@ export interface IGetPollData {
* Whether the MACI contract's stateAq has been merged by this contract
*/
isStateAqMerged: boolean;

/**
* Available voice credits for the user
*/
voiceCredits: bigint;
}

/**
Expand Down

0 comments on commit d8f3e0c

Please sign in to comment.