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

feat(cli): add provider fallback for poll data sdk method #1320

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
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
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
Loading