@dorafactory/maci-sdk 0.0.20 → 0.0.21

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/browser.js CHANGED
@@ -26026,6 +26026,8 @@ var ERROR = {
26026
26026
  ERROR_CIRCUIT_NOT_FOUND: "ERROR_CIRCUIT_NOT_FOUND",
26027
26027
  ERROR_OPERATOR_INVALID_ADDRESS: "ERROR_OPERATOR_INVALID_ADDRESS",
26028
26028
  ERROR_OPERATOR_NOT_FOUND: "ERROR_OPERATOR_NOT_FOUND",
26029
+ ERROR_OPERATOR_DELAY_HISTORY_NOT_FOUND: "ERROR_OPERATOR_DELAY_HISTORY_NOT_FOUND",
26030
+ ERROR_QUERY_MISS_RATE_FAILED: "ERROR_QUERY_MISS_RATE_FAILED",
26029
26031
  ERROR_OPERATORS_NOT_FOUND: "ERROR_OPERATORS_NOT_FOUND",
26030
26032
  ERROR_PROOF_NOT_FOUND: "ERROR_PROOF_NOT_FOUND",
26031
26033
  ERROR_ROUND_INVALID_ADDRESS: "ERROR_ROUND_INVALID_ADDRESS",
@@ -26460,6 +26462,64 @@ var Operator = class {
26460
26462
  return handleError(error);
26461
26463
  }
26462
26464
  }
