@human-protocol/sdk 6.1.0 → 7.0.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 (122) hide show
  1. package/CHANGELOG.md +21 -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 +388 -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/statistics.d.ts +1 -0
  29. package/dist/graphql/queries/statistics.d.ts.map +1 -1
  30. package/dist/graphql/queries/statistics.js +36 -2
  31. package/dist/graphql/types.d.ts +3 -1
  32. package/dist/graphql/types.d.ts.map +1 -1
  33. package/dist/index.d.ts +10 -10
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +23 -22
  36. package/dist/interfaces.d.ts +0 -5
  37. package/dist/interfaces.d.ts.map +1 -1
  38. package/dist/kvstore/index.d.ts +3 -0
  39. package/dist/kvstore/index.d.ts.map +1 -0
  40. package/dist/kvstore/index.js +7 -0
  41. package/dist/{kvstore.d.ts → kvstore/kvstore_client.d.ts} +3 -108
  42. package/dist/kvstore/kvstore_client.d.ts.map +1 -0
  43. package/dist/{kvstore.js → kvstore/kvstore_client.js} +22 -184
  44. package/dist/kvstore/kvstore_utils.d.ts +105 -0
  45. package/dist/kvstore/kvstore_utils.d.ts.map +1 -0
  46. package/dist/kvstore/kvstore_utils.js +184 -0
  47. package/dist/operator/index.d.ts +2 -0
  48. package/dist/operator/index.d.ts.map +1 -0
  49. package/dist/operator/index.js +5 -0
  50. package/dist/{operator.d.ts → operator/operator_utils.d.ts} +3 -3
  51. package/dist/operator/operator_utils.d.ts.map +1 -0
  52. package/dist/{operator.js → operator/operator_utils.js} +6 -6
  53. package/dist/staking/index.d.ts +3 -0
  54. package/dist/staking/index.d.ts.map +1 -0
  55. package/dist/staking/index.js +7 -0
  56. package/dist/{staking.d.ts → staking/staking_client.d.ts} +4 -65
  57. package/dist/staking/staking_client.d.ts.map +1 -0
  58. package/dist/{staking.js → staking/staking_client.js} +6 -136
  59. package/dist/staking/staking_utils.d.ts +63 -0
  60. package/dist/staking/staking_utils.d.ts.map +1 -0
  61. package/dist/staking/staking_utils.js +137 -0
  62. package/dist/statistics/index.d.ts +2 -0
  63. package/dist/statistics/index.d.ts.map +1 -0
  64. package/dist/statistics/index.js +5 -0
  65. package/dist/{statistics.d.ts → statistics/statistics_utils.d.ts} +4 -13
  66. package/dist/statistics/statistics_utils.d.ts.map +1 -0
  67. package/dist/{statistics.js → statistics/statistics_utils.js} +8 -22
  68. package/dist/transaction/index.d.ts +2 -0
  69. package/dist/transaction/index.d.ts.map +1 -0
  70. package/dist/transaction/index.js +5 -0
  71. package/dist/{transaction.d.ts → transaction/transaction_utils.d.ts} +3 -3
  72. package/dist/transaction/transaction_utils.d.ts.map +1 -0
  73. package/dist/{transaction.js → transaction/transaction_utils.js} +5 -5
  74. package/dist/types.d.ts +8 -0
  75. package/dist/types.d.ts.map +1 -1
  76. package/dist/utils.d.ts +15 -0
  77. package/dist/utils.d.ts.map +1 -1
  78. package/dist/utils.js +71 -4
  79. package/dist/worker/index.d.ts +2 -0
  80. package/dist/worker/index.d.ts.map +1 -0
  81. package/dist/worker/index.js +5 -0
  82. package/dist/{worker.d.ts → worker/worker_utils.d.ts} +3 -3
  83. package/dist/worker/worker_utils.d.ts.map +1 -0
  84. package/dist/{worker.js → worker/worker_utils.js} +5 -5
  85. package/package.json +4 -4
  86. package/src/constants.ts +38 -14
  87. package/src/{encryption.ts → encryption/encryption.ts} +1 -193
  88. package/src/encryption/encryption_utils.ts +179 -0
  89. package/src/encryption/index.ts +3 -0
  90. package/src/encryption/types.ts +15 -0
  91. package/src/error.ts +11 -17
  92. package/src/{escrow.ts → escrow/escrow_client.ts} +36 -556
  93. package/src/escrow/escrow_utils.ts +510 -0
  94. package/src/escrow/index.ts +2 -0
  95. package/src/graphql/queries/statistics.ts +37 -1
  96. package/src/graphql/types.ts +4 -1
  97. package/src/index.ts +11 -25
  98. package/src/interfaces.ts +0 -5
  99. package/src/kvstore/index.ts +2 -0
  100. package/src/{kvstore.ts → kvstore/kvstore_client.ts} +27 -241
  101. package/src/kvstore/kvstore_utils.ts +244 -0
  102. package/src/operator/index.ts +1 -0
  103. package/src/{operator.ts → operator/operator_utils.ts} +8 -8
  104. package/src/staking/index.ts +2 -0
  105. package/src/{staking.ts → staking/staking_client.ts} +8 -174
  106. package/src/staking/staking_utils.ts +170 -0
  107. package/src/statistics/index.ts +1 -0
  108. package/src/{statistics.ts → statistics/statistics_utils.ts} +14 -26
  109. package/src/transaction/index.ts +1 -0
  110. package/src/{transaction.ts → transaction/transaction_utils.ts} +7 -7
  111. package/src/types.ts +8 -0
  112. package/src/utils.ts +80 -3
  113. package/src/worker/index.ts +1 -0
  114. package/src/{worker.ts → worker/worker_utils.ts} +7 -7
  115. package/dist/encryption.d.ts +0 -189
  116. package/dist/encryption.d.ts.map +0 -1
  117. package/dist/kvstore.d.ts.map +0 -1
  118. package/dist/operator.d.ts.map +0 -1
  119. package/dist/staking.d.ts.map +0 -1
  120. package/dist/statistics.d.ts.map +0 -1
  121. package/dist/transaction.d.ts.map +0 -1
  122. package/dist/worker.d.ts.map +0 -1
