@human-protocol/sdk 4.0.3 → 4.1.2

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 (50) hide show
  1. package/dist/constants.js +6 -6
  2. package/dist/escrow.d.ts +47 -27
  3. package/dist/escrow.d.ts.map +1 -1
  4. package/dist/escrow.js +73 -31
  5. package/dist/graphql/queries/escrow.d.ts.map +1 -1
  6. package/dist/graphql/queries/hmtoken.d.ts.map +1 -1
  7. package/dist/graphql/queries/operator.d.ts.map +1 -1
  8. package/dist/graphql/queries/payout.d.ts +1 -1
  9. package/dist/graphql/queries/payout.d.ts.map +1 -1
  10. package/dist/graphql/queries/payout.js +6 -5
  11. package/dist/graphql/queries/statistics.d.ts.map +1 -1
  12. package/dist/graphql/queries/statistics.js +1 -1
  13. package/dist/graphql/queries/transaction.d.ts.map +1 -1
  14. package/dist/graphql/queries/transaction.js +7 -1
  15. package/dist/graphql/queries/worker.d.ts +4 -0
  16. package/dist/graphql/queries/worker.d.ts.map +1 -0
  17. package/dist/graphql/queries/worker.js +44 -0
  18. package/dist/graphql/types.d.ts +1 -8
  19. package/dist/graphql/types.d.ts.map +1 -1
  20. package/dist/index.d.ts +2 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +3 -1
  23. package/dist/interfaces.d.ts +44 -1
  24. package/dist/interfaces.d.ts.map +1 -1
  25. package/dist/statistics.js +2 -2
  26. package/dist/transaction.d.ts +25 -2
  27. package/dist/transaction.d.ts.map +1 -1
  28. package/dist/transaction.js +28 -2
  29. package/dist/types.d.ts +25 -0
  30. package/dist/types.d.ts.map +1 -1
  31. package/dist/utils.d.ts.map +1 -1
  32. package/dist/utils.js +2 -1
  33. package/dist/worker.d.ts +63 -0
  34. package/dist/worker.d.ts.map +1 -0
  35. package/dist/worker.js +109 -0
  36. package/package.json +13 -9
  37. package/src/constants.ts +6 -6
  38. package/src/escrow.ts +103 -45
  39. package/src/graphql/queries/payout.ts +7 -6
  40. package/src/graphql/queries/statistics.ts +1 -1
  41. package/src/graphql/queries/transaction.ts +17 -2
  42. package/src/graphql/queries/worker.ts +41 -0
  43. package/src/graphql/types.ts +1 -9
  44. package/src/index.ts +2 -0
  45. package/src/interfaces.ts +48 -1
  46. package/src/statistics.ts +2 -2
  47. package/src/transaction.ts +28 -2
  48. package/src/types.ts +26 -0
  49. package/src/utils.ts +2 -1
  50. package/src/worker.ts +120 -0
package/src/worker.ts ADDED
@@ -0,0 +1,120 @@
1
+ import gqlFetch from 'graphql-request';
2
+ import { NETWORKS } from './constants';
3
+ import { ChainId, OrderDirection } from './enums';
4
+ import { ErrorInvalidAddress, ErrorUnsupportedChainID } from './error';
5
+ import { GET_WORKER_QUERY, GET_WORKERS_QUERY } from './graphql/queries/worker';
6
+ import { IWorker, IWorkersFilter } from './interfaces';
7
+ import { getSubgraphUrl } from './utils';
8
+ import { ethers } from 'ethers';
9
+
10
+ export class WorkerUtils {
11
+ /**
12
+ * This function returns the worker data for the given address.
13
+ *
14
+ * @param {ChainId} chainId The chain ID.
15
+ * @param {string} address The worker address.
16
+ * @returns {Promise<IWorker>} Returns the worker details.
17
+ *
18
+ * **Code example**
19
+ *
20
+ * ```ts
21
+ * import { WorkerUtils, ChainId } from '@human-protocol/sdk';
22
+ *
23
+ * const worker = await WorkerUtils.getWorker(ChainId.POLYGON, '0x1234567890abcdef1234567890abcdef12345678');
24
+ * ```
25
+ */
26
+ public static async getWorker(
27
+ chainId: ChainId,
28
+ address: string
29
+ ): Promise<IWorker | null> {
30
+ const networkData = NETWORKS[chainId];
31
+
32
+ if (!networkData) {
33
+ throw ErrorUnsupportedChainID;
34
+ }
35
+ if (!ethers.isAddress(address)) {
36
+ throw ErrorInvalidAddress;
37
+ }
38
+
39
+ const { worker } = await gqlFetch<{
40
+ worker: IWorker;
41
+ }>(getSubgraphUrl(networkData), GET_WORKER_QUERY, {
42
+ address: address.toLowerCase(),
43
+ });
44
+
45
+ return worker || null;
46
+ }
47
+
48
+ /**
49
+ * This function returns all worker details based on the provided filter.
50
+ *
51
+ * **Input parameters**
52
+ *
53
+ * ```ts
54
+ * interface IWorkersFilter {
55
+ * chainId: ChainId; // List of chain IDs to query.
56
+ * address?: string; // (Optional) The worker address to filter by.
57
+ * orderBy?: string; // (Optional) The field to order by. Default is 'payoutCount'.
58
+ * orderDirection?: OrderDirection; // (Optional) The direction of the order. Default is 'DESC'.
59
+ * first?: number; // (Optional) Number of workers per page. Default is 10.
60
+ * skip?: number; // (Optional) Number of workers to skip. Default is 0.
61
+ * }
62
+ * ```
63
+ *
64
+ * ```ts
65
+ * type IWorker = {
66
+ * id: string;
67
+ * address: string;
68
+ * totalHMTAmountReceived: string;
69
+ * payoutCount: number;
70
+ * };
71
+ * ```
72
+ *
73
+ * @param {IWorkersFilter} filter Filter for the workers.
74
+ * @returns {Promise<IWorker[]>} Returns an array with all the worker details.
75
+ *
76
+ * **Code example**
77
+ *
78
+ * ```ts
79
+ * import { WorkerUtils, ChainId } from '@human-protocol/sdk';
80
+ *
81
+ * const filter: IWorkersFilter = {
82
+ * chainId: ChainId.POLYGON,
83
+ * first: 10,
84
+ * skip: 0,
85
+ * };
86
+ * const workers = await WorkerUtils.getWorkers(filter);
87
+ * ```
88
+ */
89
+ public static async getWorkers(filter: IWorkersFilter): Promise<IWorker[]> {
90
+ const first =
91
+ filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
92
+ const skip = filter.skip || 0;
93
+ const orderBy = filter.orderBy || 'payoutCount';
94
+ const orderDirection = filter.orderDirection || OrderDirection.DESC;
95
+
96
+ const networkData = NETWORKS[filter.chainId];
97
+ if (!networkData) {
98
+ throw ErrorUnsupportedChainID;
99
+ }
100
+ if (filter.address && !ethers.isAddress(filter.address)) {
101
+ throw ErrorInvalidAddress;
102
+ }
103
+
104
+ const { workers } = await gqlFetch<{
105
+ workers: IWorker[];
106
+ }>(getSubgraphUrl(networkData), GET_WORKERS_QUERY(filter), {
107
+ address: filter?.address?.toLowerCase(),
108
+ first: first,
109
+ skip: skip,
110
+ orderBy: orderBy,
111
+ orderDirection: orderDirection,
112
+ });
113
+
114
+ if (!workers) {
115
+ return [];
116
+ }
117
+
118
+ return workers;
119
+ }
120
+ }