@human-protocol/sdk 3.0.0 → 3.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 (52) hide show
  1. package/README.md +23 -80
  2. package/dist/enums.d.ts +4 -0
  3. package/dist/enums.d.ts.map +1 -1
  4. package/dist/enums.js +6 -1
  5. package/dist/error.d.ts +7 -0
  6. package/dist/error.d.ts.map +1 -1
  7. package/dist/error.js +8 -1
  8. package/dist/escrow.d.ts +90 -5
  9. package/dist/escrow.d.ts.map +1 -1
  10. package/dist/escrow.js +153 -37
  11. package/dist/graphql/queries/escrow.d.ts +1 -0
  12. package/dist/graphql/queries/escrow.d.ts.map +1 -1
  13. package/dist/graphql/queries/escrow.js +50 -9
  14. package/dist/graphql/queries/hmtoken.d.ts +1 -1
  15. package/dist/graphql/queries/hmtoken.d.ts.map +1 -1
  16. package/dist/graphql/queries/hmtoken.js +23 -7
  17. package/dist/graphql/queries/statistics.d.ts.map +1 -1
  18. package/dist/graphql/queries/statistics.js +2 -0
  19. package/dist/graphql/queries/transaction.d.ts.map +1 -1
  20. package/dist/graphql/queries/transaction.js +12 -7
  21. package/dist/graphql/types.d.ts +11 -0
  22. package/dist/graphql/types.d.ts.map +1 -1
  23. package/dist/index.d.ts +2 -1
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +3 -1
  26. package/dist/interfaces.d.ts +15 -6
  27. package/dist/interfaces.d.ts.map +1 -1
  28. package/dist/kvstore.d.ts +1 -1
  29. package/dist/kvstore.js +1 -1
  30. package/dist/operator.d.ts.map +1 -1
  31. package/dist/operator.js +64 -81
  32. package/dist/statistics.d.ts +28 -2
  33. package/dist/statistics.d.ts.map +1 -1
  34. package/dist/statistics.js +46 -1
  35. package/dist/transaction.d.ts +10 -4
  36. package/dist/transaction.d.ts.map +1 -1
  37. package/dist/transaction.js +36 -27
  38. package/package.json +5 -4
  39. package/src/enums.ts +5 -0
  40. package/src/error.ts +8 -0
  41. package/src/escrow.ts +197 -48
  42. package/src/graphql/queries/escrow.ts +53 -8
  43. package/src/graphql/queries/hmtoken.ts +23 -7
  44. package/src/graphql/queries/statistics.ts +2 -0
  45. package/src/graphql/queries/transaction.ts +12 -7
  46. package/src/graphql/types.ts +13 -0
  47. package/src/index.ts +2 -0
  48. package/src/interfaces.ts +17 -6
  49. package/src/kvstore.ts +1 -1
  50. package/src/operator.ts +79 -91
  51. package/src/statistics.ts +54 -2
  52. package/src/transaction.ts +40 -30
@@ -362,7 +362,7 @@ class StatisticsClient {
362
362
  async getHMTStatistics(params = {}) {
363
363
  try {
364
364
  const { hmtokenStatistics } = await (0, graphql_request_1.default)(this.subgraphUrl, graphql_1.GET_HMTOKEN_STATISTICS_QUERY);
365
- const { holders } = await (0, graphql_request_1.default)(this.subgraphUrl, graphql_1.GET_HOLDERS_QUERY);
365
+ const { holders } = await (0, graphql_request_1.default)(this.subgraphUrl, (0, graphql_1.GET_HOLDERS_QUERY)());
366
366
  const { eventDayDatas } = await (0, graphql_request_1.default)(this.subgraphUrl, (0, graphql_1.GET_EVENT_DAY_DATA_QUERY)(params), {
367
367
  from: params.from ? params.from.getTime() / 1000 : undefined,
368
368
  to: params.to ? params.to.getTime() / 1000 : undefined,
@@ -379,6 +379,8 @@ class StatisticsClient {
379
379
  timestamp: new Date(+eventDayData.timestamp * 1000),
380
380
  totalTransactionAmount: ethers_1.ethers.toBigInt(eventDayData.dailyHMTTransferAmount),
381
381
  totalTransactionCount: +eventDayData.dailyHMTTransferCount,
382
+ dailyUniqueSenders: +eventDayData.dailyUniqueSenders,
383
+ dailyUniqueReceivers: +eventDayData.dailyUniqueReceivers,
382
384
  })),
383
385
  };
384
386
  }
@@ -386,5 +388,48 @@ class StatisticsClient {
386
388
  return (0, utils_1.throwError)(e);
387
389
  }
388
390
  }
