@human-protocol/sdk 6.0.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 +34 -0
- package/dist/base.d.ts +4 -2
- package/dist/base.d.ts.map +1 -1
- package/dist/base.js +14 -0
- package/dist/constants.d.ts +0 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +27 -16
- 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} +16 -196
- package/dist/escrow/escrow_client.d.ts.map +1 -0
- package/dist/{escrow.js → escrow/escrow_client.js} +50 -430
- 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 +2 -7
- 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} +7 -112
- package/dist/kvstore/kvstore_client.d.ts.map +1 -0
- package/dist/{kvstore.js → kvstore/kvstore_client.js} +25 -187
- 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} +10 -71
- package/dist/staking/staking_client.d.ts.map +1 -0
- package/dist/{staking.js → staking/staking_client.js} +11 -141
- 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} +5 -5
- package/dist/transaction/transaction_utils.d.ts.map +1 -0
- package/dist/{transaction.js → transaction/transaction_utils.js} +9 -9
- package/dist/types.d.ts +22 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +16 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +72 -6
- 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 +5 -5
- package/src/base.ts +38 -2
- package/src/constants.ts +38 -16
- 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} +151 -659
- 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 +2 -7
- package/src/kvstore/index.ts +2 -0
- package/src/kvstore/kvstore_client.ts +291 -0
- 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} +49 -199
- 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} +11 -11
- package/src/types.ts +24 -1
- package/src/utils.ts +82 -9
- 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/escrow.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
- package/src/kvstore.ts +0 -497
|
@@ -6,11 +6,11 @@ import {
|
|
|
6
6
|
Staking,
|
|
7
7
|
Staking__factory,
|
|
8
8
|
} from '@human-protocol/core/typechain-types';
|
|
9
|
-
import { ContractRunner,
|
|
10
|
-
import { BaseEthersClient } from '
|
|
11
|
-
import { NETWORKS } from '
|
|
12
|
-
import { requiresSigner } from '
|
|
13
|
-
import { ChainId
|
|
9
|
+
import { ContractRunner, ethers } from 'ethers';
|
|
10
|
+
import { BaseEthersClient } from '../base';
|
|
11
|
+
import { NETWORKS } from '../constants';
|
|
12
|
+
import { requiresSigner } from '../decorators';
|
|
13
|
+
import { ChainId } from '../enums';
|
|
14
14
|
import {
|
|
15
15
|
ErrorEscrowAddressIsNotProvidedByFactory,
|
|
16
16
|
ErrorInvalidEscrowAddressProvided,
|
|
@@ -19,22 +19,11 @@ import {
|
|
|
19
19
|
ErrorInvalidStakingValueSign,
|
|
20
20
|
ErrorInvalidStakingValueType,
|
|
21
21
|
ErrorProviderDoesNotExist,
|
|
22
|
-
ErrorStakerNotFound,
|
|
23
22
|
ErrorUnsupportedChainID,
|
|
24
|
-
} from '
|
|
25
|
-
import {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
StakerInfo,
|
|
29
|
-
SubgraphOptions,
|
|
30
|
-
} from './interfaces';
|
|
31
|
-
import { StakerData } from './graphql';
|
|
32
|
-
import { NetworkData } from './types';
|
|
33
|
-
import { getSubgraphUrl, customGqlFetch, throwError } from './utils';
|
|
34
|
-
import {
|
|
35
|
-
GET_STAKER_BY_ADDRESS_QUERY,
|
|
36
|
-
GET_STAKERS_QUERY,
|
|
37
|
-
} from './graphql/queries/staking';
|
|
23
|
+
} from '../error';
|
|
24
|
+
import { StakerInfo } from '../interfaces';
|
|
25
|
+
import { NetworkData, TransactionOverrides } from '../types';
|
|
26
|
+
import { throwError } from '../utils';
|
|
38
27
|
|
|
39
28
|
/**
|
|
40
29
|
* Client for staking actions on HUMAN Protocol.
|
|
@@ -194,7 +183,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
194
183
|
@requiresSigner
|
|
195
184
|
public async approveStake(
|
|
196
185
|
amount: bigint,
|
|
197
|
-
txOptions:
|
|
186
|
+
txOptions: TransactionOverrides = {}
|
|
198
187
|
): Promise<void> {
|
|
199
188
|
if (typeof amount !== 'bigint') {
|
|
200
189
|
throw ErrorInvalidStakingValueType;
|
|
@@ -205,13 +194,15 @@ export class StakingClient extends BaseEthersClient {
|
|
|
205
194
|
}
|
|
206
195
|
|
|
207
196
|
try {
|
|
208
|
-
await (
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
197
|
+
await this.sendTxAndWait(
|
|
198
|
+
async (overrides) =>
|
|
199
|
+
this.tokenContract.approve(
|
|
200
|
+
await this.stakingContract.getAddress(),
|
|
201
|
+
amount,
|
|
202
|
+
overrides
|
|
203
|
+
),
|
|
204
|
+
txOptions
|
|
205
|
+
);
|
|
215
206
|
return;
|
|
216
207
|
} catch (e) {
|
|
217
208
|
return throwError(e);
|
|
@@ -240,7 +231,10 @@ export class StakingClient extends BaseEthersClient {
|
|
|
240
231
|
* ```
|
|
241
232
|
*/
|
|
242
233
|
@requiresSigner
|
|
243
|
-
public async stake(
|
|
234
|
+
public async stake(
|
|
235
|
+
amount: bigint,
|
|
236
|
+
txOptions: TransactionOverrides = {}
|
|
237
|
+
): Promise<void> {
|
|
244
238
|
if (typeof amount !== 'bigint') {
|
|
245
239
|
throw ErrorInvalidStakingValueType;
|
|
246
240
|
}
|
|
@@ -250,7 +244,10 @@ export class StakingClient extends BaseEthersClient {
|
|
|
250
244
|
}
|
|
251
245
|
|
|
252
246
|
try {
|
|
253
|
-
await
|
|
247
|
+
await this.sendTxAndWait(
|
|
248
|
+
(overrides) => this.stakingContract.stake(amount, overrides),
|
|
249
|
+
txOptions
|
|
250
|
+
);
|
|
254
251
|
return;
|
|
255
252
|
} catch (e) {
|
|
256
253
|
return throwError(e);
|
|
@@ -280,7 +277,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
280
277
|
@requiresSigner
|
|
281
278
|
public async unstake(
|
|
282
279
|
amount: bigint,
|
|
283
|
-
txOptions:
|
|
280
|
+
txOptions: TransactionOverrides = {}
|
|
284
281
|
): Promise<void> {
|
|
285
282
|
if (typeof amount !== 'bigint') {
|
|
286
283
|
throw ErrorInvalidStakingValueType;
|
|
@@ -291,7 +288,10 @@ export class StakingClient extends BaseEthersClient {
|
|
|
291
288
|
}
|
|
292
289
|
|
|
293
290
|
try {
|
|
294
|
-
await
|
|
291
|
+
await this.sendTxAndWait(
|
|
292
|
+
(overrides) => this.stakingContract.unstake(amount, overrides),
|
|
293
|
+
txOptions
|
|
294
|
+
);
|
|
295
295
|
return;
|
|
296
296
|
} catch (e) {
|
|
297
297
|
return throwError(e);
|
|
@@ -312,9 +312,12 @@ export class StakingClient extends BaseEthersClient {
|
|
|
312
312
|
* ```
|
|
313
313
|
*/
|
|
314
314
|
@requiresSigner
|
|
315
|
-
public async withdraw(txOptions:
|
|
315
|
+
public async withdraw(txOptions: TransactionOverrides = {}): Promise<void> {
|
|
316
316
|
try {
|
|
317
|
-
await
|
|
317
|
+
await this.sendTxAndWait(
|
|
318
|
+
(overrides) => this.stakingContract.withdraw(overrides),
|
|
319
|
+
txOptions
|
|
320
|
+
);
|
|
318
321
|
return;
|
|
319
322
|
} catch (e) {
|
|
320
323
|
return throwError(e);
|
|
@@ -356,7 +359,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
356
359
|
staker: string,
|
|
357
360
|
escrowAddress: string,
|
|
358
361
|
amount: bigint,
|
|
359
|
-
txOptions:
|
|
362
|
+
txOptions: TransactionOverrides = {}
|
|
360
363
|
): Promise<void> {
|
|
361
364
|
if (typeof amount !== 'bigint') {
|
|
362
365
|
throw ErrorInvalidStakingValueType;
|
|
@@ -377,15 +380,17 @@ export class StakingClient extends BaseEthersClient {
|
|
|
377
380
|
await this.checkValidEscrow(escrowAddress);
|
|
378
381
|
|
|
379
382
|
try {
|
|
380
|
-
await (
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
383
|
+
await this.sendTxAndWait(
|
|
384
|
+
(overrides) =>
|
|
385
|
+
this.stakingContract.slash(
|
|
386
|
+
slasher,
|
|
387
|
+
staker,
|
|
388
|
+
escrowAddress,
|
|
389
|
+
amount,
|
|
390
|
+
overrides
|
|
391
|
+
),
|
|
392
|
+
txOptions
|
|
393
|
+
);
|
|
389
394
|
|
|
390
395
|
return;
|
|
391
396
|
} catch (e) {
|
|
@@ -440,158 +445,3 @@ export class StakingClient extends BaseEthersClient {
|
|
|
440
445
|
}
|
|
441
446
|
}
|
|
442
447
|
}
|
|
443
|
-
|
|
444
|
-
/**
|
|
445
|
-
* Utility helpers for Staking-related queries.
|
|
446
|
-
*
|
|
447
|
-
* @example
|
|
448
|
-
* ```ts
|
|
449
|
-
* import { StakingUtils, ChainId } from '@human-protocol/sdk';
|
|
450
|
-
*
|
|
451
|
-
* const staker = await StakingUtils.getStaker(
|
|
452
|
-
* ChainId.POLYGON_AMOY,
|
|
453
|
-
* '0xYourStakerAddress'
|
|
454
|
-
* );
|
|
455
|
-
* console.log('Staked amount:', staker.stakedAmount);
|
|
456
|
-
* ```
|
|
457
|
-
*/
|
|
458
|
-
export class StakingUtils {
|
|
459
|
-
/**
|
|
460
|
-
* Gets staking info for a staker from the subgraph.
|
|
461
|
-
*
|
|
462
|
-
* @param chainId - Network in which the staking contract is deployed
|
|
463
|
-
* @param stakerAddress - Address of the staker
|
|
464
|
-
* @param options - Optional configuration for subgraph requests.
|
|
465
|
-
* @returns Staker info from subgraph
|
|
466
|
-
* @throws ErrorInvalidStakerAddressProvided If the staker address is invalid
|
|
467
|
-
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
468
|
-
* @throws ErrorStakerNotFound If the staker is not found
|
|
469
|
-
*
|
|
470
|
-
* @example
|
|
471
|
-
* ```ts
|
|
472
|
-
* import { StakingUtils, ChainId } from '@human-protocol/sdk';
|
|
473
|
-
*
|
|
474
|
-
* const staker = await StakingUtils.getStaker(
|
|
475
|
-
* ChainId.POLYGON_AMOY,
|
|
476
|
-
* '0xYourStakerAddress'
|
|
477
|
-
* );
|
|
478
|
-
* console.log('Staked amount:', staker.stakedAmount);
|
|
479
|
-
* ```
|
|
480
|
-
*/
|
|
481
|
-
public static async getStaker(
|
|
482
|
-
chainId: ChainId,
|
|
483
|
-
stakerAddress: string,
|
|
484
|
-
options?: SubgraphOptions
|
|
485
|
-
): Promise<IStaker> {
|
|
486
|
-
if (!ethers.isAddress(stakerAddress)) {
|
|
487
|
-
throw ErrorInvalidStakerAddressProvided;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
const networkData: NetworkData | undefined = NETWORKS[chainId];
|
|
491
|
-
if (!networkData) {
|
|
492
|
-
throw ErrorUnsupportedChainID;
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
const { staker } = await customGqlFetch<{ staker: StakerData }>(
|
|
496
|
-
getSubgraphUrl(networkData),
|
|
497
|
-
GET_STAKER_BY_ADDRESS_QUERY,
|
|
498
|
-
{ id: stakerAddress.toLowerCase() },
|
|
499
|
-
options
|
|
500
|
-
);
|
|
501
|
-
|
|
502
|
-
if (!staker) {
|
|
503
|
-
throw ErrorStakerNotFound;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
return mapStaker(staker);
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
/**
|
|
510
|
-
* Gets all stakers from the subgraph with filters, pagination and ordering.
|
|
511
|
-
*
|
|
512
|
-
* @param filter - Stakers filter with pagination and ordering
|
|
513
|
-
* @param options - Optional configuration for subgraph requests.
|
|
514
|
-
* @returns Array of stakers
|
|
515
|
-
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
516
|
-
*
|
|
517
|
-
* @example
|
|
518
|
-
* ```ts
|
|
519
|
-
* import { ChainId } from '@human-protocol/sdk';
|
|
520
|
-
*
|
|
521
|
-
* const filter = {
|
|
522
|
-
* chainId: ChainId.POLYGON_AMOY,
|
|
523
|
-
* minStakedAmount: '1000000000000000000', // 1 token in WEI
|
|
524
|
-
* };
|
|
525
|
-
* const stakers = await StakingUtils.getStakers(filter);
|
|
526
|
-
* console.log('Stakers:', stakers.length);
|
|
527
|
-
* ```
|
|
528
|
-
*/
|
|
529
|
-
public static async getStakers(
|
|
530
|
-
filter: IStakersFilter,
|
|
531
|
-
options?: SubgraphOptions
|
|
532
|
-
): Promise<IStaker[]> {
|
|
533
|
-
const first =
|
|
534
|
-
filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
|
|
535
|
-
const skip = filter.skip || 0;
|
|
536
|
-
const orderDirection = filter.orderDirection || OrderDirection.DESC;
|
|
537
|
-
const orderBy = filter.orderBy || 'lastDepositTimestamp';
|
|
538
|
-
|
|
539
|
-
const networkData = NETWORKS[filter.chainId];
|
|
540
|
-
if (!networkData) {
|
|
541
|
-
throw ErrorUnsupportedChainID;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
const { stakers } = await customGqlFetch<{ stakers: StakerData[] }>(
|
|
545
|
-
getSubgraphUrl(networkData),
|
|
546
|
-
GET_STAKERS_QUERY(filter),
|
|
547
|
-
{
|
|
548
|
-
minStakedAmount: filter.minStakedAmount
|
|
549
|
-
? filter.minStakedAmount
|
|
550
|
-
: undefined,
|
|
551
|
-
maxStakedAmount: filter.maxStakedAmount
|
|
552
|
-
? filter.maxStakedAmount
|
|
553
|
-
: undefined,
|
|
554
|
-
minLockedAmount: filter.minLockedAmount
|
|
555
|
-
? filter.minLockedAmount
|
|
556
|
-
: undefined,
|
|
557
|
-
maxLockedAmount: filter.maxLockedAmount
|
|
558
|
-
? filter.maxLockedAmount
|
|
559
|
-
: undefined,
|
|
560
|
-
minWithdrawnAmount: filter.minWithdrawnAmount
|
|
561
|
-
? filter.minWithdrawnAmount
|
|
562
|
-
: undefined,
|
|
563
|
-
maxWithdrawnAmount: filter.maxWithdrawnAmount
|
|
564
|
-
? filter.maxWithdrawnAmount
|
|
565
|
-
: undefined,
|
|
566
|
-
minSlashedAmount: filter.minSlashedAmount
|
|
567
|
-
? filter.minSlashedAmount
|
|
568
|
-
: undefined,
|
|
569
|
-
maxSlashedAmount: filter.maxSlashedAmount
|
|
570
|
-
? filter.maxSlashedAmount
|
|
571
|
-
: undefined,
|
|
572
|
-
orderBy: orderBy,
|
|
573
|
-
orderDirection: orderDirection,
|
|
574
|
-
first: first,
|
|
575
|
-
skip: skip,
|
|
576
|
-
},
|
|
577
|
-
options
|
|
578
|
-
);
|
|
579
|
-
if (!stakers) {
|
|
580
|
-
return [];
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
return stakers.map((s) => mapStaker(s));
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
function mapStaker(s: StakerData): IStaker {
|
|
588
|
-
return {
|
|
589
|
-
address: s.address,
|
|
590
|
-
stakedAmount: BigInt(s.stakedAmount),
|
|
591
|
-
lockedAmount: BigInt(s.lockedAmount),
|
|
592
|
-
withdrawableAmount: BigInt(s.withdrawnAmount),
|
|
593
|
-
slashedAmount: BigInt(s.slashedAmount),
|
|
594
|
-
lockedUntil: Number(s.lockedUntilTimestamp) * 1000,
|
|
595
|
-
lastDepositTimestamp: Number(s.lastDepositTimestamp) * 1000,
|
|
596
|
-
};
|
|
597
|
-
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { NETWORKS } from '../constants';
|
|
3
|
+
import { ChainId, OrderDirection } from '../enums';
|
|
4
|
+
import {
|
|
5
|
+
ErrorInvalidStakerAddressProvided,
|
|
6
|
+
ErrorStakerNotFound,
|
|
7
|
+
ErrorUnsupportedChainID,
|
|
8
|
+
} from '../error';
|
|
9
|
+
import { StakerData } from '../graphql';
|
|
10
|
+
import {
|
|
11
|
+
GET_STAKER_BY_ADDRESS_QUERY,
|
|
12
|
+
GET_STAKERS_QUERY,
|
|
13
|
+
} from '../graphql/queries/staking';
|
|
14
|
+
import { IStaker, IStakersFilter, SubgraphOptions } from '../interfaces';
|
|
15
|
+
import { NetworkData } from '../types';
|
|
16
|
+
import { customGqlFetch, getSubgraphUrl } from '../utils';
|
|
17
|
+
/**
|
|
18
|
+
* Utility helpers for Staking-related queries.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { StakingUtils, ChainId } from '@human-protocol/sdk';
|
|
23
|
+
*
|
|
24
|
+
* const staker = await StakingUtils.getStaker(
|
|
25
|
+
* ChainId.POLYGON_AMOY,
|
|
26
|
+
* '0xYourStakerAddress'
|
|
27
|
+
* );
|
|
28
|
+
* console.log('Staked amount:', staker.stakedAmount);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export class StakingUtils {
|
|
32
|
+
/**
|
|
33
|
+
* Gets staking info for a staker from the subgraph.
|
|
34
|
+
*
|
|
35
|
+
* @param chainId - Network in which the staking contract is deployed
|
|
36
|
+
* @param stakerAddress - Address of the staker
|
|
37
|
+
* @param options - Optional configuration for subgraph requests.
|
|
38
|
+
* @returns Staker info from subgraph
|
|
39
|
+
* @throws ErrorInvalidStakerAddressProvided If the staker address is invalid
|
|
40
|
+
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
41
|
+
* @throws ErrorStakerNotFound If the staker is not found
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { StakingUtils, ChainId } from '@human-protocol/sdk';
|
|
46
|
+
*
|
|
47
|
+
* const staker = await StakingUtils.getStaker(
|
|
48
|
+
* ChainId.POLYGON_AMOY,
|
|
49
|
+
* '0xYourStakerAddress'
|
|
50
|
+
* );
|
|
51
|
+
* console.log('Staked amount:', staker.stakedAmount);
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
public static async getStaker(
|
|
55
|
+
chainId: ChainId,
|
|
56
|
+
stakerAddress: string,
|
|
57
|
+
options?: SubgraphOptions
|
|
58
|
+
): Promise<IStaker> {
|
|
59
|
+
if (!ethers.isAddress(stakerAddress)) {
|
|
60
|
+
throw ErrorInvalidStakerAddressProvided;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const networkData: NetworkData | undefined = NETWORKS[chainId];
|
|
64
|
+
if (!networkData) {
|
|
65
|
+
throw ErrorUnsupportedChainID;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const { staker } = await customGqlFetch<{ staker: StakerData }>(
|
|
69
|
+
getSubgraphUrl(networkData),
|
|
70
|
+
GET_STAKER_BY_ADDRESS_QUERY,
|
|
71
|
+
{ id: stakerAddress.toLowerCase() },
|
|
72
|
+
options
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
if (!staker) {
|
|
76
|
+
throw ErrorStakerNotFound;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return mapStaker(staker);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Gets all stakers from the subgraph with filters, pagination and ordering.
|
|
84
|
+
*
|
|
85
|
+
* @param filter - Stakers filter with pagination and ordering
|
|
86
|
+
* @param options - Optional configuration for subgraph requests.
|
|
87
|
+
* @returns Array of stakers
|
|
88
|
+
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { ChainId } from '@human-protocol/sdk';
|
|
93
|
+
*
|
|
94
|
+
* const filter = {
|
|
95
|
+
* chainId: ChainId.POLYGON_AMOY,
|
|
96
|
+
* minStakedAmount: '1000000000000000000', // 1 token in WEI
|
|
97
|
+
* };
|
|
98
|
+
* const stakers = await StakingUtils.getStakers(filter);
|
|
99
|
+
* console.log('Stakers:', stakers.length);
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
public static async getStakers(
|
|
103
|
+
filter: IStakersFilter,
|
|
104
|
+
options?: SubgraphOptions
|
|
105
|
+
): Promise<IStaker[]> {
|
|
106
|
+
const first =
|
|
107
|
+
filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
|
|
108
|
+
const skip = filter.skip || 0;
|
|
109
|
+
const orderDirection = filter.orderDirection || OrderDirection.DESC;
|
|
110
|
+
const orderBy = filter.orderBy || 'lastDepositTimestamp';
|
|
111
|
+
|
|
112
|
+
const networkData = NETWORKS[filter.chainId];
|
|
113
|
+
if (!networkData) {
|
|
114
|
+
throw ErrorUnsupportedChainID;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const { stakers } = await customGqlFetch<{ stakers: StakerData[] }>(
|
|
118
|
+
getSubgraphUrl(networkData),
|
|
119
|
+
GET_STAKERS_QUERY(filter),
|
|
120
|
+
{
|
|
121
|
+
minStakedAmount: filter.minStakedAmount
|
|
122
|
+
? filter.minStakedAmount
|
|
123
|
+
: undefined,
|
|
124
|
+
maxStakedAmount: filter.maxStakedAmount
|
|
125
|
+
? filter.maxStakedAmount
|
|
126
|
+
: undefined,
|
|
127
|
+
minLockedAmount: filter.minLockedAmount
|
|
128
|
+
? filter.minLockedAmount
|
|
129
|
+
: undefined,
|
|
130
|
+
maxLockedAmount: filter.maxLockedAmount
|
|
131
|
+
? filter.maxLockedAmount
|
|
132
|
+
: undefined,
|
|
133
|
+
minWithdrawnAmount: filter.minWithdrawnAmount
|
|
134
|
+
? filter.minWithdrawnAmount
|
|
135
|
+
: undefined,
|
|
136
|
+
maxWithdrawnAmount: filter.maxWithdrawnAmount
|
|
137
|
+
? filter.maxWithdrawnAmount
|
|
138
|
+
: undefined,
|
|
139
|
+
minSlashedAmount: filter.minSlashedAmount
|
|
140
|
+
? filter.minSlashedAmount
|
|
141
|
+
: undefined,
|
|
142
|
+
maxSlashedAmount: filter.maxSlashedAmount
|
|
143
|
+
? filter.maxSlashedAmount
|
|
144
|
+
: undefined,
|
|
145
|
+
orderBy: orderBy,
|
|
146
|
+
orderDirection: orderDirection,
|
|
147
|
+
first: first,
|
|
148
|
+
skip: skip,
|
|
149
|
+
},
|
|
150
|
+
options
|
|
151
|
+
);
|
|
152
|
+
if (!stakers) {
|
|
153
|
+
return [];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return stakers.map((s) => mapStaker(s));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function mapStaker(s: StakerData): IStaker {
|
|
161
|
+
return {
|
|
162
|
+
address: s.address,
|
|
163
|
+
stakedAmount: BigInt(s.stakedAmount),
|
|
164
|
+
lockedAmount: BigInt(s.lockedAmount),
|
|
165
|
+
withdrawableAmount: BigInt(s.withdrawnAmount),
|
|
166
|
+
slashedAmount: BigInt(s.slashedAmount),
|
|
167
|
+
lockedUntil: Number(s.lockedUntilTimestamp) * 1000,
|
|
168
|
+
lastDepositTimestamp: Number(s.lastDepositTimestamp) * 1000,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { StatisticsUtils } from './statistics_utils';
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { OrderDirection } from '
|
|
2
|
+
import { OrderDirection } from '../enums';
|
|
3
3
|
import {
|
|
4
4
|
EscrowStatisticsData,
|
|
5
5
|
EventDayData,
|
|
6
|
+
HMTEventDayData,
|
|
6
7
|
GET_ESCROW_STATISTICS_QUERY,
|
|
7
8
|
GET_EVENT_DAY_DATA_QUERY,
|
|
9
|
+
GET_HMT_EVENT_DAY_DATA_QUERY,
|
|
8
10
|
GET_HMTOKEN_STATISTICS_QUERY,
|
|
9
11
|
GET_HOLDERS_QUERY,
|
|
10
12
|
HMTHolderData,
|
|
11
13
|
HMTStatisticsData,
|
|
12
|
-
} from '
|
|
14
|
+
} from '../graphql';
|
|
13
15
|
import {
|
|
14
16
|
IDailyHMT,
|
|
15
17
|
IEscrowStatistics,
|
|
@@ -20,14 +22,15 @@ import {
|
|
|
20
22
|
IStatisticsFilter,
|
|
21
23
|
IWorkerStatistics,
|
|
22
24
|
SubgraphOptions,
|
|
23
|
-
} from '
|
|
24
|
-
import { NetworkData } from '
|
|
25
|
+
} from '../interfaces';
|
|
26
|
+
import { NetworkData } from '../types';
|
|
25
27
|
import {
|
|
28
|
+
getHMTSubgraphUrl,
|
|
26
29
|
getSubgraphUrl,
|
|
27
30
|
getUnixTimestamp,
|
|
28
31
|
customGqlFetch,
|
|
29
32
|
throwError,
|
|
30
|
-
} from '
|
|
33
|
+
} from '../utils';
|
|
31
34
|
|
|
32
35
|
/**
|
|
33
36
|
* Utility class for statistics-related queries.
|
|
@@ -253,9 +256,7 @@ export class StatisticsUtils {
|
|
|
253
256
|
* ```ts
|
|
254
257
|
* interface IDailyPayment {
|
|
255
258
|
* timestamp: number;
|
|
256
|
-
* totalAmountPaid: bigint;
|
|
257
259
|
* totalCount: number;
|
|
258
|
-
* averageAmountPerWorker: bigint;
|
|
259
260
|
* };
|
|
260
261
|
*
|
|
261
262
|
* interface IPaymentStatistics {
|
|
@@ -274,14 +275,7 @@ export class StatisticsUtils {
|
|
|
274
275
|
*
|
|
275
276
|
* const networkData = NETWORKS[ChainId.POLYGON_AMOY];
|
|
276
277
|
* const paymentStats = await StatisticsUtils.getPaymentStatistics(networkData);
|
|
277
|
-
* console.log(
|
|
278
|
-
* 'Payment statistics:',
|
|
279
|
-
* paymentStats.dailyPaymentsData.map((p) => ({
|
|
280
|
-
* ...p,
|
|
281
|
-
* totalAmountPaid: p.totalAmountPaid.toString(),
|
|
282
|
-
* averageAmountPerWorker: p.averageAmountPerWorker.toString(),
|
|
283
|
-
* }))
|
|
284
|
-
* );
|
|
278
|
+
* console.log('Payment statistics:', paymentStats.dailyPaymentsData);
|
|
285
279
|
*
|
|
286
280
|
* const paymentStatsRange = await StatisticsUtils.getPaymentStatistics(
|
|
287
281
|
* networkData,
|
|
@@ -323,13 +317,7 @@ export class StatisticsUtils {
|
|
|
323
317
|
return {
|
|
324
318
|
dailyPaymentsData: eventDayDatas.map((eventDayData) => ({
|
|
325
319
|
timestamp: +eventDayData.timestamp * 1000,
|
|
326
|
-
totalAmountPaid: BigInt(eventDayData.dailyHMTPayoutAmount),
|
|
327
320
|
totalCount: +eventDayData.dailyPayoutCount,
|
|
328
|
-
averageAmountPerWorker:
|
|
329
|
-
eventDayData.dailyWorkerCount === '0'
|
|
330
|
-
? BigInt(0)
|
|
331
|
-
: BigInt(eventDayData.dailyHMTPayoutAmount) /
|
|
332
|
-
BigInt(eventDayData.dailyWorkerCount),
|
|
333
321
|
})),
|
|
334
322
|
};
|
|
335
323
|
} catch (e: any) {
|
|
@@ -369,7 +357,7 @@ export class StatisticsUtils {
|
|
|
369
357
|
options?: SubgraphOptions
|
|
370
358
|
): Promise<IHMTStatistics> {
|
|
371
359
|
try {
|
|
372
|
-
const subgraphUrl =
|
|
360
|
+
const subgraphUrl = getHMTSubgraphUrl(networkData);
|
|
373
361
|
const { hmtokenStatistics } = await customGqlFetch<{
|
|
374
362
|
hmtokenStatistics: HMTStatisticsData;
|
|
375
363
|
}>(subgraphUrl, GET_HMTOKEN_STATISTICS_QUERY, options);
|
|
@@ -412,7 +400,7 @@ export class StatisticsUtils {
|
|
|
412
400
|
options?: SubgraphOptions
|
|
413
401
|
): Promise<IHMTHolder[]> {
|
|
414
402
|
try {
|
|
415
|
-
const subgraphUrl =
|
|
403
|
+
const subgraphUrl = getHMTSubgraphUrl(networkData);
|
|
416
404
|
const { address, orderDirection } = params;
|
|
417
405
|
const query = GET_HOLDERS_QUERY(address);
|
|
418
406
|
|
|
@@ -490,17 +478,17 @@ export class StatisticsUtils {
|
|
|
490
478
|
options?: SubgraphOptions
|
|
491
479
|
): Promise<IDailyHMT[]> {
|
|
492
480
|
try {
|
|
493
|
-
const subgraphUrl =
|
|
481
|
+
const subgraphUrl = getHMTSubgraphUrl(networkData);
|
|
494
482
|
const first =
|
|
495
483
|
filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
|
|
496
484
|
const skip = filter.skip || 0;
|
|
497
485
|
const orderDirection = filter.orderDirection || OrderDirection.ASC;
|
|
498
486
|
|
|
499
487
|
const { eventDayDatas } = await customGqlFetch<{
|
|
500
|
-
eventDayDatas:
|
|
488
|
+
eventDayDatas: HMTEventDayData[];
|
|
501
489
|
}>(
|
|
502
490
|
subgraphUrl,
|
|
503
|
-
|
|
491
|
+
GET_HMT_EVENT_DAY_DATA_QUERY(filter),
|
|
504
492
|
{
|
|
505
493
|
from: filter.from ? getUnixTimestamp(filter.from) : undefined,
|
|
506
494
|
to: filter.to ? getUnixTimestamp(filter.to) : undefined,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { TransactionUtils } from './transaction_utils';
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import { ethers } from 'ethers';
|
|
2
|
-
import { NETWORKS } from '
|
|
3
|
-
import { ChainId, OrderDirection } from '
|
|
2
|
+
import { NETWORKS } from '../constants';
|
|
3
|
+
import { ChainId, OrderDirection } from '../enums';
|
|
4
4
|
import {
|
|
5
5
|
ErrorCannotUseDateAndBlockSimultaneously,
|
|
6
6
|
ErrorInvalidHashProvided,
|
|
7
7
|
ErrorUnsupportedChainID,
|
|
8
|
-
} from '
|
|
9
|
-
import { TransactionData } from '
|
|
8
|
+
} from '../error';
|
|
9
|
+
import { TransactionData } from '../graphql';
|
|
10
10
|
import {
|
|
11
11
|
GET_TRANSACTION_QUERY,
|
|
12
12
|
GET_TRANSACTIONS_QUERY,
|
|
13
|
-
} from '
|
|
13
|
+
} from '../graphql/queries/transaction';
|
|
14
14
|
import {
|
|
15
15
|
InternalTransaction,
|
|
16
16
|
ITransaction,
|
|
17
17
|
ITransactionsFilter,
|
|
18
18
|
SubgraphOptions,
|
|
19
|
-
} from '
|
|
20
|
-
import { getSubgraphUrl, getUnixTimestamp, customGqlFetch } from '
|
|
19
|
+
} from '../interfaces';
|
|
20
|
+
import { getSubgraphUrl, getUnixTimestamp, customGqlFetch } from '../utils';
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Utility class for transaction-related queries.
|
|
@@ -129,8 +129,8 @@ export class TransactionUtils {
|
|
|
129
129
|
* token?: string; // (Optional) The token address to filter transactions.
|
|
130
130
|
* startDate?: Date; // (Optional) The start date to filter transactions (inclusive).
|
|
131
131
|
* endDate?: Date; // (Optional) The end date to filter transactions (inclusive).
|
|
132
|
-
* startBlock?: number; // (Optional) The start block
|
|
133
|
-
* endBlock?: number; // (Optional) The end block
|
|
132
|
+
* startBlock?: bigint | number; // (Optional) The start block to filter transactions (inclusive).
|
|
133
|
+
* endBlock?: bigint | number; // (Optional) The end block to filter transactions (inclusive).
|
|
134
134
|
* first?: number; // (Optional) Number of transactions per page. Default is 10.
|
|
135
135
|
* skip?: number; // (Optional) Number of transactions to skip. Default is 0.
|
|
136
136
|
* orderDirection?: OrderDirection; // (Optional) Order of the results. Default is DESC.
|
|
@@ -220,8 +220,8 @@ export class TransactionUtils {
|
|
|
220
220
|
? getUnixTimestamp(filter?.startDate)
|
|
221
221
|
: undefined,
|
|
222
222
|
endDate: filter.endDate ? getUnixTimestamp(filter.endDate) : undefined,
|
|
223
|
-
startBlock: filter.startBlock ? filter.startBlock : undefined,
|
|
224
|
-
endBlock: filter.endBlock ? filter.endBlock : undefined,
|
|
223
|
+
startBlock: filter.startBlock ? Number(filter.startBlock) : undefined,
|
|
224
|
+
endBlock: filter.endBlock ? Number(filter.endBlock) : undefined,
|
|
225
225
|
method: filter.method ? filter.method : undefined,
|
|
226
226
|
escrow: filter.escrow ? filter.escrow : undefined,
|
|
227
227
|
token: filter.token ? filter.token : undefined,
|