@human-protocol/sdk 5.0.0-beta.3 → 5.1.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/dist/base.d.ts +1 -10
- package/dist/base.d.ts.map +1 -1
- package/dist/base.js +0 -21
- package/dist/constants.d.ts +0 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +7 -22
- package/dist/enums.d.ts +0 -1
- package/dist/enums.d.ts.map +1 -1
- package/dist/enums.js +0 -1
- package/dist/error.d.ts +4 -0
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +5 -1
- package/dist/escrow.d.ts +75 -19
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +144 -58
- package/dist/interfaces.d.ts +9 -0
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/kvstore.d.ts +9 -5
- package/dist/kvstore.d.ts.map +1 -1
- package/dist/kvstore.js +15 -15
- package/dist/operator.d.ts +9 -5
- package/dist/operator.d.ts.map +1 -1
- package/dist/operator.js +16 -16
- package/dist/staking.d.ts +6 -3
- package/dist/staking.d.ts.map +1 -1
- package/dist/staking.js +13 -14
- package/dist/statistics.d.ts +13 -7
- package/dist/statistics.d.ts.map +1 -1
- package/dist/statistics.js +24 -22
- package/dist/transaction.d.ts +5 -3
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +8 -10
- package/dist/utils.d.ts +7 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +51 -1
- package/dist/worker.d.ts +5 -3
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +8 -10
- package/package.json +3 -2
- package/src/base.ts +1 -23
- package/src/constants.ts +6 -24
- package/src/enums.ts +0 -1
- package/src/error.ts +7 -0
- package/src/escrow.ts +232 -97
- package/src/interfaces.ts +10 -0
- package/src/kvstore.ts +26 -24
- package/src/operator.ts +54 -26
- package/src/staking.ts +27 -26
- package/src/statistics.ts +87 -47
- package/src/transaction.ts +39 -25
- package/src/utils.ts +64 -0
- package/src/worker.ts +32 -17
package/src/statistics.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import gqlFetch from 'graphql-request';
|
|
3
|
-
|
|
4
2
|
import { OrderDirection } from './enums';
|
|
5
3
|
import {
|
|
6
4
|
EscrowStatisticsData,
|
|
@@ -21,9 +19,15 @@ import {
|
|
|
21
19
|
IPaymentStatistics,
|
|
22
20
|
IStatisticsFilter,
|
|
23
21
|
IWorkerStatistics,
|
|
22
|
+
SubgraphOptions,
|
|
24
23
|
} from './interfaces';
|
|
25
24
|
import { NetworkData } from './types';
|
|
26
|
-
import {
|
|
25
|
+
import {
|
|
26
|
+
getSubgraphUrl,
|
|
27
|
+
getUnixTimestamp,
|
|
28
|
+
customGqlFetch,
|
|
29
|
+
throwError,
|
|
30
|
+
} from './utils';
|
|
27
31
|
|
|
28
32
|
/**
|
|
29
33
|
* ## Introduction
|
|
@@ -103,6 +107,7 @@ export class StatisticsClient {
|
|
|
103
107
|
* ```
|
|
104
108
|
*
|
|
105
109
|
* @param {IStatisticsFilter} filter Statistics params with duration data
|
|
110
|
+
* @param {SubgraphOptions} options Optional configuration for subgraph requests.
|
|
106
111
|
* @returns {Promise<IEscrowStatistics>} Escrow statistics data.
|
|
107
112
|
*
|
|
108
113
|
* **Code example**
|
|
@@ -120,7 +125,8 @@ export class StatisticsClient {
|
|
|
120
125
|
* ```
|
|
121
126
|
*/
|
|
122
127
|
async getEscrowStatistics(
|
|
123
|
-
filter: IStatisticsFilter = {}
|
|
128
|
+
filter: IStatisticsFilter = {},
|
|
129
|
+
options?: SubgraphOptions
|
|
124
130
|
): Promise<IEscrowStatistics> {
|
|
125
131
|
try {
|
|
126
132
|
const first =
|
|
@@ -128,19 +134,24 @@ export class StatisticsClient {
|
|
|
128
134
|
const skip = filter.skip || 0;
|
|
129
135
|
const orderDirection = filter.orderDirection || OrderDirection.ASC;
|
|
130
136
|
|
|
131
|
-
const { escrowStatistics } = await
|
|
137
|
+
const { escrowStatistics } = await customGqlFetch<{
|
|
132
138
|
escrowStatistics: EscrowStatisticsData;
|
|
133
|
-
}>(this.subgraphUrl, GET_ESCROW_STATISTICS_QUERY);
|
|
139
|
+
}>(this.subgraphUrl, GET_ESCROW_STATISTICS_QUERY, options);
|
|
134
140
|
|
|
135
|
-
const { eventDayDatas } = await
|
|
141
|
+
const { eventDayDatas } = await customGqlFetch<{
|
|
136
142
|
eventDayDatas: EventDayData[];
|
|
137
|
-
}>(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
143
|
+
}>(
|
|
144
|
+
this.subgraphUrl,
|
|
145
|
+
GET_EVENT_DAY_DATA_QUERY(filter),
|
|
146
|
+
{
|
|
147
|
+
from: filter.from ? getUnixTimestamp(filter.from) : undefined,
|
|
148
|
+
to: filter.to ? getUnixTimestamp(filter.to) : undefined,
|
|
149
|
+
orderDirection: orderDirection,
|
|
150
|
+
first: first,
|
|
151
|
+
skip: skip,
|
|
152
|
+
},
|
|
153
|
+
options
|
|
154
|
+
);
|
|
144
155
|
|
|
145
156
|
return {
|
|
146
157
|
totalEscrows: escrowStatistics?.totalEscrowCount
|
|
@@ -187,6 +198,7 @@ export class StatisticsClient {
|
|
|
187
198
|
* ```
|
|
188
199
|
*
|
|
189
200
|
* @param {IStatisticsFilter} filter Statistics params with duration data
|
|
201
|
+
* @param {SubgraphOptions} options Optional configuration for subgraph requests.
|
|
190
202
|
* @returns {Promise<IWorkerStatistics>} Worker statistics data.
|
|
191
203
|
*
|
|
192
204
|
* **Code example**
|
|
@@ -204,7 +216,8 @@ export class StatisticsClient {
|
|
|
204
216
|
* ```
|
|
205
217
|
*/
|
|
206
218
|
async getWorkerStatistics(
|
|
207
|
-
filter: IStatisticsFilter = {}
|
|
219
|
+
filter: IStatisticsFilter = {},
|
|
220
|
+
options?: SubgraphOptions
|
|
208
221
|
): Promise<IWorkerStatistics> {
|
|
209
222
|
try {
|
|
210
223
|
const first =
|
|
@@ -212,15 +225,20 @@ export class StatisticsClient {
|
|
|
212
225
|
const skip = filter.skip || 0;
|
|
213
226
|
const orderDirection = filter.orderDirection || OrderDirection.ASC;
|
|
214
227
|
|
|
215
|
-
const { eventDayDatas } = await
|
|
228
|
+
const { eventDayDatas } = await customGqlFetch<{
|
|
216
229
|
eventDayDatas: EventDayData[];
|
|
217
|
-
}>(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
230
|
+
}>(
|
|
231
|
+
this.subgraphUrl,
|
|
232
|
+
GET_EVENT_DAY_DATA_QUERY(filter),
|
|
233
|
+
{
|
|
234
|
+
from: filter.from ? getUnixTimestamp(filter.from) : undefined,
|
|
235
|
+
to: filter.to ? getUnixTimestamp(filter.to) : undefined,
|
|
236
|
+
orderDirection: orderDirection,
|
|
237
|
+
first: first,
|
|
238
|
+
skip: skip,
|
|
239
|
+
},
|
|
240
|
+
options
|
|
241
|
+
);
|
|
224
242
|
|
|
225
243
|
return {
|
|
226
244
|
dailyWorkersData: eventDayDatas.map((eventDayData) => ({
|
|
@@ -262,6 +280,7 @@ export class StatisticsClient {
|
|
|
262
280
|
* ```
|
|
263
281
|
*
|
|
264
282
|
* @param {IStatisticsFilter} filter Statistics params with duration data
|
|
283
|
+
* @param {SubgraphOptions} options Optional configuration for subgraph requests.
|
|
265
284
|
* @returns {Promise<IPaymentStatistics>} Payment statistics data.
|
|
266
285
|
*
|
|
267
286
|
* **Code example**
|
|
@@ -300,7 +319,8 @@ export class StatisticsClient {
|
|
|
300
319
|
* ```
|
|
301
320
|
*/
|
|
302
321
|
async getPaymentStatistics(
|
|
303
|
-
filter: IStatisticsFilter = {}
|
|
322
|
+
filter: IStatisticsFilter = {},
|
|
323
|
+
options?: SubgraphOptions
|
|
304
324
|
): Promise<IPaymentStatistics> {
|
|
305
325
|
try {
|
|
306
326
|
const first =
|
|
@@ -308,15 +328,20 @@ export class StatisticsClient {
|
|
|
308
328
|
const skip = filter.skip || 0;
|
|
309
329
|
const orderDirection = filter.orderDirection || OrderDirection.ASC;
|
|
310
330
|
|
|
311
|
-
const { eventDayDatas } = await
|
|
331
|
+
const { eventDayDatas } = await customGqlFetch<{
|
|
312
332
|
eventDayDatas: EventDayData[];
|
|
313
|
-
}>(
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
333
|
+
}>(
|
|
334
|
+
this.subgraphUrl,
|
|
335
|
+
GET_EVENT_DAY_DATA_QUERY(filter),
|
|
336
|
+
{
|
|
337
|
+
from: filter.from ? getUnixTimestamp(filter.from) : undefined,
|
|
338
|
+
to: filter.to ? getUnixTimestamp(filter.to) : undefined,
|
|
339
|
+
orderDirection: orderDirection,
|
|
340
|
+
first: first,
|
|
341
|
+
skip: skip,
|
|
342
|
+
},
|
|
343
|
+
options
|
|
344
|
+
);
|
|
320
345
|
|
|
321
346
|
return {
|
|
322
347
|
dailyPaymentsData: eventDayDatas.map((eventDayData) => ({
|
|
@@ -346,6 +371,7 @@ export class StatisticsClient {
|
|
|
346
371
|
* };
|
|
347
372
|
* ```
|
|
348
373
|
*
|
|
374
|
+
* @param {SubgraphOptions} options Optional configuration for subgraph requests.
|
|
349
375
|
* @returns {Promise<IHMTStatistics>} HMToken statistics data.
|
|
350
376
|
*
|
|
351
377
|
* **Code example**
|
|
@@ -363,11 +389,11 @@ export class StatisticsClient {
|
|
|
363
389
|
* });
|
|
364
390
|
* ```
|
|
365
391
|
*/
|
|
366
|
-
async getHMTStatistics(): Promise<IHMTStatistics> {
|
|
392
|
+
async getHMTStatistics(options?: SubgraphOptions): Promise<IHMTStatistics> {
|
|
367
393
|
try {
|
|
368
|
-
const { hmtokenStatistics } = await
|
|
394
|
+
const { hmtokenStatistics } = await customGqlFetch<{
|
|
369
395
|
hmtokenStatistics: HMTStatisticsData;
|
|
370
|
-
}>(this.subgraphUrl, GET_HMTOKEN_STATISTICS_QUERY);
|
|
396
|
+
}>(this.subgraphUrl, GET_HMTOKEN_STATISTICS_QUERY, options);
|
|
371
397
|
|
|
372
398
|
return {
|
|
373
399
|
totalTransferAmount: BigInt(hmtokenStatistics.totalValueTransfered),
|
|
@@ -385,6 +411,7 @@ export class StatisticsClient {
|
|
|
385
411
|
* **Input parameters**
|
|
386
412
|
*
|
|
387
413
|
* @param {IHMTHoldersParams} params HMT Holders params with filters and ordering
|
|
414
|
+
* @param {SubgraphOptions} options Optional configuration for subgraph requests.
|
|
388
415
|
* @returns {Promise<IHMTHolder[]>} List of HMToken holders.
|
|
389
416
|
*
|
|
390
417
|
* **Code example**
|
|
@@ -404,19 +431,23 @@ export class StatisticsClient {
|
|
|
404
431
|
* })));
|
|
405
432
|
* ```
|
|
406
433
|
*/
|
|
407
|
-
async getHMTHolders(
|
|
434
|
+
async getHMTHolders(
|
|
435
|
+
params: IHMTHoldersParams = {},
|
|
436
|
+
options?: SubgraphOptions
|
|
437
|
+
): Promise<IHMTHolder[]> {
|
|
408
438
|
try {
|
|
409
439
|
const { address, orderDirection } = params;
|
|
410
440
|
const query = GET_HOLDERS_QUERY(address);
|
|
411
441
|
|
|
412
|
-
const { holders } = await
|
|
442
|
+
const { holders } = await customGqlFetch<{ holders: HMTHolderData[] }>(
|
|
413
443
|
this.subgraphUrl,
|
|
414
444
|
query,
|
|
415
445
|
{
|
|
416
446
|
address,
|
|
417
447
|
orderBy: 'balance',
|
|
418
448
|
orderDirection,
|
|
419
|
-
}
|
|
449
|
+
},
|
|
450
|
+
options
|
|
420
451
|
);
|
|
421
452
|
|
|
422
453
|
return holders.map((holder) => ({
|
|
@@ -454,6 +485,7 @@ export class StatisticsClient {
|
|
|
454
485
|
* ```
|
|
455
486
|
*
|
|
456
487
|
* @param {IStatisticsFilter} filter Statistics params with duration data
|
|
488
|
+
* @param {SubgraphOptions} options Optional configuration for subgraph requests.
|
|
457
489
|
* @returns {Promise<IDailyHMT[]>} Daily HMToken statistics data.
|
|
458
490
|
*
|
|
459
491
|
* **Code example**
|
|
@@ -475,22 +507,30 @@ export class StatisticsClient {
|
|
|
475
507
|
* console.log('HMT statistics from 5/8 - 6/8:', hmtStatisticsRange);
|
|
476
508
|
* ```
|
|
477
509
|
*/
|
|
478
|
-
async getHMTDailyData(
|
|
510
|
+
async getHMTDailyData(
|
|
511
|
+
filter: IStatisticsFilter = {},
|
|
512
|
+
options?: SubgraphOptions
|
|
513
|
+
): Promise<IDailyHMT[]> {
|
|
479
514
|
try {
|
|
480
515
|
const first =
|
|
481
516
|
filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
|
|
482
517
|
const skip = filter.skip || 0;
|
|
483
518
|
const orderDirection = filter.orderDirection || OrderDirection.ASC;
|
|
484
519
|
|
|
485
|
-
const { eventDayDatas } = await
|
|
520
|
+
const { eventDayDatas } = await customGqlFetch<{
|
|
486
521
|
eventDayDatas: EventDayData[];
|
|
487
|
-
}>(
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
522
|
+
}>(
|
|
523
|
+
this.subgraphUrl,
|
|
524
|
+
GET_EVENT_DAY_DATA_QUERY(filter),
|
|
525
|
+
{
|
|
526
|
+
from: filter.from ? getUnixTimestamp(filter.from) : undefined,
|
|
527
|
+
to: filter.to ? getUnixTimestamp(filter.to) : undefined,
|
|
528
|
+
orderDirection: orderDirection,
|
|
529
|
+
first: first,
|
|
530
|
+
skip: skip,
|
|
531
|
+
},
|
|
532
|
+
options
|
|
533
|
+
);
|
|
494
534
|
|
|
495
535
|
return eventDayDatas.map((eventDayData) => ({
|
|
496
536
|
timestamp: +eventDayData.timestamp * 1000,
|
package/src/transaction.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { ethers } from 'ethers';
|
|
3
|
-
import gqlFetch from 'graphql-request';
|
|
4
3
|
import { NETWORKS } from './constants';
|
|
5
4
|
import { ChainId, OrderDirection } from './enums';
|
|
6
5
|
import {
|
|
@@ -17,8 +16,9 @@ import {
|
|
|
17
16
|
InternalTransaction,
|
|
18
17
|
ITransaction,
|
|
19
18
|
ITransactionsFilter,
|
|
19
|
+
SubgraphOptions,
|
|
20
20
|
} from './interfaces';
|
|
21
|
-
import { getSubgraphUrl, getUnixTimestamp } from './utils';
|
|
21
|
+
import { getSubgraphUrl, getUnixTimestamp, customGqlFetch } from './utils';
|
|
22
22
|
|
|
23
23
|
export class TransactionUtils {
|
|
24
24
|
/**
|
|
@@ -54,6 +54,7 @@ export class TransactionUtils {
|
|
|
54
54
|
*
|
|
55
55
|
* @param {ChainId} chainId The chain ID.
|
|
56
56
|
* @param {string} hash The transaction hash.
|
|
57
|
+
* @param {SubgraphOptions} options Optional configuration for subgraph requests.
|
|
57
58
|
* @returns {Promise<ITransaction | null>} - Returns the transaction details or null if not found.
|
|
58
59
|
*
|
|
59
60
|
* **Code example**
|
|
@@ -66,7 +67,8 @@ export class TransactionUtils {
|
|
|
66
67
|
*/
|
|
67
68
|
public static async getTransaction(
|
|
68
69
|
chainId: ChainId,
|
|
69
|
-
hash: string
|
|
70
|
+
hash: string,
|
|
71
|
+
options?: SubgraphOptions
|
|
70
72
|
): Promise<ITransaction | null> {
|
|
71
73
|
if (!ethers.isHexString(hash)) {
|
|
72
74
|
throw ErrorInvalidHashProvided;
|
|
@@ -77,11 +79,16 @@ export class TransactionUtils {
|
|
|
77
79
|
throw ErrorUnsupportedChainID;
|
|
78
80
|
}
|
|
79
81
|
|
|
80
|
-
const { transaction } = await
|
|
82
|
+
const { transaction } = await customGqlFetch<{
|
|
81
83
|
transaction: TransactionData | null;
|
|
82
|
-
}>(
|
|
83
|
-
|
|
84
|
-
|
|
84
|
+
}>(
|
|
85
|
+
getSubgraphUrl(networkData),
|
|
86
|
+
GET_TRANSACTION_QUERY,
|
|
87
|
+
{
|
|
88
|
+
hash: hash.toLowerCase(),
|
|
89
|
+
},
|
|
90
|
+
options
|
|
91
|
+
);
|
|
85
92
|
if (!transaction) return null;
|
|
86
93
|
|
|
87
94
|
return mapTransaction(transaction);
|
|
@@ -141,6 +148,7 @@ export class TransactionUtils {
|
|
|
141
148
|
* ```
|
|
142
149
|
*
|
|
143
150
|
* @param {ITransactionsFilter} filter Filter for the transactions.
|
|
151
|
+
* @param {SubgraphOptions} options Optional configuration for subgraph requests.
|
|
144
152
|
* @returns {Promise<ITransaction[]>} Returns an array with all the transaction details.
|
|
145
153
|
*
|
|
146
154
|
* **Code example**
|
|
@@ -160,7 +168,8 @@ export class TransactionUtils {
|
|
|
160
168
|
* ```
|
|
161
169
|
*/
|
|
162
170
|
public static async getTransactions(
|
|
163
|
-
filter: ITransactionsFilter
|
|
171
|
+
filter: ITransactionsFilter,
|
|
172
|
+
options?: SubgraphOptions
|
|
164
173
|
): Promise<ITransaction[]> {
|
|
165
174
|
if (
|
|
166
175
|
(!!filter.startDate || !!filter.endDate) &&
|
|
@@ -179,24 +188,29 @@ export class TransactionUtils {
|
|
|
179
188
|
throw ErrorUnsupportedChainID;
|
|
180
189
|
}
|
|
181
190
|
|
|
182
|
-
const { transactions } = await
|
|
191
|
+
const { transactions } = await customGqlFetch<{
|
|
183
192
|
transactions: TransactionData[];
|
|
184
|
-
}>(
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
:
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
193
|
+
}>(
|
|
194
|
+
getSubgraphUrl(networkData),
|
|
195
|
+
GET_TRANSACTIONS_QUERY(filter),
|
|
196
|
+
{
|
|
197
|
+
fromAddress: filter?.fromAddress,
|
|
198
|
+
toAddress: filter?.toAddress,
|
|
199
|
+
startDate: filter?.startDate
|
|
200
|
+
? getUnixTimestamp(filter?.startDate)
|
|
201
|
+
: undefined,
|
|
202
|
+
endDate: filter.endDate ? getUnixTimestamp(filter.endDate) : undefined,
|
|
203
|
+
startBlock: filter.startBlock ? filter.startBlock : undefined,
|
|
204
|
+
endBlock: filter.endBlock ? filter.endBlock : undefined,
|
|
205
|
+
method: filter.method ? filter.method : undefined,
|
|
206
|
+
escrow: filter.escrow ? filter.escrow : undefined,
|
|
207
|
+
token: filter.token ? filter.token : undefined,
|
|
208
|
+
orderDirection: orderDirection,
|
|
209
|
+
first: first,
|
|
210
|
+
skip: skip,
|
|
211
|
+
},
|
|
212
|
+
options
|
|
213
|
+
);
|
|
200
214
|
|
|
201
215
|
if (!transactions) {
|
|
202
216
|
return [];
|
package/src/utils.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { ethers } from 'ethers';
|
|
3
|
+
import gqlFetch from 'graphql-request';
|
|
3
4
|
|
|
4
5
|
import { isURL } from 'validator';
|
|
5
6
|
import { SUBGRAPH_API_KEY_PLACEHOLDER } from './constants';
|
|
6
7
|
import { ChainId } from './enums';
|
|
7
8
|
import {
|
|
8
9
|
ContractExecutionError,
|
|
10
|
+
ErrorRetryParametersMissing,
|
|
9
11
|
EthereumError,
|
|
10
12
|
InvalidArgumentError,
|
|
11
13
|
NonceExpired,
|
|
@@ -15,6 +17,7 @@ import {
|
|
|
15
17
|
WarnSubgraphApiKeyNotProvided,
|
|
16
18
|
} from './error';
|
|
17
19
|
import { NetworkData } from './types';
|
|
20
|
+
import { SubgraphOptions } from './interfaces';
|
|
18
21
|
|
|
19
22
|
/**
|
|
20
23
|
* **Handle and throw the error.*
|
|
@@ -99,3 +102,64 @@ export const getSubgraphUrl = (networkData: NetworkData) => {
|
|
|
99
102
|
export const getUnixTimestamp = (date: Date): number => {
|
|
100
103
|
return Math.floor(date.getTime() / 1000);
|
|
101
104
|
};
|
|
105
|
+
|
|
106
|
+
export const isIndexerError = (error: any): boolean => {
|
|
107
|
+
if (!error) return false;
|
|
108
|
+
|
|
109
|
+
const errorMessage =
|
|
110
|
+
error.response?.errors?.[0]?.message ||
|
|
111
|
+
error.message ||
|
|
112
|
+
error.toString() ||
|
|
113
|
+
'';
|
|
114
|
+
return errorMessage.toLowerCase().includes('bad indexers');
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const sleep = (ms: number): Promise<void> => {
|
|
118
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Execute a GraphQL request with automatic retry logic for bad indexer errors.
|
|
123
|
+
* Only retries if options is provided.
|
|
124
|
+
*/
|
|
125
|
+
export const customGqlFetch = async <T = any>(
|
|
126
|
+
url: string,
|
|
127
|
+
query: any,
|
|
128
|
+
variables?: any,
|
|
129
|
+
options?: SubgraphOptions
|
|
130
|
+
): Promise<T> => {
|
|
131
|
+
if (!options) {
|
|
132
|
+
return await gqlFetch<T>(url, query, variables);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (
|
|
136
|
+
(options.maxRetries && options.baseDelay === undefined) ||
|
|
137
|
+
(options.baseDelay && options.maxRetries === undefined)
|
|
138
|
+
) {
|
|
139
|
+
throw ErrorRetryParametersMissing;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let lastError: any;
|
|
143
|
+
|
|
144
|
+
for (let attempt = 0; attempt <= (options.maxRetries as number); attempt++) {
|
|
145
|
+
try {
|
|
146
|
+
const result = await gqlFetch<T>(url, query, variables);
|
|
147
|
+
return result;
|
|
148
|
+
} catch (error) {
|
|
149
|
+
lastError = error;
|
|
150
|
+
|
|
151
|
+
if (attempt === options.maxRetries) {
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (!isIndexerError(error)) {
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const delay = (options.baseDelay as number) * attempt;
|
|
160
|
+
await sleep(delay);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
throw lastError;
|
|
165
|
+
};
|
package/src/worker.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { ethers } from 'ethers';
|
|
2
|
-
import gqlFetch from 'graphql-request';
|
|
3
2
|
import { NETWORKS } from './constants';
|
|
4
3
|
import { ChainId, OrderDirection } from './enums';
|
|
5
4
|
import { ErrorInvalidAddress, ErrorUnsupportedChainID } from './error';
|
|
6
5
|
import { WorkerData } from './graphql';
|
|
7
6
|
import { GET_WORKER_QUERY, GET_WORKERS_QUERY } from './graphql/queries/worker';
|
|
8
|
-
import { IWorker, IWorkersFilter } from './interfaces';
|
|
9
|
-
import { getSubgraphUrl } from './utils';
|
|
7
|
+
import { IWorker, IWorkersFilter, SubgraphOptions } from './interfaces';
|
|
8
|
+
import { getSubgraphUrl, customGqlFetch } from './utils';
|
|
10
9
|
|
|
11
10
|
export class WorkerUtils {
|
|
12
11
|
/**
|
|
@@ -14,6 +13,7 @@ export class WorkerUtils {
|
|
|
14
13
|
*
|
|
15
14
|
* @param {ChainId} chainId The chain ID.
|
|
16
15
|
* @param {string} address The worker address.
|
|
16
|
+
* @param {SubgraphOptions} options Optional configuration for subgraph requests.
|
|
17
17
|
* @returns {Promise<IWorker | null>} - Returns the worker details or null if not found.
|
|
18
18
|
*
|
|
19
19
|
* **Code example**
|
|
@@ -26,7 +26,8 @@ export class WorkerUtils {
|
|
|
26
26
|
*/
|
|
27
27
|
public static async getWorker(
|
|
28
28
|
chainId: ChainId,
|
|
29
|
-
address: string
|
|
29
|
+
address: string,
|
|
30
|
+
options?: SubgraphOptions
|
|
30
31
|
): Promise<IWorker | null> {
|
|
31
32
|
const networkData = NETWORKS[chainId];
|
|
32
33
|
|
|
@@ -37,11 +38,16 @@ export class WorkerUtils {
|
|
|
37
38
|
throw ErrorInvalidAddress;
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
const { worker } = await
|
|
41
|
+
const { worker } = await customGqlFetch<{
|
|
41
42
|
worker: WorkerData | null;
|
|
42
|
-
}>(
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
}>(
|
|
44
|
+
getSubgraphUrl(networkData),
|
|
45
|
+
GET_WORKER_QUERY,
|
|
46
|
+
{
|
|
47
|
+
address: address.toLowerCase(),
|
|
48
|
+
},
|
|
49
|
+
options
|
|
50
|
+
);
|
|
45
51
|
|
|
46
52
|
if (!worker) return null;
|
|
47
53
|
|
|
@@ -74,6 +80,7 @@ export class WorkerUtils {
|
|
|
74
80
|
* ```
|
|
75
81
|
*
|
|
76
82
|
* @param {IWorkersFilter} filter Filter for the workers.
|
|
83
|
+
* @param {SubgraphOptions} options Optional configuration for subgraph requests.
|
|
77
84
|
* @returns {Promise<IWorker[]>} Returns an array with all the worker details.
|
|
78
85
|
*
|
|
79
86
|
* **Code example**
|
|
@@ -89,7 +96,10 @@ export class WorkerUtils {
|
|
|
89
96
|
* const workers = await WorkerUtils.getWorkers(filter);
|
|
90
97
|
* ```
|
|
91
98
|
*/
|
|
92
|
-
public static async getWorkers(
|
|
99
|
+
public static async getWorkers(
|
|
100
|
+
filter: IWorkersFilter,
|
|
101
|
+
options?: SubgraphOptions
|
|
102
|
+
): Promise<IWorker[]> {
|
|
93
103
|
const first =
|
|
94
104
|
filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
|
|
95
105
|
const skip = filter.skip || 0;
|
|
@@ -104,15 +114,20 @@ export class WorkerUtils {
|
|
|
104
114
|
throw ErrorInvalidAddress;
|
|
105
115
|
}
|
|
106
116
|
|
|
107
|
-
const { workers } = await
|
|
117
|
+
const { workers } = await customGqlFetch<{
|
|
108
118
|
workers: WorkerData[];
|
|
109
|
-
}>(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
119
|
+
}>(
|
|
120
|
+
getSubgraphUrl(networkData),
|
|
121
|
+
GET_WORKERS_QUERY(filter),
|
|
122
|
+
{
|
|
123
|
+
address: filter?.address?.toLowerCase(),
|
|
124
|
+
first: first,
|
|
125
|
+
skip: skip,
|
|
126
|
+
orderBy: orderBy,
|
|
127
|
+
orderDirection: orderDirection,
|
|
128
|
+
},
|
|
129
|
+
options
|
|
130
|
+
);
|
|
116
131
|
|
|
117
132
|
if (!workers) {
|
|
118
133
|
return [];
|