@crisp-e3/sdk 0.12.0 → 0.14.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
@@ -38,6 +38,7 @@ type RoundDetails = {
38
38
  startTime: bigint;
39
39
  endTime: bigint;
40
40
  startBlock: bigint;
41
+ snapshotBlock: bigint;
41
42
  committeePublicKey: Uint8Array;
42
43
  emojis: [string, string];
43
44
  tokenAddress: string;
@@ -47,6 +48,17 @@ type RoundDetails = {
47
48
  creditMode: CreditMode;
48
49
  credits?: bigint;
49
50
  };
51
+ /**
52
+ * Type representing the round data stored in the CRISPProgram contract
53
+ */
54
+ type OnChainRoundData = {
55
+ merkleRoot: bigint;
56
+ paramsHash: `0x${string}`;
57
+ numOptions: bigint;
58
+ creditMode: CreditMode;
59
+ inputRoot: bigint;
60
+ numberOfVotes: bigint;
61
+ };
50
62
  /**
51
63
  * Type representing the token details required for participation in a round
52
64
  */
@@ -128,6 +140,7 @@ type E3StateLiteResponse = {
128
140
  start_time: number;
129
141
  end_time: number;
130
142
  start_block: number;
143
+ snapshot_block: number;
131
144
  committee_public_key: number[];
132
145
  emojis: [string, string];
133
146
  token_address: string;
@@ -224,6 +237,19 @@ declare const getRoundDetails: (serverUrl: string, e3Id: number) => Promise<Roun
224
237
  * @returns The token address, balance threshold and snapshot block
225
238
  */
226
239
  declare const getRoundTokenDetails: (serverUrl: string, e3Id: number) => Promise<TokenDetails>;
240
+ /**
241
+ * Get the round data stored in the CRISPProgram contract, such as the merkle root
242
+ * of the census and the merkle root of the encrypted votes published so far.
243
+ *
244
+ * Unlike {@link getRoundDetails}, this reads directly from the chain and so does not
245
+ * depend on the CRISP server.
246
+ *
247
+ * @param programAddress - The address of the CRISPProgram contract
248
+ * @param e3Id - The e3Id of the round
249
+ * @param chainId - The chain ID of the network the program is deployed on
250
+ * @returns The on chain round data
251
+ */
252
+ declare const getOnChainRoundData: (programAddress: string, e3Id: number, chainId: number) => Promise<OnChainRoundData>;
227
253
  /**
228
254
  * Get the previous ciphertext for a slot from the CRISP server.
229
255
  * Returns undefined when the slot is empty (404).
@@ -531,6 +557,17 @@ declare class CrispSDK {
531
557
  * @returns The round details
532
558
  */
533
559
  getRoundDetails(e3Id: number): Promise<RoundDetails>;
560
+ /**
561
+ * Get the round data stored in the CRISPProgram contract, read directly from the chain.
562
+ *
563
+ * When the chain id is omitted it is looked up on the CRISP server.
564
+ *
565
+ * @param programAddress - The address of the CRISPProgram contract
566
+ * @param e3Id - The e3Id of the round
567
+ * @param chainId - The chain ID of the network the program is deployed on
568
+ * @returns The on chain round data
569
+ */
570
+ getOnChainRoundData(programAddress: string, e3Id: number, chainId?: number): Promise<OnChainRoundData>;
534
571
  /**
535
572
  * Get the token address, balance threshold and snapshot block for a specific round.
536
573
  * @param e3Id - The e3Id of the round
@@ -559,4 +596,4 @@ declare class CrispSDK {
559
596
  getPreviousCiphertext(e3Id: number, address: string): Promise<Uint8Array | undefined>;
560
597
  }
561
598
 
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 };
599
+ 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}`, {
@@ -175,6 +172,7 @@ var getRoundDetails = async (serverUrl, e3Id) => {
175
172
  startTime: BigInt(data.start_time),
176
173
  endTime: BigInt(data.end_time),
177
174
  startBlock: BigInt(data.start_block),
175
+ snapshotBlock: BigInt(data.snapshot_block),
178
176
  committeePublicKey: new Uint8Array(data.committee_public_key),
179
177
  emojis: data.emojis,
180
178
  numOptions: BigInt(data.num_options),
@@ -188,7 +186,26 @@ var getRoundTokenDetails = async (serverUrl, e3Id) => {
188
186
  return {
189
187
  tokenAddress: roundDetails.tokenAddress,
190
188
  threshold: roundDetails.balanceThreshold,
191
- snapshotBlock: roundDetails.startBlock
189
+ snapshotBlock: roundDetails.snapshotBlock
190
+ };
191
+ };
192
+ var getOnChainRoundData = async (programAddress, e3Id, chainId) => {
193
+ const publicClient = getPublicClient(chainId);
194
+ const [merkleRoot, paramsHash, numOptions, creditMode, inputRoot, numberOfVotes] = await publicClient.readContract({
195
+ address: programAddress,
196
+ abi: parseAbi2([
197
+ "function getRoundData(uint256 e3Id) view returns (uint256 merkleRoot, bytes32 paramsHash, uint256 numOptions, uint8 creditMode, uint256 inputRoot, uint40 numberOfVotes)"
198
+ ]),
199
+ functionName: "getRoundData",
200
+ args: [BigInt(e3Id)]
201
+ });
202
+ return {
203
+ merkleRoot,
204
+ paramsHash,
205
+ numOptions,
206
+ creditMode,
207
+ inputRoot,
208
+ numberOfVotes: BigInt(numberOfVotes)
192
209
  };
193
210
  };
194
211
  var getPreviousCiphertext = async (serverUrl, e3Id, address) => {
@@ -3543,6 +3560,20 @@ var CrispSDK = class {
3543
3560
  async getRoundDetails(e3Id) {
3544
3561
  return getRoundDetails(this.serverUrl, e3Id);
3545
3562
  }
3563
+ /**
3564
+ * Get the round data stored in the CRISPProgram contract, read directly from the chain.
3565
+ *
3566
+ * When the chain id is omitted it is looked up on the CRISP server.
3567
+ *
3568
+ * @param programAddress - The address of the CRISPProgram contract
3569
+ * @param e3Id - The e3Id of the round
3570
+ * @param chainId - The chain ID of the network the program is deployed on
3571
+ * @returns The on chain round data
3572
+ */
3573
+ async getOnChainRoundData(programAddress, e3Id, chainId) {
3574
+ const chain = chainId ?? Number((await getRoundDetails(this.serverUrl, e3Id)).chainId);
3575
+ return getOnChainRoundData(programAddress, e3Id, chain);
3576
+ }
3546
3577
  /**
3547
3578
  * Get the token address, balance threshold and snapshot block for a specific round.
3548
3579
  * @param e3Id - The e3Id of the round
@@ -3609,6 +3640,7 @@ export {
3609
3640
  getCurrentRound,
3610
3641
  getEligibleAddresses,
3611
3642
  getMaxVoteValue,
3643
+ getOnChainRoundData,
3612
3644
  getPreviousCiphertext,
3613
3645
  getRoundCiphertext,
3614
3646
  getRoundDetails,