@human-protocol/sdk 6.1.0 → 7.1.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.
Files changed (129) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/dist/constants.d.ts.map +1 -1
  3. package/dist/constants.js +26 -14
  4. package/dist/encryption/encryption.d.ts +84 -0
  5. package/dist/encryption/encryption.d.ts.map +1 -0
  6. package/dist/{encryption.js → encryption/encryption.js} +3 -161
  7. package/dist/encryption/encryption_utils.d.ts +101 -0
  8. package/dist/encryption/encryption_utils.d.ts.map +1 -0
  9. package/dist/encryption/encryption_utils.js +191 -0
  10. package/dist/encryption/index.d.ts +4 -0
  11. package/dist/encryption/index.d.ts.map +1 -0
  12. package/dist/encryption/index.js +7 -0
  13. package/dist/encryption/types.d.ts +9 -0
  14. package/dist/encryption/types.d.ts.map +1 -0
  15. package/dist/encryption/types.js +9 -0
  16. package/dist/error.d.ts +6 -10
  17. package/dist/error.d.ts.map +1 -1
  18. package/dist/error.js +12 -14
  19. package/dist/{escrow.d.ts → escrow/escrow_client.d.ts} +4 -184
  20. package/dist/{escrow.d.ts.map → escrow/escrow_client.d.ts.map} +1 -1
  21. package/dist/{escrow.js → escrow/escrow_client.js} +35 -411
  22. package/dist/escrow/escrow_utils.d.ts +172 -0
  23. package/dist/escrow/escrow_utils.d.ts.map +1 -0
  24. package/dist/escrow/escrow_utils.js +394 -0
  25. package/dist/escrow/index.d.ts +3 -0
  26. package/dist/escrow/index.d.ts.map +1 -0
  27. package/dist/escrow/index.js +7 -0
  28. package/dist/graphql/queries/escrow.d.ts +1 -1
  29. package/dist/graphql/queries/escrow.d.ts.map +1 -1
  30. package/dist/graphql/queries/escrow.js +5 -1
  31. package/dist/graphql/queries/statistics.d.ts +1 -0
  32. package/dist/graphql/queries/statistics.d.ts.map +1 -1
  33. package/dist/graphql/queries/statistics.js +36 -2
  34. package/dist/graphql/queries/worker.d.ts.map +1 -1
  35. package/dist/graphql/queries/worker.js +0 -2
  36. package/dist/graphql/types.d.ts +5 -2
  37. package/dist/graphql/types.d.ts.map +1 -1
  38. package/dist/index.d.ts +10 -10
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +23 -22
  41. package/dist/interfaces.d.ts +4 -7
  42. package/dist/interfaces.d.ts.map +1 -1
  43. package/dist/kvstore/index.d.ts +3 -0
  44. package/dist/kvstore/index.d.ts.map +1 -0
  45. package/dist/kvstore/index.js +7 -0
  46. package/dist/{kvstore.d.ts → kvstore/kvstore_client.d.ts} +3 -108
  47. package/dist/kvstore/kvstore_client.d.ts.map +1 -0
  48. package/dist/{kvstore.js → kvstore/kvstore_client.js} +22 -184
  49. package/dist/kvstore/kvstore_utils.d.ts +105 -0
  50. package/dist/kvstore/kvstore_utils.d.ts.map +1 -0
  51. package/dist/kvstore/kvstore_utils.js +184 -0
  52. package/dist/operator/index.d.ts +2 -0
  53. package/dist/operator/index.d.ts.map +1 -0
  54. package/dist/operator/index.js +5 -0
  55. package/dist/{operator.d.ts → operator/operator_utils.d.ts} +3 -3
  56. package/dist/operator/operator_utils.d.ts.map +1 -0
  57. package/dist/{operator.js → operator/operator_utils.js} +6 -6
  58. package/dist/staking/index.d.ts +3 -0
  59. package/dist/staking/index.d.ts.map +1 -0
  60. package/dist/staking/index.js +7 -0
  61. package/dist/{staking.d.ts → staking/staking_client.d.ts} +4 -65
  62. package/dist/staking/staking_client.d.ts.map +1 -0
  63. package/dist/{staking.js → staking/staking_client.js} +6 -136
  64. package/dist/staking/staking_utils.d.ts +63 -0
  65. package/dist/staking/staking_utils.d.ts.map +1 -0
  66. package/dist/staking/staking_utils.js +137 -0
  67. package/dist/statistics/index.d.ts +2 -0
  68. package/dist/statistics/index.d.ts.map +1 -0
  69. package/dist/statistics/index.js +5 -0
  70. package/dist/{statistics.d.ts → statistics/statistics_utils.d.ts} +4 -13
  71. package/dist/statistics/statistics_utils.d.ts.map +1 -0
  72. package/dist/{statistics.js → statistics/statistics_utils.js} +8 -22
  73. package/dist/transaction/index.d.ts +2 -0
  74. package/dist/transaction/index.d.ts.map +1 -0
  75. package/dist/transaction/index.js +5 -0
  76. package/dist/{transaction.d.ts → transaction/transaction_utils.d.ts} +3 -3
  77. package/dist/transaction/transaction_utils.d.ts.map +1 -0
  78. package/dist/{transaction.js → transaction/transaction_utils.js} +5 -5
  79. package/dist/types.d.ts +8 -0
  80. package/dist/types.d.ts.map +1 -1
  81. package/dist/utils.d.ts +15 -0
  82. package/dist/utils.d.ts.map +1 -1
  83. package/dist/utils.js +71 -4
  84. package/dist/worker/index.d.ts +2 -0
  85. package/dist/worker/index.d.ts.map +1 -0
  86. package/dist/worker/index.js +5 -0
  87. package/dist/{worker.d.ts → worker/worker_utils.d.ts} +3 -4
  88. package/dist/worker/worker_utils.d.ts.map +1 -0
  89. package/dist/{worker.js → worker/worker_utils.js} +5 -7
  90. package/package.json +12 -8
  91. package/src/constants.ts +38 -14
  92. package/src/{encryption.ts → encryption/encryption.ts} +1 -193
  93. package/src/encryption/encryption_utils.ts +179 -0
  94. package/src/encryption/index.ts +3 -0
  95. package/src/encryption/types.ts +15 -0
  96. package/src/error.ts +11 -17
  97. package/src/{escrow.ts → escrow/escrow_client.ts} +36 -556
  98. package/src/escrow/escrow_utils.ts +518 -0
  99. package/src/escrow/index.ts +2 -0
  100. package/src/graphql/queries/escrow.ts +6 -1
  101. package/src/graphql/queries/statistics.ts +37 -1
  102. package/src/graphql/queries/worker.ts +0 -2
  103. package/src/graphql/types.ts +6 -2
  104. package/src/index.ts +11 -25
  105. package/src/interfaces.ts +4 -7
  106. package/src/kvstore/index.ts +2 -0
  107. package/src/{kvstore.ts → kvstore/kvstore_client.ts} +27 -241
  108. package/src/kvstore/kvstore_utils.ts +244 -0
  109. package/src/operator/index.ts +1 -0
  110. package/src/{operator.ts → operator/operator_utils.ts} +8 -8
  111. package/src/staking/index.ts +2 -0
  112. package/src/{staking.ts → staking/staking_client.ts} +8 -174
  113. package/src/staking/staking_utils.ts +170 -0
  114. package/src/statistics/index.ts +1 -0
  115. package/src/{statistics.ts → statistics/statistics_utils.ts} +14 -26
  116. package/src/transaction/index.ts +1 -0
  117. package/src/{transaction.ts → transaction/transaction_utils.ts} +7 -7
  118. package/src/types.ts +8 -0
  119. package/src/utils.ts +80 -3
  120. package/src/worker/index.ts +1 -0
  121. package/src/{worker.ts → worker/worker_utils.ts} +7 -9
  122. package/dist/encryption.d.ts +0 -189
  123. package/dist/encryption.d.ts.map +0 -1
  124. package/dist/kvstore.d.ts.map +0 -1
  125. package/dist/operator.d.ts.map +0 -1
  126. package/dist/staking.d.ts.map +0 -1
  127. package/dist/statistics.d.ts.map +0 -1
  128. package/dist/transaction.d.ts.map +0 -1
  129. package/dist/worker.d.ts.map +0 -1