26465
+ async getOperatorDelayOperationsByAddress(address, after, limit) {
26466
+ try {
26467
+ if (!isValidAddress(address)) {
26468
+ return {
26469
+ code: 400,
26470
+ error: {
26471
+ message: "Invalid operator address format",
26472
+ type: ERROR.ERROR_OPERATOR_INVALID_ADDRESS
26473
+ }
26474
+ };
26475
+ }
26476
+ const OPERATORS_QUERY = `query ($limit: Int, $after: Cursor) {
26477
+ operatorDelayOperations(first: $limit, after: $after, filter: {operatorAddress: {equalTo: "${address}"}}, orderBy: [TIMESTAMP_DESC]) {
26478
+ pageInfo {
26479
+ endCursor
26480
+ hasNextPage
26481
+ }
26482
+ totalCount
26483
+ edges {
26484
+ cursor
26485
+ node {
26486
+ blockHeight
26487
+ delayProcessDmsgCount
26488
+ delayDuration
26489
+ delayReason
26490
+ delayType
26491
+ id
26492
+ nodeId
26493
+ operatorAddress
26494
+ timestamp
26495
+ roundAddress
26496
+ }
26497
+ }
26498
+ }
26499
+ }`;
26500
+ const response = await this.http.fetchGraphql(
26501
+ OPERATORS_QUERY,
26502
+ after,
26503
+ limit
26504
+ );
26505
+ if (!response || !response.data || !response.data.operatorDelayOperations || !response.data.operatorDelayOperations.edges || response.data.operatorDelayOperations.edges.length === 0) {
26506
+ return {
26507
+ code: 404,
26508
+ error: {
26509
+ message: `No operatorDelayOperations found for address ${address}`,
26510
+ type: ERROR.ERROR_OPERATOR_DELAY_HISTORY_NOT_FOUND
26511
+ }
26512
+ };
26513
+ }
26514
+ const operator = {
26515
+ code: 200,
26516
+ data: response.data
26517
+ };
26518
+ return operator;
26519
+ } catch (error) {
26520
+ return handleError(error);
26521
+ }
26522
+ }
26463
26523
  async getOperators(after, limit) {
26464
26524
  try {
26465
26525
  const OPERATORS_QUERY = `query ($limit: Int, $after: Cursor) {
@@ -26563,6 +26623,228 @@ var Operator = class {
26563
26623
  return handleError(error);
26564
26624
  }
26565
26625
  }
26626
+ async queryMissRate(address, durationDay) {
26627
+ try {
26628
+ const now = /* @__PURE__ */ new Date();
26629
+ const startTime = new Date(
26630
+ now.getTime() - durationDay * 24 * 60 * 60 * 1e3
26631
+ );
26632
+ const startTimestamp = Math.floor(startTime.getTime() / 1e3);
26633
+ const endNanosTimestamp = Math.floor(startTime.getTime() * 1e6);
26634
+ const txTimestamp = Math.floor(startTime.getTime());
26635
+ const QUERY = `query ($limit: Int, $after: Cursor) {
26636
+ operatorDelayOperations(
26637
+ first: $limit,
26638
+ after: $after,
26639
+ filter: {
26640
+ operatorAddress: {equalTo: "${address}"},
26641
+ timestamp: { greaterThanOrEqualTo: "${startTimestamp}" }
26642
+ },
26643
+ orderBy: [TIMESTAMP_DESC]
26644
+ ) {
26645
+ edges {
26646
+ node {
26647
+ blockHeight
26648
+ delayProcessDmsgCount
26649
+ delayDuration
26650
+ delayReason
26651
+ delayType
26652
+ id
26653
+ nodeId
26654
+ operatorAddress
26655
+ timestamp
26656
+ roundAddress
26657
+ }
26658
+ }
26659
+ }
26660
+ }`;
26661
+ const ROUNDS_QUERY = `query ($limit: Int, $after: Cursor) {
26662
+ rounds(first: $limit, after: $after,
26663
+ filter: {
26664
+ operator: {equalTo: "${address}"},
26665
+ votingEnd: { greaterThanOrEqualTo: "${endNanosTimestamp}" }
26666
+ },
26667
+ orderBy: [TIMESTAMP_DESC]
26668
+ ){
26669
+ pageInfo {
26670
+ endCursor
26671
+ hasNextPage
26672
+ }
26673
+ totalCount
26674
+ edges {
26675
+ node {
26676
+ id
26677
+ blockHeight
26678
+ txHash
26679
+ caller
26680
+ admin
26681
+ operator
26682
+ contractAddress
26683
+ circuitName
26684
+ timestamp
26685
+ votingStart
26686
+ votingEnd
26687
+ status
26688
+ period
26689
+ actionType
26690
+ roundTitle
26691
+ roundDescription
26692
+ roundLink
26693
+ coordinatorPubkeyX
26694
+ coordinatorPubkeyY
26695
+ voteOptionMap
26696
+ results
26697
+ allResult
26698
+ gasStationEnable
26699
+ totalGrant
26700
+ baseGrant
26701
+ totalBond
26702
+ circuitType
26703
+ circuitPower
26704
+ certificationSystem
26705
+ codeId
26706
+ maciType
26707
+ voiceCreditAmount
26708
+ preDeactivateRoot
26709
+ }
26710
+ cursor
26711
+ }
26712
+ }
26713
+ }
26714
+ `;
26715
+ const roundsResponse = await this.http.fetchGraphql(
26716
+ ROUNDS_QUERY,
26717
+ "",
26718
+ 9999
26719
+ );
26720
+ const roundContractAddresses = roundsResponse?.data?.rounds?.edges?.map(
26721
+ (edge) => edge.node.contractAddress
26722
+ ) || [];
26723
+ const TRANSACTIONS_QUERY = `query transactions($limit: Int, $after: Cursor) {
26724
+ transactions(first: $limit, after: $after,
26725
+ filter: {
26726
+ timestamp: { greaterThanOrEqualTo: "${txTimestamp}" },
26727
+ type: { equalTo: "op:procDeactivate" },
26728
+ contractAddress: { in: ${JSON.stringify(roundContractAddresses)} }
26729
+ },
26730
+ orderBy: [TIMESTAMP_DESC]
26731
+ ){
26732
+ pageInfo {
26733
+ endCursor
26734
+ hasNextPage
26735
+ }
26736
+ totalCount
26737
+ edges {
26738
+ cursor
26739
+ node {
26740
+ id
26741
+ blockHeight
26742
+ txHash
26743
+ timestamp
26744
+ type
26745
+ status
26746
+ circuitName
26747
+ fee
26748
+ gasUsed
26749
+ gasWanted
26750
+ caller
26751
+ contractAddress
26752
+ }
26753
+ }
26754
+ }
26755
+ }`;
26756
+ const [delayResponse, transactionsResponse] = await Promise.all([
26757
+ this.http.fetchGraphql(
26758
+ QUERY,
26759
+ "",
26760
+ 9999
26761
+ ),
26762
+ this.http.fetchGraphql(
26763
+ TRANSACTIONS_QUERY,
26764
+ "",
26765
+ 9999
26766
+ )
26767
+ ]);
26768
+ const dailyStats = /* @__PURE__ */ new Map();
26769
+ const endDate = /* @__PURE__ */ new Date();
26770
+ for (let i = 0; i < durationDay; i++) {
26771
+ const date = new Date(endDate.getTime() - i * 24 * 60 * 60 * 1e3).toISOString().split("T")[0];
26772
+ dailyStats.set(date, {
26773
+ delayCount: 0,
26774
+ deactivateDelay: {
26775
+ count: 0,
26776
+ dmsgCount: 0
26777
+ },
26778
+ tallyDelay: {
26779
+ count: 0
26780
+ },
26781
+ totalDelayDuration: 0,
26782
+ avgDelayDuration: 0,
26783
+ tallyCount: 0,
26784
+ deactivateCount: 0,
26785
+ missRate: 0
26786
+ });
26787
+ }
26788
+ delayResponse.data.operatorDelayOperations.edges.forEach(({ node }) => {
26789
+ const date = new Date(parseInt(node.timestamp) * 1e3).toISOString().split("T")[0];
26790
+ if (dailyStats.has(date)) {
26791
+ const stats = dailyStats.get(date);
26792
+ stats.delayCount++;
26793
+ stats.totalDelayDuration += parseInt(node.delayDuration);
26794
+ if (node.delayType === "deactivate_delay") {
26795
+ stats.deactivateDelay.count++;
26796
+ stats.deactivateDelay.dmsgCount += node.delayProcessDmsgCount;
26797
+ } else if (node.delayType === "tally_delay") {
26798
+ stats.tallyDelay.count++;
26799
+ }
26800
+ }
26801
+ });
26802
+ if (roundsResponse?.data?.rounds?.edges) {
26803
+ roundsResponse.data.rounds.edges.forEach(({ node }) => {
26804
+ const date = new Date(parseInt(node.votingEnd) / 1e6).toISOString().split("T")[0];
26805
+ if (dailyStats.has(date)) {
26806
+ const stats = dailyStats.get(date);
26807
+ stats.tallyCount++;
26808
+ }
26809
+ });
26810
+ }
26811
+ if (transactionsResponse?.data?.transactions?.edges) {
26812
+ transactionsResponse.data.transactions.edges.forEach(({ node }) => {
26813
+ const date = new Date(parseInt(node.timestamp)).toISOString().split("T")[0];
26814
+ if (dailyStats.has(date)) {
26815
+ const stats = dailyStats.get(date);
26816
+ stats.deactivateCount++;
26817
+ }
26818
+ });
26819
+ }
26820
+ return {
26821
+ code: 200,
26822
+ data: {
26823
+ missRate: Array.from(dailyStats.entries()).map(([date, stats]) => ({
26824
+ date,
26825
+ delayCount: stats.delayCount,
26826
+ deactivateDelay: stats.deactivateDelay,
26827
+ tallyDelay: stats.tallyDelay,
26828
+ totalDelayDuration: stats.totalDelayDuration,
26829
+ avgDelayDuration: stats.delayCount > 0 ? stats.totalDelayDuration / stats.delayCount : 0,
26830
+ tallyCount: stats.tallyCount,
26831
+ deactivateCount: stats.deactivateCount,
26832
+ missRate: stats.deactivateCount + stats.tallyCount > 0 ? parseFloat(
26833
+ ((stats.deactivateDelay.count + stats.tallyDelay.count) / (stats.deactivateCount + stats.tallyCount)).toFixed(2)
26834
+ ) : 0
26835
+ })).sort((a, b) => b.date.localeCompare(a.date))
26836
+ }
26837
+ };
26838
+ } catch (error) {
26839
+ return {
26840
+ code: 404,
26841
+ error: {
26842
+ message: "Query miss rate failed",
26843
+ type: ERROR.ERROR_QUERY_MISS_RATE_FAILED
26844
+ }
26845
+ };
26846
+ }
26847
+ }
26566
26848
  };
26567
26849
 
26568
26850
  // src/libs/query/round.ts
@@ -27398,6 +27680,16 @@ var Indexer = class {
27398
27680
  async getOperatorByAddress(address) {
27399
27681
  return await this.operator.getOperatorByAddress(address);
27400
27682
  }
27683
+ async getOperatorDelayOperationsByAddress(address, after, limit) {
27684
+ return await this.operator.getOperatorDelayOperationsByAddress(
27685
+ address,
27686
+ after,
27687
+ limit
27688
+ );
27689
+ }
27690
+ async queryMissRate(address, durationDay) {
27691
+ return await this.operator.queryMissRate(address, durationDay);
27692
+ }
27401
27693
  /**
27402
27694
  * @method getOperators
27403
27695
  * @description Get multiple operators.
@@ -29550,7 +29842,7 @@ function getContractParams(type, circuitType, proofSystem, maxVoter, maxOption)
29550
29842
  `Invalid proof system ${proofSystem}, only support GROTH16 and PLONK`
29551
29843
  );
29552
29844
  }
29553
- if (Number(maxVoter) <= 25 && Number(maxOption) <= 5) {
29845
+ if (maxVoter <= 25 && maxOption <= 5) {
29554
29846
  parameters = CIRCUIT_INFO["2-1-1-5"].parameter;
29555
29847
  if (proofSystem === "groth16" /* GROTH16 */) {
29556
29848
  groth16ProcessVkey = CIRCUIT_INFO["2-1-1-5"]["groth16"].process_vkey;
@@ -29559,7 +29851,7 @@ function getContractParams(type, circuitType, proofSystem, maxVoter, maxOption)
29559
29851
  plonkProcessVkey = CIRCUIT_INFO["2-1-1-5"]["plonk"]?.process_vkey;
29560
29852
  plonkTallyVkey = CIRCUIT_INFO["2-1-1-5"]["plonk"]?.tally_vkey;
29561
29853
  }
29562
- } else if (Number(maxVoter) <= 625 && Number(maxOption) <= 25) {
29854
+ } else if (maxVoter <= 625 && maxOption <= 25) {
29563
29855
  parameters = CIRCUIT_INFO["4-2-2-25"].parameter;
29564
29856
  if (proofSystem === "groth16" /* GROTH16 */) {
29565
29857
  groth16ProcessVkey = CIRCUIT_INFO["4-2-2-25"]["groth16"].process_vkey;
@@ -29568,7 +29860,7 @@ function getContractParams(type, circuitType, proofSystem, maxVoter, maxOption)
29568
29860
  plonkProcessVkey = CIRCUIT_INFO["4-2-2-25"]["plonk"]?.process_vkey;
29569
29861
  plonkTallyVkey = CIRCUIT_INFO["4-2-2-25"]["plonk"]?.tally_vkey;
29570
29862
  }
29571
- } else if (Number(maxVoter) <= 15625 && Number(maxOption) <= 125) {
29863
+ } else if (maxVoter <= 15625 && maxOption <= 125) {
29572
29864
  parameters = CIRCUIT_INFO["6-3-3-125"].parameter;
29573
29865
  if (proofSystem === "groth16" /* GROTH16 */) {
29574
29866
  groth16ProcessVkey = CIRCUIT_INFO["6-3-3-125"]["groth16"].process_vkey;
@@ -29577,7 +29869,7 @@ function getContractParams(type, circuitType, proofSystem, maxVoter, maxOption)
29577
29869
  plonkProcessVkey = CIRCUIT_INFO["6-3-3-125"]["plonk"]?.process_vkey;
29578
29870
  plonkTallyVkey = CIRCUIT_INFO["6-3-3-125"]["plonk"]?.tally_vkey;
29579
29871
  }
29580
- } else if (Number(maxVoter) <= 1953125 && Number(maxOption) <= 125) {
29872
+ } else if (maxVoter <= 1953125 && maxOption <= 125) {
29581
29873
  parameters = CIRCUIT_INFO["9-4-3-625"].parameter;
29582
29874
  if (proofSystem === "groth16" /* GROTH16 */) {
29583
29875
  if (circuitType === "0" /* IP1V */) {
@@ -29631,6 +29923,20 @@ function getContractParams(type, circuitType, proofSystem, maxVoter, maxOption)
29631
29923
  };
29632
29924
  }
29633
29925
  }
29926
+ function getAMaciRoundCircuitFee(maxVoter, maxOption) {
29927
+ let requiredFee = {
29928
+ denom: "peaka",
29929
+ amount: "0"
29930
+ };
29931
+ if (maxVoter <= 25 && maxOption <= 5) {
29932
+ requiredFee.amount = "50000000000000000000";
29933
+ } else if (maxVoter <= 625 && maxOption <= 25) {
29934
+ requiredFee.amount = "100000000000000000000";
29935
+ } else {
29936
+ throw new Error("Number of voters or options is too large.");
29937
+ }
29938
+ return requiredFee;
29939
+ }
29634
29940
 
29635
29941
  // src/libs/contract/contract.ts
29636
29942
  var Contract = class {
@@ -29671,26 +29977,32 @@ var Contract = class {
29671
29977
  wallet: signer,
29672
29978
  contractAddress: this.registryAddress
29673
29979
  });
29980
+ const requiredFee = getAMaciRoundCircuitFee(maxVoter, maxOption);
29674
29981
  preDeactivateRoot = preDeactivateRoot || "0";
29675
- const res = await client.createRound({
29676
- operator,
29677
- preDeactivateRoot,
29678
- voiceCreditAmount,
29679
- whitelist,
29680
- roundInfo: {
29681
- title,
29682
- description: description || "",
29683
- link: link || ""
29684
- },
29685
- votingTime: {
29686
- start_time,
29687
- end_time
29982
+ const res = await client.createRound(
29983
+ {
29984
+ operator,
29985
+ preDeactivateRoot,
29986
+ voiceCreditAmount,
29987
+ whitelist,
29988
+ roundInfo: {
29989
+ title,
29990
+ description: description || "",
29991
+ link: link || ""
29992
+ },
29993
+ votingTime: {
29994
+ start_time,
29995
+ end_time
29996
+ },
29997
+ maxVoter: maxVoter.toString(),
29998
+ maxOption: maxOption.toString(),
29999
+ certificationSystem: "0",
30000
+ circuitType
29688
30001
  },
29689
- maxVoter,
29690
- maxOption,
29691
- certificationSystem: "0",
29692
- circuitType
29693
- });
30002
+ "auto",
30003
+ void 0,
30004
+ [requiredFee]
30005
+ );
29694
30006
  let contractAddress = "";
29695
30007
  res.events.map((event) => {
29696
30008
  if (event.type === "wasm") {
@@ -29790,8 +30102,8 @@ var Contract = class {
29790
30102
  "2" /* ORACLE_MACI */,
29791
30103
  circuitType,
29792
30104
  "groth16" /* GROTH16 */,
29793
- "0",
29794
- "0"
30105
+ 0,
30106
+ 0
29795
30107
  );
29796
30108
  const instantiateResponse = await client.instantiate(
29797
30109
  address,
@@ -30356,6 +30668,50 @@ var MACI = class {
30356
30668
  const circuitType = await this.getRoundCircuitType({ contractAddress });
30357
30669
  return circuitType === "1";
30358
30670
  }
30671
+ async queryRoundClaimable({
30672
+ contractAddress
30673
+ }) {
30674
+ try {
30675
+ const roundInfo = await this.getRoundInfo({ contractAddress });
30676
+ if (roundInfo.maciType !== "aMACI") {
30677
+ return {
30678
+ claimable: null,
30679
+ balance: null
30680
+ };
30681
+ }
30682
+ const votingEndTime = new Date(Number(roundInfo.votingEnd) / 10 ** 6);
30683
+ const currentTime = /* @__PURE__ */ new Date();
30684
+ const threeDaysInMs = 3 * 24 * 60 * 60 * 1e3;
30685
+ if (currentTime.getTime() - votingEndTime.getTime() <= threeDaysInMs) {
30686
+ return {
30687
+ claimable: null,
30688
+ balance: null
30689
+ };
30690
+ }
30691
+ const roundBalance = await this.indexer.balanceOf(contractAddress);
30692
+ if (isErrorResponse(roundBalance)) {
30693
+ throw new Error(
30694
+ `Failed to query round balance: ${roundBalance.error.type} ${roundBalance.error.message}`
30695
+ );
30696
+ }
30697
+ if (roundBalance.data.balance && roundBalance.data.balance !== "0" && roundBalance.data.balance !== "") {
30698
+ return {
30699
+ claimable: true,
30700
+ balance: roundBalance.data.balance
30701
+ };
30702
+ }
30703
+ return {
30704
+ claimable: false,
30705
+ balance: roundBalance.data.balance
30706
+ };
30707
+ } catch (error) {
30708
+ console.error("Error in queryRoundClaimable:", error);
30709
+ return {
30710
+ claimable: null,
30711
+ balance: null
30712
+ };
30713
+ }
30714
+ }
30359
30715
  async queryRoundGasStation({ contractAddress }) {
30360
30716
  const roundInfo = await this.getRoundInfo({ contractAddress });
30361
30717
  return roundInfo.gasStationEnable;