@crisp-e3/sdk 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -47,6 +47,17 @@ type RoundDetails = {
47
47
  creditMode: CreditMode;
48
48
  credits?: bigint;
49
49
  };
50
+ /**
51
+ * Type representing the round data stored in the CRISPProgram contract
52
+ */
53
+ type OnChainRoundData = {
54
+ merkleRoot: bigint;
55
+ paramsHash: `0x${string}`;
56
+ numOptions: bigint;
57
+ creditMode: CreditMode;
58
+ inputRoot: bigint;
59
+ numberOfVotes: bigint;
60
+ };
50
61
  /**
51
62
  * Type representing the token details required for participation in a round
52
63
  */
@@ -224,6 +235,19 @@ declare const getRoundDetails: (serverUrl: string, e3Id: number) => Promise<Roun
224
235
  * @returns The token address, balance threshold and snapshot block
225
236
  */
226
237
  declare const getRoundTokenDetails: (serverUrl: string, e3Id: number) => Promise<TokenDetails>;
238
+ /**
239
+ * Get the round data stored in the CRISPProgram contract, such as the merkle root
240
+ * of the census and the merkle root of the encrypted votes published so far.
241
+ *
242
+ * Unlike {@link getRoundDetails}, this reads directly from the chain and so does not
243
+ * depend on the CRISP server.
244
+ *
245
+ * @param programAddress - The address of the CRISPProgram contract
246
+ * @param e3Id - The e3Id of the round
247
+ * @param chainId - The chain ID of the network the program is deployed on
248
+ * @returns The on chain round data
249
+ */
250
+ declare const getOnChainRoundData: (programAddress: string, e3Id: number, chainId: number) => Promise<OnChainRoundData>;
227
251
  /**
228
252
  * Get the previous ciphertext for a slot from the CRISP server.
229
253
  * Returns undefined when the slot is empty (404).
@@ -531,6 +555,17 @@ declare class CrispSDK {
531
555
  * @returns The round details
532
556
  */
533
557
  getRoundDetails(e3Id: number): Promise<RoundDetails>;
