@human-protocol/sdk 5.0.0-beta.2 → 5.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/src/statistics.ts CHANGED
@@ -1,27 +1,29 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { ethers } from 'ethers';
3
2
  import gqlFetch from 'graphql-request';
4
3
 
4
+ import { OrderDirection } from './enums';
5
5
  import {
6
+ EscrowStatisticsData,
7
+ EventDayData,
6
8
  GET_ESCROW_STATISTICS_QUERY,
7
9
  GET_EVENT_DAY_DATA_QUERY,
8
- GET_HOLDERS_QUERY,
9
10
  GET_HMTOKEN_STATISTICS_QUERY,
10
- EscrowStatistics,
11
- EscrowStatisticsData,
12
- EventDayData,
13
- HMTStatistics,
14
- HMTStatisticsData,
15
- PaymentStatistics,
16
- WorkerStatistics,
11
+ GET_HOLDERS_QUERY,
17
12
  HMTHolderData,
18
- HMTHolder,
19
- DailyHMTData,
13
+ HMTStatisticsData,
20
14
  } from './graphql';
21
- import { IHMTHoldersParams, IStatisticsFilter } from './interfaces';
15
+ import {
16
+ IDailyHMT,
17
+ IEscrowStatistics,
18
+ IHMTHolder,
19
+ IHMTHoldersParams,
20
+ IHMTStatistics,
21
+ IPaymentStatistics,
22
+ IStatisticsFilter,
23
+ IWorkerStatistics,
24
+ } from './interfaces';
22
25
  import { NetworkData } from './types';
23
26
  import { getSubgraphUrl, getUnixTimestamp, throwError } from './utils';
24
- import { OrderDirection } from './enums';
25
27
 
26
28
  /**
27
29
  * ## Introduction
@@ -85,8 +87,8 @@ export class StatisticsClient {
85
87
  * ```
86
88
  *
87
89
  * ```ts
88
- * type DailyEscrowsData = {
89
- * timestamp: Date;
90
+ * interface IDailyEscrow {
91
+ * timestamp: number;
90
92
  * escrowsTotal: number;
91
93
  * escrowsPending: number;
92
94
  * escrowsSolved: number;
@@ -94,14 +96,14 @@ export class StatisticsClient {
94
96
  * escrowsCancelled: number;
95
97
  * };
96
98
  *
97
- * type EscrowStatistics = {
99
+ * interface IEscrowStatistics {
98
100
  * totalEscrows: number;
99
- * dailyEscrowsData: DailyEscrowsData[];
101
+ * dailyEscrowsData: IDailyEscrow[];
100
102
  * };
101
103
  * ```
102
104
  *
103
105
  * @param {IStatisticsFilter} filter Statistics params with duration data
104
- * @returns {Promise<EscrowStatistics>} Escrow statistics data.
106
+ * @returns {Promise<IEscrowStatistics>} Escrow statistics data.
105
107
  *
106
108
  * **Code example**
107
109
  *
@@ -119,7 +121,7 @@ export class StatisticsClient {
119
121
  */