@@ -10,10 +10,10 @@ import {
10
10
  HMToken__factory,
11
11
  } from '@human-protocol/core/typechain-types';
12
12
  import { ContractRunner, EventLog, Overrides, Signer, ethers } from 'ethers';
13
- import { BaseEthersClient } from './base';
14
- import { ESCROW_BULK_PAYOUT_MAX_ITEMS, NETWORKS } from './constants';
15
- import { requiresSigner } from './decorators';
16
- import { ChainId, OrderDirection } from './enums';
13
+ import { BaseEthersClient } from '../base';
14
+ import { ESCROW_BULK_PAYOUT_MAX_ITEMS, NETWORKS } from '../constants';
15
+ import { requiresSigner } from '../decorators';
16
+ import { ChainId } from '../enums';
17
17
  import {
18
18
  ErrorAmountMustBeGreaterThanZero,
19
19
  ErrorAmountsCannotBeEmptyArray,
@@ -21,7 +21,6 @@ import {
21
21
  ErrorEscrowAddressIsNotProvidedByFactory,
22
22
  ErrorEscrowDoesNotHaveEnoughBalance,
23
23
  ErrorHashIsEmptyString,
24
- ErrorInvalidAddress,
25
24
  ErrorInvalidEscrowAddressProvided,
26
25
  ErrorInvalidExchangeOracleAddressProvided,
27
26
  ErrorInvalidManifest,
@@ -35,51 +34,19 @@ import {
35
34
  ErrorRecipientCannotBeEmptyArray,
36
35
  ErrorStoreResultsVersion,
37
36
  ErrorTooManyRecipients,
38
- ErrorTotalFeeMustBeLessThanHundred,
39
37
  ErrorTransferEventNotFoundInTransactionLogs,
40
38
  ErrorUnsupportedChainID,
41
39
  InvalidEthereumAddressError,
42
40
  WarnVersionMismatch,
43
- } from './error';
44
- import {
45
- CancellationRefundData,
46
- EscrowData,
47
- GET_CANCELLATION_REFUNDS_QUERY,
48
- GET_CANCELLATION_REFUND_BY_ADDRESS_QUERY,
49
- GET_ESCROWS_QUERY,
50
- GET_ESCROW_BY_ADDRESS_QUERY,
51
- GET_PAYOUTS_QUERY,
52
- GET_STATUS_UPDATES_QUERY,
53
- PayoutData,
54
- StatusEvent,
55
- } from './graphql';
56
- import {
57
- IEscrow,
58
- IEscrowConfig,
59
- IEscrowsFilter,
60
- IPayoutFilter,
61
- IStatusEventFilter,
62
- IStatusEvent,
63
- ICancellationRefund,
64
- ICancellationRefundFilter,
65
- IPayout,
66
- IEscrowWithdraw,
67
- SubgraphOptions,
68
- } from './interfaces';
41
+ } from '../error';
42
+ import { IEscrowConfig, IEscrowWithdraw } from '../interfaces';
69
43
  import {
70
44
  EscrowStatus,
71
45
  NetworkData,
72
46
  TransactionLikeWithNonce,
73
47
  TransactionOverrides,
74
- } from './types';
75
- import {
76
- getSubgraphUrl,
77
- getUnixTimestamp,
78
- customGqlFetch,
79
- isValidJson,
80
- isValidUrl,
81
- throwError,
82
- } from './utils';
48
+ } from '../types';
49
+ import { getErrorMessage, isValidJson, isValidUrl, throwError } from '../utils';
83
50
 