391
+ /**
392
+ * This function returns the holders of the HMToken with optional filters and ordering.
393
+ *
394
+ * **Input parameters**
395
+ *
396
+ * @param {IHMTHoldersParams} params HMT Holders params with filters and ordering
397
+ * @returns {HMTHolder[]} List of HMToken holders.
398
+ *
399
+ * **Code example**
400
+ *
401
+ * ```ts
402
+ * import { StatisticsClient, ChainId, NETWORKS } from '@human-protocol/sdk';
403
+ *
404
+ * const statisticsClient = new StatisticsClient(NETWORKS[ChainId.POLYGON_AMOY]);
405
+ *
406
+ * const hmtHolders = await statisticsClient.getHMTHolders({
407
+ * orderDirection: 'asc',
408
+ * });
409
+ *
410
+ * console.log('HMT holders:', hmtHolders.map((h) => ({
411
+ * ...h,
412
+ * balance: h.balance.toString(),
413
+ * })));
414
+ * ```
415
+ */
416
+ async getHMTHolders(params = {}) {
417
+ try {
418
+ const { address, orderDirection } = params;
419
+ const query = (0, graphql_1.GET_HOLDERS_QUERY)(address);
420
+ const { holders } = await (0, graphql_request_1.default)(this.subgraphUrl, query, {
421
+ address,
422
+ orderBy: 'balance',
423
+ orderDirection,
424
+ });
425
+ return holders.map((holder) => ({
426
+ address: holder.address,
427
+ balance: ethers_1.ethers.toBigInt(holder.balance),
428
+ }));
429
+ }
430
+ catch (e) {
431
+ return (0, utils_1.throwError)(e);
432
+ }
433
+ }
389
434
  }
390
435
  exports.StatisticsClient = StatisticsClient;