120
122
  async getEscrowStatistics(
121
123
  filter: IStatisticsFilter = {}
122
- ): Promise<EscrowStatistics> {
124
+ ): Promise<IEscrowStatistics> {
123
125
  try {
124
126
  const first =
125
127
  filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
@@ -145,7 +147,7 @@ export class StatisticsClient {
145
147
  ? +escrowStatistics.totalEscrowCount
146
148
  : 0,
147
149
  dailyEscrowsData: eventDayDatas.map((eventDayData) => ({
148
- timestamp: new Date(+eventDayData.timestamp * 1000),
150
+ timestamp: +eventDayData.timestamp * 1000,
149
151
  escrowsTotal: +eventDayData.dailyEscrowCount,
150
152
  escrowsPending: +eventDayData.dailyPendingStatusEventCount,
151
153
  escrowsSolved: +eventDayData.dailyCompletedStatusEventCount,
@@ -174,18 +176,18 @@ export class StatisticsClient {
174
176
  * ```
175
177
  *
176
178
  * ```ts
177
- * type DailyWorkerData = {
178
- * timestamp: Date;
179
+ * interface IDailyWorker {
180
+ * timestamp: number;
179
181
  * activeWorkers: number;
180
182
  * };
181
183
  *
182
- * type WorkerStatistics = {
183
- * dailyWorkersData: DailyWorkerData[];
184
+ * interface IWorkerStatistics {
185
+ * dailyWorkersData: IDailyWorker[];
184
186
  * };
185
187
  * ```
186
188
  *
187
189
  * @param {IStatisticsFilter} filter Statistics params with duration data
188
- * @returns {Promise<WorkerStatistics>} Worker statistics data.
190
+ * @returns {Promise<IWorkerStatistics>} Worker statistics data.
189
191
  *
190
192
  * **Code example**
191
193
  *
@@ -203,7 +205,7 @@ export class StatisticsClient {
203
205
  */
204
206
  async getWorkerStatistics(
205
207
  filter: IStatisticsFilter = {}
206
- ): Promise<WorkerStatistics> {
208
+ ): Promise<IWorkerStatistics> {
207
209
  try {
208
210
  const first =
209
211
  filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
@@ -222,7 +224,7 @@ export class StatisticsClient {
222
224
 
223
225
  return {
224
226
  dailyWorkersData: eventDayDatas.map((eventDayData) => ({
225
- timestamp: new Date(+eventDayData.timestamp * 1000),
227
+ timestamp: +eventDayData.timestamp * 1000,
226
228
  activeWorkers: +eventDayData.dailyWorkerCount,
227
229
  })),
228
230
  };
@@ -247,20 +249,20 @@ export class StatisticsClient {
247
249
  * ```
248
250
  *
249
251
  * ```ts
250
- * type DailyPaymentData = {
251
- * timestamp: Date;
252
- * totalAmountPaid: BigNumber;
252
+ * interface IDailyPayment {
253
+ * timestamp: number;
254
+ * totalAmountPaid: bigint;
253
255
  * totalCount: number;
254
- * averageAmountPerWorker: BigNumber;
256
+ * averageAmountPerWorker: bigint;
255
257
  * };
256
258
  *
257
- * type PaymentStatistics = {
258
- * dailyPaymentsData: DailyPaymentData[];
259
+ * interface IPaymentStatistics {
260
+ * dailyPaymentsData: IDailyPayment[];
259
261
  * };
260
262
  * ```
261
263
  *
262
264
  * @param {IStatisticsFilter} filter Statistics params with duration data
263
- * @returns {Promise<PaymentStatistics>} Payment statistics data.
265
+ * @returns {Promise<IPaymentStatistics>} Payment statistics data.
264
266
  *
265
267
  * **Code example**
266
268
  *
@@ -299,7 +301,7 @@ export class StatisticsClient {
299
301
  */
300
302
  async getPaymentStatistics(
301
303
  filter: IStatisticsFilter = {}
302
- ): Promise<PaymentStatistics> {
304
+ ): Promise<IPaymentStatistics> {
303
305
  try {
304
306
  const first =
305
307
  filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
@@ -318,14 +320,14 @@ export class StatisticsClient {
318
320
 
319
321
  return {
320
322
  dailyPaymentsData: eventDayDatas.map((eventDayData) => ({
321
- timestamp: new Date(+eventDayData.timestamp * 1000),
322
- totalAmountPaid: ethers.toBigInt(eventDayData.dailyHMTPayoutAmount),
323
+ timestamp: +eventDayData.timestamp * 1000,
324
+ totalAmountPaid: BigInt(eventDayData.dailyHMTPayoutAmount),
323
325
  totalCount: +eventDayData.dailyPayoutCount,
324
326
  averageAmountPerWorker:
325
327
  eventDayData.dailyWorkerCount === '0'
326
- ? ethers.toBigInt(0)
327
- : ethers.toBigInt(eventDayData.dailyHMTPayoutAmount) /
328
- ethers.toBigInt(eventDayData.dailyWorkerCount),
328
+ ? BigInt(0)
329
+ : BigInt(eventDayData.dailyHMTPayoutAmount) /
330
+ BigInt(eventDayData.dailyWorkerCount),
329
331
  })),
330
332
  };
331
333
  } catch (e: any) {
@@ -337,14 +339,14 @@ export class StatisticsClient {
337
339
  * This function returns the statistical data of HMToken.
338
340
  *
339
341
  * ```ts
340
- * type HMTStatistics = {
341
- * totalTransferAmount: BigNumber;
342
- * totalTransferCount: BigNumber;
342
+ * interface IHMTStatistics {
343
+ * totalTransferAmount: bigint;
344
+ * totalTransferCount: number;
343
345
  * totalHolders: number;
344
346
  * };
345
347
  * ```
346
348
  *
347
- * @returns {Promise<HMTStatistics>} HMToken statistics data.
349
+ * @returns {Promise<IHMTStatistics>} HMToken statistics data.
348
350
  *
349
351
  * **Code example**
350
352
  *
@@ -361,17 +363,15 @@ export class StatisticsClient {
361
363
  * });
362
364
  * ```
363
365
  */
364
- async getHMTStatistics(): Promise<HMTStatistics> {
366
+ async getHMTStatistics(): Promise<IHMTStatistics> {
365
367
  try {
366
368
  const { hmtokenStatistics } = await gqlFetch<{
367
369
  hmtokenStatistics: HMTStatisticsData;
368
370
  }>(this.subgraphUrl, GET_HMTOKEN_STATISTICS_QUERY);
369
371
 
370
372
  return {
371
- totalTransferAmount: ethers.toBigInt(
372
- hmtokenStatistics.totalValueTransfered
373
- ),
374
- totalTransferCount: Number(hmtokenStatistics.totalTransferEventCount),
373
+ totalTransferAmount: BigInt(hmtokenStatistics.totalValueTransfered),
374
+ totalTransferCount: +hmtokenStatistics.totalTransferEventCount,
375
375
  totalHolders: +hmtokenStatistics.holders,
376
376
  };
377
377
  } catch (e: any) {
@@ -385,7 +385,7 @@ export class StatisticsClient {
385
385
  * **Input parameters**
386
386
  *
387
387
  * @param {IHMTHoldersParams} params HMT Holders params with filters and ordering
388
- * @returns {Promise<HMTHolder[]>} List of HMToken holders.
388
+ * @returns {Promise<IHMTHolder[]>} List of HMToken holders.
389
389
  *
390
390
  * **Code example**
391
391
  *
@@ -404,7 +404,7 @@ export class StatisticsClient {
404
404
  * })));
405
405
  * ```
406
406
  */
407
- async getHMTHolders(params: IHMTHoldersParams = {}): Promise<HMTHolder[]> {
407
+ async getHMTHolders(params: IHMTHoldersParams = {}): Promise<IHMTHolder[]> {
408
408
  try {
409
409
  const { address, orderDirection } = params;
410
410
  const query = GET_HOLDERS_QUERY(address);
@@ -421,7 +421,7 @@ export class StatisticsClient {
421
421
 
422
422
  return holders.map((holder) => ({
423
423
  address: holder.address,
424
- balance: ethers.toBigInt(holder.balance),
424
+ balance: BigInt(holder.balance),
425
425
  }));
426
426
  } catch (e: any) {
427
427
  return throwError(e);
@@ -444,8 +444,8 @@ export class StatisticsClient {
444
444
  * ```
445
445
  *
446
446
  * ```ts
447
- * type DailyHMTData = {
448
- * timestamp: Date;
447
+ * interface IDailyHMT {
448
+ * timestamp: number;
449
449
  * totalTransactionAmount: bigint;
450
450
  * totalTransactionCount: number;
451
451
  * dailyUniqueSenders: number;
@@ -454,7 +454,7 @@ export class StatisticsClient {
454
454
  * ```
455
455
  *
456
456
  * @param {IStatisticsFilter} filter Statistics params with duration data
457
- * @returns {Promise<DailyHMTData[]>} Daily HMToken statistics data.
457
+ * @returns {Promise<IDailyHMT[]>} Daily HMToken statistics data.
458
458
  *
459
459
  * **Code example**
460
460
  *
@@ -475,9 +475,7 @@ export class StatisticsClient {
475
475
  * console.log('HMT statistics from 5/8 - 6/8:', hmtStatisticsRange);
476
476
  * ```
477
477
  */
478
- async getHMTDailyData(
479
- filter: IStatisticsFilter = {}
480
- ): Promise<DailyHMTData[]> {
478
+ async getHMTDailyData(filter: IStatisticsFilter = {}): Promise<IDailyHMT[]> {
481
479
  try {
482
480
  const first =
483
481
  filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
@@ -495,10 +493,8 @@ export class StatisticsClient {
495
493
  });
496
494
 
497
495
  return eventDayDatas.map((eventDayData) => ({
498
- timestamp: new Date(+eventDayData.timestamp * 1000),
499
- totalTransactionAmount: ethers.toBigInt(
500
- eventDayData.dailyHMTTransferAmount
501
- ),
496
+ timestamp: +eventDayData.timestamp * 1000,
497
+ totalTransactionAmount: BigInt(eventDayData.dailyHMTTransferAmount),
502
498
  totalTransactionCount: +eventDayData.dailyHMTTransferCount,
503
499
  dailyUniqueSenders: +eventDayData.dailyUniqueSenders,
504
500
  dailyUniqueReceivers: +eventDayData.dailyUniqueReceivers,
@@ -8,11 +8,16 @@ import {
8
8
  ErrorInvalidHashProvided,
9
9
  ErrorUnsupportedChainID,
10
10
  } from './error';
11
+ import { TransactionData } from './graphql';
11
12
  import {
12
- GET_TRANSACTIONS_QUERY,
13
13
  GET_TRANSACTION_QUERY,
14
+ GET_TRANSACTIONS_QUERY,
14
15
  } from './graphql/queries/transaction';
15
- import { ITransaction, ITransactionsFilter } from './interfaces';
16
+ import {
17
+ InternalTransaction,
18
+ ITransaction,
19
+ ITransactionsFilter,
20
+ } from './interfaces';
16
21
  import { getSubgraphUrl, getUnixTimestamp } from './utils';
17
22
 
18
23
  export class TransactionUtils {
@@ -26,7 +31,7 @@ export class TransactionUtils {
26
31
  * from: string;
27
32
  * to: string;
28
33
  * timestamp: bigint;
29
- * value: string;
34
+ * value: bigint;
30
35
  * method: string;
31
36
  * receiver?: string;
32
37
  * escrow?: string;
@@ -35,6 +40,18 @@ export class TransactionUtils {
35
40
  * };
36
41
  * ```
37
42
  *
43
+ * ```ts
44
+ * type InternalTransaction = {
45
+ * from: string;
46
+ * to: string;
47
+ * value: bigint;
48
+ * method: string;
49
+ * receiver?: string;
50
+ * escrow?: string;
51
+ * token?: string;
52
+ * };
53
+ * ```
54
+ *
38
55
  * @param {ChainId} chainId The chain ID.
39
56
  * @param {string} hash The transaction hash.
40
57
  * @returns {Promise<ITransaction | null>} - Returns the transaction details or null if not found.
@@ -61,12 +78,13 @@ export class TransactionUtils {
61
78
  }
62
79
 
63
80
  const { transaction } = await gqlFetch<{
64
- transaction: ITransaction;
81
+ transaction: TransactionData | null;
65
82
  }>(getSubgraphUrl(networkData), GET_TRANSACTION_QUERY, {
66
83
  hash: hash.toLowerCase(),
67
84
  });
85
+ if (!transaction) return null;
68
86
 
69
- return transaction || null;
87
+ return mapTransaction(transaction);
70
88
  }
71
89
 
72
90
  /**
@@ -92,6 +110,18 @@ export class TransactionUtils {
92
110
  * skip?: number; // (Optional) Number of transactions to skip. Default is 0.
93
111
  * orderDirection?: OrderDirection; // (Optional) Order of the results. Default is DESC.
94
112
  * }
113
+ *
114
+ *
115
+ * ```ts
116
+ * type InternalTransaction = {
117
+ * from: string;
118
+ * to: string;
119
+ * value: bigint;
120
+ * method: string;
121
+ * receiver?: string;
122
+ * escrow?: string;
123
+ * token?: string;
124
+ * };
95
125
  * ```
96
126
  *
97
127
  * ```ts
@@ -101,7 +131,7 @@ export class TransactionUtils {
101
131
  * from: string;
102
132
  * to: string;
103
133
  * timestamp: bigint;
104
- * value: string;
134
+ * value: bigint;
105
135
  * method: string;
106
136
  * receiver?: string;
107
137
  * escrow?: string;
@@ -150,7 +180,7 @@ export class TransactionUtils {
150
180
  }
151
181
 
152
182
  const { transactions } = await gqlFetch<{
153
- transactions: ITransaction[];
183
+ transactions: TransactionData[];
154
184
  }>(getSubgraphUrl(networkData), GET_TRANSACTIONS_QUERY(filter), {
155
185
  fromAddress: filter?.fromAddress,
156
186
  toAddress: filter?.toAddress,
@@ -172,6 +202,34 @@ export class TransactionUtils {
172
202
  return [];
173
203
  }
174
204
 
175
- return transactions;
205
+ return transactions.map((transaction) => mapTransaction(transaction));
176
206
  }
177
207
  }
208
+
209
+ function mapTransaction(t: TransactionData): ITransaction {
210
+ const internalTransactions: InternalTransaction[] = (
211
+ t.internalTransactions || []
212
+ ).map((itx) => ({
213
+ from: itx.from,
214
+ to: itx.to,
215
+ value: BigInt(itx.value),
216
+ method: itx.method,
217
+ receiver: itx.receiver,
218
+ escrow: itx.escrow,
219
+ token: itx.token,
220
+ }));
221
+
222
+ return {
223
+ block: BigInt(t.block),
224
+ txHash: t.txHash,
225
+ from: t.from,
226
+ to: t.to,
227
+ timestamp: Number(t.timestamp) * 1000,
228
+ value: BigInt(t.value),
229
+ method: t.method,
230
+ receiver: t.receiver,
231
+ escrow: t.escrow,
232
+ token: t.token,
233
+ internalTransactions,
234
+ };
235
+ }
package/src/types.ts CHANGED
@@ -143,83 +143,4 @@ export type NetworkData = {
143
143
  oldFactoryAddress: string;
144
144
  };
145
145
 
146
- /**
147
- * Represents the response data for an escrow withdrawal.
148
- */
149
- export type EscrowWithdraw = {
150
- /**
151
- * The hash of the transaction associated with the escrow withdrawal.
152
- */
153
- txHash: string;
154
- /**
155
- * The address of the token used for the withdrawal.
156
- */
157
- tokenAddress: string;
158
- /**
159
- * The amount withdrawn from the escrow.
160
- */
161
- withdrawnAmount: bigint;
162
- };
163
-
164
- /**
165
- * Represents a payout from an escrow.
166
- */
167
- export type Payout = {
168
- /**
169
- * Unique identifier of the payout.
170
- */
171
- id: string;
172
- /**
173
- * The address of the escrow associated with the payout.
174
- */
175
- escrowAddress: string;
176
- /**
177
- * The address of the recipient who received the payout.
178
- */
179
- recipient: string;
180
- /**
181
- * The amount paid to the recipient.
182
- */
183
- amount: bigint;
184
- /**
185
- * The timestamp when the payout was created (in UNIX format).
186
- */
187
- createdAt: number;
188
- };
189
-
190
- /**
191
- * Represents a cancellation refund event.
192
- */
193
- export type CancellationRefund = {
194
- /**
195
- * Unique identifier of the cancellation refund event.
196
- */
197
- id: string;
198
- /**
199
- * The address of the escrow associated with the cancellation refund.
200
- */
201
- escrowAddress: string;
202
- /**
203
- * The address of the receiver who received the refund.
204
- */
205
- receiver: string;
206
- /**
207
- * The amount refunded to the receiver.
208
- */
209
- amount: bigint;
210
- /**
211
- * The block number in which the cancellation refund event occurred.
212
- */
213
-
214
- block: number;
215
- /**
216
- * The timestamp when the cancellation refund event occurred (in UNIX format).
217
- */
218
- timestamp: number;
219
- /**
220
- * The transaction hash of the cancellation refund event.
221
- */
222
- txHash: string;
223
- };
224
-
225
146
  export type TransactionLikeWithNonce = TransactionLike & { nonce: number };
package/src/worker.ts CHANGED
@@ -1,11 +1,12 @@
1
+ import { ethers } from 'ethers';
1
2
  import gqlFetch from 'graphql-request';
2
3
  import { NETWORKS } from './constants';
3
4
  import { ChainId, OrderDirection } from './enums';
4
5
  import { ErrorInvalidAddress, ErrorUnsupportedChainID } from './error';
6
+ import { WorkerData } from './graphql';
5
7
  import { GET_WORKER_QUERY, GET_WORKERS_QUERY } from './graphql/queries/worker';
6
8
  import { IWorker, IWorkersFilter } from './interfaces';
7
9
  import { getSubgraphUrl } from './utils';
8
- import { ethers } from 'ethers';
9
10
 
10
11
  export class WorkerUtils {
11
12
  /**
@@ -37,12 +38,14 @@ export class WorkerUtils {
37
38
  }
38
39
 
39
40
  const { worker } = await gqlFetch<{
40
- worker: IWorker;
41
+ worker: WorkerData | null;
41
42
  }>(getSubgraphUrl(networkData), GET_WORKER_QUERY, {
42
43
  address: address.toLowerCase(),
43
44
  });
44
45
 
45
- return worker || null;
46
+ if (!worker) return null;
47
+
48
+ return mapWorker(worker);
46
49
  }
47
50
 
48
51
  /**
@@ -65,7 +68,7 @@ export class WorkerUtils {
65
68
  * type IWorker = {
66
69
  * id: string;
67
70
  * address: string;
68
- * totalHMTAmountReceived: string;
71
+ * totalHMTAmountReceived: bigint;
69
72
  * payoutCount: number;
70
73
  * };
71
74
  * ```
@@ -102,7 +105,7 @@ export class WorkerUtils {
102
105
  }
103
106
 
104
107
  const { workers } = await gqlFetch<{
105
- workers: IWorker[];
108
+ workers: WorkerData[];
106
109
  }>(getSubgraphUrl(networkData), GET_WORKERS_QUERY(filter), {
107
110
  address: filter?.address?.toLowerCase(),
108
111
  first: first,
@@ -115,6 +118,15 @@ export class WorkerUtils {
115
118
  return [];
116
119
  }
117
120
 
118
- return workers;
121
+ return workers.map((w) => mapWorker(w));
119
122
  }
120
123
  }
124
+
125
+ function mapWorker(w: WorkerData): IWorker {
126
+ return {
127
+ id: w.id,
128
+ address: w.address,
129
+ totalHMTAmountReceived: BigInt(w.totalHMTAmountReceived || 0),
130
+ payoutCount: Number(w.payoutCount || 0),
131
+ };
132
+ }