@@ -0,0 +1,518 @@
1
+ import { ethers } from 'ethers';
2
+ import { NETWORKS } from '../constants';
3
+ import { ChainId, OrderDirection } from '../enums';
4
+ import {
5
+ ErrorInvalidAddress,
6
+ ErrorInvalidEscrowAddressProvided,
7
+ ErrorUnsupportedChainID,
8
+ } from '../error';
9
+ import {
10
+ CancellationRefundData,
11
+ EscrowData,
12
+ GET_CANCELLATION_REFUNDS_QUERY,
13
+ GET_CANCELLATION_REFUND_BY_ADDRESS_QUERY,
14
+ GET_ESCROWS_QUERY,
15
+ GET_ESCROW_BY_ADDRESS_QUERY,
16
+ GET_PAYOUTS_QUERY,
17
+ GET_STATUS_UPDATES_QUERY,
18
+ PayoutData,
19
+ StatusEvent,
20
+ } from '../graphql';
21
+ import {
22
+ ICancellationRefund,
23
+ ICancellationRefundFilter,
24
+ IEscrow,
25
+ IEscrowsFilter,
26
+ IPayout,
27
+ IPayoutFilter,
28
+ IStatusEvent,
29
+ IStatusEventFilter,
30
+ SubgraphOptions,
31
+ } from '../interfaces';
32
+ import { EscrowStatus } from '../types';
33
+ import { customGqlFetch, getSubgraphUrl, getUnixTimestamp } from '../utils';
34
+ /**
35
+ * Utility helpers for escrow-related queries.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import { ChainId, EscrowUtils } from '@human-protocol/sdk';
40
+ *
41
+ * const escrows = await EscrowUtils.getEscrows({
42
+ * chainId: ChainId.POLYGON_AMOY
43
+ * });
44
+ * console.log('Escrows:', escrows);
45
+ * ```
46
+ */
47
+ export class EscrowUtils {
48
+ /**
49
+ * This function returns an array of escrows based on the specified filter parameters.
50
+ *
51
+ * @param filter - Filter parameters.
52
+ * @param options - Optional configuration for subgraph requests.
53
+ * @returns List of escrows that match the filter.
54
+ * @throws ErrorInvalidAddress If any filter address is invalid
55
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * import { ChainId, EscrowStatus } from '@human-protocol/sdk';
60
+ *
61
+ * const filters = {
62
+ * status: EscrowStatus.Pending,
63
+ * from: new Date(2023, 4, 8),
64
+ * to: new Date(2023, 5, 8),
65
+ * chainId: ChainId.POLYGON_AMOY
66
+ * };
67
+ * const escrows = await EscrowUtils.getEscrows(filters);
68
+ * console.log('Found escrows:', escrows.length);
69
+ * ```
70
+ */
71
+ public static async getEscrows(
72
+ filter: IEscrowsFilter,
73
+ options?: SubgraphOptions
74
+ ): Promise<IEscrow[]> {
75
+ if (filter.launcher && !ethers.isAddress(filter.launcher)) {
76
+ throw ErrorInvalidAddress;
77
+ }
78
+
79
+ if (filter.recordingOracle && !ethers.isAddress(filter.recordingOracle)) {
80
+ throw ErrorInvalidAddress;
81
+ }
82
+
83
+ if (filter.reputationOracle && !ethers.isAddress(filter.reputationOracle)) {
84
+ throw ErrorInvalidAddress;
85
+ }
86
+
87
+ if (filter.exchangeOracle && !ethers.isAddress(filter.exchangeOracle)) {
88
+ throw ErrorInvalidAddress;
89
+ }
90
+
91
+ const first =
92
+ filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
93
+ const skip = filter.skip || 0;
94
+ const orderDirection = filter.orderDirection || OrderDirection.DESC;
95
+
96
+ const networkData = NETWORKS[filter.chainId];
97
+
98
+ if (!networkData) {
99
+ throw ErrorUnsupportedChainID;
100
+ }
101
+
102
+ let statuses;
103
+ if (filter.status !== undefined) {
104
+ statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
105
+ statuses = statuses.map((status) => EscrowStatus[status]);
106
+ }
107
+ const { escrows } = await customGqlFetch<{ escrows: EscrowData[] }>(
108
+ getSubgraphUrl(networkData),
109
+ GET_ESCROWS_QUERY(filter),
110
+ {
111
+ ...filter,
112
+ launcher: filter.launcher?.toLowerCase(),
113
+ reputationOracle: filter.reputationOracle?.toLowerCase(),
114
+ recordingOracle: filter.recordingOracle?.toLowerCase(),
115
+ exchangeOracle: filter.exchangeOracle?.toLowerCase(),
116
+ status: statuses,
117
+ from: filter.from ? getUnixTimestamp(filter.from) : undefined,
118
+ to: filter.to ? getUnixTimestamp(filter.to) : undefined,
119
+ orderDirection: orderDirection,
120
+ first: first,
121
+ skip: skip,
122
+ },
123
+ options
124
+ );
125
+ return (escrows || []).map((e) => mapEscrow(e, networkData.chainId));
126
+ }
127
+
128
+ /**
129
+ * This function returns the escrow data for a given address.
130
+ *
131
+ * > This uses Subgraph
132
+ *
133
+ * @param chainId - Network in which the escrow has been deployed
134
+ * @param escrowAddress - Address of the escrow
135
+ * @param options - Optional configuration for subgraph requests.
136
+ * @returns Escrow data or null if not found.
137
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
138
+ * @throws ErrorInvalidAddress If the escrow address is invalid
139
+ *
140
+ * @example
141
+ * ```ts
142
+ * import { ChainId } from '@human-protocol/sdk';
143
+ *
144
+ * const escrow = await EscrowUtils.getEscrow(
145
+ * ChainId.POLYGON_AMOY,
146
+ * "0x1234567890123456789012345678901234567890"
147
+ * );
148
+ * if (escrow) {
149
+ * console.log('Escrow status:', escrow.status);
150
+ * }
151
+ * ```
152
+ */
153
+ public static async getEscrow(
154
+ chainId: ChainId,
155
+ escrowAddress: string,
156
+ options?: SubgraphOptions
157
+ ): Promise<IEscrow | null> {
158
+ const networkData = NETWORKS[chainId];
159
+
160
+ if (!networkData) {
161
+ throw ErrorUnsupportedChainID;
162
+ }
163
+
164
+ if (escrowAddress && !ethers.isAddress(escrowAddress)) {
165
+ throw ErrorInvalidAddress;
166
+ }
167
+
168
+ const { escrow } = await customGqlFetch<{ escrow: EscrowData | null }>(
169
+ getSubgraphUrl(networkData),
170
+ GET_ESCROW_BY_ADDRESS_QUERY(),
171
+ { escrowAddress: escrowAddress.toLowerCase() },
172
+ options
173
+ );
174
+ if (!escrow) return null;
175
+
176
+ return mapEscrow(escrow, networkData.chainId);
177
+ }
178
+
179
+ /**
180
+ * This function returns the status events for a given set of networks within an optional date range.
181
+ *
182
+ * > This uses Subgraph
183
+ *
184
+ * @param filter - Filter parameters.
185
+ * @param options - Optional configuration for subgraph requests.
186
+ * @returns Array of status events with their corresponding statuses.
187
+ * @throws ErrorInvalidAddress If the launcher address is invalid
188
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
189
+ *
190
+ * @example
191
+ * ```ts
192
+ * import { ChainId, EscrowStatus } from '@human-protocol/sdk';
193
+ *
194
+ * const fromDate = new Date('2023-01-01');
195
+ * const toDate = new Date('2023-12-31');
196
+ * const statusEvents = await EscrowUtils.getStatusEvents({
197
+ * chainId: ChainId.POLYGON,
198
+ * statuses: [EscrowStatus.Pending, EscrowStatus.Complete],
199
+ * from: fromDate,
200
+ * to: toDate
201
+ * });
202
+ * console.log('Status events:', statusEvents.length);
203
+ * ```
204
+ */
205
+ public static async getStatusEvents(
206
+ filter: IStatusEventFilter,
207
+ options?: SubgraphOptions
208
+ ): Promise<IStatusEvent[]> {
209
+ const {
210
+ chainId,
211
+ statuses,
212
+ from,
213
+ to,
214
+ launcher,
215
+ escrowAddress,
216
+ first = 10,
217
+ skip = 0,
218
+ orderDirection = OrderDirection.DESC,
219
+ } = filter;
220
+
221
+ if (launcher && !ethers.isAddress(launcher)) {
222
+ throw ErrorInvalidAddress;
223
+ }
224
+
225
+ if (escrowAddress && !ethers.isAddress(escrowAddress)) {
226
+ throw ErrorInvalidAddress;
227
+ }
228
+
229
+ const networkData = NETWORKS[chainId];
230
+ if (!networkData) {
231
+ throw ErrorUnsupportedChainID;
232
+ }
233
+
234
+ // If statuses are not provided, use all statuses except Launched
235
+ const effectiveStatuses = statuses ?? [
236
+ EscrowStatus.Launched,
237
+ EscrowStatus.Pending,
238
+ EscrowStatus.Partial,
239
+ EscrowStatus.Paid,
240
+ EscrowStatus.Complete,
241
+ EscrowStatus.Cancelled,
242
+ ];
243
+
244
+ const statusNames = effectiveStatuses.map((status) => EscrowStatus[status]);
245
+
246
+ const data = await customGqlFetch<{
247
+ escrowStatusEvents: StatusEvent[];
248
+ }>(
249
+ getSubgraphUrl(networkData),
250
+ GET_STATUS_UPDATES_QUERY(from, to, launcher, escrowAddress),
251
+ {
252
+ status: statusNames,
253
+ from: from ? getUnixTimestamp(from) : undefined,
254
+ to: to ? getUnixTimestamp(to) : undefined,
255
+ launcher: launcher || undefined,
256
+ escrowAddress: escrowAddress || undefined,
257
+ orderDirection,
258
+ first: Math.min(first, 1000),
259
+ skip,
260
+ },
261
+ options
262
+ );
263
+
264
+ if (!data || !data['escrowStatusEvents']) {
265
+ return [];
266
+ }
267
+
268
+ return data['escrowStatusEvents'].map((event) => ({
269
+ timestamp: Number(event.timestamp) * 1000,
270
+ escrowAddress: event.escrowAddress,
271
+ status: event.status as keyof typeof EscrowStatus,
272
+ chainId,
273
+ block: BigInt(event.block),
274
+ txHash: event.txHash,
275
+ }));
276
+ }
277
+
278
+ /**
279
+ * This function returns the payouts for a given set of networks.
280
+ *
281
+ * > This uses Subgraph
282
+ *
283
+ * @param filter - Filter parameters.
284
+ * @param options - Optional configuration for subgraph requests.
285
+ * @returns List of payouts matching the filters.
286
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
287
+ * @throws ErrorInvalidAddress If any filter address is invalid
288
+ *
289
+ * @example
290
+ * ```ts
291
+ * import { ChainId } from '@human-protocol/sdk';
292
+ *
293
+ * const payouts = await EscrowUtils.getPayouts({
294
+ * chainId: ChainId.POLYGON,
295
+ * escrowAddress: '0x1234567890123456789012345678901234567890',
296
+ * recipient: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef',
297
+ * from: new Date('2023-01-01'),
298
+ * to: new Date('2023-12-31')
299
+ * });
300
+ * console.log('Payouts:', payouts.length);
301
+ * ```
302
+ */
303
+ public static async getPayouts(
304
+ filter: IPayoutFilter,
305
+ options?: SubgraphOptions
306
+ ): Promise<IPayout[]> {
307
+ const networkData = NETWORKS[filter.chainId];
308
+ if (!networkData) {
309
+ throw ErrorUnsupportedChainID;
310
+ }
311
+ if (filter.escrowAddress && !ethers.isAddress(filter.escrowAddress)) {
312
+ throw ErrorInvalidAddress;
313
+ }
314
+ if (filter.recipient && !ethers.isAddress(filter.recipient)) {
315
+ throw ErrorInvalidAddress;
316
+ }
317
+
318
+ const first =
319
+ filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
320
+ const skip = filter.skip || 0;
321
+ const orderDirection = filter.orderDirection || OrderDirection.DESC;
322
+
323
+ const { payouts } = await customGqlFetch<{ payouts: PayoutData[] }>(
324
+ getSubgraphUrl(networkData),
325
+ GET_PAYOUTS_QUERY(filter),
326
+ {
327
+ escrowAddress: filter.escrowAddress?.toLowerCase(),
328
+ recipient: filter.recipient?.toLowerCase(),
329
+ from: filter.from ? getUnixTimestamp(filter.from) : undefined,
330
+ to: filter.to ? getUnixTimestamp(filter.to) : undefined,
331
+ first: Math.min(first, 1000),
332
+ skip,
333
+ orderDirection,
334
+ },
335
+ options
336
+ );
337
+ if (!payouts) {
338
+ return [];
339
+ }
340
+
341
+ return payouts.map((payout) => ({
342
+ id: payout.id,
343
+ escrowAddress: payout.escrowAddress,
344
+ recipient: payout.recipient,
345
+ amount: BigInt(payout.amount),
346
+ createdAt: Number(payout.createdAt) * 1000,
347
+ }));
348
+ }
349
+
350
+ /**
351
+ * This function returns the cancellation refunds for a given set of networks.
352
+ *
353
+ * > This uses Subgraph
354
+ *
355
+ * @param filter - Filter parameters.
356
+ * @param options - Optional configuration for subgraph requests.
357
+ * @returns List of cancellation refunds matching the filters.
358
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
359
+ * @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
360
+ * @throws ErrorInvalidAddress If the receiver address is invalid
361
+ *
362
+ * @example
363
+ * ```ts
364
+ * import { ChainId } from '@human-protocol/sdk';
365
+ *
366
+ * const cancellationRefunds = await EscrowUtils.getCancellationRefunds({
367
+ * chainId: ChainId.POLYGON_AMOY,
368
+ * escrowAddress: '0x1234567890123456789012345678901234567890',
369
+ * });
370
+ * console.log('Cancellation refunds:', cancellationRefunds.length);
371
+ * ```
372
+ */
373
+ public static async getCancellationRefunds(
374
+ filter: ICancellationRefundFilter,
375
+ options?: SubgraphOptions
376
+ ): Promise<ICancellationRefund[]> {
377
+ const networkData = NETWORKS[filter.chainId];
378
+ if (!networkData) throw ErrorUnsupportedChainID;
379
+ if (filter.escrowAddress && !ethers.isAddress(filter.escrowAddress)) {
380
+ throw ErrorInvalidEscrowAddressProvided;
381
+ }
382
+ if (filter.receiver && !ethers.isAddress(filter.receiver)) {
383
+ throw ErrorInvalidAddress;
384
+ }
385
+
386
+ const first =
387
+ filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
388
+ const skip = filter.skip || 0;
389
+ const orderDirection = filter.orderDirection || OrderDirection.DESC;
390
+
391
+ const { cancellationRefundEvents } = await customGqlFetch<{
392
+ cancellationRefundEvents: CancellationRefundData[];
393
+ }>(
394
+ getSubgraphUrl(networkData),
395
+ GET_CANCELLATION_REFUNDS_QUERY(filter),
396
+ {
397
+ escrowAddress: filter.escrowAddress?.toLowerCase(),
398
+ receiver: filter.receiver?.toLowerCase(),
399
+ from: filter.from ? getUnixTimestamp(filter.from) : undefined,
400
+ to: filter.to ? getUnixTimestamp(filter.to) : undefined,
401
+ first,
402
+ skip,
403
+ orderDirection,
404
+ },
405
+ options
406
+ );
407
+
408
+ if (!cancellationRefundEvents || cancellationRefundEvents.length === 0) {
409
+ return [];
410
+ }
411
+
412
+ return cancellationRefundEvents.map((event) => ({
413
+ id: event.id,
414
+ escrowAddress: event.escrowAddress,
415
+ receiver: event.receiver,
416
+ amount: BigInt(event.amount),
417
+ block: Number(event.block),
418
+ timestamp: Number(event.timestamp) * 1000,
419
+ txHash: event.txHash,
420
+ }));
421
+ }
422
+
423
+ /**
424
+ * This function returns the cancellation refund for a given escrow address.
425
+ *
426
+ * > This uses Subgraph
427
+ *
428
+ * @param chainId - Network in which the escrow has been deployed
429
+ * @param escrowAddress - Address of the escrow
430
+ * @param options - Optional configuration for subgraph requests.
431
+ * @returns Cancellation refund data or null if not found.
432
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
433
+ * @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
434
+ *
435
+ * @example
436
+ * ```ts
437
+ * import { ChainId } from '@human-protocol/sdk';
438
+ *
439
+ *
440
+ * const cancellationRefund = await EscrowUtils.getCancellationRefund(
441
+ * ChainId.POLYGON_AMOY,
442
+ * "0x1234567890123456789012345678901234567890"
443
+ * );
444
+ * if (cancellationRefund) {
445
+ * console.log('Refund amount:', cancellationRefund.amount);
446
+ * }
447
+ * ```
448
+ */
449
+ public static async getCancellationRefund(
450
+ chainId: ChainId,
451
+ escrowAddress: string,
452
+ options?: SubgraphOptions
453
+ ): Promise<ICancellationRefund | null> {
454
+ const networkData = NETWORKS[chainId];
455
+ if (!networkData) throw ErrorUnsupportedChainID;
456
+
457
+ if (!ethers.isAddress(escrowAddress)) {
458
+ throw ErrorInvalidEscrowAddressProvided;
459
+ }
460
+
461
+ const { cancellationRefundEvents } = await customGqlFetch<{
462
+ cancellationRefundEvents: CancellationRefundData[];
463
+ }>(
464
+ getSubgraphUrl(networkData),
465
+ GET_CANCELLATION_REFUND_BY_ADDRESS_QUERY(),
466
+ { escrowAddress: escrowAddress.toLowerCase() },
467
+ options
468
+ );
469
+
470
+ if (!cancellationRefundEvents || cancellationRefundEvents.length === 0) {
471
+ return null;
472
+ }
473
+
474
+ return {
475
+ id: cancellationRefundEvents[0].id,
476
+ escrowAddress: cancellationRefundEvents[0].escrowAddress,
477
+ receiver: cancellationRefundEvents[0].receiver,
478
+ amount: BigInt(cancellationRefundEvents[0].amount),
479
+ block: Number(cancellationRefundEvents[0].block),
480
+ timestamp: Number(cancellationRefundEvents[0].timestamp) * 1000,
481
+ txHash: cancellationRefundEvents[0].txHash,
482
+ };
483
+ }
484
+ }
485
+
486
+ function mapEscrow(e: EscrowData, chainId: ChainId | number): IEscrow {
487
+ return {
488
+ id: e.id,
489
+ address: e.address,
490
+ amountPaid: BigInt(e.amountPaid),
491
+ balance: BigInt(e.balance),
492
+ count: Number(e.count),
493
+ factoryAddress: e.factoryAddress,
494
+ finalResultsUrl: e.finalResultsUrl,
495
+ finalResultsHash: e.finalResultsHash,
496
+ intermediateResultsUrl: e.intermediateResultsUrl,
497
+ intermediateResultsHash: e.intermediateResultsHash,
498
+ launcher: e.launcher,
499
+ jobRequesterId: e.jobRequesterId,
500
+ manifestHash: e.manifestHash,
501
+ manifest: e.manifest,
502
+ recordingOracle: e.recordingOracle,
503
+ reputationOracle: e.reputationOracle,
504
+ exchangeOracle: e.exchangeOracle,
505
+ recordingOracleFee: e.recordingOracleFee
506
+ ? Number(e.recordingOracleFee)
507
+ : null,
508
+ reputationOracleFee: e.reputationOracleFee
509
+ ? Number(e.reputationOracleFee)
510
+ : null,
511
+ exchangeOracleFee: e.exchangeOracleFee ? Number(e.exchangeOracleFee) : null,
512
+ status: e.status,
513
+ token: e.token,
514
+ totalFundedAmount: BigInt(e.totalFundedAmount),
515
+ createdAt: Number(e.createdAt) * 1000,
516
+ chainId: Number(chainId),
517
+ };
518
+ }
@@ -0,0 +1,2 @@
1
+ export { EscrowClient } from './escrow_client';
2
+ export { EscrowUtils } from './escrow_utils';
@@ -107,7 +107,8 @@ export const GET_ESCROWS_QUERY = (filter: IEscrowsFilter) => {
107
107
  export const GET_STATUS_UPDATES_QUERY = (
108
108
  from?: Date,
109
109
  to?: Date,
110
- launcher?: string
110
+ launcher?: string,
111
+ escrowAddress?: string
111
112
  ) => {
112
113
  const WHERE_CLAUSE = `
113
114
  where: {
@@ -115,6 +116,7 @@ export const GET_STATUS_UPDATES_QUERY = (
115
116
  ${from ? `timestamp_gte: $from` : ''}
116
117
  ${to ? `timestamp_lte: $to` : ''}
117
118
  ${launcher ? `launcher: $launcher` : ''}
119
+ ${escrowAddress ? `escrowAddress: $escrowAddress` : ''}
118
120
  }
119
121
  `;
120
122
  return gql`
@@ -123,6 +125,7 @@ export const GET_STATUS_UPDATES_QUERY = (
123
125
  $from: Int
124
126
  $to: Int
125
127
  $launcher: String
128
+ $escrowAddress: String
126
129
  $orderDirection: String
127
130
  $first: Int
128
131
  $skip: Int
@@ -137,6 +140,8 @@ export const GET_STATUS_UPDATES_QUERY = (
137
140
  escrowAddress,
138
141
  timestamp,
139
142
  status,
143
+ block,
144
+ txHash,
140
145
  }
141
146
  }
142
147
  `;
@@ -42,7 +42,12 @@ const EVENT_DAY_DATA_FRAGMENT = gql`
42
42
  dailyEscrowCount
43
43
  dailyWorkerCount
44
44
  dailyPayoutCount
45
- dailyHMTPayoutAmount
45
+ }
46
+ `;
47
+
48
+ const HMT_EVENT_DAY_DATA_FRAGMENT = gql`
49
+ fragment HMTEventDayDataFields on EventDayData {
50
+ timestamp
46
51
  dailyHMTTransferCount
47
52
  dailyHMTTransferAmount
48
53
  dailyUniqueSenders
@@ -102,3 +107,34 @@ export const GET_EVENT_DAY_DATA_QUERY = (params: IStatisticsFilter) => {
102
107
  ${EVENT_DAY_DATA_FRAGMENT}
103
108
  `;
104
109
  };
110
+
111
+ export const GET_HMT_EVENT_DAY_DATA_QUERY = (params: IStatisticsFilter) => {
112
+ const { from, to } = params;
113
+ const WHERE_CLAUSE = `
114
+ where: {
115
+ ${from !== undefined ? `timestamp_gte: $from` : ''}
116
+ ${to !== undefined ? `timestamp_lte: $to` : ''}
117
+ }
118
+ `;
119
+
120
+ return gql`
121
+ query GetHMTDayData(
122
+ $from: Int,
123
+ $to: Int,
124
+ $orderDirection: String
125
+ $first: Int
126
+ $skip: Int
127
+ ) {
128
+ eventDayDatas(
129
+ ${WHERE_CLAUSE},
130
+ orderBy: timestamp,
131
+ orderDirection: $orderDirection,
132
+ first: $first,
133
+ skip: $skip
134
+ ) {
135
+ ...HMTEventDayDataFields
136
+ }
137
+ }
138
+ ${HMT_EVENT_DAY_DATA_FRAGMENT}
139
+ `;
140
+ };
@@ -6,7 +6,6 @@ export const GET_WORKER_QUERY = gql`
6
6
  worker(id: $address) {
7
7
  id
8
8
  address
9
- totalHMTAmountReceived
10
9
  payoutCount
11
10
  }
12
11
  }
@@ -34,7 +33,6 @@ export const GET_WORKERS_QUERY = (filter: IWorkersFilter) => {
34
33
  ) {
35
34
  id
36
35
  address
37
- totalHMTAmountReceived
38
36
  payoutCount
39
37
  }
40
38
  }`;
@@ -30,7 +30,6 @@ export type EscrowData = {
30
30
  export type WorkerData = {
31
31
  id: string;
32
32
  address: string;
33
- totalHMTAmountReceived: string;
34
33
  payoutCount: string;
35
34
  };
36
35
 
@@ -95,7 +94,10 @@ export type EventDayData = {
95
94
  dailyEscrowCount: string;
96
95
  dailyWorkerCount: string;
97
96
  dailyPayoutCount: string;
98
- dailyHMTPayoutAmount: string;
97
+ };
98
+
99
+ export type HMTEventDayData = {
100
+ timestamp: string;
99
101
  dailyHMTTransferCount: string;
100
102
  dailyHMTTransferAmount: string;
101
103
  dailyUniqueSenders: string;
@@ -118,6 +120,8 @@ export type StatusEvent = {
118
120
  timestamp: string;
119
121
  escrowAddress: string;
120
122
  status: string;
123
+ block: string;
124
+ txHash: string;
121
125
  };
122
126
 
123
127
  export type KVStoreData = {
package/src/index.ts CHANGED
@@ -1,12 +1,3 @@
1
- import { StakingClient, StakingUtils } from './staking';
2
- import { KVStoreClient, KVStoreUtils } from './kvstore';
3
- import { EscrowClient, EscrowUtils } from './escrow';
4
- import { StatisticsUtils } from './statistics';
5
- import { Encryption, EncryptionUtils, MessageDataType } from './encryption';
6
- import { OperatorUtils } from './operator';
7
- import { TransactionUtils } from './transaction';
8
- import { WorkerUtils } from './worker';
9
-
10
1
  export * from './constants';
11
2
  export * from './types';
12
3
  export * from './enums';
@@ -21,21 +12,16 @@ export {
21
12
  TransactionReplaced,
22
13
  ContractExecutionError,
23
14
  InvalidEthereumAddressError,
24
- InvalidKeyError,
15
+ SubgraphBadIndexerError,
16
+ SubgraphRequestError,
25
17
  } from './error';
26
18
 
27
- export {
28
- StakingClient,
29
- KVStoreClient,
30
- KVStoreUtils,
31
- EscrowClient,
32
- EscrowUtils,
33
- StatisticsUtils,
34
- Encryption,
35
- EncryptionUtils,
36
- OperatorUtils,
37
- TransactionUtils,
38
- WorkerUtils,
39
- StakingUtils,
40
- MessageDataType,
41
- };
19
+ export { StakingClient, StakingUtils } from './staking';
20
+ export { KVStoreClient, KVStoreUtils } from './kvstore';
21
+ export { EscrowClient, EscrowUtils } from './escrow';
22
+ export { StatisticsUtils } from './statistics';
23
+ export { Encryption, EncryptionUtils } from './encryption';
24
+ export type { MessageDataType } from './encryption';
25
+ export { OperatorUtils } from './operator';
26
+ export { TransactionUtils } from './transaction';
27
+ export { WorkerUtils } from './worker';