Skip to content

Commit 97e8ca7

Browse files
authored
Merge pull request #42 from privacy-scaling-explorations/feat/get-results
feat: get results from tally contract
2 parents 92ca538 + 71c2441 commit 97e8ca7

File tree

4 files changed

+70
-18
lines changed

4 files changed

+70
-18
lines changed

bun.lock

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"@layerzerolabs/lz-definitions": "^2.3.27",
99
"@layerzerolabs/lz-v2-utilities": "^2.3.27",
1010
"@layerzerolabs/scan-client": "^0.0.8",
11-
"@maci-protocol/domainobjs": "0.0.0-ci.8198a70",
12-
"@maci-protocol/sdk": "0.0.0-ci.8198a70",
11+
"@maci-protocol/domainobjs": "0.0.0-ci.ef4f2d5",
12+
"@maci-protocol/sdk": "0.0.0-ci.ef4f2d5",
1313
"@react-native-async-storage/async-storage": "^1.23.1",
1414
"@shazow/whatsabi": "0.11.0",
1515
"@tanstack/query-async-storage-persister": "^5.49.1",
@@ -312,15 +312,15 @@
312312

313313
"@lit/reactive-element": ["@lit/reactive-element@2.1.0", "", { "dependencies": { "@lit-labs/ssr-dom-shim": "^1.2.0" } }, "sha512-L2qyoZSQClcBmq0qajBVbhYEcG6iK0XfLn66ifLe/RfC0/ihpc+pl0Wdn8bJ8o+hj38cG0fGXRgSS20MuXn7qA=="],
314314

315-
"@maci-protocol/contracts": ["@maci-protocol/contracts@0.0.0-ci.8198a70", "", { "dependencies": { "@excubiae/contracts": "^0.11.0", "@maci-protocol/core": "0.0.0-ci.8198a70", "@maci-protocol/crypto": "0.0.0-ci.8198a70", "@maci-protocol/domainobjs": "0.0.0-ci.8198a70", "@nomicfoundation/hardhat-ethers": "^3.0.8", "@nomicfoundation/hardhat-toolbox": "^5.0.0", "@nomicfoundation/hardhat-verify": "^2.0.14", "@openzeppelin/contracts": "^5.3.0", "@openzeppelin/merkle-tree": "^1.0.8", "@pcd/util": "^0.9.0", "@zk-kit/imt.sol": "2.0.0-beta.12", "@zk-kit/lean-imt": "^2.2.3", "circomlibjs": "^0.1.7", "ethers": "^6.14.0", "hardhat": "^2.24.1", "lowdb": "^1.0.0", "snarkjs": "^0.7.5", "solady": "^0.1.18", "solidity-docgen": "^0.6.0-beta.36", "uuid": "^11.1.0" }, "bin": { "maci-contracts": "./build/ts/index.js" } }, "sha512-fTRyrL/R32r2nDlKAqdyvPd/szGQgHxz071HgZwbnA0OX1cnF5e/p0B2nM+eYn/Ztz8aYRROggGiyTbLBK771w=="],
315+
"@maci-protocol/contracts": ["@maci-protocol/contracts@0.0.0-ci.ef4f2d5", "", { "dependencies": { "@excubiae/contracts": "^0.11.0", "@maci-protocol/core": "0.0.0-ci.ef4f2d5", "@maci-protocol/crypto": "0.0.0-ci.ef4f2d5", "@maci-protocol/domainobjs": "0.0.0-ci.ef4f2d5", "@nomicfoundation/hardhat-ethers": "^3.0.8", "@nomicfoundation/hardhat-toolbox": "^5.0.0", "@nomicfoundation/hardhat-verify": "^2.0.14", "@openzeppelin/contracts": "^5.3.0", "@openzeppelin/merkle-tree": "^1.0.8", "@pcd/util": "^0.9.0", "@zk-kit/imt.sol": "2.0.0-beta.12", "@zk-kit/lean-imt": "^2.2.3", "circomlibjs": "^0.1.7", "ethers": "^6.14.0", "hardhat": "^2.24.1", "lowdb": "^1.0.0", "snarkjs": "^0.7.5", "solady": "^0.1.18", "solidity-docgen": "^0.6.0-beta.36", "uuid": "^11.1.0" }, "bin": { "maci-contracts": "./build/ts/index.js" } }, "sha512-YeNphvikPpdpmI4FOj3abV5jZyEKvdY6MF/FFmRxd31y01ShZLF5wRpIiF4qAAwAdOzvvSuSZIEF87ek2AKAuA=="],
316316

