@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,263 @@
1
+ import { ERROR } from '../errors/types';
2
+ import { Http } from '../../libs';
3
+ import {
4
+ OperatorsGraphqlResponse,
5
+ RoundsCountGraphqlResponse,
6
+ OperatorResponse,
7
+ OperatorsResponse,
8
+ } from '../../types';
9
+ import { isValidAddress } from '../../utils';
10
+ import { handleError, ErrorType } from '../errors';
11
+
12
+ export class Operator {
13
+ public http: Http;
14
+ public amaciRegistryContract: string;
15
+
16
+ constructor(http: Http, amaciRegistryContract: string) {
17
+ this.http = http;
18
+ this.amaciRegistryContract = amaciRegistryContract;
19
+ }
20
+
21
+ async getOperatorByAddress(address: string): Promise<OperatorResponse> {
22
+ try {
23
+ if (!isValidAddress(address)) {
24
+ return {
25
+ code: 400,
26
+ error: {
27
+ message: 'Invalid operator address format',
28
+ type: ERROR.ERROR_OPERATOR_INVALID_ADDRESS,
29
+ },
30
+ };
31
+ }
32
+
33
+ const OPERATORS_QUERY = `query {
34
+ operators(filter: { operatorAddress: { equalTo: "${address}" } }) {
35
+ edges {
36
+ node {
37
+ id
38
+ validatorAddress
39
+ operatorAddress
40
+ coordinatorPubkeyX
41
+ coordinatorPubkeyY
42
+ identity
43
+ }
44
+ }
45
+ }
46
+ }`;
47
+
48
+ const response = await this.http.fetchGraphql<OperatorsGraphqlResponse>(
49
+ OPERATORS_QUERY,
50
+ ''
51
+ );
52
+
53
+ if (
54
+ !response ||
55
+ !response.data ||
56
+ !response.data.operators ||
57
+ !response.data.operators.edges ||
58
+ response.data.operators.edges.length === 0
59
+ ) {
60
+ return {
61
+ code: 404,
62
+ error: {
63
+ message: `No operator found for address ${address}`,
64
+ type: ERROR.ERROR_OPERATOR_NOT_FOUND,
65
+ },
66
+ };
67
+ }
68
+
69
+ const operatorResponse = response.data.operators.edges[0].node;
70
+
71
+ if (
72
+ operatorResponse.operatorAddress === '' ||
73
+ operatorResponse.coordinatorPubkeyX === ''
74
+ ) {
75
+ operatorResponse.activeRoundsCount = 0;
76
+ operatorResponse.completedRoundsCount = 0;
77
+ } else {
78
+ const ROUNDS_QUERY = `query {
79
+ activeRoundsCount: rounds(filter: {
80
+ period: {notEqualTo: "Ended"},
81
+ operator: {equalTo: "${operatorResponse.operatorAddress}"},
82
+ caller: {equalTo: "${this.amaciRegistryContract}"}
83
+ }) {
84
+ totalCount
85
+ }
86
+ completedRoundsCount: rounds(filter: {
87
+ period: {equalTo: "Ended"},
88
+ operator: {equalTo: "${operatorResponse.operatorAddress}"},
89
+ caller: {equalTo: "${this.amaciRegistryContract}"}
90
+ }) {
91
+ totalCount
92
+ }
93
+ }`;
94
+
95
+ const roundsCount =
96
+ await this.http.fetchGraphql<RoundsCountGraphqlResponse>(
97
+ ROUNDS_QUERY,
98
+ ''
99
+ );
100
+ operatorResponse.activeRoundsCount =
101
+ roundsCount.data.activeRoundsCount.totalCount;
102
+ operatorResponse.completedRoundsCount =
103
+ roundsCount.data.completedRoundsCount.totalCount;
104
+ }
105
+
106
+ operatorResponse.logoUrl = '';
107
+ operatorResponse.moniker = '';
108
+ const identity = response.data.operators.edges[0].node.identity;
109
+
110
+ const keybaseUrl = `https://keybase.io/_/api/1.0/user/lookup.json?key_suffix=${identity}`;
111
+ const keybaseResponse = await this.http.fetch(keybaseUrl);
112
+ const keybaseData = await keybaseResponse.json();
113
+
114
+ if (keybaseData.status.code === 0) {
115
+ if (keybaseData.them[0]?.pictures?.primary?.url) {
116
+ operatorResponse.logoUrl = keybaseData.them[0].pictures.primary.url;
117
+ }
118
+ if (keybaseData.them[0]?.basics?.username_cased) {
119
+ operatorResponse.moniker = keybaseData.them[0].profile.full_name;
120
+ }
121
+ }
122
+ const operator: OperatorResponse = {
123
+ code: 200,
124
+ data: {
125
+ operator: operatorResponse,
126
+ },
127
+ };
128
+ return operator;
129
+ } catch (error) {
130
+ return handleError(error as ErrorType);
131
+ }
132
+ }
133
+
134
+ async getOperators(
135
+ after: string,
136
+ limit?: number
137
+ ): Promise<OperatorsResponse> {
138
+ try {
139
+ const OPERATORS_QUERY = `query ($limit: Int, $after: Cursor) {
140
+ operators(first: $limit, after: $after) {
141
+ pageInfo {
142
+ endCursor
143
+ hasNextPage
144
+ }
145
+ totalCount
146
+ edges {
147
+ cursor
148
+ node {
149
+ id
150
+ validatorAddress
151
+ operatorAddress
152
+ coordinatorPubkeyX
153
+ coordinatorPubkeyY
154
+ identity
155
+ }
156
+ }
157
+ }
158
+ }`;
159
+
160
+ const response = await this.http.fetchGraphql<OperatorsGraphqlResponse>(
161
+ OPERATORS_QUERY,
162
+ after,
163
+ limit
164
+ );
165
+ if (
166
+ !response ||
167
+ !response.data ||
168
+ !response.data.operators ||
169
+ !response.data.operators.edges
170
+ ) {
171
+ return {
172
+ code: 404,
173
+ error: {
174
+ message: 'No operators found',
175
+ type: ERROR.ERROR_OPERATORS_NOT_FOUND,
176
+ },
177
+ };
178
+ }
179
+ const operatorsWithRounds = await Promise.all(
180
+ response.data.operators.edges.map(async (edge) => {
181
+ const operator = edge.node;
182
+ if (
183
+ operator.operatorAddress === '' ||
184
+ operator.coordinatorPubkeyX === ''
185
+ ) {
186
+ operator.activeRoundsCount = 0;
187
+ operator.completedRoundsCount = 0;
188
+ return operator;
189
+ }
190
+ const ROUNDS_QUERY = `query {
191
+ activeRoundsCount: rounds(filter: {
192
+ period: {notEqualTo: "Ended"},
193
+ operator: {equalTo: "${operator.operatorAddress}"},
194
+ caller: {equalTo: "${this.amaciRegistryContract}"}
195
+ }) {
196
+ totalCount
197
+ }
198
+ completedRoundsCount: rounds(filter: {
199
+ period: {equalTo: "Ended"},
200
+ operator: {equalTo: "${operator.operatorAddress}"},
201
+ caller: {equalTo: "${this.amaciRegistryContract}"}
202
+ }) {
203
+ totalCount
204
+ }
205
+ }`;
206
+
207
+ const roundsCount =
208
+ await this.http.fetchGraphql<RoundsCountGraphqlResponse>(
209
+ ROUNDS_QUERY,
210
+ ''
211
+ );
212
+ operator.activeRoundsCount =
213
+ roundsCount.data.activeRoundsCount.totalCount;
214
+ operator.completedRoundsCount =
215
+ roundsCount.data.completedRoundsCount.totalCount;
216
+
217
+ return operator;
218
+ })
219
+ );
220
+
221
+ response.data.operators.edges = operatorsWithRounds.map(
222
+ (operator, index) => ({
223
+ cursor: response.data.operators.edges[index].cursor,
224
+ node: operator,
225
+ })
226
+ );
227
+
228
+ await Promise.all(
229
+ response.data.operators.edges.map(async (edge) => {
230
+ const operator = edge.node;
231
+ const identity = operator.identity;
232
+ operator.logoUrl = '';
233
+ operator.moniker = '';
234
+
235
+ // try {
236
+ const keybaseUrl = `https://keybase.io/_/api/1.0/user/lookup.json?key_suffix=${identity}`;
237
+ const keybaseResponse = await this.http.fetch(keybaseUrl);
238
+ const keybaseData = await keybaseResponse.json();
239
+
240
+ if (keybaseData.status.code === 0) {
241
+ if (keybaseData.them[0]?.pictures?.primary?.url) {
242
+ operator.logoUrl = keybaseData.them[0].pictures.primary.url;
243
+ }
244
+ if (keybaseData.them[0]?.basics?.username_cased) {
245
+ operator.moniker = keybaseData.them[0].profile.full_name;
246
+ }
247
+ }
248
+ // } catch (error) {
249
+ // console.error('Error fetching keybase data:', error);
250
+ // }
251
+ return operator;
252
+ })
253
+ );
254
+
255
+ return {
256
+ code: 200,
257
+ data: response.data,
258
+ };
259
+ } catch (error) {
260
+ return handleError(error as ErrorType);
261
+ }
262
+ }
263
+ }
@@ -0,0 +1,76 @@
1
+ import { Http } from '../../libs';
2
+ import { isValidAddress } from '../../utils';
3
+ import { ProofResponse, ProofGraphqlResponse } from '../../types';
4
+ import { handleError, ErrorType } from '../errors';
5
+ import { ERROR } from '../errors/types';
6
+
7
+ export class Proof {
8
+ public http: Http;
9
+
10
+ constructor(http: Http) {
11
+ this.http = http;
12
+ }
13
+
14
+ async getProofByContractAddress(address: string): Promise<ProofResponse> {
15
+ try {
16
+ if (!isValidAddress(address)) {
17
+ return {
18
+ code: 400,
19
+ error: {
20
+ message: 'Invalid round address format',
21
+ type: ERROR.ERROR_ROUND_INVALID_ADDRESS,
22
+ },
23
+ };
24
+ }
25
+
26
+ const PROOF_QUERY = `query HomePage {
27
+ proofData(first: 100, filter:{
28
+ contractAddress:{
29
+ equalTo: "${address}"
30
+ }
31
+ } ,orderBy: [TIMESTAMP_DESC]
32
+ ){
33
+ nodes {
34
+ id
35
+ blockHeight
36
+ txHash
37
+ contractAddress
38
+ timestamp
39
+ actionType
40
+ commitment
41
+ proof
42
+ }
43
+ }
44
+ }
45
+ `;
46
+
47
+ const response = await this.http.fetchGraphql<ProofGraphqlResponse>(
48
+ PROOF_QUERY,
49
+ ''
50
+ );
51
+
52
+ if (
53
+ !response ||
54
+ !response.data ||
55
+ !response.data.proofData ||
56
+ !response.data.proofData.nodes ||
57
+ response.data.proofData.nodes.length === 0
58
+ ) {
59
+ return {
60
+ code: 404,
61
+ error: {
62
+ message: `No proof found for round address ${address}`,
63
+ type: ERROR.ERROR_PROOF_NOT_FOUND,
64
+ },
65
+ };
66
+ }
67
+
68
+ return {
69
+ code: 200,
70
+ data: response.data,
71
+ };
72
+ } catch (error) {
73
+ return handleError(error as ErrorType);
74
+ }
75
+ }
76
+ }