@human-protocol/sdk 3.0.1 → 3.0.3

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 (40) hide show
  1. package/dist/constants.d.ts +1 -0
  2. package/dist/constants.d.ts.map +1 -1
  3. package/dist/constants.js +15 -14
  4. package/dist/escrow.d.ts +0 -6
  5. package/dist/escrow.d.ts.map +1 -1
  6. package/dist/escrow.js +0 -6
  7. package/dist/graphql/queries/escrow.d.ts.map +1 -1
  8. package/dist/graphql/queries/escrow.js +0 -3
  9. package/dist/graphql/queries/kvstore.d.ts +1 -0
  10. package/dist/graphql/queries/kvstore.d.ts.map +1 -1
  11. package/dist/graphql/queries/kvstore.js +12 -1
  12. package/dist/graphql/queries/operator.js +1 -1
  13. package/dist/graphql/queries/statistics.d.ts +2 -2
  14. package/dist/graphql/queries/statistics.d.ts.map +1 -1
  15. package/dist/graphql/queries/statistics.js +11 -7
  16. package/dist/graphql/types.d.ts +0 -5
  17. package/dist/graphql/types.d.ts.map +1 -1
  18. package/dist/index.d.ts +2 -2
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +2 -1
  21. package/dist/interfaces.d.ts +3 -5
  22. package/dist/interfaces.d.ts.map +1 -1
  23. package/dist/kvstore.d.ts +69 -106
  24. package/dist/kvstore.d.ts.map +1 -1
  25. package/dist/kvstore.js +123 -159
  26. package/dist/statistics.d.ts +72 -66
  27. package/dist/statistics.d.ts.map +1 -1
  28. package/dist/statistics.js +121 -89
  29. package/package.json +1 -1
  30. package/src/constants.ts +15 -14
  31. package/src/escrow.ts +0 -6
  32. package/src/graphql/queries/escrow.ts +0 -3
  33. package/src/graphql/queries/kvstore.ts +11 -0
  34. package/src/graphql/queries/operator.ts +1 -1
  35. package/src/graphql/queries/statistics.ts +13 -9
  36. package/src/graphql/types.ts +0 -5
  37. package/src/index.ts +2 -1
  38. package/src/interfaces.ts +3 -5
  39. package/src/kvstore.ts +145 -158
  40. package/src/statistics.ts +138 -100
package/src/statistics.ts CHANGED
@@ -16,10 +16,12 @@ import {
16
16
  WorkerStatistics,
17
17
  HMTHolderData,
18
18
  HMTHolder,
19
+ DailyHMTData,
19
20
  } from './graphql';
20
- import { IHMTHoldersParams, IStatisticsParams } from './interfaces';
21
+ import { IHMTHoldersParams, IStatisticsFilter } from './interfaces';
21
22
  import { NetworkData } from './types';
22
23
  import { getSubgraphUrl, throwError } from './utils';
24
+ import { OrderDirection } from './enums';
23
25
 
24
26
  /**
25
27
  * ## Introduction
@@ -79,10 +81,12 @@ export class StatisticsClient {
79
81
  * **Input parameters**
80
82
  *
81
83
  * ```ts
82
- * interface IStatisticsParams {
84
+ * interface IStatisticsFilter {
83
85
  * from?: Date;
84
86
  * to?: Date;
85
- * limit?: number;
87
+ * first?: number; // (Optional) Number of transactions per page. Default is 10.
88
+ * skip?: number; // (Optional) Number of transactions to skip. Default is 0.
89
+ * orderDirection?: OrderDirection; // (Optional) Order of the results. Default is ASC.
86
90
  * }
87
91
  * ```
88
92
  *
@@ -103,7 +107,7 @@ export class StatisticsClient {
103
107
  * ```
104
108
  *
105
109
  *
106
- * @param {IStatisticsParams} params Statistics params with duration data
110
+ * @param {IStatisticsFilter} filter Statistics params with duration data
107
111
  * @returns {EscrowStatistics} Escrow statistics data.
108
112
  *
109
113
  *
@@ -122,18 +126,26 @@ export class StatisticsClient {
122
126
  * ```
123
127
  */