317-
"@maci-protocol/core": ["@maci-protocol/core@0.0.0-ci.8198a70", "", { "dependencies": { "@maci-protocol/crypto": "0.0.0-ci.8198a70", "@maci-protocol/domainobjs": "0.0.0-ci.8198a70", "@zk-kit/lean-imt": "^2.2.3", "lodash": "^4.17.21" } }, "sha512-uMsnOmcD1mP3+KPOMbj1LZYVted5oSgMO5glK7lSaXMHkNnNMXwYwv2whcy2uNB6lBp/VtcFpbqKWwvmp7CaLw=="],
317+
"@maci-protocol/core": ["@maci-protocol/core@0.0.0-ci.ef4f2d5", "", { "dependencies": { "@maci-protocol/crypto": "0.0.0-ci.ef4f2d5", "@maci-protocol/domainobjs": "0.0.0-ci.ef4f2d5", "@zk-kit/lean-imt": "^2.2.3", "lodash": "^4.17.21" } }, "sha512-5QC7kfdZsGx/90JNJD5pqNuLY9ccfDFMQs/UWiYd1lLl0FnPM9oGXJAKh35yiB7q7tsXBvomoc3BmnGsEq8wCQ=="],
318318

319-
"@maci-protocol/crypto": ["@maci-protocol/crypto@0.0.0-ci.8198a70", "", { "dependencies": { "@zk-kit/baby-jubjub": "^1.0.3", "@zk-kit/eddsa-poseidon": "^1.1.0", "@zk-kit/poseidon-cipher": "^0.3.2", "ethers": "^6.14.0" } }, "sha512-kPTdcVqOlG9BJ5Dwp84whfT7can6yWG+fJ42KXIHHILa8/JZ1N1QyvImkTW9lnfeGvf9Sd6t48u55Eh3zEoIOA=="],
319+
"@maci-protocol/crypto": ["@maci-protocol/crypto@0.0.0-ci.ef4f2d5", "", { "dependencies": { "@zk-kit/baby-jubjub": "^1.0.3", "@zk-kit/eddsa-poseidon": "^1.1.0", "@zk-kit/poseidon-cipher": "^0.3.2", "ethers": "^6.14.0" } }, "sha512-Vu/MrsHfmFWyo7T/kjxmNeQO/bU5+bUSCSB3Xl4bbcenyPApRB2DtkmhoHiSoz2Vn6z/VZg8M83Wzg98to6J0w=="],
320320

321-
"@maci-protocol/domainobjs": ["@maci-protocol/domainobjs@0.0.0-ci.8198a70", "", { "dependencies": { "@maci-protocol/crypto": "0.0.0-ci.8198a70" } }, "sha512-VwNFpylA3kyUW2fir8oCBCbo4wXSw9oY3kpxN+qpM0k0siLWLrINRt1o/H2/2CT+LDhciTMM3KS5p01LPp3o+Q=="],
321+
"@maci-protocol/domainobjs": ["@maci-protocol/domainobjs@0.0.0-ci.ef4f2d5", "", { "dependencies": { "@maci-protocol/crypto": "0.0.0-ci.ef4f2d5" } }, "sha512-pXOrho2c/0h/ht20ojr9kOmm9pDLLI4Hx4JDs0e9ieYDkDzC28eE+4E0koEB73ocanpG/3J0dHjGY65Q1MBgZA=="],
322322

