@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
@@ -0,0 +1,172 @@
1
+ import { ChainId } from '../enums';
2
+ import { ICancellationRefund, ICancellationRefundFilter, IEscrow, IEscrowsFilter, IPayout, IPayoutFilter, IStatusEvent, IStatusEventFilter, SubgraphOptions } from '../interfaces';
3
+ /**
4
+ * Utility helpers for escrow-related queries.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { ChainId, EscrowUtils } from '@human-protocol/sdk';
9
+ *
10
+ * const escrows = await EscrowUtils.getEscrows({
11
+ * chainId: ChainId.POLYGON_AMOY
12
+ * });
13
+ * console.log('Escrows:', escrows);
14
+ * ```
15
+ */
16
+ export declare class EscrowUtils {
17
+ /**
18
+ * This function returns an array of escrows based on the specified filter parameters.
19
+ *
20
+ * @param filter - Filter parameters.
21
+ * @param options - Optional configuration for subgraph requests.
22
+ * @returns List of escrows that match the filter.
23
+ * @throws ErrorInvalidAddress If any filter address is invalid
24
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * import { ChainId, EscrowStatus } from '@human-protocol/sdk';
29
+ *
30
+ * const filters = {
31
+ * status: EscrowStatus.Pending,
32
+ * from: new Date(2023, 4, 8),
33
+ * to: new Date(2023, 5, 8),
34
+ * chainId: ChainId.POLYGON_AMOY
35
+ * };
36
+ * const escrows = await EscrowUtils.getEscrows(filters);
37
+ * console.log('Found escrows:', escrows.length);
38
+ * ```
39
+ */
40
+ static getEscrows(filter: IEscrowsFilter, options?: SubgraphOptions): Promise<IEscrow[]>;
41
+ /**
42
+ * This function returns the escrow data for a given address.
43
+ *
44
+ * > This uses Subgraph
45
+ *
46
+ * @param chainId - Network in which the escrow has been deployed
47
+ * @param escrowAddress - Address of the escrow
48
+ * @param options - Optional configuration for subgraph requests.
49
+ * @returns Escrow data or null if not found.
50
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
51
+ * @throws ErrorInvalidAddress If the escrow address is invalid
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * import { ChainId } from '@human-protocol/sdk';
56
+ *
57
+ * const escrow = await EscrowUtils.getEscrow(
58
+ * ChainId.POLYGON_AMOY,
59
+ * "0x1234567890123456789012345678901234567890"
60
+ * );
61
+ * if (escrow) {
62
+ * console.log('Escrow status:', escrow.status);
63
+ * }
64
+ * ```
65
+ */
66
+ static getEscrow(chainId: ChainId, escrowAddress: string, options?: SubgraphOptions): Promise<IEscrow | null>;
67
+ /**
68
+ * This function returns the status events for a given set of networks within an optional date range.
69
+ *
70
+ * > This uses Subgraph
71
+ *
72
+ * @param filter - Filter parameters.
73
+ * @param options - Optional configuration for subgraph requests.
74
+ * @returns Array of status events with their corresponding statuses.
75
+ * @throws ErrorInvalidAddress If the launcher address is invalid
76
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * import { ChainId, EscrowStatus } from '@human-protocol/sdk';
81
+ *
82
+ * const fromDate = new Date('2023-01-01');
83
+ * const toDate = new Date('2023-12-31');
84
+ * const statusEvents = await EscrowUtils.getStatusEvents({
85
+ * chainId: ChainId.POLYGON,
86
+ * statuses: [EscrowStatus.Pending, EscrowStatus.Complete],
87
+ * from: fromDate,
88
+ * to: toDate
89
+ * });
90
+ * console.log('Status events:', statusEvents.length);
91
+ * ```
92
+ */
93
+ static getStatusEvents(filter: IStatusEventFilter, options?: SubgraphOptions): Promise<IStatusEvent[]>;
94
+ /**
95
+ * This function returns the payouts for a given set of networks.
96
+ *
97
+ * > This uses Subgraph
98
+ *
99
+ * @param filter - Filter parameters.
100
+ * @param options - Optional configuration for subgraph requests.
101
+ * @returns List of payouts matching the filters.
102
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
103
+ * @throws ErrorInvalidAddress If any filter address is invalid
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * import { ChainId } from '@human-protocol/sdk';
108
+ *
109
+ * const payouts = await EscrowUtils.getPayouts({
110
+ * chainId: ChainId.POLYGON,
111
+ * escrowAddress: '0x1234567890123456789012345678901234567890',
112
+ * recipient: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef',
113
+ * from: new Date('2023-01-01'),
114
+ * to: new Date('2023-12-31')
115
+ * });
116
+ * console.log('Payouts:', payouts.length);
117
+ * ```
118
+ */
119
+ static getPayouts(filter: IPayoutFilter, options?: SubgraphOptions): Promise<IPayout[]>;
120
+ /**
121
+ * This function returns the cancellation refunds for a given set of networks.
122
+ *
123
+ * > This uses Subgraph
124
+ *
125
+ * @param filter - Filter parameters.
126
+ * @param options - Optional configuration for subgraph requests.
127
+ * @returns List of cancellation refunds matching the filters.
128
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
129
+ * @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
130
+ * @throws ErrorInvalidAddress If the receiver address is invalid
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * import { ChainId } from '@human-protocol/sdk';
135
+ *
136
+ * const cancellationRefunds = await EscrowUtils.getCancellationRefunds({
137
+ * chainId: ChainId.POLYGON_AMOY,
138
+ * escrowAddress: '0x1234567890123456789012345678901234567890',
139
+ * });
140
+ * console.log('Cancellation refunds:', cancellationRefunds.length);
141
+ * ```
142
+ */
143
+ static getCancellationRefunds(filter: ICancellationRefundFilter, options?: SubgraphOptions): Promise<ICancellationRefund[]>;
144
+ /**
145
+ * This function returns the cancellation refund for a given escrow address.
146
+ *
147
+ * > This uses Subgraph
148
+ *
149
+ * @param chainId - Network in which the escrow has been deployed
150
+ * @param escrowAddress - Address of the escrow
151
+ * @param options - Optional configuration for subgraph requests.
152
+ * @returns Cancellation refund data or null if not found.
153
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
154
+ * @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * import { ChainId } from '@human-protocol/sdk';
159
+ *
160
+ *
161
+ * const cancellationRefund = await EscrowUtils.getCancellationRefund(
162
+ * ChainId.POLYGON_AMOY,
163
+ * "0x1234567890123456789012345678901234567890"
164
+ * );
165
+ * if (cancellationRefund) {
166
+ * console.log('Refund amount:', cancellationRefund.amount);
167
+ * }
168
+ * ```
169
+ */
170
+ static getCancellationRefund(chainId: ChainId, escrowAddress: string, options?: SubgraphOptions): Promise<ICancellationRefund | null>;
171
+ }
172
+ //# sourceMappingURL=escrow_utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escrow_utils.d.ts","sourceRoot":"","sources":["../../src/escrow/escrow_utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAkB,MAAM,UAAU,CAAC;AAkBnD,OAAO,EACL,mBAAmB,EACnB,yBAAyB,EACzB,OAAO,EACP,cAAc,EACd,OAAO,EACP,aAAa,EACb,YAAY,EACZ,kBAAkB,EAClB,eAAe,EAChB,MAAM,eAAe,CAAC;AAGvB;;;;;;;;;;;;GAYG;AACH,qBAAa,WAAW;IACtB;;;;;;;;;;;;;;;;;;;;;;OAsBG;WACiB,UAAU,CAC5B,MAAM,EAAE,cAAc,EACtB,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,OAAO,EAAE,CAAC;IAsDrB;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;WACiB,SAAS,CAC3B,OAAO,EAAE,OAAO,EAChB,aAAa,EAAE,MAAM,EACrB,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAsB1B;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;WACiB,eAAe,CACjC,MAAM,EAAE,kBAAkB,EAC1B,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,YAAY,EAAE,CAAC;IA8D1B;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;WACiB,UAAU,CAC5B,MAAM,EAAE,aAAa,EACrB,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,OAAO,EAAE,CAAC;IA4CrB;;;;;;;;;;;;;;;;;;;;;;OAsBG;WACiB,sBAAsB,CACxC,MAAM,EAAE,yBAAyB,EACjC,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,mBAAmB,EAAE,CAAC;IA+CjC;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;WACiB,qBAAqB,CACvC,OAAO,EAAE,OAAO,EAChB,aAAa,EAAE,MAAM,EACrB,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC;CA+BvC"}
@@ -0,0 +1,388 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EscrowUtils = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const constants_1 = require("../constants");
6
+ const enums_1 = require("../enums");
7
+ const error_1 = require("../error");
8
+ const graphql_1 = require("../graphql");
9
+ const types_1 = require("../types");
10
+ const utils_1 = require("../utils");
11
+ /**
12
+ * Utility helpers for escrow-related queries.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { ChainId, EscrowUtils } from '@human-protocol/sdk';
17
+ *
18
+ * const escrows = await EscrowUtils.getEscrows({
19
+ * chainId: ChainId.POLYGON_AMOY
20
+ * });
21
+ * console.log('Escrows:', escrows);
22
+ * ```
23
+ */
24
+ class EscrowUtils {
25
+ /**
26
+ * This function returns an array of escrows based on the specified filter parameters.
27
+ *
28
+ * @param filter - Filter parameters.
29
+ * @param options - Optional configuration for subgraph requests.
30
+ * @returns List of escrows that match the filter.
31
+ * @throws ErrorInvalidAddress If any filter address is invalid
32
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * import { ChainId, EscrowStatus } from '@human-protocol/sdk';
37
+ *
38
+ * const filters = {
39
+ * status: EscrowStatus.Pending,
40
+ * from: new Date(2023, 4, 8),
41
+ * to: new Date(2023, 5, 8),
42
+ * chainId: ChainId.POLYGON_AMOY
43
+ * };
44
+ * const escrows = await EscrowUtils.getEscrows(filters);
45
+ * console.log('Found escrows:', escrows.length);
46
+ * ```
47
+ */
48
+ static async getEscrows(filter, options) {
49
+ if (filter.launcher && !ethers_1.ethers.isAddress(filter.launcher)) {
50
+ throw error_1.ErrorInvalidAddress;
51
+ }
52
+ if (filter.recordingOracle && !ethers_1.ethers.isAddress(filter.recordingOracle)) {
53
+ throw error_1.ErrorInvalidAddress;
54
+ }
55
+ if (filter.reputationOracle && !ethers_1.ethers.isAddress(filter.reputationOracle)) {
56
+ throw error_1.ErrorInvalidAddress;
57
+ }
58
+ if (filter.exchangeOracle && !ethers_1.ethers.isAddress(filter.exchangeOracle)) {
59
+ throw error_1.ErrorInvalidAddress;
60
+ }
61
+ const first = filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
62
+ const skip = filter.skip || 0;
63
+ const orderDirection = filter.orderDirection || enums_1.OrderDirection.DESC;
64
+ const networkData = constants_1.NETWORKS[filter.chainId];
65
+ if (!networkData) {
66
+ throw error_1.ErrorUnsupportedChainID;
67
+ }
68
+ let statuses;
69
+ if (filter.status !== undefined) {
70
+ statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
71
+ statuses = statuses.map((status) => types_1.EscrowStatus[status]);
72
+ }
73
+ const { escrows } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_ESCROWS_QUERY)(filter), {
74
+ ...filter,
75
+ launcher: filter.launcher?.toLowerCase(),
76
+ reputationOracle: filter.reputationOracle?.toLowerCase(),
77
+ recordingOracle: filter.recordingOracle?.toLowerCase(),
78
+ exchangeOracle: filter.exchangeOracle?.toLowerCase(),
79
+ status: statuses,
80
+ from: filter.from ? (0, utils_1.getUnixTimestamp)(filter.from) : undefined,
81
+ to: filter.to ? (0, utils_1.getUnixTimestamp)(filter.to) : undefined,
82
+ orderDirection: orderDirection,
83
+ first: first,
84
+ skip: skip,
85
+ }, options);
86
+ return (escrows || []).map((e) => mapEscrow(e, networkData.chainId));
87
+ }
88
+ /**
89
+ * This function returns the escrow data for a given address.
90
+ *
91
+ * > This uses Subgraph
92
+ *
93
+ * @param chainId - Network in which the escrow has been deployed
94
+ * @param escrowAddress - Address of the escrow
95
+ * @param options - Optional configuration for subgraph requests.
96
+ * @returns Escrow data or null if not found.
97
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
98
+ * @throws ErrorInvalidAddress If the escrow address is invalid
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * import { ChainId } from '@human-protocol/sdk';
103
+ *
104
+ * const escrow = await EscrowUtils.getEscrow(
105
+ * ChainId.POLYGON_AMOY,
106
+ * "0x1234567890123456789012345678901234567890"
107
+ * );
108
+ * if (escrow) {
109
+ * console.log('Escrow status:', escrow.status);
110
+ * }
111
+ * ```
112
+ */
113
+ static async getEscrow(chainId, escrowAddress, options) {
114
+ const networkData = constants_1.NETWORKS[chainId];
115
+ if (!networkData) {
116
+ throw error_1.ErrorUnsupportedChainID;
117
+ }
118
+ if (escrowAddress && !ethers_1.ethers.isAddress(escrowAddress)) {
119
+ throw error_1.ErrorInvalidAddress;
120
+ }
121
+ const { escrow } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_ESCROW_BY_ADDRESS_QUERY)(), { escrowAddress: escrowAddress.toLowerCase() }, options);
122
+ if (!escrow)
123
+ return null;
124
+ return mapEscrow(escrow, networkData.chainId);
125
+ }
126
+ /**
127
+ * This function returns the status events for a given set of networks within an optional date range.
128
+ *
129
+ * > This uses Subgraph
130
+ *
131
+ * @param filter - Filter parameters.
132
+ * @param options - Optional configuration for subgraph requests.
133
+ * @returns Array of status events with their corresponding statuses.
134
+ * @throws ErrorInvalidAddress If the launcher address is invalid
135
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
136
+ *
137
+ * @example
138
+ * ```ts
139
+ * import { ChainId, EscrowStatus } from '@human-protocol/sdk';
140
+ *
141
+ * const fromDate = new Date('2023-01-01');
142
+ * const toDate = new Date('2023-12-31');
143
+ * const statusEvents = await EscrowUtils.getStatusEvents({
144
+ * chainId: ChainId.POLYGON,
145
+ * statuses: [EscrowStatus.Pending, EscrowStatus.Complete],
146
+ * from: fromDate,
147
+ * to: toDate
148
+ * });
149
+ * console.log('Status events:', statusEvents.length);
150
+ * ```
151
+ */
152
+ static async getStatusEvents(filter, options) {
153
+ const { chainId, statuses, from, to, launcher, first = 10, skip = 0, orderDirection = enums_1.OrderDirection.DESC, } = filter;
154
+ if (launcher && !ethers_1.ethers.isAddress(launcher)) {
155
+ throw error_1.ErrorInvalidAddress;
156
+ }
157
+ const networkData = constants_1.NETWORKS[chainId];
158
+ if (!networkData) {
159
+ throw error_1.ErrorUnsupportedChainID;
160
+ }
161
+ // If statuses are not provided, use all statuses except Launched
162
+ const effectiveStatuses = statuses ?? [
163
+ types_1.EscrowStatus.Launched,
164
+ types_1.EscrowStatus.Pending,
165
+ types_1.EscrowStatus.Partial,
166
+ types_1.EscrowStatus.Paid,
167
+ types_1.EscrowStatus.Complete,
168
+ types_1.EscrowStatus.Cancelled,
169
+ ];
170
+ const statusNames = effectiveStatuses.map((status) => types_1.EscrowStatus[status]);
171
+ const data = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_STATUS_UPDATES_QUERY)(from, to, launcher), {
172
+ status: statusNames,
173
+ from: from ? (0, utils_1.getUnixTimestamp)(from) : undefined,
174
+ to: to ? (0, utils_1.getUnixTimestamp)(to) : undefined,
175
+ launcher: launcher || undefined,
176
+ orderDirection,
177
+ first: Math.min(first, 1000),
178
+ skip,
179
+ }, options);
180
+ if (!data || !data['escrowStatusEvents']) {
181
+ return [];
182
+ }
183
+ return data['escrowStatusEvents'].map((event) => ({
184
+ timestamp: Number(event.timestamp) * 1000,
185
+ escrowAddress: event.escrowAddress,
186
+ status: types_1.EscrowStatus[event.status],
187
+ chainId,
188
+ }));
189
+ }
190
+ /**
191
+ * This function returns the payouts for a given set of networks.
192
+ *
193
+ * > This uses Subgraph
194
+ *
195
+ * @param filter - Filter parameters.
196
+ * @param options - Optional configuration for subgraph requests.
197
+ * @returns List of payouts matching the filters.
198
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
199
+ * @throws ErrorInvalidAddress If any filter address is invalid
200
+ *
201
+ * @example
202
+ * ```ts
203
+ * import { ChainId } from '@human-protocol/sdk';
204
+ *
205
+ * const payouts = await EscrowUtils.getPayouts({
206
+ * chainId: ChainId.POLYGON,
207
+ * escrowAddress: '0x1234567890123456789012345678901234567890',
208
+ * recipient: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef',
209
+ * from: new Date('2023-01-01'),
210
+ * to: new Date('2023-12-31')
211
+ * });
212
+ * console.log('Payouts:', payouts.length);
213
+ * ```
214
+ */
215
+ static async getPayouts(filter, options) {
216
+ const networkData = constants_1.NETWORKS[filter.chainId];
217
+ if (!networkData) {
218
+ throw error_1.ErrorUnsupportedChainID;
219
+ }
220
+ if (filter.escrowAddress && !ethers_1.ethers.isAddress(filter.escrowAddress)) {
221
+ throw error_1.ErrorInvalidAddress;
222
+ }
223
+ if (filter.recipient && !ethers_1.ethers.isAddress(filter.recipient)) {
224
+ throw error_1.ErrorInvalidAddress;
225
+ }
226
+ const first = filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
227
+ const skip = filter.skip || 0;
228
+ const orderDirection = filter.orderDirection || enums_1.OrderDirection.DESC;
229
+ const { payouts } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_PAYOUTS_QUERY)(filter), {
230
+ escrowAddress: filter.escrowAddress?.toLowerCase(),
231
+ recipient: filter.recipient?.toLowerCase(),
232
+ from: filter.from ? (0, utils_1.getUnixTimestamp)(filter.from) : undefined,
233
+ to: filter.to ? (0, utils_1.getUnixTimestamp)(filter.to) : undefined,
234
+ first: Math.min(first, 1000),
235
+ skip,
236
+ orderDirection,
237
+ }, options);
238
+ if (!payouts) {
239
+ return [];
240
+ }
241
+ return payouts.map((payout) => ({
242
+ id: payout.id,
243
+ escrowAddress: payout.escrowAddress,
244
+ recipient: payout.recipient,
245
+ amount: BigInt(payout.amount),
246
+ createdAt: Number(payout.createdAt) * 1000,
247
+ }));
248
+ }
249
+ /**
250
+ * This function returns the cancellation refunds for a given set of networks.
251
+ *
252
+ * > This uses Subgraph
253
+ *
254
+ * @param filter - Filter parameters.
255
+ * @param options - Optional configuration for subgraph requests.
256
+ * @returns List of cancellation refunds matching the filters.
257
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
258
+ * @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
259
+ * @throws ErrorInvalidAddress If the receiver address is invalid
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * import { ChainId } from '@human-protocol/sdk';
264
+ *
265
+ * const cancellationRefunds = await EscrowUtils.getCancellationRefunds({
266
+ * chainId: ChainId.POLYGON_AMOY,
267
+ * escrowAddress: '0x1234567890123456789012345678901234567890',
268
+ * });
269
+ * console.log('Cancellation refunds:', cancellationRefunds.length);
270
+ * ```
271
+ */
272
+ static async getCancellationRefunds(filter, options) {
273
+ const networkData = constants_1.NETWORKS[filter.chainId];
274
+ if (!networkData)
275
+ throw error_1.ErrorUnsupportedChainID;
276
+ if (filter.escrowAddress && !ethers_1.ethers.isAddress(filter.escrowAddress)) {
277
+ throw error_1.ErrorInvalidEscrowAddressProvided;
278
+ }
279
+ if (filter.receiver && !ethers_1.ethers.isAddress(filter.receiver)) {
280
+ throw error_1.ErrorInvalidAddress;
281
+ }
282
+ const first = filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
283
+ const skip = filter.skip || 0;
284
+ const orderDirection = filter.orderDirection || enums_1.OrderDirection.DESC;
285
+ const { cancellationRefundEvents } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_CANCELLATION_REFUNDS_QUERY)(filter), {
286
+ escrowAddress: filter.escrowAddress?.toLowerCase(),
287
+ receiver: filter.receiver?.toLowerCase(),
288
+ from: filter.from ? (0, utils_1.getUnixTimestamp)(filter.from) : undefined,
289
+ to: filter.to ? (0, utils_1.getUnixTimestamp)(filter.to) : undefined,
290
+ first,
291
+ skip,
292
+ orderDirection,
293
+ }, options);
294
+ if (!cancellationRefundEvents || cancellationRefundEvents.length === 0) {
295
+ return [];
296
+ }
297
+ return cancellationRefundEvents.map((event) => ({
298
+ id: event.id,
299
+ escrowAddress: event.escrowAddress,
300
+ receiver: event.receiver,
301
+ amount: BigInt(event.amount),
302
+ block: Number(event.block),
303
+ timestamp: Number(event.timestamp) * 1000,
304
+ txHash: event.txHash,
305
+ }));
306
+ }
307
+ /**
308
+ * This function returns the cancellation refund for a given escrow address.
309
+ *
310
+ * > This uses Subgraph
311
+ *
312
+ * @param chainId - Network in which the escrow has been deployed
313
+ * @param escrowAddress - Address of the escrow
314
+ * @param options - Optional configuration for subgraph requests.
315
+ * @returns Cancellation refund data or null if not found.
316
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
317
+ * @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
318
+ *
319
+ * @example
320
+ * ```ts
321
+ * import { ChainId } from '@human-protocol/sdk';
322
+ *
323
+ *
324
+ * const cancellationRefund = await EscrowUtils.getCancellationRefund(
325
+ * ChainId.POLYGON_AMOY,
326
+ * "0x1234567890123456789012345678901234567890"
327
+ * );
328
+ * if (cancellationRefund) {
329
+ * console.log('Refund amount:', cancellationRefund.amount);
330
+ * }
331
+ * ```
332
+ */
333
+ static async getCancellationRefund(chainId, escrowAddress, options) {
334
+ const networkData = constants_1.NETWORKS[chainId];
335
+ if (!networkData)
336
+ throw error_1.ErrorUnsupportedChainID;
337
+ if (!ethers_1.ethers.isAddress(escrowAddress)) {
338
+ throw error_1.ErrorInvalidEscrowAddressProvided;
339
+ }
340
+ const { cancellationRefundEvents } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_CANCELLATION_REFUND_BY_ADDRESS_QUERY)(), { escrowAddress: escrowAddress.toLowerCase() }, options);
341
+ if (!cancellationRefundEvents || cancellationRefundEvents.length === 0) {
342
+ return null;
343
+ }
344
+ return {
345
+ id: cancellationRefundEvents[0].id,
346
+ escrowAddress: cancellationRefundEvents[0].escrowAddress,
347
+ receiver: cancellationRefundEvents[0].receiver,
348
+ amount: BigInt(cancellationRefundEvents[0].amount),
349
+ block: Number(cancellationRefundEvents[0].block),
350
+ timestamp: Number(cancellationRefundEvents[0].timestamp) * 1000,
351
+ txHash: cancellationRefundEvents[0].txHash,
352
+ };
353
+ }
354
+ }
355
+ exports.EscrowUtils = EscrowUtils;
356
+ function mapEscrow(e, chainId) {
357
+ return {
358
+ id: e.id,
359
+ address: e.address,
360
+ amountPaid: BigInt(e.amountPaid),
361
+ balance: BigInt(e.balance),
362
+ count: Number(e.count),
363
+ factoryAddress: e.factoryAddress,
364
+ finalResultsUrl: e.finalResultsUrl,
365
+ finalResultsHash: e.finalResultsHash,
366
+ intermediateResultsUrl: e.intermediateResultsUrl,
367
+ intermediateResultsHash: e.intermediateResultsHash,
368
+ launcher: e.launcher,
369
+ jobRequesterId: e.jobRequesterId,
370
+ manifestHash: e.manifestHash,
371
+ manifest: e.manifest,
372
+ recordingOracle: e.recordingOracle,
373
+ reputationOracle: e.reputationOracle,
374
+ exchangeOracle: e.exchangeOracle,
375
+ recordingOracleFee: e.recordingOracleFee
376
+ ? Number(e.recordingOracleFee)
377
+ : null,
378
+ reputationOracleFee: e.reputationOracleFee
379
+ ? Number(e.reputationOracleFee)
380
+ : null,
381
+ exchangeOracleFee: e.exchangeOracleFee ? Number(e.exchangeOracleFee) : null,
382
+ status: e.status,
383
+ token: e.token,
384
+ totalFundedAmount: BigInt(e.totalFundedAmount),
385
+ createdAt: Number(e.createdAt) * 1000,
386
+ chainId: Number(chainId),
387
+ };
388
+ }
@@ -0,0 +1,3 @@
1
+ export { EscrowClient } from './escrow_client';
2
+ export { EscrowUtils } from './escrow_utils';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/escrow/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EscrowUtils = exports.EscrowClient = void 0;
4
+ var escrow_client_1 = require("./escrow_client");
5
+ Object.defineProperty(exports, "EscrowClient", { enumerable: true, get: function () { return escrow_client_1.EscrowClient; } });
6
+ var escrow_utils_1 = require("./escrow_utils");
7
+ Object.defineProperty(exports, "EscrowUtils", { enumerable: true, get: function () { return escrow_utils_1.EscrowUtils; } });
@@ -2,4 +2,5 @@ import { IStatisticsFilter } from '../../interfaces';
2
2
  export declare const GET_HMTOKEN_STATISTICS_QUERY: import("graphql").DocumentNode;