124
128
  async getEscrowStatistics(
125
- params: IStatisticsParams = {}
129
+ filter: IStatisticsFilter = {}
126
130
  ): Promise<EscrowStatistics> {
127
131
  try {
132
+ const first =
133
+ filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
134
+ const skip = filter.skip || 0;
135
+ const orderDirection = filter.orderDirection || OrderDirection.ASC;
136
+
128
137
  const { escrowStatistics } = await gqlFetch<{
129
138
  escrowStatistics: EscrowStatisticsData;
130
139
  }>(this.subgraphUrl, GET_ESCROW_STATISTICS_QUERY);
131
140
 
132
141
  const { eventDayDatas } = await gqlFetch<{
133
142
  eventDayDatas: EventDayData[];
134
- }>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
135
- from: params.from ? params.from.getTime() / 1000 : undefined,
136
- to: params.to ? params.to.getTime() / 1000 : undefined,
143
+ }>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(filter), {
144
+ from: filter.from ? filter.from.getTime() / 1000 : undefined,
145
+ to: filter.to ? filter.to.getTime() / 1000 : undefined,
146
+ orderDirection: orderDirection,
147
+ first: first,
148
+ skip: skip,
137
149
  });
138
150
 
139
151
  return {
@@ -159,10 +171,12 @@ export class StatisticsClient {
159
171
  * **Input parameters**
160
172
  *
161
173
  * ```ts
162
- * interface IStatisticsParams {
174
+ * interface IStatisticsFilter {
163
175
  * from?: Date;
164
176
  * to?: Date;
165
- * limit?: number;
177
+ * first?: number; // (Optional) Number of transactions per page. Default is 10.
178
+ * skip?: number; // (Optional) Number of transactions to skip. Default is 0.
179
+ * orderDirection?: OrderDirection; // (Optional) Order of the results. Default is ASC.
166
180
  * }
167
181
  * ```
168
182
  *
@@ -178,7 +192,7 @@ export class StatisticsClient {
178
192
  * ```
179
193
  *
180
194
  *
181
- * @param {IStatisticsParams} params Statistics params with duration data
195
+ * @param {IStatisticsFilter} filter Statistics params with duration data
182
196
  * @returns {WorkerStatistics} Worker statistics data.
183
197
  *
184
198
  *
@@ -197,14 +211,22 @@ export class StatisticsClient {
197
211
  * ```
198
212
  */
199
213
  async getWorkerStatistics(
200
- params: IStatisticsParams = {}
214
+ filter: IStatisticsFilter = {}
201
215
  ): Promise<WorkerStatistics> {
202
216
  try {
217
+ const first =
218
+ filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
219
+ const skip = filter.skip || 0;
220
+ const orderDirection = filter.orderDirection || OrderDirection.ASC;
221
+
203
222
  const { eventDayDatas } = await gqlFetch<{
204
223
  eventDayDatas: EventDayData[];
205
- }>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
206
- from: params.from ? params.from.getTime() / 1000 : undefined,
207
- to: params.to ? params.to.getTime() / 1000 : undefined,
224
+ }>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(filter), {
225
+ from: filter.from ? filter.from.getTime() / 1000 : undefined,
226
+ to: filter.to ? filter.to.getTime() / 1000 : undefined,
227
+ orderDirection: orderDirection,
228
+ first: first,
229
+ skip: skip,
208
230
  });
209
231
 
210
232
  return {
@@ -225,10 +247,12 @@ export class StatisticsClient {
225
247
  * **Input parameters**
226
248
  *
227
249
  * ```ts
228
- * interface IStatisticsParams {
250
+ * interface IStatisticsFilter {
229
251
  * from?: Date;
230
252
  * to?: Date;
231
- * limit?: number;
253
+ * first?: number; // (Optional) Number of transactions per page. Default is 10.
254
+ * skip?: number; // (Optional) Number of transactions to skip. Default is 0.
255
+ * orderDirection?: OrderDirection; // (Optional) Order of the results. Default is ASC.
232
256
  * }
233
257
  * ```
234
258
  *
@@ -246,7 +270,7 @@ export class StatisticsClient {
246
270
  * ```
247
271
  *
248
272
  *
249
- * @param {IStatisticsParams} params Statistics params with duration data
273
+ * @param {IStatisticsFilter} filter Statistics params with duration data
250
274
  * @returns {PaymentStatistics} Payment statistics data.
251
275
  *
252
276
  *
@@ -286,14 +310,22 @@ export class StatisticsClient {
286
310
  * ```
287
311
  */
288
312
  async getPaymentStatistics(
289
- params: IStatisticsParams = {}
313
+ filter: IStatisticsFilter = {}
290
314
  ): Promise<PaymentStatistics> {
291
315
  try {
316
+ const first =
317
+ filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
318
+ const skip = filter.skip || 0;
319
+ const orderDirection = filter.orderDirection || OrderDirection.ASC;
320
+
292
321
  const { eventDayDatas } = await gqlFetch<{
293
322
  eventDayDatas: EventDayData[];
294
- }>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
295
- from: params.from ? params.from.getTime() / 1000 : undefined,
296
- to: params.to ? params.to.getTime() / 1000 : undefined,
323
+ }>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(filter), {
324
+ from: filter.from ? filter.from.getTime() / 1000 : undefined,
325
+ to: filter.to ? filter.to.getTime() / 1000 : undefined,
326
+ orderDirection: orderDirection,
327
+ first: first,
328
+ skip: skip,
297
329
  });
298
330
 
299
331
  return {
@@ -317,39 +349,14 @@ export class StatisticsClient {
317
349
  * This function returns the statistical data of HMToken.
318
350
  *
319
351
  *
320
- * **Input parameters**
321
- *
322
- * ```ts
323
- * interface IStatisticsParams {
324
- * from?: Date;
325
- * to?: Date;
326
- * limit?: number;
327
- * }
328
- * ```
329
- *
330
- * ```ts
331
- * type HMTHolder = {
332
- * address: string;
333
- * balance: BigNumber;
334
- * }
335
- *
336
- * type DailyHMTData = {
337
- * timestamp: Date;
338
- * totalTransactionAmount: BigNumber;
339
- * totalTransactionCount: number;
340
- * };
341
- *
342
352
  * type HMTStatistics = {
343
353
  * totalTransferAmount: BigNumber;
344
354
  * totalTransferCount: BigNumber;
345
355
  * totalHolders: number;
346
- * holders: HMTHolder[];
347
- * dailyHMTData: DailyHMTData[];
348
356
  * };
349
357
  * ```
350
358
  *
351
359
  *
352
- * @param {IStatisticsParams} params Statistics params with duration data
353
360
  * @returns {HMTStatistics} HMToken statistics data.
354
361
  *
355
362
  *
@@ -365,73 +372,21 @@ export class StatisticsClient {
365
372
  * console.log('HMT statistics:', {
366
373
  * ...hmtStatistics,
367
374
  * totalTransferAmount: hmtStatistics.totalTransferAmount.toString(),
368
- * holders: hmtStatistics.holders.map((h) => ({
369
- * ...h,
370
- * balance: h.balance.toString(),
371
- * })),
372
- * dailyHMTData: hmtStatistics.dailyHMTData.map((d) => ({
373
- * ...d,
374
- * totalTransactionAmount: d.totalTransactionAmount.toString(),
375
- * })),
376
- * });
377
- *
378
- * const hmtStatisticsRange = await statisticsClient.getHMTStatistics({
379
- * from: new Date(2023, 4, 8),
380
- * to: new Date(2023, 5, 8),
381
- * });
382
- *
383
- * console.log('HMT statistics from 5/8 - 6/8:', {
384
- * ...hmtStatisticsRange,
385
- * totalTransferAmount: hmtStatisticsRange.totalTransferAmount.toString(),
386
- * holders: hmtStatisticsRange.holders.map((h) => ({
387
- * ...h,
388
- * balance: h.balance.toString(),
389
- * })),
390
- * dailyHMTData: hmtStatisticsRange.dailyHMTData.map((d) => ({
391
- * ...d,
392
- * totalTransactionAmount: d.totalTransactionAmount.toString(),
393
- * })),
394
375
  * });
395
376
  * ```
396
377
  */
397
- async getHMTStatistics(
398
- params: IStatisticsParams = {}
399
- ): Promise<HMTStatistics> {
378
+ async getHMTStatistics(): Promise<HMTStatistics> {
400
379
  try {
401
380
  const { hmtokenStatistics } = await gqlFetch<{
402
381
  hmtokenStatistics: HMTStatisticsData;
403
382
  }>(this.subgraphUrl, GET_HMTOKEN_STATISTICS_QUERY);
404
383
 
405
- const { holders } = await gqlFetch<{
406
- holders: HMTHolderData[];
407
- }>(this.subgraphUrl, GET_HOLDERS_QUERY());
408
-
409
- const { eventDayDatas } = await gqlFetch<{
410
- eventDayDatas: EventDayData[];
411
- }>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
412
- from: params.from ? params.from.getTime() / 1000 : undefined,
413
- to: params.to ? params.to.getTime() / 1000 : undefined,
414
- });
415
-
416
384
  return {
417
385
  totalTransferAmount: ethers.toBigInt(
418
386
  hmtokenStatistics.totalValueTransfered
419
387
  ),
420
388
  totalTransferCount: Number(hmtokenStatistics.totalTransferEventCount),
421
389
  totalHolders: +hmtokenStatistics.holders,
422
- holders: holders.map((holder) => ({
423
- address: holder.address,
424
- balance: ethers.toBigInt(holder.balance),
425
- })),
426
- dailyHMTData: eventDayDatas.map((eventDayData) => ({
427
- timestamp: new Date(+eventDayData.timestamp * 1000),
428
- totalTransactionAmount: ethers.toBigInt(
429
- eventDayData.dailyHMTTransferAmount
430
- ),
431
- totalTransactionCount: +eventDayData.dailyHMTTransferCount,
432
- dailyUniqueSenders: +eventDayData.dailyUniqueSenders,
433
- dailyUniqueReceivers: +eventDayData.dailyUniqueReceivers,
434
- })),
435
390
  };
436
391
  } catch (e: any) {
437
392
  return throwError(e);
@@ -486,4 +441,87 @@ export class StatisticsClient {
486
441
  return throwError(e);
487
442
  }
488
443
  }
444
+
445
+ /**
446
+ * This function returns the statistical data of HMToken day by day.
447
+ *
448
+ *
449
+ * **Input parameters**
450
+ *
451
+ * ```ts
452
+ * interface IStatisticsFilter {
453
+ * from?: Date;
454
+ * to?: Date;
455
+ * first?: number; // (Optional) Number of transactions per page. Default is 10.
456
+ * skip?: number; // (Optional) Number of transactions to skip. Default is 0.
457
+ * orderDirection?: OrderDirection; // (Optional) Order of the results. Default is ASC.
458
+ * }
459
+ * ```
460
+ *
461
+ * ```ts
462
+ * type DailyHMTData = {
463
+ * timestamp: Date;
464
+ * totalTransactionAmount: bigint;
465
+ * totalTransactionCount: number;
466
+ * dailyUniqueSenders: number;
467
+ * dailyUniqueReceivers: number;
468
+ * }
469
+ * ```
470
+ *
471
+ *
472
+ * @param {IStatisticsFilter} filter Statistics params with duration data
473
+ * @returns {DailyHMTData[]} Daily HMToken statistics data.
474
+ *
475
+ *
476
+ * **Code example**
477
+ *
478
+ * ```ts
479
+ * import { StatisticsClient, ChainId, NETWORKS } from '@human-protocol/sdk';
480
+ *
481
+ * const statisticsClient = new StatisticsClient(NETWORKS[ChainId.POLYGON_AMOY]);
482
+ *
483
+ * const dailyHMTStats = await statisticsClient.getHMTStatistics();
484
+ *
485
+ * console.log('Daily HMT statistics:', dailyHMTStats);
486
+ *
487
+ * const hmtStatisticsRange = await statisticsClient.getHMTStatistics({
488
+ * from: new Date(2023, 4, 8),
489
+ * to: new Date(2023, 5, 8),
490
+ * });
491
+ *
492
+ * console.log('HMT statistics from 5/8 - 6/8:', hmtStatisticsRange);
493
+ * ```
494
+ */
495
+ async getHMTDailyData(
496
+ filter: IStatisticsFilter = {}
497
+ ): Promise<DailyHMTData[]> {
498
+ try {
499
+ const first =
500
+ filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
501
+ const skip = filter.skip || 0;
502
+ const orderDirection = filter.orderDirection || OrderDirection.ASC;
503
+
504
+ const { eventDayDatas } = await gqlFetch<{
505
+ eventDayDatas: EventDayData[];
506
+ }>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(filter), {
507
+ from: filter.from ? filter.from.getTime() / 1000 : undefined,
508
+ to: filter.to ? filter.to.getTime() / 1000 : undefined,
509
+ orderDirection: orderDirection,
510
+ first: first,
511
+ skip: skip,
512
+ });
513
+
514
+ return eventDayDatas.map((eventDayData) => ({
515
+ timestamp: new Date(+eventDayData.timestamp * 1000),
516
+ totalTransactionAmount: ethers.toBigInt(
517
+ eventDayData.dailyHMTTransferAmount
518
+ ),
519
+ totalTransactionCount: +eventDayData.dailyHMTTransferCount,
520
+ dailyUniqueSenders: +eventDayData.dailyUniqueSenders,
521
+ dailyUniqueReceivers: +eventDayData.dailyUniqueReceivers,
522
+ }));
523
+ } catch (e: any) {
524
+ return throwError(e);
525
+ }
526
+ }
489
527
  }