323-
"@maci-protocol/sdk": ["@maci-protocol/sdk@0.0.0-ci.8198a70", "", { "dependencies": { "@maci-protocol/contracts": "0.0.0-ci.8198a70", "@maci-protocol/core": "0.0.0-ci.8198a70", "@maci-protocol/crypto": "0.0.0-ci.8198a70", "@maci-protocol/domainobjs": "0.0.0-ci.8198a70", "@zk-kit/lean-imt": "^2.2.3", "ethers": "^6.14.0", "multiformats": "9.9.0", "snarkjs": "^0.7.5" }, "bin": { "maci-sdk": "./build/ts/index.js" } }, "sha512-uK+eUhrYqgR7CanzZKU/TDr9T1VrgdS67JxXQmmsODD6/Yan6sCU+yg4SjRotUwHHCrxwtM4aNUAp51X0EBIxg=="],
323+
"@maci-protocol/sdk": ["@maci-protocol/sdk@0.0.0-ci.ef4f2d5", "", { "dependencies": { "@maci-protocol/contracts": "0.0.0-ci.ef4f2d5", "@maci-protocol/core": "0.0.0-ci.ef4f2d5", "@maci-protocol/crypto": "0.0.0-ci.ef4f2d5", "@maci-protocol/domainobjs": "0.0.0-ci.ef4f2d5", "@zk-kit/lean-imt": "^2.2.3", "ethers": "^6.14.0", "multiformats": "9.9.0", "snarkjs": "^0.7.5" }, "bin": { "maci-sdk": "./build/ts/index.js" } }, "sha512-LrtaULgwY/+asPoy+yhEe5brKvA/1wzX78kxOslL7bXGdNLNvA4G9LcwgYEbMYNYX6YuWZIGFr4o43/wbFq+rQ=="],
324324

325325
"@metamask/abi-utils": ["@metamask/abi-utils@2.0.4", "", { "dependencies": { "@metamask/superstruct": "^3.1.0", "@metamask/utils": "^9.0.0" } }, "sha512-StnIgUB75x7a7AgUhiaUZDpCsqGp7VkNnZh2XivXkJ6mPkE83U8ARGQj5MbRis7VJY8BC5V1AbB1fjdh0hupPQ=="],
326326

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
"@layerzerolabs/lz-definitions": "^2.3.27",
2626
"@layerzerolabs/lz-v2-utilities": "^2.3.27",
2727
"@layerzerolabs/scan-client": "^0.0.8",
28-
"@maci-protocol/domainobjs": "0.0.0-ci.8198a70",
29-
"@maci-protocol/sdk": "0.0.0-ci.8198a70",
28+
"@maci-protocol/domainobjs": "0.0.0-ci.ef4f2d5",
29+
"@maci-protocol/sdk": "0.0.0-ci.ef4f2d5",
3030
"@react-native-async-storage/async-storage": "^1.23.1",
3131
"@shazow/whatsabi": "0.11.0",
3232
"@tanstack/query-async-storage-persister": "^5.49.1",

plugins/maciVoting/components/PollCard.tsx

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ import { useMaci } from "../hooks/useMaci";
44
import { VoteOption } from "../utils/types";
55
import { PleaseWaitSpinner } from "@/components/please-wait";
66
import { PUBLIC_MACI_ADDRESS } from "@/constants";
7-
import { getPoll } from "@maci-protocol/sdk/browser";
7+
import { getPoll, getResults, type IResult } from "@maci-protocol/sdk/browser";
88
import { useEthersSigner } from "../hooks/useEthersSigner";
99
import { unixTimestampToDate } from "../utils/formatPollDate";
10+
import { useCoordinator } from "../hooks/useCoordinator";
1011

1112
const PollCard = ({ pollId }: { pollId: bigint }) => {
1213
// check if the user joined the poll
1314
const { setPollId, onJoinPoll, onVote, isRegistered, hasJoinedPoll, isLoading, error: maciError } = useMaci();
15+
const { checkIsTallied } = useCoordinator();
1416
const signer = useEthersSigner();
1517
const [error, setError] = useState<string | undefined>(undefined);
1618
const [voteStartDate, setVoteStartDate] = useState(0);
1719
const [voteEnded, setVoteEnded] = useState(false);
1820
const [voteOption, setVoteOption] = useState<VoteOption | undefined>(undefined);
21+
const [results, setResults] = useState<IResult[] | undefined>(undefined);
22+
const [tallied, setTallied] = useState(false);
1923

2024
const disabled = useMemo(() => {
2125
return isLoading || voteEnded || voteStartDate > Math.round(Date.now() / 1000);
@@ -43,8 +47,30 @@ const PollCard = ({ pollId }: { pollId: bigint }) => {
4347
setVoteEnded(endDate < now);
4448
};
4549

50+
const checkPollTallied = async () => {
51+
const isTallied = await checkIsTallied(Number(pollId));
52+
setTallied(isTallied);
53+
};
54+
55+
const getPollResults = async () => {
56+
const contractResults = await getResults({
57+
maciAddress: PUBLIC_MACI_ADDRESS,
58+
pollId: pollId.toString(),
59+
signer,
60+
});
61+
setResults(contractResults);
62+
};
63+
4664
checkVoteEnded();
47-
}, [voteEnded, setVoteEnded, pollId, signer]);
65+
66+
if (voteEnded) {
67+
checkPollTallied();
68+
}
69+
70+
if (tallied) {
71+
getPollResults();
72+
}
73+
}, [voteEnded, setVoteEnded, pollId, signer, checkIsTallied, tallied, getResults]);
4874

