@dorafactory/maci-sdk 0.0.1

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.
Files changed (74) hide show
  1. package/README.md +1 -0
  2. package/dist/index.d.ts +11 -0
  3. package/dist/index.js +4157 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +4123 -0
  6. package/dist/index.mjs.map +1 -0
  7. package/dist/libs/const.d.ts +117 -0
  8. package/dist/libs/contract/config.d.ts +29 -0
  9. package/dist/libs/contract/contract.d.ts +37 -0
  10. package/dist/libs/contract/index.d.ts +1 -0
  11. package/dist/libs/contract/ts/AMaci.client.d.ts +216 -0
  12. package/dist/libs/contract/ts/AMaci.types.d.ts +203 -0
  13. package/dist/libs/contract/ts/Maci.client.d.ts +206 -0
  14. package/dist/libs/contract/ts/Maci.types.d.ts +217 -0
  15. package/dist/libs/contract/ts/OracleMaci.client.d.ts +206 -0
  16. package/dist/libs/contract/ts/OracleMaci.types.d.ts +253 -0
  17. package/dist/libs/contract/ts/Registry.client.d.ts +128 -0
  18. package/dist/libs/contract/ts/Registry.types.d.ts +110 -0
  19. package/dist/libs/contract/types.d.ts +50 -0
  20. package/dist/libs/contract/utils.d.ts +67 -0
  21. package/dist/libs/contract/vars.d.ts +65 -0
  22. package/dist/libs/errors/index.d.ts +28 -0
  23. package/dist/libs/errors/types.d.ts +14 -0
  24. package/dist/libs/http/http.d.ts +16 -0
  25. package/dist/libs/http/index.d.ts +1 -0
  26. package/dist/libs/index.d.ts +4 -0
  27. package/dist/libs/indexer/index.d.ts +1 -0
  28. package/dist/libs/indexer/indexer.d.ts +133 -0
  29. package/dist/libs/indexer/types.d.ts +7 -0
  30. package/dist/libs/query/account.d.ts +7 -0
  31. package/dist/libs/query/circuit.d.ts +8 -0
  32. package/dist/libs/query/index.d.ts +6 -0
  33. package/dist/libs/query/operator.d.ts +9 -0
  34. package/dist/libs/query/proof.d.ts +7 -0
  35. package/dist/libs/query/round.d.ts +11 -0
  36. package/dist/libs/query/transaction.d.ts +9 -0
  37. package/dist/maci.d.ts +151 -0
  38. package/dist/types/index.d.ts +254 -0
  39. package/dist/utils/index.d.ts +1 -0
  40. package/package.json +154 -0
  41. package/src/index.ts +11 -0
  42. package/src/libs/const.ts +196 -0
  43. package/src/libs/contract/config.ts +117 -0
  44. package/src/libs/contract/contract.ts +330 -0
  45. package/src/libs/contract/index.ts +1 -0
  46. package/src/libs/contract/ts/AMaci.client.ts +893 -0
  47. package/src/libs/contract/ts/AMaci.types.ts +252 -0
  48. package/src/libs/contract/ts/Maci.client.ts +906 -0
  49. package/src/libs/contract/ts/Maci.types.ts +263 -0
  50. package/src/libs/contract/ts/OracleMaci.client.ts +561 -0
  51. package/src/libs/contract/ts/OracleMaci.types.ts +254 -0
  52. package/src/libs/contract/ts/Registry.client.ts +466 -0
  53. package/src/libs/contract/ts/Registry.types.ts +127 -0
  54. package/src/libs/contract/types.ts +57 -0
  55. package/src/libs/contract/utils.ts +175 -0
  56. package/src/libs/contract/vars.ts +420 -0
  57. package/src/libs/errors/index.ts +122 -0
  58. package/src/libs/errors/types.ts +14 -0
  59. package/src/libs/http/http.ts +152 -0
  60. package/src/libs/http/index.ts +1 -0
  61. package/src/libs/index.ts +4 -0
  62. package/src/libs/indexer/index.ts +1 -0
  63. package/src/libs/indexer/indexer.ts +240 -0
  64. package/src/libs/indexer/types.ts +8 -0
  65. package/src/libs/query/account.ts +39 -0
  66. package/src/libs/query/circuit.ts +99 -0
  67. package/src/libs/query/index.ts +6 -0
  68. package/src/libs/query/operator.ts +263 -0
  69. package/src/libs/query/proof.ts +76 -0
  70. package/src/libs/query/round.ts +533 -0
  71. package/src/libs/query/transaction.ts +204 -0
  72. package/src/maci.ts +313 -0
  73. package/src/types/index.ts +301 -0
  74. package/src/utils/index.ts +44 -0