@@ -26,13 +26,16 @@ export declare class TransactionUtils {
26
26
  *
27
27
  * ```ts
28
28
  * interface ITransactionsFilter {
29
- * networks: ChainId[]; // List of chain IDs to query.
29
+ * chainId: ChainId; // List of chain IDs to query.
30
30
  * fromAddress?: string; // (Optional) The address from which transactions are sent.
31
31
  * toAddress?: string; // (Optional) The address to which transactions are sent.
32
32
  * startDate?: Date; // (Optional) The start date to filter transactions (inclusive).
33
33
  * endDate?: Date; // (Optional) The end date to filter transactions (inclusive).
34
34
  * startBlock?: number; // (Optional) The start block number to filter transactions (inclusive).
35
35
  * endBlock?: number; // (Optional) The end block number to filter transactions (inclusive).
36
+ * first?: number; // (Optional) Number of transactions per page. Default is 10.
37
+ * skip?: number; // (Optional) Number of transactions to skip. Default is 0.
38
+ * orderDirection?: OrderDirection; // (Optional) Order of the results. Default is DESC.
36
39
  * }
37
40
  * ```
38
41
  *
@@ -54,12 +57,15 @@ export declare class TransactionUtils {
54
57
  * **Code example**
55
58
  *
56
59
  * ```ts
57
- * import { TransactionUtils, ChainId } from '@human-protocol/sdk';
60
+ * import { TransactionUtils, ChainId, OrderDirection } from '@human-protocol/sdk';
58
61
  *
59
62
  * const filter: ITransactionsFilter = {
60
- * networks: [ChainId.POLYGON],
63
+ * chainId: ChainId.POLYGON,
61
64
  * startDate: new Date('2022-01-01'),
62
- * endDate: new Date('2022-12-31')
65
+ * endDate: new Date('2022-12-31'),
66
+ * first: 10,
67
+ * skip: 0,
68
+ * orderDirection: OrderDirection.DESC,
63
69
  * };
64
70
  * const transactions = await TransactionUtils.getTransactions(filter);
65
71
  * ```
@@ -1 +1 @@
1
- {"version":3,"file":"transaction.d.ts","sourceRoot":"","sources":["../src/transaction.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAUlC,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAGjE,qBAAa,gBAAgB;IAC3B;;;;;;;;;;;;;;OAcG;WACiB,cAAc,CAChC,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,YAAY,CAAC;IAmBxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8CG;WACiB,eAAe,CACjC,MAAM,EAAE,mBAAmB,GAC1B,OAAO,CAAC,YAAY,EAAE,CAAC;CAqC3B"}
1
+ {"version":3,"file":"transaction.d.ts","sourceRoot":"","sources":["../src/transaction.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,OAAO,EAAkB,MAAM,SAAS,CAAC;AAUlD,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAGjE,qBAAa,gBAAgB;IAC3B;;;;;;;;;;;;;;OAcG;WACiB,cAAc,CAChC,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,YAAY,CAAC;IAmBxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoDG;WACiB,eAAe,CACjC,MAAM,EAAE,mBAAmB,GAC1B,OAAO,CAAC,YAAY,EAAE,CAAC;CAyC3B"}
@@ -8,6 +8,7 @@ exports.TransactionUtils = void 0;
8
8
  const ethers_1 = require("ethers");
9
9
  const graphql_request_1 = __importDefault(require("graphql-request"));
10
10
  const constants_1 = require("./constants");
11
+ const enums_1 = require("./enums");
11
12
  const error_1 = require("./error");
12
13
  const transaction_1 = require("./graphql/queries/transaction");
13
14
  const utils_1 = require("./utils");
@@ -49,13 +50,16 @@ class TransactionUtils {
49
50
  *
50
51
  * ```ts
51
52
  * interface ITransactionsFilter {
52
- * networks: ChainId[]; // List of chain IDs to query.
53
+ * chainId: ChainId; // List of chain IDs to query.
53
54
  * fromAddress?: string; // (Optional) The address from which transactions are sent.
54
55
  * toAddress?: string; // (Optional) The address to which transactions are sent.
55
56
  * startDate?: Date; // (Optional) The start date to filter transactions (inclusive).
56
57
  * endDate?: Date; // (Optional) The end date to filter transactions (inclusive).
57
58
  * startBlock?: number; // (Optional) The start block number to filter transactions (inclusive).
58
59
  * endBlock?: number; // (Optional) The end block number to filter transactions (inclusive).
60
+ * first?: number; // (Optional) Number of transactions per page. Default is 10.
61
+ * skip?: number; // (Optional) Number of transactions to skip. Default is 0.
62
+ * orderDirection?: OrderDirection; // (Optional) Order of the results. Default is DESC.
59
63
  * }
60
64
  * ```
61
65
  *
@@ -77,12 +81,15 @@ class TransactionUtils {
77
81
  * **Code example**
78
82
  *
79
83
  * ```ts
80
- * import { TransactionUtils, ChainId } from '@human-protocol/sdk';
84
+ * import { TransactionUtils, ChainId, OrderDirection } from '@human-protocol/sdk';
81
85
  *
82
86
  * const filter: ITransactionsFilter = {
83
- * networks: [ChainId.POLYGON],
87
+ * chainId: ChainId.POLYGON,
84
88
  * startDate: new Date('2022-01-01'),
85
- * endDate: new Date('2022-12-31')
89
+ * endDate: new Date('2022-12-31'),
90
+ * first: 10,
91
+ * skip: 0,
92
+ * orderDirection: OrderDirection.DESC,
86
93
  * };
87
94
  * const transactions = await TransactionUtils.getTransactions(filter);
88
95
  * ```
@@ -92,30 +99,32 @@ class TransactionUtils {
92
99
  (!!filter.startBlock || !!filter.endBlock)) {
93
100
  throw error_1.ErrorCannotUseDateAndBlockSimultaneously;
94
101
  }
95
- const transactions_data = [];
96
- for (const chainId of filter.networks) {
97
- const networkData = constants_1.NETWORKS[chainId];
98
- if (!networkData) {
99
- throw error_1.ErrorUnsupportedChainID;
100
- }
101
- const { transactions } = await (0, graphql_request_1.default)((0, utils_1.getSubgraphUrl)(networkData), (0, transaction_1.GET_TRANSACTIONS_QUERY)(filter), {
102
- fromAddress: filter?.fromAddress,
103
- toAddress: filter?.toAddress,
104
- startDate: filter?.startDate
105
- ? Math.floor(filter?.startDate.getTime() / 1000)
106
- : undefined,
107
- endDate: filter.endDate
108
- ? Math.floor(filter.endDate.getTime() / 1000)
109
- : undefined,
110
- startBlock: filter.startBlock ? filter.startBlock : undefined,
111
- endBlock: filter.endBlock ? filter.endBlock : undefined,
112
- });
113
- if (!transactions) {
114
- continue;
115
- }
116
- transactions_data.push(...transactions);
102
+ const first = filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
103
+ const skip = filter.skip || 0;
104
+ const orderDirection = filter.orderDirection || enums_1.OrderDirection.DESC;
105
+ const networkData = constants_1.NETWORKS[filter.chainId];
106
+ if (!networkData) {
107
+ throw error_1.ErrorUnsupportedChainID;
108
+ }
109
+ const { transactions } = await (0, graphql_request_1.default)((0, utils_1.getSubgraphUrl)(networkData), (0, transaction_1.GET_TRANSACTIONS_QUERY)(filter), {
110
+ fromAddress: filter?.fromAddress,
111
+ toAddress: filter?.toAddress,
112
+ startDate: filter?.startDate
113
+ ? Math.floor(filter?.startDate.getTime() / 1000)
114
+ : undefined,
115
+ endDate: filter.endDate
116
+ ? Math.floor(filter.endDate.getTime() / 1000)
117
+ : undefined,
118
+ startBlock: filter.startBlock ? filter.startBlock : undefined,
119
+ endBlock: filter.endBlock ? filter.endBlock : undefined,
120
+ orderDirection: orderDirection,
121
+ first: first,
122
+ skip: skip,
123
+ });
124
+ if (!transactions) {
125
+ return [];
117
126
  }
118
- return transactions_data;
127
+ return transactions;
119
128
  }
120
129
  }
121
130
  exports.TransactionUtils = TransactionUtils;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@human-protocol/sdk",
3
3
  "description": "Human Protocol SDK",
4
- "version": "3.0.0",
4
+ "version": "3.0.1",
5
5
  "files": [
6
6
  "src",
7
7
  "dist"
@@ -52,8 +52,8 @@
52
52
  "winston": "^3.13.0"
53
53
  },
54
54
  "devDependencies": {
55
- "typedoc": "^0.25.1",
56
- "typedoc-plugin-markdown": "^4.0.3"
55
+ "typedoc": "^0.26.5",
56
+ "typedoc-plugin-markdown": "^4.2.3"
57
57
  },
58
58
  "typedocOptions": {
59
59
  "entryPoints": [
@@ -64,7 +64,8 @@
64
64
  "./src/operator.ts",
65
65
  "./src/staking.ts",
66
66
  "./src/storage.ts",
67
- "./src/statistics.ts"
67
+ "./src/statistics.ts",
68
+ "./src/transaction.ts"
68
69
  ]
69
70
  }
70
71
  }
package/src/enums.ts CHANGED
@@ -19,3 +19,8 @@ export enum ChainId {
19
19
  LOCALHOST = 1338,
20
20
  XLAYER = 196,
21
21
  }
22
+
23
+ export enum OrderDirection {
24
+ ASC = 'asc',
25
+ DESC = 'desc',
26
+ }
package/src/error.ts CHANGED
@@ -335,5 +335,13 @@ export class InvalidEthereumAddressError extends Error {
335
335
  */
336
336
  export const ErrorInvalidHash = new Error('Invalid hash');
337
337
 
338
+ /**
339
+ * @constant {Error} - The Status is not supported
340
+ */
341
+ export const ErrorUnsupportedStatus = new Error('Unsupported status for query');
342
+
343
+ /**
344
+ * @constant {Error} - The SUBGRAPH_API_KEY is not being provided
345
+ */
338
346
  export const WarnSubgraphApiKeyNotProvided =
339
347
  '"SUBGRAPH_API_KEY" is not being provided. It might cause issues with the subgraph.';
package/src/escrow.ts CHANGED
@@ -12,7 +12,7 @@ import gqlFetch from 'graphql-request';
12
12
  import { BaseEthersClient } from './base';
13
13
  import { DEFAULT_TX_ID, NETWORKS } from './constants';
14
14
  import { requiresSigner } from './decorators';
15
- import { ChainId } from './enums';
15
+ import { ChainId, OrderDirection } from './enums';
16
16
  import {
17
17
  ErrorAmountMustBeGreaterThanZero,
18
18
  ErrorAmountsCannotBeEmptyArray,
@@ -41,6 +41,8 @@ import {
41
41
  EscrowData,
42
42
  GET_ESCROWS_QUERY,
43
43
  GET_ESCROW_BY_ADDRESS_QUERY,
44
+ GET_STATUS_UPDATES_QUERY,
45
+ StatusEvent,
44
46
  } from './graphql';
45
47
  import { IEscrowConfig, IEscrowsFilter } from './interfaces';
46
48
  import { EscrowCancel, EscrowStatus, NetworkData } from './types';
@@ -1399,7 +1401,7 @@ export class EscrowClient extends BaseEthersClient {
1399
1401
  * import { ChainId, EscrowUtils } from '@human-protocol/sdk';
1400
1402
  *
1401
1403
  * const escrowAddresses = new EscrowUtils.getEscrows({
1402
- * networks: [ChainId.POLYGON_AMOY]
1404
+ * network: ChainId.POLYGON_AMOY
1403
1405
  * });
1404
1406
  * ```
1405
1407
  */
@@ -1412,7 +1414,7 @@ export class EscrowUtils {
1412
1414
  *
1413
1415
  * ```ts
1414
1416
  * interface IEscrowsFilter {
1415
- * networks: ChainId[];
1417
+ * chainId: ChainId;
1416
1418
  * launcher?: string;
1417
1419
  * reputationOracle?: string;
1418
1420
  * recordingOracle?: string;
@@ -1421,6 +1423,9 @@ export class EscrowUtils {
1421
1423
  * status?: EscrowStatus;
1422
1424
  * from?: Date;
1423
1425
  * to?: Date;
1426
+ * first?: number;
1427
+ * skip?: number;
1428
+ * orderDirection?: OrderDirection;
1424
1429
  * }
1425
1430
  * ```
1426
1431
  *
@@ -1447,6 +1452,13 @@ export class EscrowUtils {
1447
1452
  * ```
1448
1453
  *
1449
1454
  * ```ts
1455
+ * enum OrderDirection {
1456
+ * ASC = 'asc',
1457
+ * DESC = 'desc',
1458
+ * }
1459
+ * ```
1460
+ *
1461
+ * ```ts
1450
1462
  * enum EscrowStatus {
1451
1463
  * Launched,
1452
1464
  * Pending,
@@ -1497,7 +1509,7 @@ export class EscrowUtils {
1497
1509
  * status: EscrowStatus.Pending,
1498
1510
  * from: new Date(2023, 4, 8),
1499
1511
  * to: new Date(2023, 5, 8),
1500
- * networks: [ChainId.POLYGON_AMOY]
1512
+ * chainId: ChainId.POLYGON_AMOY
1501
1513
  * };
1502
1514
  * const escrowDatas = await EscrowUtils.getEscrows(filters);
1503
1515
  * ```
@@ -1505,9 +1517,6 @@ export class EscrowUtils {
1505
1517
  public static async getEscrows(
1506
1518
  filter: IEscrowsFilter
1507
1519
  ): Promise<EscrowData[]> {
1508
- if (!filter?.networks?.length) {
1509
- throw ErrorUnsupportedChainID;
1510
- }
1511
1520
  if (filter.launcher && !ethers.isAddress(filter.launcher)) {
1512
1521
  throw ErrorInvalidAddress;
1513
1522
  }
@@ -1524,42 +1533,46 @@ export class EscrowUtils {
1524
1533
  throw ErrorInvalidAddress;
1525
1534
  }
1526
1535
 
1527
- try {
1528
- const escrowAddresses: EscrowData[] = [];
1529
- for (const chainId of filter.networks) {
1530
- const networkData = NETWORKS[chainId];
1536
+ const first =
1537
+ filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
1538
+ const skip = filter.skip || 0;
1539
+ const orderDirection = filter.orderDirection || OrderDirection.DESC;
1531
1540
 
1532
- if (!networkData) {
1533
- throw ErrorUnsupportedChainID;
1534
- }
1541
+ const networkData = NETWORKS[filter.chainId];
1535
1542
 
1536
- const { escrows } = await gqlFetch<{ escrows: EscrowData[] }>(
1537
- getSubgraphUrl(networkData),
1538
- GET_ESCROWS_QUERY(filter),
1539
- {
1540
- ...filter,
1541
- launcher: filter.launcher?.toLowerCase(),
1542
- reputationOracle: filter.reputationOracle?.toLowerCase(),
1543
- recordingOracle: filter.recordingOracle?.toLowerCase(),
1544
- exchangeOracle: filter.exchangeOracle?.toLowerCase(),
1545
- status:
1546
- filter.status !== undefined
1547
- ? Object.entries(EscrowStatus).find(
1548
- ([, value]) => value === filter.status
1549
- )?.[0]
1550
- : undefined,
1551
- from: filter.from ? +filter.from.getTime() / 1000 : undefined,
1552
- to: filter.to ? +filter.to.getTime() / 1000 : undefined,
1553
- }
1554
- );
1555
- escrows.map((escrow) => (escrow.chainId = networkData.chainId));
1556
- escrowAddresses.push(...escrows);
1543
+ if (!networkData) {
1544
+ throw ErrorUnsupportedChainID;
1545
+ }
1546
+
1547
+ const { escrows } = await gqlFetch<{ escrows: EscrowData[] }>(
1548
+ getSubgraphUrl(networkData),
1549
+ GET_ESCROWS_QUERY(filter),
1550
+ {
1551
+ ...filter,
1552
+ launcher: filter.launcher?.toLowerCase(),
1553
+ reputationOracle: filter.reputationOracle?.toLowerCase(),
1554
+ recordingOracle: filter.recordingOracle?.toLowerCase(),
1555
+ exchangeOracle: filter.exchangeOracle?.toLowerCase(),
1556
+ status:
1557
+ filter.status !== undefined
1558
+ ? Object.entries(EscrowStatus).find(
1559
+ ([, value]) => value === filter.status
1560
+ )?.[0]
1561
+ : undefined,
1562
+ from: filter.from ? +filter.from.getTime() / 1000 : undefined,
1563
+ to: filter.to ? +filter.to.getTime() / 1000 : undefined,
1564
+ orderDirection: orderDirection,
1565
+ first: first,
1566
+ skip: skip,
1557
1567
  }
1558
- escrowAddresses.sort((a, b) => Number(b.createdAt) - Number(a.createdAt));
1559
- return escrowAddresses;
1560
- } catch (e: any) {
1561
- return throwError(e);
1568
+ );
1569
+ escrows.map((escrow) => (escrow.chainId = networkData.chainId));
1570
+
1571
+ if (!escrows) {
1572
+ return [];
1562
1573
  }
1574
+
1575
+ return escrows;
1563
1576
  }
1564
1577
 
1565
1578
  /**
@@ -1644,16 +1657,152 @@ export class EscrowUtils {
1644
1657
  throw ErrorInvalidAddress;
1645
1658
  }
1646
1659
 
1647
- try {
1648
- const { escrow } = await gqlFetch<{ escrow: EscrowData }>(
1649
- getSubgraphUrl(networkData),
1650
- GET_ESCROW_BY_ADDRESS_QUERY(),
1651
- { escrowAddress: escrowAddress.toLowerCase() }
1652
- );
1660
+ const { escrow } = await gqlFetch<{ escrow: EscrowData }>(
1661
+ getSubgraphUrl(networkData),
1662
+ GET_ESCROW_BY_ADDRESS_QUERY(),
1663
+ { escrowAddress: escrowAddress.toLowerCase() }
1664
+ );
1653
1665
 
1654
- return escrow || null;
1655
- } catch (e: any) {
1656
- return throwError(e);
1666
+ return escrow || null;
1667
+ }
1668
+
1669
+ /**
1670
+ * This function returns the status events for a given set of networks within an optional date range.
1671
+ *
1672
+ * > This uses Subgraph
1673
+ *
1674
+ * **Input parameters**
1675
+ *
1676
+ * ```ts
1677
+ * enum ChainId {
1678
+ * ALL = -1,
1679
+ * MAINNET = 1,
1680
+ * RINKEBY = 4,
1681
+ * GOERLI = 5,
1682
+ * SEPOLIA = 11155111,
1683
+ * BSC_MAINNET = 56,
1684
+ * BSC_TESTNET = 97,
1685
+ * POLYGON = 137,
1686
+ * POLYGON_MUMBAI = 80001,
1687
+ * POLYGON_AMOY = 80002,
1688
+ * MOONBEAM = 1284,
1689
+ * MOONBASE_ALPHA = 1287,
1690
+ * AVALANCHE = 43114,
1691
+ * AVALANCHE_TESTNET = 43113,
1692
+ * CELO = 42220,
1693
+ * CELO_ALFAJORES = 44787,
1694
+ * LOCALHOST = 1338,
1695
+ * XLAYER_TESTNET = 195,
1696
+ * XLAYER = 196,
1697
+ * }
1698
+ * ```
1699
+ *
1700
+ * ```ts
1701
+ * enum OrderDirection {
1702
+ * ASC = 'asc',
1703
+ * DESC = 'desc',
1704
+ * }
1705
+ * ```
1706
+ *
1707
+ * ```ts
1708
+ * type Status = {
1709
+ * escrowAddress: string;
1710
+ * timestamp: string;
1711
+ * status: string;
1712
+ * };
1713
+ * ```
1714
+ *
1715
+ * @param {ChainId} chainId - List of network IDs to query for status events.
1716
+ * @param {EscrowStatus[]} [statuses] - Optional array of statuses to query for. If not provided, queries for all statuses.
1717
+ * @param {Date} [from] - Optional start date to filter events.
1718
+ * @param {Date} [to] - Optional end date to filter events.
1719
+ * @param {string} [launcher] - Optional launcher address to filter events. Must be a valid Ethereum address.
1720
+ * @param {number} [first] - Optional number of transactions per page. Default is 10.
1721
+ * @param {number} [skip] - Optional number of transactions to skip. Default is 0.
1722
+ * @param {OrderDirection} [orderDirection] - Optional order of the results. Default is DESC.
1723
+ * @returns {Promise<StatusEvent[]>} - Array of status events with their corresponding statuses.
1724
+ *
1725
+ * **Code example**
1726
+ *
1727
+ * ```ts
1728
+ * import { ChainId, EscrowUtils, EscrowStatus } from '@human-protocol/sdk';
1729
+ *
1730
+ * (async () => {
1731
+ * const fromDate = new Date('2023-01-01');
1732
+ * const toDate = new Date('2023-12-31');
1733
+ * const statusEvents = await EscrowUtils.getStatusEvents(
1734
+ * [ChainId.POLYGON, ChainId.MAINNET],
1735
+ * [EscrowStatus.Pending, EscrowStatus.Complete],
1736
+ * fromDate,
1737
+ * toDate
1738
+ * );
1739
+ * console.log(statusEvents);
1740
+ * })();
1741
+ * ```
1742
+ */
1743
+
1744
+ public static async getStatusEvents(
1745
+ chainId: ChainId,
1746
+ statuses?: EscrowStatus[],
1747
+ from?: Date,
1748
+ to?: Date,
1749
+ launcher?: string,
1750
+ first?: number,
1751
+ skip?: number,
1752
+ orderDirection?: OrderDirection
1753
+ ): Promise<StatusEvent[]> {
1754
+ if (launcher && !ethers.isAddress(launcher)) {
1755
+ throw ErrorInvalidAddress;
1657
1756
  }
1757
+
1758
+ first = first !== undefined ? Math.min(first, 1000) : 10;
1759
+ skip = skip || 0;
1760
+ orderDirection = orderDirection || OrderDirection.DESC;
1761
+
1762
+ // If statuses are not provided, use all statuses except Launched
1763
+ const effectiveStatuses = statuses ?? [
1764
+ EscrowStatus.Launched,
1765
+ EscrowStatus.Pending,
1766
+ EscrowStatus.Partial,
1767
+ EscrowStatus.Paid,
1768
+ EscrowStatus.Complete,
1769
+ EscrowStatus.Cancelled,
1770
+ ];
1771
+
1772
+ const networkData = NETWORKS[chainId];
1773
+ if (!networkData) {
1774
+ throw ErrorUnsupportedChainID;
1775
+ }
1776
+
1777
+ const statusNames = effectiveStatuses.map((status) => EscrowStatus[status]);
1778
+
1779
+ const data = await gqlFetch<{
1780
+ escrowStatusEvents: StatusEvent[];
1781
+ }>(
1782
+ getSubgraphUrl(networkData),
1783
+ GET_STATUS_UPDATES_QUERY(from, to, launcher),
1784
+ {
1785
+ status: statusNames,
1786
+ from: from ? Math.floor(from.getTime() / 1000) : undefined,
1787
+ to: to ? Math.floor(to.getTime() / 1000) : undefined,
1788
+ launcher: launcher || undefined,
1789
+ orderDirection: orderDirection,
1790
+ first: first,
1791
+ skip: skip,
1792
+ }
1793
+ );
1794
+
1795
+ if (!data || !data['escrowStatusEvents']) {
1796
+ return [];
1797
+ }
1798
+
1799
+ const statusEvents = data['escrowStatusEvents'] as StatusEvent[];
1800
+
1801
+ const eventsWithChainId = statusEvents.map((event) => ({
1802
+ ...event,
1803
+ chainId,
1804
+ }));
1805
+
1806
+ return eventsWithChainId;
1658
1807
  }
1659
1808
  }
@@ -51,14 +51,14 @@ export const GET_ESCROWS_QUERY = (filter: IEscrowsFilter) => {
51
51
 
52
52
  const WHERE_CLAUSE = `
53
53
  where: {
54
- ${launcher ? `launcher: $launcher` : ''}
55
- ${jobRequesterId ? `jobRequesterId: $jobRequesterId` : ''}
56
- ${reputationOracle ? `reputationOracle: $reputationOracle` : ''}
57
- ${recordingOracle ? `recordingOracle: $recordingOracle` : ''}
58
- ${exchangeOracle ? `exchangeOracle: $exchangeOracle` : ''}
59
- ${status !== undefined ? `status: $status` : ''}
60
- ${from ? `createdAt_gte: $from` : ''}
61
- ${to ? `createdAt_lte: $to` : ''}
54
+ ${launcher ? `launcher: $launcher,` : ''}
55
+ ${jobRequesterId ? `jobRequesterId: $jobRequesterId,` : ''}
56
+ ${reputationOracle ? `reputationOracle: $reputationOracle,` : ''}
57
+ ${recordingOracle ? `recordingOracle: $recordingOracle,` : ''}
58
+ ${exchangeOracle ? `exchangeOracle: $exchangeOracle,` : ''}
59
+ ${status !== undefined ? `status: $status,` : ''}
60
+ ${from ? `createdAt_gte: $from,` : ''}
61
+ ${to ? `createdAt_lte: $to,` : ''}
62
62
  }
63
63
  `;
64
64
 
@@ -72,9 +72,16 @@ export const GET_ESCROWS_QUERY = (filter: IEscrowsFilter) => {
72
72
  $status: String
73
73
  $from: Int
74
74
  $to: Int
75
+ $orderDirection: String
76
+ $first: Int
77
+ $skip: Int
75
78
  ) {
76
79
  escrows(
77
80
  ${WHERE_CLAUSE}
81
+ orderBy: createdAt,
82
+ orderDirection: $orderDirection,
83
+ first: $first,
84
+ skip: $skip
78
85
  ) {
79
86
  ...EscrowFields
80
87
  }
@@ -82,3 +89,41 @@ export const GET_ESCROWS_QUERY = (filter: IEscrowsFilter) => {
82
89
  ${ESCROW_FRAGMENT}
83
90
  `;
84
91
  };
92
+
93
+ export const GET_STATUS_UPDATES_QUERY = (
94
+ from?: Date,
95
+ to?: Date,
96
+ launcher?: string
97
+ ) => {
98
+ const WHERE_CLAUSE = `
99
+ where: {
100
+ status_in: $status
101
+ ${from ? `timestamp_gte: $from` : ''}
102
+ ${to ? `timestamp_lte: $to` : ''}
103
+ ${launcher ? `launcher: $launcher` : ''}
104
+ }
105
+ `;
106
+ return gql`
107
+ query getStatus(
108
+ $status: [String!]!
109
+ $from: Int
110
+ $to: Int
111
+ $launcher: String
112
+ $orderDirection: String
113
+ $first: Int
114
+ $skip: Int
115
+ ) {
116
+ escrowStatusEvents(
117
+ ${WHERE_CLAUSE}
118
+ orderBy: timestamp,
119
+ orderDirection: $orderDirection,
120
+ first: $first,
121
+ skip: $skip
122
+ ) {
123
+ escrowAddress,
124
+ timestamp,
125
+ status,
126
+ }
127
+ }
128
+ `;
129
+ };