4975
useEffect(() => {
5076
setPollId(pollId);
@@ -83,16 +109,42 @@ const PollCard = ({ pollId }: { pollId: bigint }) => {
83109
return "Join poll";
84110
}, [hasJoinedPoll, isLoading]);
85111

86-
if (voteEnded)
112+
if (voteEnded && !tallied)
113+
return (
114+
<Card className="flex flex-col gap-y-4 p-6 shadow-neutral">
115+
<Heading size="h3">MACI Poll</Heading>
116+
<div className="flex flex-col justify-between">
117+
<p className="text-sm text-critical-500">{error}</p>
118+
</div>
119+
<p>The voting period has ended and you can no longer vote. Please wait for the results to be tallied.</p>
120+
</Card>
121+
);
122+
123+
if (voteEnded && tallied && !results)
87124
return (
88125
<Card className="flex flex-col gap-y-4 p-6 shadow-neutral">
89126
<Heading size="h3">MACI Poll</Heading>
90127
<div className="flex flex-col justify-between">
91128
<p className="text-sm text-critical-500">{error}</p>
92129
</div>
93-
<p>The voting period has ended. You can no longer vote.</p>
130+
<p>The results have been tallied. Results will be displayed here soon.</p>
131+
</Card>
132+
);
133+
134+
if (voteEnded && tallied && results) {
135+
return (
136+
<Card className="flex flex-col gap-y-4 p-6 shadow-neutral">
137+
<Heading size="h3">Results</Heading>
138+
<div className="flex flex-col justify-between">
139+
<p className="text-sm text-critical-500">{error}</p>
140+
</div>
141+
<p>The voting period has ended. Here are the results:</p>
142+
<p style={{ color: "green" }}>Yes: {results[0].value.toString()}</p>
143+
<p style={{ color: "red" }}>No: {results[1].value.toString()}</p>
144+
<p style={{ color: "blue" }}>Abstain: {results[2].value.toString()}</p>
94145
</Card>
95146
);
147+
}
96148

97149
if (!isRegistered)
98150
return (

plugins/maciVoting/contexts/CoordinatorContext.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EMode } from "@maci-protocol/core";
2-
import { getPoll, getPollContracts, type ITallyData, Poll__factory as PollFactory } from "@maci-protocol/sdk/browser";
2+
import { getPoll, isTallied, type ITallyData, Poll__factory as PollFactory } from "@maci-protocol/sdk/browser";
33
import { PUBLIC_CHAIN_NAME, PUBLIC_COORDINATOR_SERVICE_URL, PUBLIC_MACI_ADDRESS } from "@/constants";
44
import { createContext, type ReactNode, useCallback, useMemo, useState } from "react";
55
import {
@@ -105,13 +105,12 @@ export const CoordinatorProvider = ({ children }: { children: ReactNode }) => {
105105
return false;
106106
}
107107

108-
const pollContracts = await getPollContracts({
108+
const isPollTallied = await isTallied({
109109
maciAddress: PUBLIC_MACI_ADDRESS,
110-
pollId,
110+
pollId: pollId.toString(),
111111
signer,
112112
});
113-
const isTallied = await pollContracts.tally.isTallied();
114-
return isTallied;
113+
return isPollTallied;
115114
},
116115
[signer]
117116
);
@@ -124,6 +123,7 @@ export const CoordinatorProvider = ({ children }: { children: ReactNode }) => {
124123
}
125124

126125
const isTallied = await checkIsTallied(pollId);
126+
127127
if (isTallied) {
128128
setFinalizeStatus("notStarted");
129129
return;

0 commit comments

Comments
 (0)