3
3
  export declare const GET_ESCROW_STATISTICS_QUERY: import("graphql").DocumentNode;
4
4
  export declare const GET_EVENT_DAY_DATA_QUERY: (params: IStatisticsFilter) => import("graphql").DocumentNode;
5
+ export declare const GET_HMT_EVENT_DAY_DATA_QUERY: (params: IStatisticsFilter) => import("graphql").DocumentNode;
5
6
  //# sourceMappingURL=statistics.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"statistics.d.ts","sourceRoot":"","sources":["../../../src/graphql/queries/statistics.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAqDrD,eAAO,MAAM,4BAA4B,gCAOxC,CAAC;AAIF,eAAO,MAAM,2BAA2B,gCAOvC,CAAC;AAEF,eAAO,MAAM,wBAAwB,GAAI,QAAQ,iBAAiB,mCA6BjE,CAAC"}
1
+ {"version":3,"file":"statistics.d.ts","sourceRoot":"","sources":["../../../src/graphql/queries/statistics.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AA0DrD,eAAO,MAAM,4BAA4B,gCAOxC,CAAC;AAIF,eAAO,MAAM,2BAA2B,gCAOvC,CAAC;AAEF,eAAO,MAAM,wBAAwB,GAAI,QAAQ,iBAAiB,mCA6BjE,CAAC;AAEF,eAAO,MAAM,4BAA4B,GAAI,QAAQ,iBAAiB,mCA6BrE,CAAC"}
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.GET_EVENT_DAY_DATA_QUERY = exports.GET_ESCROW_STATISTICS_QUERY = exports.GET_HMTOKEN_STATISTICS_QUERY = void 0;
6
+ exports.GET_HMT_EVENT_DAY_DATA_QUERY = exports.GET_EVENT_DAY_DATA_QUERY = exports.GET_ESCROW_STATISTICS_QUERY = exports.GET_HMTOKEN_STATISTICS_QUERY = void 0;
7
7
  const graphql_tag_1 = __importDefault(require("graphql-tag"));
8
8
  const HMTOKEN_STATISTICS_FRAGMENT = (0, graphql_tag_1.default) `
9
9
  fragment HMTokenStatisticsFields on HMTokenStatistics {
@@ -44,7 +44,11 @@ const EVENT_DAY_DATA_FRAGMENT = (0, graphql_tag_1.default) `
44
44
  dailyEscrowCount
45
45
  dailyWorkerCount
46
46
  dailyPayoutCount
47
- dailyHMTPayoutAmount
47
+ }
48
+ `;
49
+ const HMT_EVENT_DAY_DATA_FRAGMENT = (0, graphql_tag_1.default) `
50
+ fragment HMTEventDayDataFields on EventDayData {
51
+ timestamp
48
52
  dailyHMTTransferCount
49
53
  dailyHMTTransferAmount
50
54
  dailyUniqueSenders
@@ -99,3 +103,33 @@ const GET_EVENT_DAY_DATA_QUERY = (params) => {
99
103
  `;
100
104
  };
101
105
  exports.GET_EVENT_DAY_DATA_QUERY = GET_EVENT_DAY_DATA_QUERY;
106
+ const GET_HMT_EVENT_DAY_DATA_QUERY = (params) => {
107
+ const { from, to } = params;
108
+ const WHERE_CLAUSE = `
109
+ where: {
110
+ ${from !== undefined ? `timestamp_gte: $from` : ''}
111
+ ${to !== undefined ? `timestamp_lte: $to` : ''}
112
+ }
113
+ `;
114
+ return (0, graphql_tag_1.default) `
115
+ query GetHMTDayData(
116
+ $from: Int,
117
+ $to: Int,
118
+ $orderDirection: String
119
+ $first: Int
120
+ $skip: Int
121
+ ) {
122
+ eventDayDatas(
123
+ ${WHERE_CLAUSE},
124
+ orderBy: timestamp,
125
+ orderDirection: $orderDirection,
126
+ first: $first,
127
+ skip: $skip
128
+ ) {
129
+ ...HMTEventDayDataFields
130
+ }
131
+ }
132
+ ${HMT_EVENT_DAY_DATA_FRAGMENT}
133
+ `;
134
+ };
135
+ exports.GET_HMT_EVENT_DAY_DATA_QUERY = GET_HMT_EVENT_DAY_DATA_QUERY;
@@ -88,7 +88,9 @@ export type EventDayData = {
88
88
  dailyEscrowCount: string;
89
89
  dailyWorkerCount: string;
90
90
  dailyPayoutCount: string;
91
- dailyHMTPayoutAmount: string;
91
+ };
92
+ export type HMTEventDayData = {
93
+ timestamp: string;
92
94
  dailyHMTTransferCount: string;
93
95
  dailyHMTTransferAmount: string;
94
96
  dailyUniqueSenders: string;