84
51
  /**
85
52
  * Client to perform actions on Escrow contracts and obtain information from the contracts.
@@ -248,9 +215,6 @@ export class EscrowClient extends BaseEthersClient {
248
215
  recordingOracle,
249
216
  reputationOracle,
250
217
  exchangeOracle,
251
- recordingOracleFee,
252
- reputationOracleFee,
253
- exchangeOracleFee,
254
218
  manifest,
255
219
  manifestHash,
256
220
  } = escrowConfig;
@@ -267,18 +231,6 @@ export class EscrowClient extends BaseEthersClient {
267
231
  throw ErrorInvalidExchangeOracleAddressProvided;
268
232
  }
269
233
 
270
- if (
271
- recordingOracleFee <= 0 ||
272
- reputationOracleFee <= 0 ||
273
- exchangeOracleFee <= 0
274
- ) {
275
- throw ErrorAmountMustBeGreaterThanZero;
276
- }
277
-
278
- if (recordingOracleFee + reputationOracleFee + exchangeOracleFee > 100) {
279
- throw ErrorTotalFeeMustBeLessThanHundred;
280
- }
281
-
282
234
  const isManifestValid = isValidUrl(manifest) || isValidJson(manifest);
283
235
  if (!isManifestValid) {
284
236
  throw ErrorInvalidManifest;
@@ -303,8 +255,6 @@ export class EscrowClient extends BaseEthersClient {
303
255
  * @throws ErrorInvalidRecordingOracleAddressProvided If the recording oracle address is invalid
304
256
  * @throws ErrorInvalidReputationOracleAddressProvided If the reputation oracle address is invalid
305
257
  * @throws ErrorInvalidExchangeOracleAddressProvided If the exchange oracle address is invalid
306
- * @throws ErrorAmountMustBeGreaterThanZero If any oracle fee is less than or equal to zero
307
- * @throws ErrorTotalFeeMustBeLessThanHundred If the total oracle fees exceed 100
308
258
  * @throws ErrorInvalidManifest If the manifest is not a valid URL or JSON string
309
259
  * @throws ErrorHashIsEmptyString If the manifest hash is empty
310
260
  * @throws ErrorLaunchedEventIsNotEmitted If the LaunchedV2 event is not emitted
@@ -325,9 +275,6 @@ export class EscrowClient extends BaseEthersClient {
325
275
  * recordingOracle: '0xRecordingOracleAddress',
326
276
  * reputationOracle: '0xReputationOracleAddress',
327
277
  * exchangeOracle: '0xExchangeOracleAddress',
328
- * recordingOracleFee: 5n,
329
- * reputationOracleFee: 5n,
330
- * exchangeOracleFee: 5n,
331
278
  * manifest: 'https://example.com/manifest.json',
332
279
  * manifestHash: 'manifestHash-123',
333
280
  * };
@@ -359,9 +306,6 @@ export class EscrowClient extends BaseEthersClient {
359
306
  recordingOracle,
360
307
  reputationOracle,
361
308
  exchangeOracle,
362
- recordingOracleFee,
363
- reputationOracleFee,
364
- exchangeOracleFee,
365
309
  manifest,
366
310
  manifestHash,
367
311
  } = escrowConfig;
@@ -376,9 +320,6 @@ export class EscrowClient extends BaseEthersClient {
376
320
  reputationOracle,
377
321
  recordingOracle,
378
322
  exchangeOracle,
379
- reputationOracleFee,
380
- recordingOracleFee,
381
- exchangeOracleFee,
382
323
  manifest,
383
324
  manifestHash,
384
325
  overrides
@@ -414,8 +355,6 @@ export class EscrowClient extends BaseEthersClient {
414
355
  * @throws ErrorInvalidRecordingOracleAddressProvided If the recording oracle address is invalid
415
356
  * @throws ErrorInvalidReputationOracleAddressProvided If the reputation oracle address is invalid
416
357
  * @throws ErrorInvalidExchangeOracleAddressProvided If the exchange oracle address is invalid
417
- * @throws ErrorAmountMustBeGreaterThanZero If any oracle fee is less than or equal to zero
418
- * @throws ErrorTotalFeeMustBeLessThanHundred If the total oracle fees exceed 100
419
358
  * @throws ErrorInvalidManifest If the manifest is not a valid URL or JSON string
420
359
  * @throws ErrorHashIsEmptyString If the manifest hash is empty
421
360
  * @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
@@ -429,9 +368,6 @@ export class EscrowClient extends BaseEthersClient {
429
368
  * recordingOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
430
369
  * reputationOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
431
370
  * exchangeOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
432
- * recordingOracleFee: 10n,
433
- * reputationOracleFee: 10n,
434
- * exchangeOracleFee: 10n,
435
371
  * manifest: 'http://localhost/manifest.json',
436
372
  * manifestHash: 'b5dad76bf6772c0f07fd5e048f6e75a5f86ee079',
437
373
  * };
@@ -448,9 +384,6 @@ export class EscrowClient extends BaseEthersClient {
448
384
  recordingOracle,
449
385
  reputationOracle,
450
386
  exchangeOracle,
451
- recordingOracleFee,
452
- reputationOracleFee,
453
- exchangeOracleFee,
454
387
  manifest,
455
388
  manifestHash,
456
389
  } = escrowConfig;
@@ -474,9 +407,6 @@ export class EscrowClient extends BaseEthersClient {
474
407
  reputationOracle,
475
408
  recordingOracle,
476
409
  exchangeOracle,
477
- reputationOracleFee,
478
- recordingOracleFee,
479
- exchangeOracleFee,
480
410
  manifest,
481
411
  manifestHash,
482
412
  overrides
@@ -1354,7 +1284,34 @@ export class EscrowClient extends BaseEthersClient {
1354
1284
  try {
1355
1285
  const escrowContract = this.getEscrowContract(escrowAddress);
1356
1286
 
1357
- return escrowContract.manifestUrl();
1287
+ try {
1288
+ return await escrowContract.manifest();
1289
+ } catch (manifestError) {
1290
+ // Fallback for legacy escrows exposing `manifestUrl()` instead of `manifest()`.
1291
+ try {
1292
+ const provider = this.runner.provider;
1293
+ if (!provider) {
1294
+ throw ErrorProviderDoesNotExist;
1295
+ }
1296
+
1297
+ const manifestInterface = new ethers.Interface([
1298
+ 'function manifestUrl() view returns (string)',
1299
+ ]);
1300
+ const target = escrowContract.target as string;
1301
+ const data = manifestInterface.encodeFunctionData('manifestUrl');
1302
+ const result = await provider.call({ to: target, data });
1303
+ return manifestInterface.decodeFunctionResult(
1304
+ 'manifestUrl',
1305
+ result
1306
+ )[0] as string;
1307
+ } catch (fallbackError) {
1308
+ throw new Error(
1309
+ `Failed to fetch manifest using both manifest() and manifestUrl(). ` +
1310
+ `manifest() error: ${getErrorMessage(manifestError)}. ` +
1311
+ `manifestUrl() error: ${getErrorMessage(fallbackError)}.`
1312
+ );
1313
+ }
1314
+ }
1358
1315
  } catch (e) {
1359
1316
  return throwError(e);
1360
1317
  }
@@ -1682,480 +1639,3 @@ export class EscrowClient extends BaseEthersClient {
1682
1639
  }
1683
1640
  }
1684
1641
  }
1685
- /**
1686
- * Utility helpers for escrow-related queries.
1687
- *
1688
- * @example
1689
- * ```ts
1690
- * import { ChainId, EscrowUtils } from '@human-protocol/sdk';
1691
- *
1692
- * const escrows = await EscrowUtils.getEscrows({
1693
- * chainId: ChainId.POLYGON_AMOY
1694
- * });
1695
- * console.log('Escrows:', escrows);
1696
- * ```
1697
- */
1698
- export class EscrowUtils {
1699
- /**
1700
- * This function returns an array of escrows based on the specified filter parameters.
1701
- *
1702
- * @param filter - Filter parameters.
1703
- * @param options - Optional configuration for subgraph requests.
1704
- * @returns List of escrows that match the filter.
1705
- * @throws ErrorInvalidAddress If any filter address is invalid
1706
- * @throws ErrorUnsupportedChainID If the chain ID is not supported
1707
- *
1708
- * @example
1709
- * ```ts
1710
- * import { ChainId, EscrowStatus } from '@human-protocol/sdk';
1711
- *
1712
- * const filters = {
1713
- * status: EscrowStatus.Pending,
1714
- * from: new Date(2023, 4, 8),
1715
- * to: new Date(2023, 5, 8),
1716
- * chainId: ChainId.POLYGON_AMOY
1717
- * };
1718
- * const escrows = await EscrowUtils.getEscrows(filters);
1719
- * console.log('Found escrows:', escrows.length);
1720
- * ```
1721
- */
1722
- public static async getEscrows(
1723
- filter: IEscrowsFilter,
1724
- options?: SubgraphOptions
1725
- ): Promise<IEscrow[]> {
1726
- if (filter.launcher && !ethers.isAddress(filter.launcher)) {
1727
- throw ErrorInvalidAddress;
1728
- }
1729
-
1730
- if (filter.recordingOracle && !ethers.isAddress(filter.recordingOracle)) {
1731
- throw ErrorInvalidAddress;
1732
- }
1733
-
1734
- if (filter.reputationOracle && !ethers.isAddress(filter.reputationOracle)) {
1735
- throw ErrorInvalidAddress;
1736
- }
1737
-
1738
- if (filter.exchangeOracle && !ethers.isAddress(filter.exchangeOracle)) {
1739
- throw ErrorInvalidAddress;
1740
- }
1741
-
1742
- const first =
1743
- filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
1744
- const skip = filter.skip || 0;
1745
- const orderDirection = filter.orderDirection || OrderDirection.DESC;
1746
-
1747
- const networkData = NETWORKS[filter.chainId];
1748
-
1749
- if (!networkData) {
1750
- throw ErrorUnsupportedChainID;
1751
- }
1752
-
1753
- let statuses;
1754
- if (filter.status !== undefined) {
1755
- statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
1756
- statuses = statuses.map((status) => EscrowStatus[status]);
1757
- }
1758
- const { escrows } = await customGqlFetch<{ escrows: EscrowData[] }>(
1759
- getSubgraphUrl(networkData),
1760
- GET_ESCROWS_QUERY(filter),
1761
- {
1762
- ...filter,
1763
- launcher: filter.launcher?.toLowerCase(),
1764
- reputationOracle: filter.reputationOracle?.toLowerCase(),
1765
- recordingOracle: filter.recordingOracle?.toLowerCase(),
1766
- exchangeOracle: filter.exchangeOracle?.toLowerCase(),
1767
- status: statuses,
1768
- from: filter.from ? getUnixTimestamp(filter.from) : undefined,
1769
- to: filter.to ? getUnixTimestamp(filter.to) : undefined,
1770
- orderDirection: orderDirection,
1771
- first: first,
1772
- skip: skip,
1773
- },
1774
- options
1775
- );
1776
- return (escrows || []).map((e) => mapEscrow(e, networkData.chainId));
1777
- }
1778
-
1779
- /**
1780
- * This function returns the escrow data for a given address.
1781
- *
1782
- * > This uses Subgraph
1783
- *
1784
- * @param chainId - Network in which the escrow has been deployed
1785
- * @param escrowAddress - Address of the escrow
1786
- * @param options - Optional configuration for subgraph requests.
1787
- * @returns Escrow data or null if not found.
1788
- * @throws ErrorUnsupportedChainID If the chain ID is not supported
1789
- * @throws ErrorInvalidAddress If the escrow address is invalid
1790
- *
1791
- * @example
1792
- * ```ts
1793
- * import { ChainId } from '@human-protocol/sdk';
1794
- *
1795
- * const escrow = await EscrowUtils.getEscrow(
1796
- * ChainId.POLYGON_AMOY,
1797
- * "0x1234567890123456789012345678901234567890"
1798
- * );
1799
- * if (escrow) {
1800
- * console.log('Escrow status:', escrow.status);
1801
- * }
1802
- * ```
1803
- */
1804
- public static async getEscrow(
1805
- chainId: ChainId,
1806
- escrowAddress: string,
1807
- options?: SubgraphOptions
1808
- ): Promise<IEscrow | null> {
1809
- const networkData = NETWORKS[chainId];
1810
-
1811
- if (!networkData) {
1812
- throw ErrorUnsupportedChainID;
1813
- }
1814
-
1815
- if (escrowAddress && !ethers.isAddress(escrowAddress)) {
1816
- throw ErrorInvalidAddress;
1817
- }
1818
-
1819
- const { escrow } = await customGqlFetch<{ escrow: EscrowData | null }>(
1820
- getSubgraphUrl(networkData),
1821
- GET_ESCROW_BY_ADDRESS_QUERY(),
1822
- { escrowAddress: escrowAddress.toLowerCase() },
1823
- options
1824
- );
1825
- if (!escrow) return null;
1826
-
1827
- return mapEscrow(escrow, networkData.chainId);
1828
- }
1829
-
1830
- /**
1831
- * This function returns the status events for a given set of networks within an optional date range.
1832
- *
1833
- * > This uses Subgraph
1834
- *
1835
- * @param filter - Filter parameters.
1836
- * @param options - Optional configuration for subgraph requests.
1837
- * @returns Array of status events with their corresponding statuses.
1838
- * @throws ErrorInvalidAddress If the launcher address is invalid
1839
- * @throws ErrorUnsupportedChainID If the chain ID is not supported
1840
- *
1841
- * @example
1842
- * ```ts
1843
- * import { ChainId, EscrowStatus } from '@human-protocol/sdk';
1844
- *
1845
- * const fromDate = new Date('2023-01-01');
1846
- * const toDate = new Date('2023-12-31');
1847
- * const statusEvents = await EscrowUtils.getStatusEvents({
1848
- * chainId: ChainId.POLYGON,
1849
- * statuses: [EscrowStatus.Pending, EscrowStatus.Complete],
1850
- * from: fromDate,
1851
- * to: toDate
1852
- * });
1853
- * console.log('Status events:', statusEvents.length);
1854
- * ```
1855
- */
1856
- public static async getStatusEvents(
1857
- filter: IStatusEventFilter,
1858
- options?: SubgraphOptions
1859
- ): Promise<IStatusEvent[]> {
1860
- const {
1861
- chainId,
1862
- statuses,
1863
- from,
1864
- to,
1865
- launcher,
1866
- first = 10,
1867
- skip = 0,
1868
- orderDirection = OrderDirection.DESC,
1869
- } = filter;
1870
-
1871
- if (launcher && !ethers.isAddress(launcher)) {
1872
- throw ErrorInvalidAddress;
1873
- }
1874
-
1875
- const networkData = NETWORKS[chainId];
1876
- if (!networkData) {
1877
- throw ErrorUnsupportedChainID;
1878
- }
1879
-
1880
- // If statuses are not provided, use all statuses except Launched
1881
- const effectiveStatuses = statuses ?? [
1882
- EscrowStatus.Launched,
1883
- EscrowStatus.Pending,
1884
- EscrowStatus.Partial,
1885
- EscrowStatus.Paid,
1886
- EscrowStatus.Complete,
1887
- EscrowStatus.Cancelled,
1888
- ];
1889
-
1890
- const statusNames = effectiveStatuses.map((status) => EscrowStatus[status]);
1891
-
1892
- const data = await customGqlFetch<{
1893
- escrowStatusEvents: StatusEvent[];
1894
- }>(
1895
- getSubgraphUrl(networkData),
1896
- GET_STATUS_UPDATES_QUERY(from, to, launcher),
1897
- {
1898
- status: statusNames,
1899
- from: from ? getUnixTimestamp(from) : undefined,
1900
- to: to ? getUnixTimestamp(to) : undefined,
1901
- launcher: launcher || undefined,
1902
- orderDirection,
1903
- first: Math.min(first, 1000),
1904
- skip,
1905
- },
1906
- options
1907
- );
1908
-
1909
- if (!data || !data['escrowStatusEvents']) {
1910
- return [];
1911
- }
1912
-
1913
- return data['escrowStatusEvents'].map((event) => ({
1914
- timestamp: Number(event.timestamp) * 1000,
1915
- escrowAddress: event.escrowAddress,
1916
- status: EscrowStatus[event.status as keyof typeof EscrowStatus],
1917
- chainId,
1918
- }));
1919
- }
1920
-
1921
- /**
1922
- * This function returns the payouts for a given set of networks.
1923
- *
1924
- * > This uses Subgraph
1925
- *
1926
- * @param filter - Filter parameters.
1927
- * @param options - Optional configuration for subgraph requests.
1928
- * @returns List of payouts matching the filters.
1929
- * @throws ErrorUnsupportedChainID If the chain ID is not supported
1930
- * @throws ErrorInvalidAddress If any filter address is invalid
1931
- *
1932
- * @example
1933
- * ```ts
1934
- * import { ChainId } from '@human-protocol/sdk';
1935
- *
1936
- * const payouts = await EscrowUtils.getPayouts({
1937
- * chainId: ChainId.POLYGON,
1938
- * escrowAddress: '0x1234567890123456789012345678901234567890',
1939
- * recipient: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef',
1940
- * from: new Date('2023-01-01'),
1941
- * to: new Date('2023-12-31')
1942
- * });
1943
- * console.log('Payouts:', payouts.length);
1944
- * ```
1945
- */
1946
- public static async getPayouts(
1947
- filter: IPayoutFilter,
1948
- options?: SubgraphOptions
1949
- ): Promise<IPayout[]> {
1950
- const networkData = NETWORKS[filter.chainId];
1951
- if (!networkData) {
1952
- throw ErrorUnsupportedChainID;
1953
- }
1954
- if (filter.escrowAddress && !ethers.isAddress(filter.escrowAddress)) {
1955
- throw ErrorInvalidAddress;
1956
- }
1957
- if (filter.recipient && !ethers.isAddress(filter.recipient)) {
1958
- throw ErrorInvalidAddress;
1959
- }
1960
-
1961
- const first =
1962
- filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
1963
- const skip = filter.skip || 0;
1964
- const orderDirection = filter.orderDirection || OrderDirection.DESC;
1965
-
1966
- const { payouts } = await customGqlFetch<{ payouts: PayoutData[] }>(
1967
- getSubgraphUrl(networkData),
1968
- GET_PAYOUTS_QUERY(filter),
1969
- {
1970
- escrowAddress: filter.escrowAddress?.toLowerCase(),
1971
- recipient: filter.recipient?.toLowerCase(),
1972
- from: filter.from ? getUnixTimestamp(filter.from) : undefined,
1973
- to: filter.to ? getUnixTimestamp(filter.to) : undefined,
1974
- first: Math.min(first, 1000),
1975
- skip,
1976
- orderDirection,
1977
- },
1978
- options
1979
- );
1980
- if (!payouts) {
1981
- return [];
1982
- }
1983
-
1984
- return payouts.map((payout) => ({
1985
- id: payout.id,
1986
- escrowAddress: payout.escrowAddress,
1987
- recipient: payout.recipient,
1988
- amount: BigInt(payout.amount),
1989
- createdAt: Number(payout.createdAt) * 1000,
1990
- }));
1991
- }
1992
-
1993
- /**
1994
- * This function returns the cancellation refunds for a given set of networks.
1995
- *
1996
- * > This uses Subgraph
1997
- *
1998
- * @param filter - Filter parameters.
1999
- * @param options - Optional configuration for subgraph requests.
2000
- * @returns List of cancellation refunds matching the filters.
2001
- * @throws ErrorUnsupportedChainID If the chain ID is not supported
2002
- * @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
2003
- * @throws ErrorInvalidAddress If the receiver address is invalid
2004
- *
2005
- * @example
2006
- * ```ts
2007
- * import { ChainId } from '@human-protocol/sdk';
2008
- *
2009
- * const cancellationRefunds = await EscrowUtils.getCancellationRefunds({
2010
- * chainId: ChainId.POLYGON_AMOY,
2011
- * escrowAddress: '0x1234567890123456789012345678901234567890',
2012
- * });
2013
- * console.log('Cancellation refunds:', cancellationRefunds.length);
2014
- * ```
2015
- */
2016
- public static async getCancellationRefunds(
2017
- filter: ICancellationRefundFilter,
2018
- options?: SubgraphOptions
2019
- ): Promise<ICancellationRefund[]> {
2020
- const networkData = NETWORKS[filter.chainId];
2021
- if (!networkData) throw ErrorUnsupportedChainID;
2022
- if (filter.escrowAddress && !ethers.isAddress(filter.escrowAddress)) {
2023
- throw ErrorInvalidEscrowAddressProvided;
2024
- }
2025
- if (filter.receiver && !ethers.isAddress(filter.receiver)) {
2026
- throw ErrorInvalidAddress;
2027
- }
2028
-
2029
- const first =
2030
- filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
2031
- const skip = filter.skip || 0;
2032
- const orderDirection = filter.orderDirection || OrderDirection.DESC;
2033
-
2034
- const { cancellationRefundEvents } = await customGqlFetch<{
2035
- cancellationRefundEvents: CancellationRefundData[];
2036
- }>(
2037
- getSubgraphUrl(networkData),
2038
- GET_CANCELLATION_REFUNDS_QUERY(filter),
2039
- {
2040
- escrowAddress: filter.escrowAddress?.toLowerCase(),
2041
- receiver: filter.receiver?.toLowerCase(),
2042
- from: filter.from ? getUnixTimestamp(filter.from) : undefined,
2043
- to: filter.to ? getUnixTimestamp(filter.to) : undefined,
2044
- first,
2045
- skip,
2046
- orderDirection,
2047
- },
2048
- options
2049
- );
2050
-
2051
- if (!cancellationRefundEvents || cancellationRefundEvents.length === 0) {
2052
- return [];
2053
- }
2054
-
2055
- return cancellationRefundEvents.map((event) => ({
2056
- id: event.id,
2057
- escrowAddress: event.escrowAddress,
2058
- receiver: event.receiver,
2059
- amount: BigInt(event.amount),
2060
- block: Number(event.block),
2061
- timestamp: Number(event.timestamp) * 1000,
2062
- txHash: event.txHash,
2063
- }));
2064
- }
2065
-
2066
- /**
2067
- * This function returns the cancellation refund for a given escrow address.
2068
- *
2069
- * > This uses Subgraph
2070
- *
2071
- * @param chainId - Network in which the escrow has been deployed
2072
- * @param escrowAddress - Address of the escrow
2073
- * @param options - Optional configuration for subgraph requests.
2074
- * @returns Cancellation refund data or null if not found.
2075
- * @throws ErrorUnsupportedChainID If the chain ID is not supported
2076
- * @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
2077
- *
2078
- * @example
2079
- * ```ts
2080
- * import { ChainId } from '@human-protocol/sdk';
2081
- *
2082
- *
2083
- * const cancellationRefund = await EscrowUtils.getCancellationRefund(
2084
- * ChainId.POLYGON_AMOY,
2085
- * "0x1234567890123456789012345678901234567890"
2086
- * );
2087
- * if (cancellationRefund) {
2088
- * console.log('Refund amount:', cancellationRefund.amount);
2089
- * }
2090
- * ```
2091
- */
2092
- public static async getCancellationRefund(
2093
- chainId: ChainId,
2094
- escrowAddress: string,
2095
- options?: SubgraphOptions
2096
- ): Promise<ICancellationRefund | null> {
2097
- const networkData = NETWORKS[chainId];
2098
- if (!networkData) throw ErrorUnsupportedChainID;
2099
-
2100
- if (!ethers.isAddress(escrowAddress)) {
2101
- throw ErrorInvalidEscrowAddressProvided;
2102
- }
2103
-
2104
- const { cancellationRefundEvents } = await customGqlFetch<{
2105
- cancellationRefundEvents: CancellationRefundData[];
2106
- }>(
2107
- getSubgraphUrl(networkData),
2108
- GET_CANCELLATION_REFUND_BY_ADDRESS_QUERY(),
2109
- { escrowAddress: escrowAddress.toLowerCase() },
2110
- options
2111
- );
2112
-
2113
- if (!cancellationRefundEvents || cancellationRefundEvents.length === 0) {
2114
- return null;
2115
- }
2116
-
2117
- return {
2118
- id: cancellationRefundEvents[0].id,
2119
- escrowAddress: cancellationRefundEvents[0].escrowAddress,
2120
- receiver: cancellationRefundEvents[0].receiver,
2121
- amount: BigInt(cancellationRefundEvents[0].amount),
2122
- block: Number(cancellationRefundEvents[0].block),
2123
- timestamp: Number(cancellationRefundEvents[0].timestamp) * 1000,
2124
- txHash: cancellationRefundEvents[0].txHash,
2125
- };
2126
- }
2127
- }
2128
-
2129
- function mapEscrow(e: EscrowData, chainId: ChainId | number): IEscrow {
2130
- return {
2131
- id: e.id,
2132
- address: e.address,
2133
- amountPaid: BigInt(e.amountPaid),
2134
- balance: BigInt(e.balance),
2135
- count: Number(e.count),
2136
- factoryAddress: e.factoryAddress,
2137
- finalResultsUrl: e.finalResultsUrl,
2138
- finalResultsHash: e.finalResultsHash,
2139
- intermediateResultsUrl: e.intermediateResultsUrl,
2140
- intermediateResultsHash: e.intermediateResultsHash,
2141
- launcher: e.launcher,
2142
- jobRequesterId: e.jobRequesterId,
2143
- manifestHash: e.manifestHash,
2144
- manifest: e.manifest,
2145
- recordingOracle: e.recordingOracle,
2146
- reputationOracle: e.reputationOracle,
2147
- exchangeOracle: e.exchangeOracle,
2148
- recordingOracleFee: e.recordingOracleFee
2149
- ? Number(e.recordingOracleFee)
2150
- : null,
2151
- reputationOracleFee: e.reputationOracleFee
2152
- ? Number(e.reputationOracleFee)
2153
- : null,
2154
- exchangeOracleFee: e.exchangeOracleFee ? Number(e.exchangeOracleFee) : null,
2155
- status: e.status,
2156
- token: e.token,
2157
- totalFundedAmount: BigInt(e.totalFundedAmount),
2158
- createdAt: Number(e.createdAt) * 1000,
2159
- chainId: Number(chainId),
2160
- };
2161
- }