558
+ /**
559
+ * Get the round data stored in the CRISPProgram contract, read directly from the chain.
560
+ *
561
+ * When the chain id is omitted it is looked up on the CRISP server.
562
+ *
563
+ * @param programAddress - The address of the CRISPProgram contract
564
+ * @param e3Id - The e3Id of the round
565
+ * @param chainId - The chain ID of the network the program is deployed on
566
+ * @returns The on chain round data
567
+ */
568
+ getOnChainRoundData(programAddress: string, e3Id: number, chainId?: number): Promise<OnChainRoundData>;
534
569
  /**
535
570
  * Get the token address, balance threshold and snapshot block for a specific round.
536
571
  * @param e3Id - The e3Id of the round
@@ -559,4 +594,4 @@ declare class CrispSDK {
559
594
  getPreviousCiphertext(e3Id: number, address: string): Promise<Uint8Array | undefined>;
560
595
  }
561
596
 
562
- export { type BroadcastVoteRequest, type BroadcastVoteResponse, CreditMode, CrispSDK, type CurrentRoundResponse, type E3StateLiteResponse, type JsonResponse, MAX_MSG_NON_ZERO_COEFFS, MAX_VOTE_OPTIONS, MERKLE_TREE_MAX_DEPTH, type MaskVoteProofInputs, type NewRoundRequest, type ProofData, type RoundDetails, SIGNATURE_MESSAGE, SIGNATURE_MESSAGE_HASH, type TokenDetails, type TokenHolder, type Vote, type VoteProofInputs, type VoteResponseStatus, type VoteStatusResponse, type WebResultResponse, broadcastVote, decodeTally, destroyBBApi, encodeSolidityProof, encryptVote, generateBFVKeys, generateMaskVoteProof, generateMerkleProof, generateMerkleTree, generateVoteProof, getAddressFromSignature, getAllRoundResults, getBalanceAt, getCurrentRound, getEligibleAddresses, getMaxVoteValue, getPreviousCiphertext, getRoundCiphertext, getRoundDetails, getRoundPublicKey, getRoundResult, getRoundStateLite, getRoundTokenDetails, getScaledBalance, getTokenHolderHashes, getTotalSupplyAt, getTreeData, getVoteStatus, getZeroVote, hashLeaf, requestNewRound, validateVote, verifyProof };
597
+ export { type BroadcastVoteRequest, type BroadcastVoteResponse, CreditMode, CrispSDK, type CurrentRoundResponse, type E3StateLiteResponse, type JsonResponse, MAX_MSG_NON_ZERO_COEFFS, MAX_VOTE_OPTIONS, MERKLE_TREE_MAX_DEPTH, type MaskVoteProofInputs, type NewRoundRequest, type OnChainRoundData, type ProofData, type RoundDetails, SIGNATURE_MESSAGE, SIGNATURE_MESSAGE_HASH, type TokenDetails, type TokenHolder, type Vote, type VoteProofInputs, type VoteResponseStatus, type VoteStatusResponse, type WebResultResponse, broadcastVote, decodeTally, destroyBBApi, encodeSolidityProof, encryptVote, generateBFVKeys, generateMaskVoteProof, generateMerkleProof, generateMerkleTree, generateVoteProof, getAddressFromSignature, getAllRoundResults, getBalanceAt, getCurrentRound, getEligibleAddresses, getMaxVoteValue, getOnChainRoundData, getPreviousCiphertext, getRoundCiphertext, getRoundDetails, getRoundPublicKey, getRoundResult, getRoundStateLite, getRoundTokenDetails, getScaledBalance, getTokenHolderHashes, getTotalSupplyAt, getTreeData, getVoteStatus, getZeroVote, hashLeaf, requestNewRound, validateVote, verifyProof };
package/dist/index.js CHANGED
@@ -20,8 +20,30 @@ var SIGNATURE_MESSAGE_HASH = hashMessage(SIGNATURE_MESSAGE);
20
20
  var MASK_SIGNATURE = "0x8e7d77112641d59e9409ec3052041703bb9d9e6ed39bfcf75aefbcafe829ac6b21dd7648116ad5db0466fcb4bd468dcb28f6c069def8bc47cd9d859c85a016e31b";
21
21
 
22
22
  // src/token.ts
23
- import { createPublicClient, http, parseAbi } from "viem";
23
+ import { parseAbi } from "viem";
24
+
25
+ // src/chain.ts
26
+ import { createPublicClient, http } from "viem";
24
27
  import { localhost, sepolia } from "viem/chains";
28
+ var getPublicClient = (chainId) => {
29
+ let chain;
30
+ switch (chainId) {
31
+ case 11155111:
32
+ chain = sepolia;
33
+ break;
34
+ case 31337:
35
+ chain = localhost;
36
+ break;
37
+ default:
38
+ throw new Error("Unsupported chainId");
39
+ }
40
+ return createPublicClient({
41
+ transport: http(),
42
+ chain
43
+ });
44
+ };
45
+
46
+ // src/token.ts
25
47
  var getTreeData = async (serverUrl, e3Id) => {
26
48
  const response = await fetch(`${serverUrl}/${CRISP_SERVER_TOKEN_TREE_ENDPOINT}`, {
27
49
  method: "POST",
@@ -39,21 +61,7 @@ var getTreeData = async (serverUrl, e3Id) => {
39
61
  });
40
62
  };
41
63
  var getBalanceAt = async (voterAddress, tokenAddress, snapshotBlock, chainId) => {
42
- let chain;
43
- switch (chainId) {
44
- case 11155111:
45
- chain = sepolia;
46
- break;
47
- case 31337:
48
- chain = localhost;
49
- break;
50
- default:
51
- throw new Error("Unsupported chainId");
52
- }
53
- const publicClient = createPublicClient({
54
- transport: http(),
55
- chain
56
- });
64
+ const publicClient = getPublicClient(chainId);
57
65
  const balance = await publicClient.readContract({
58
66
  address: tokenAddress,
59
67
  abi: parseAbi(["function getPastVotes(address, uint256) view returns (uint256)"]),
@@ -63,21 +71,7 @@ var getBalanceAt = async (voterAddress, tokenAddress, snapshotBlock, chainId) =>
63
71
  return balance;
64
72
  };
65
73
  var getTotalSupplyAt = async (tokenAddress, snapshotBlock, chainId) => {
66
- let chain;
67
- switch (chainId) {
68
- case 11155111:
69
- chain = sepolia;
70
- break;
71
- case 31337:
72
- chain = localhost;
73
- break;
74
- default:
75
- throw new Error("Unsupported chainId");
76
- }
77
- const publicClient = createPublicClient({
78
- transport: http(),
79
- chain
80
- });
74
+ const publicClient = getPublicClient(chainId);
81
75
  const totalSupply = await publicClient.readContract({
82
76
  address: tokenAddress,
83
77
  abi: parseAbi(["function getPastTotalSupply(uint256) view returns (uint256)"]),
@@ -87,6 +81,9 @@ var getTotalSupplyAt = async (tokenAddress, snapshotBlock, chainId) => {
87
81
  return totalSupply;
88
82
  };
89
83
 
84
+ // src/state.ts
85
+ import { parseAbi as parseAbi2 } from "viem";
86
+
90
87
  // src/api.ts
91
88
  var postJson = async (serverUrl, endpoint, body) => {
92
89
  const response = await fetch(`${serverUrl}/${endpoint}`, {
@@ -191,6 +188,25 @@ var getRoundTokenDetails = async (serverUrl, e3Id) => {
191
188
  snapshotBlock: roundDetails.startBlock
192
189
  };
193
190
  };
191
+ var getOnChainRoundData = async (programAddress, e3Id, chainId) => {
192
+ const publicClient = getPublicClient(chainId);
193
+ const [merkleRoot, paramsHash, numOptions, creditMode, inputRoot, numberOfVotes] = await publicClient.readContract({
194
+ address: programAddress,
195
+ abi: parseAbi2([
196
+ "function getRoundData(uint256 e3Id) view returns (uint256 merkleRoot, bytes32 paramsHash, uint256 numOptions, uint8 creditMode, uint256 inputRoot, uint40 numberOfVotes)"
197
+ ]),
198
+ functionName: "getRoundData",
199
+ args: [BigInt(e3Id)]
200
+ });
201
+ return {
202
+ merkleRoot,
203
+ paramsHash,
204
+ numOptions,
205
+ creditMode,
206
+ inputRoot,
207
+ numberOfVotes: BigInt(numberOfVotes)
208
+ };
209
+ };
194
210
  var getPreviousCiphertext = async (serverUrl, e3Id, address) => {
195
211
  const response = await fetch(`${serverUrl}/${CRISP_SERVER_PREVIOUS_CIPHERTEXT_ENDPOINT}`, {
196
212
  method: "POST",
@@ -3543,6 +3559,20 @@ var CrispSDK = class {
3543
3559
  async getRoundDetails(e3Id) {
3544
3560
  return getRoundDetails(this.serverUrl, e3Id);
3545
3561
  }
3562
+ /**
3563
+ * Get the round data stored in the CRISPProgram contract, read directly from the chain.
3564
+ *
3565
+ * When the chain id is omitted it is looked up on the CRISP server.
3566
+ *
3567
+ * @param programAddress - The address of the CRISPProgram contract
3568
+ * @param e3Id - The e3Id of the round
3569
+ * @param chainId - The chain ID of the network the program is deployed on
3570
+ * @returns The on chain round data
3571
+ */
3572
+ async getOnChainRoundData(programAddress, e3Id, chainId) {
3573
+ const chain = chainId ?? Number((await getRoundDetails(this.serverUrl, e3Id)).chainId);
3574
+ return getOnChainRoundData(programAddress, e3Id, chain);
3575
+ }
3546
3576
  /**
3547
3577
  * Get the token address, balance threshold and snapshot block for a specific round.
3548
3578
  * @param e3Id - The e3Id of the round
@@ -3609,6 +3639,7 @@ export {
3609
3639
  getCurrentRound,
3610
3640
  getEligibleAddresses,
3611
3641
  getMaxVoteValue,
3642
+ getOnChainRoundData,
3612
3643
  getPreviousCiphertext,
3613
3644
  getRoundCiphertext,
3614
3645
  getRoundDetails,