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