diff --git a/cli/tests/unit/poll.test.ts b/cli/tests/unit/poll.test.ts index 90bd5b34f0..f357805344 100644 --- a/cli/tests/unit/poll.test.ts +++ b/cli/tests/unit/poll.test.ts @@ -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 }); @@ -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", diff --git a/cli/ts/commands/poll.ts b/cli/ts/commands/poll.ts index 9cb1a95230..4b961a5ba2 100644 --- a/cli/ts/commands/poll.ts +++ b/cli/ts/commands/poll.ts @@ -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 => { +export const getPoll = async ({ + maciAddress, + signer, + provider, + pollId, + quiet = true, +}: IGetPollArgs): Promise => { 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); @@ -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(), diff --git a/cli/ts/utils/interfaces.ts b/cli/ts/utils/interfaces.ts index 37f478fed2..13a4c7ea5a 100644 --- a/cli/ts/utils/interfaces.ts +++ b/cli/ts/utils/interfaces.ts @@ -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"; @@ -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