Skip to content

Commit

Permalink
feat(cli): add provider fallback for poll data sdk method
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmad committed Mar 20, 2024
1 parent 9ad1edd commit 580b92e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
8 changes: 7 additions & 1 deletion cli/tests/unit/poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("poll", () => {
});

it("should get finished poll properly", async () => {
const pollData = await getPoll({ maciAddress: maciAddresses.maciAddress, signer });
const pollData = await getPoll({ maciAddress: maciAddresses.maciAddress, provider: signer.provider! });

await timeTravel({ seconds: Number(pollData.duration), signer });
await mergeMessages({ pollId: BigInt(pollData.id), signer });
Expand All @@ -66,6 +66,12 @@ describe("poll", () => {
expect(finishedPollData.isStateAqMerged).to.eq(true);
});

it("should throw error if there are no signer and provider", async () => {
await expect(getPoll({ maciAddress: maciAddresses.maciAddress, pollId: -1n })).eventually.rejectedWith(
"No signer and provider are provided",
);
});

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",
Expand Down
16 changes: 13 additions & 3 deletions cli/ts/commands/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ 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,
provider,
pollId,
quiet = true,
}: IGetPollArgs): Promise<IGetPollData> => {
banner(quiet);

const maciContract = MACIFactory.connect(maciAddress, signer);
if (!signer && !provider) {
logError("No signer and provider are provided");
}

const maciContract = MACIFactory.connect(maciAddress, signer ?? provider);
const id =
pollId === undefined ? await maciContract.nextPollId().then((nextPollId) => nextPollId - 1n) : BigInt(pollId);

Expand All @@ -28,7 +38,7 @@ export const getPoll = async ({ maciAddress, signer, pollId, quiet = true }: IGe
logError(`MACI contract doesn't have any deployed poll ${id}`);
}

const pollContract = PollFactory.connect(pollAddress, signer);
const pollContract = PollFactory.connect(pollAddress, signer ?? provider);

const [[deployTime, duration], isStateAqMerged] = await Promise.all([
pollContract.getDeployTimeAndDuration(),
Expand Down
10 changes: 7 additions & 3 deletions cli/ts/utils/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Signer } from "ethers";

import type { Provider, Signer } from "ethers";
import type { SnarkProof } from "maci-contracts";
import type { CircuitInputs } from "maci-core";
import type { IMessageContractParams } from "maci-domainobjs";
Expand Down Expand Up @@ -973,7 +972,12 @@ export interface IGetPollArgs {
/**
* A signer object
*/
signer: Signer;
signer?: Signer;

/**
* A provider fallback object
*/
provider?: Provider;

/**
* The address of the MACI contract
Expand Down

0 comments on commit 580b92e

Please sign in to comment.