@@ -0,0 +1,152 @@
1
+ import {
2
+ BaseError,
3
+ HttpError,
4
+ GraphQLError,
5
+ ParseError,
6
+ } from '../errors';
7
+
8
+ export type FetchOptions = RequestInit & {
9
+ next?: {
10
+ revalidate?: boolean | number;
11
+ };
12
+ };
13
+
14
+ export class Http {
15
+ private apiEndpoint: string;
16
+ private restEndpoint: string;
17
+ private defaultOptions?: FetchOptions;
18
+
19
+ constructor(
20
+ apiEndpoint: string,
21
+ restEndpoint: string,
22
+ private customFetch?: typeof fetch,
23
+ defaultOptions?: FetchOptions
24
+ ) {
25
+ this.apiEndpoint = apiEndpoint;
26
+ this.restEndpoint = restEndpoint;
27
+ this.defaultOptions = defaultOptions;
28
+ }
29
+
30
+ private getFetch() {
31
+ return this.customFetch || fetch;
32
+ }
33
+
34
+ async fetch(url: string): Promise<Response> {
35
+ try {
36
+ const fetchFn = this.getFetch();
37
+ const response = await fetchFn(url, {
38
+ ...this.defaultOptions,
39
+ });
40
+
41
+ if (!response.ok) {
42
+ throw new HttpError(
43
+ `HTTP error! status: ${response.status}`,
44
+ response.status
45
+ );
46
+ }
47
+
48
+ return response;
49
+ } catch (error) {
50
+ if (error instanceof HttpError) {
51
+ throw error;
52
+ }
53
+ throw new HttpError(`Failed to fetch: ${(error as Error).message}`, 500);
54
+ }
55
+ }
56
+
57
+ async fetchGraphql<T>(
58
+ query: string,
59
+ after: string,
60
+ limit: number | null = 10
61
+ ): Promise<T> {
62
+ try {
63
+ const isFirstPage = after === 'first';
64
+ const fetchFn = this.getFetch();
65
+
66
+ const response = await fetchFn(this.apiEndpoint, {
67
+ method: 'POST',
68
+ headers: {
69
+ 'Content-Type': 'application/json',
70
+ Accept: 'application/json',
71
+ },
72
+ body: JSON.stringify({
73
+ query,
74
+ variables: { limit, after: isFirstPage ? undefined : after },
75
+ }),
76
+ ...this.defaultOptions,
77
+ });
78
+
79
+ if (!response.ok) {
80
+ const errorData = await response.json();
81
+
82
+ if (errorData.errors?.[0]?.message?.includes('Syntax Error')) {
83
+ throw new GraphQLError(
84
+ `GraphQL syntax error: ${errorData.errors[0].message}`
85
+ );
86
+ }
87
+
88
+ if (errorData.errors?.length > 0) {
89
+ throw new GraphQLError(
90
+ errorData.errors[0].message || 'Unknown GraphQL error'
91
+ );
92
+ }
93
+
94
+ throw new HttpError(
95
+ `HTTP error: ${JSON.stringify(errorData)}`,
96
+ response.status
97
+ );
98
+ }
99
+
100
+ const data = await response.json();
101
+
102
+ if (data.errors) {
103
+ throw new GraphQLError(
104
+ data.errors[0]?.message || 'GraphQL query failed'
105
+ );
106
+ }
107
+
108
+ return data;
109
+ } catch (error) {
110
+ if (error instanceof BaseError) {
111
+ throw error;
112
+ }
113
+ if (error instanceof SyntaxError) {
114
+ throw new ParseError('Failed to parse JSON response');
115
+ }
116
+ throw new HttpError(
117
+ `Failed to fetch GraphQL: ${(error as Error).message}`,
118
+ 500
119
+ );
120
+ }
121
+ }
122
+
123
+ async fetchRest(path: string): Promise<any> {
124
+ try {
125
+ const fetchFn = this.getFetch();
126
+ const response = await fetchFn(`${this.restEndpoint}${path}`, {
127
+ ...this.defaultOptions,
128
+ });
129
+
130
+ if (!response.ok) {
131
+ throw new HttpError(
132
+ `HTTP error! status: ${response.status}`,
133
+ response.status
134
+ );
135
+ }
136
+
137
+ try {
138
+ return await response.json();
139
+ } catch (error) {
140
+ throw new ParseError('Failed to parse JSON response');
141
+ }
142
+ } catch (error) {
143
+ if (error instanceof BaseError) {
144
+ throw error;
145
+ }
146
+ throw new HttpError(
147
+ `Failed to fetch REST: ${(error as Error).message}`,
148
+ 500
149
+ );
150
+ }
151
+ }
152
+ }
@@ -0,0 +1 @@
1
+ export { Http } from './http';
@@ -0,0 +1,4 @@
1
+ export { Http } from './http';
2
+ export * from './query';
3
+ export * from './indexer';
4
+ export * from './contract';
@@ -0,0 +1 @@
1
+ export { Indexer } from './indexer';
@@ -0,0 +1,240 @@
1
+ import {
2
+ BalanceResponse,
3
+ RoundResponse,
4
+ RoundsResponse,
5
+ OperatorResponse,
6
+ OperatorsResponse,
7
+ CircuitResponse,
8
+ TransactionResponse,
9
+ TransactionsResponse,
10
+ CircuitsResponse,
11
+ ProofResponse,
12
+ } from '../../types';
13
+ import { IndexerParams } from './types';
14
+ import { Http } from '../http';
15
+ import {
16
+ Round,
17
+ Account,
18
+ Circuit,
19
+ Operator,
20
+ Proof,
21
+ Transaction,
22
+ } from '../query';
23
+
24
+ /**
25
+ * @class Indexer
26
+ * @description This class is used to interact with Maci Indexer.
27
+ */
28
+ export class Indexer {
29
+ public restEndpoint: string;
30
+ public apiEndpoint: string;
31
+ public registryAddress: string;
32
+
33
+ public http: Http;
34
+ public round: Round;
35
+ public account: Account;
36
+ public circuit: Circuit;
37
+ public operator: Operator;
38
+ public proof: Proof;
39
+ public transaction: Transaction;
40
+
41
+ /**
42
+ * @constructor
43
+ * @param {IndexerParams} params - The parameters for the Maci Indexer instance.
44
+ */
45
+ constructor({
46
+ restEndpoint,
47
+ apiEndpoint,
48
+ registryAddress,
49
+ http,
50
+ }: IndexerParams) {
51
+ this.http = http;
52
+
53
+ this.restEndpoint = restEndpoint;
54
+ this.apiEndpoint = apiEndpoint;
55
+ this.registryAddress = registryAddress;
56
+
57
+ this.round = new Round(this.http);
58
+ this.account = new Account(this.http);
59
+ this.circuit = new Circuit(this.http);
60
+ this.operator = new Operator(this.http, this.registryAddress);
61
+ this.proof = new Proof(this.http);
62
+ this.transaction = new Transaction(this.http);
63
+ }
64
+
65
+ /**
66
+ * @method balanceOf
67
+ * @description Get the balance of a specific address.
68
+ * @param {string} address - The address to check the balance for.
69
+ * @returns {Promise<BalanceResponse>} The balance response.
70
+ */
71
+ async balanceOf(address: string): Promise<BalanceResponse> {
72
+ return await this.account.balanceOf(address);
73
+ }
74
+
75
+ /**
76
+ * @method getRoundById
77
+ * @description Get a round by its ID.
78
+ * @param {string} id - The ID of the round.
79
+ * @returns {Promise<RoundResponse>} The round response.
80
+ */
81
+ async getRoundById(id: string): Promise<RoundResponse> {
82
+ return await this.round.getRoundById(id);
83
+ }
84
+
85
+ /**
86
+ * @method getRounds
87
+ * @description Get multiple rounds.
88
+ * @param {string} after - The cursor to start after.
89
+ * @param {number} [limit] - The number of rounds to retrieve.
90
+ * @returns {Promise<RoundsResponse>} The rounds response.
91
+ */
92
+ async getRounds(after: string, limit?: number): Promise<RoundsResponse> {
93
+ return await this.round.getRounds(after, limit);
94
+ }
95
+
96
+ /**
97
+ * @method getRoundsByStatus
98
+ * @description Get rounds by their status.
99
+ * @param {string} status - The status of the rounds to retrieve.
100
+ * @param {string} after - The cursor to start after.
101
+ * @param {number} [limit] - The number of rounds to retrieve.
102
+ * @returns {Promise<RoundsResponse>} The rounds response.
103
+ */
104
+ async getRoundsByStatus(
105
+ status: string,
106
+ after: string,
107
+ limit?: number
108
+ ): Promise<RoundsResponse> {
109
+ return await this.round.getRoundsByStatus(status, after, limit);
110
+ }
111
+
112
+ /**
113
+ * @method getRoundsByCircuitName
114
+ * @description Get rounds by their circuit name.
115
+ * @param {string} name - The name of the circuit.
116
+ * @param {string} after - The cursor to start after.
117
+ * @param {number} [limit] - The number of rounds to retrieve.
118
+ * @returns {Promise<RoundsResponse>} The rounds response.
119
+ */
120
+ async getRoundsByCircuitName(
121
+ name: string,
122
+ after: string,
123
+ limit?: number
124
+ ): Promise<RoundsResponse> {
125
+ return await this.round.getRoundsByCircuitName(name, after, limit);
126
+ }
127
+
128
+ /**
129
+ * @method getRoundsByOperator
130
+ * @description Get rounds by their operator address.
131
+ * @param {string} address - The address of the operator.
132
+ * @param {string} after - The cursor to start after.
133
+ * @param {number} [limit] - The number of rounds to retrieve.
134
+ * @returns {Promise<RoundsResponse>} The rounds response.
135
+ */
136
+ async getRoundsByOperator(
137
+ address: string,
138
+ after: string,
139
+ limit?: number
140
+ ): Promise<RoundsResponse> {
141
+ return await this.round.getRoundsByOperator(address, after, limit);
142
+ }
143
+
144
+ /**
145
+ * @method getOperatorByAddress
146
+ * @description Get an operator by their address.
147
+ * @param {string} address - The address of the operator.
148
+ * @returns {Promise<OperatorResponse>} The operator response.
149
+ */
150
+ async getOperatorByAddress(address: string): Promise<OperatorResponse> {
151
+ return await this.operator.getOperatorByAddress(address);
152
+ }
153
+
154
+ /**
155
+ * @method getOperators
156
+ * @description Get multiple operators.
157
+ * @param {string} after - The cursor to start after.
158
+ * @param {number} [limit] - The number of operators to retrieve.
159
+ * @returns {Promise<OperatorsResponse>} The operators response.
160
+ */
161
+ async getOperators(
162
+ after: string,
163
+ limit?: number
164
+ ): Promise<OperatorsResponse> {
165
+ return await this.operator.getOperators(after, limit);
166
+ }
167
+
168
+ /**
169
+ * @method getCircuitByName
170
+ * @description Get a circuit by its name.
171
+ * @param {string} name - The name of the circuit.
172
+ * @returns {Promise<CircuitResponse>} The circuit response.
173
+ */
174
+ async getCircuitByName(name: string): Promise<CircuitResponse> {
175
+ return await this.circuit.getCircuitByName(name);
176
+ }
177
+
178
+ /**
179
+ * @method getCircuits
180
+ * @description Get all available circuits.
181
+ * @returns {Promise<CircuitsResponse>} The circuits response.
182
+ */
183
+ async getCircuits(): Promise<CircuitsResponse> {
184
+ return await this.circuit.getCircuits();
185
+ }
186
+
187
+ /**
188
+ * @method getTransactionByHash
189
+ * @description Get a transaction by its hash.
190
+ * @param {string} hash - The hash of the transaction.
191
+ * @returns {Promise<TransactionResponse>} The transaction response.
192
+ */
193
+ async getTransactionByHash(hash: string): Promise<TransactionResponse> {
194
+ return await this.transaction.getTransactionByHash(hash);
195
+ }
196
+
197
+ /**
198
+ * @method getTransactions
199
+ * @description Get multiple transactions.
200
+ * @param {string} after - The cursor to start after.
201
+ * @param {number} [limit] - The number of transactions to retrieve.
202
+ * @returns {Promise<TransactionsResponse>} The transactions response.
203
+ */
204
+ async getTransactions(
205
+ after: string,
206
+ limit?: number
207
+ ): Promise<TransactionsResponse> {
208
+ return await this.transaction.getTransactions(after, limit);
209
+ }
210
+
211
+ /**
212
+ * @method getTransactionsByContractAddress
213
+ * @description Get transactions by contract address.
214
+ * @param {string} address - The contract address.
215
+ * @param {string} after - The cursor to start after.
216
+ * @param {number} [limit] - The number of transactions to retrieve.
217
+ * @returns {Promise<TransactionsResponse>} The transactions response.
218
+ */
219
+ async getTransactionsByContractAddress(
220
+ address: string,
221
+ after: string,
222
+ limit?: number
223
+ ): Promise<TransactionsResponse> {
224
+ return await this.transaction.getTransactionsByContractAddress(
225
+ address,
226
+ after,
227
+ limit
228
+ );
229
+ }
230
+
231
+ /**
232
+ * @method getProofByContractAddress
233
+ * @description Get proof data by contract address.
234
+ * @param {string} address - The contract address.
235
+ * @returns {Promise<ProofResponse>} The proof response.
236
+ */
237
+ async getProofByContractAddress(address: string): Promise<ProofResponse> {
238
+ return await this.proof.getProofByContractAddress(address);
239
+ }
240
+ }
@@ -0,0 +1,8 @@
1
+ import { FetchOptions, Http } from '../http/http';
2
+
3
+ export type IndexerParams = {
4
+ restEndpoint: string;
5
+ apiEndpoint: string;
6
+ registryAddress: string;
7
+ http: Http;
8
+ };
@@ -0,0 +1,39 @@
1
+ import { Http } from '../http';
2
+ import { BalanceResponse } from '../../types';
3
+ import { handleError, ErrorType } from '../errors';
4
+ import { ERROR } from '../errors/types';
5
+
6
+ export class Account {
7
+ public http: Http;
8
+
9
+ constructor(http: Http) {
10
+ this.http = http;
11
+ }
12
+
13
+ async balanceOf(address: string): Promise<BalanceResponse> {
14
+ try {
15
+ const path = `/cosmos/bank/v1beta1/balances/${address}/by_denom?denom=peaka`;
16
+ const data = await this.http.fetchRest(path);
17
+
18
+ if (data['code'] === undefined) {
19
+ const response: BalanceResponse = {
20
+ code: 200,
21
+ data: {
22
+ balance: data['balance']['amount'],
23
+ },
24
+ };
25
+ return response;
26
+ } else {
27
+ return {
28
+ code: 404,
29
+ error: {
30
+ message: 'Address not found',
31
+ type: ERROR.ERROR_ADDRESS_NOT_FOUND,
32
+ },
33
+ };
34
+ }
35
+ } catch (error) {
36
+ return handleError(error as ErrorType);
37
+ }
38
+ }
39
+ }
@@ -0,0 +1,99 @@
1
+ import { Http } from '../../libs';
2
+ import {
3
+ CircuitResponse,
4
+ CircuitsCountGraphqlResponse,
5
+ CircuitType,
6
+ CircuitsResponse,
7
+ } from '../../types';
8
+ import { circuits } from '../const';
9
+ import { handleError, ErrorType } from '../errors';
10
+ import { ERROR } from '../errors/types';
11
+
12
+ export class Circuit {
13
+ public http: Http;
14
+
15
+ constructor(http: Http) {
16
+ this.http = http;
17
+ }
18
+
19
+ async getCircuitByName(name: string): Promise<CircuitResponse> {
20
+ try {
21
+ const circuitName = name;
22
+ const circuitData: CircuitType = circuits[circuitName];
23
+
24
+ if (circuitData === undefined) {
25
+ return {
26
+ code: 404,
27
+ error: {
28
+ message: `Circuit ${circuitName} not found`,
29
+ type: ERROR.ERROR_CIRCUIT_NOT_FOUND,
30
+ },
31
+ };
32
+ }
33
+
34
+ const CIRCUIT_ROUNDS_COUNT_QUERY = `
35
+ query {
36
+ rounds(filter: { circuitName: { equalTo: "${circuitData.displayName}" } }) {
37
+ totalCount
38
+ }
39
+ }
40
+ `;
41
+ const circuitRoundsCountResponse =
42
+ await this.http.fetchGraphql<CircuitsCountGraphqlResponse>(
43
+ CIRCUIT_ROUNDS_COUNT_QUERY,
44
+ ''
45
+ );
46
+
47
+ circuitData.roundCount =
48
+ circuitRoundsCountResponse.data.rounds.totalCount;
49
+
50
+ const response: CircuitResponse = {
51
+ code: 200,
52
+ data: {
53
+ circuit: circuitData,
54
+ },
55
+ };
56
+
57
+ return response;
58
+ } catch (error) {
59
+ return handleError(error as ErrorType);
60
+ }
61
+ }
62
+
63
+ async getCircuits(): Promise<CircuitsResponse> {
64
+ try {
65
+ const circuitsArray: CircuitType[] = Object.values(circuits).sort(
66
+ (a, b) => a.displayName.localeCompare(b.displayName)
67
+ );
68
+
69
+ const circuitsWithRoundCount = await Promise.all(
70
+ circuitsArray.map(async (circuit) => {
71
+ const CIRCUIT_ROUNDS_COUNT_QUERY = `
72
+ query {
73
+ rounds(filter: { circuitName: { equalTo: "${circuit.displayName}" } }) {
74
+ totalCount
75
+ }
76
+ }
77
+ `;
78
+ const circuitRoundsCountResponse: CircuitsCountGraphqlResponse =
79
+ await this.http.fetchGraphql(CIRCUIT_ROUNDS_COUNT_QUERY, '');
80
+ return {
81
+ ...circuit,
82
+ roundCount: circuitRoundsCountResponse.data.rounds.totalCount,
83
+ };
84
+ })
85
+ );
86
+
87
+ const response: CircuitsResponse = {
88
+ code: 200,
89
+ data: {
90
+ circuits: circuitsWithRoundCount,
91
+ },
92
+ };
93
+
94
+ return response;
95
+ } catch (error) {
96
+ return handleError(error as ErrorType);
97
+ }
98
+ }
99
+ }
@@ -0,0 +1,6 @@
1
+ export { Account } from './account';
2
+ export { Circuit } from './circuit';
3
+ export { Operator } from './operator';
4
+ export { Round } from './round';
5
+ export { Transaction } from './transaction';
6
+ export { Proof } from './proof';