@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
|
@@ -7,10 +7,10 @@ import {
|
|
|
7
7
|
Staking__factory,
|
|
8
8
|
} from '@human-protocol/core/typechain-types';
|
|
9
9
|
import { ContractRunner, ethers } from 'ethers';
|
|
10
|
-
import { BaseEthersClient } from '
|
|
11
|
-
import { NETWORKS } from '
|
|
12
|
-
import { requiresSigner } from '
|
|
13
|
-
import { ChainId
|
|
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, TransactionOverrides } 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.
|
|
@@ -456,158 +445,3 @@ export class StakingClient extends BaseEthersClient {
|
|
|
456
445
|
}
|
|
457
446
|
}
|
|
458
447
|
}
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
* Utility helpers for Staking-related queries.
|
|
462
|
-
*
|
|
463
|
-
* @example
|
|
464
|
-
* ```ts
|
|
465
|
-
* import { StakingUtils, ChainId } from '@human-protocol/sdk';
|
|
466
|
-
*
|
|
467
|
-
* const staker = await StakingUtils.getStaker(
|
|
468
|
-
* ChainId.POLYGON_AMOY,
|
|
469
|
-
* '0xYourStakerAddress'
|
|
470
|
-
* );
|
|
471
|
-
* console.log('Staked amount:', staker.stakedAmount);
|
|
472
|
-
* ```
|
|
473
|
-
*/
|
|
474
|
-
export class StakingUtils {
|
|
475
|
-
/**
|
|
476
|
-
* Gets staking info for a staker from the subgraph.
|
|
477
|
-
*
|
|
478
|
-
* @param chainId - Network in which the staking contract is deployed
|
|
479
|
-
* @param stakerAddress - Address of the staker
|
|
480
|
-
* @param options - Optional configuration for subgraph requests.
|
|
481
|
-
* @returns Staker info from subgraph
|
|
482
|
-
* @throws ErrorInvalidStakerAddressProvided If the staker address is invalid
|
|
483
|
-
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
484
|
-
* @throws ErrorStakerNotFound If the staker is not found
|
|
485
|
-
*
|
|
486
|
-
* @example
|
|
487
|
-
* ```ts
|
|
488
|
-
* import { StakingUtils, ChainId } from '@human-protocol/sdk';
|
|
489
|
-
*
|
|
490
|
-
* const staker = await StakingUtils.getStaker(
|
|
491
|
-
* ChainId.POLYGON_AMOY,
|
|
492
|
-
* '0xYourStakerAddress'
|
|
493
|
-
* );
|
|
494
|
-
* console.log('Staked amount:', staker.stakedAmount);
|
|
495
|
-
* ```
|
|
496
|
-
*/
|
|
497
|
-
public static async getStaker(
|
|
498
|
-
chainId: ChainId,
|
|
499
|
-
stakerAddress: string,
|
|
500
|
-
options?: SubgraphOptions
|
|
501
|
-
): Promise<IStaker> {
|
|
502
|
-
if (!ethers.isAddress(stakerAddress)) {
|
|
503
|
-
throw ErrorInvalidStakerAddressProvided;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
const networkData: NetworkData | undefined = NETWORKS[chainId];
|
|
507
|
-
if (!networkData) {
|
|
508
|
-
throw ErrorUnsupportedChainID;
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
const { staker } = await customGqlFetch<{ staker: StakerData }>(
|
|
512
|
-
getSubgraphUrl(networkData),
|
|
513
|
-
GET_STAKER_BY_ADDRESS_QUERY,
|
|
514
|
-
{ id: stakerAddress.toLowerCase() },
|
|
515
|
-
options
|
|
516
|
-
);
|
|
517
|
-
|
|
518
|
-
if (!staker) {
|
|
519
|
-
throw ErrorStakerNotFound;
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
return mapStaker(staker);
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
/**
|
|
526
|
-
* Gets all stakers from the subgraph with filters, pagination and ordering.
|
|
527
|
-
*
|
|
528
|
-
* @param filter - Stakers filter with pagination and ordering
|
|
529
|
-
* @param options - Optional configuration for subgraph requests.
|
|
530
|
-
* @returns Array of stakers
|
|
531
|
-
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
532
|
-
*
|
|
533
|
-
* @example
|
|
534
|
-
* ```ts
|
|
535
|
-
* import { ChainId } from '@human-protocol/sdk';
|
|
536
|
-
*
|
|
537
|
-
* const filter = {
|
|
538
|
-
* chainId: ChainId.POLYGON_AMOY,
|
|
539
|
-
* minStakedAmount: '1000000000000000000', // 1 token in WEI
|
|
540
|
-
* };
|
|
541
|
-
* const stakers = await StakingUtils.getStakers(filter);
|
|
542
|
-
* console.log('Stakers:', stakers.length);
|
|
543
|
-
* ```
|
|
544
|
-
*/
|
|
545
|
-
public static async getStakers(
|
|
546
|
-
filter: IStakersFilter,
|
|
547
|
-
options?: SubgraphOptions
|
|
548
|
-
): Promise<IStaker[]> {
|
|
549
|
-
const first =
|
|
550
|
-
filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
|
|
551
|
-
const skip = filter.skip || 0;
|
|
552
|
-
const orderDirection = filter.orderDirection || OrderDirection.DESC;
|
|
553
|
-
const orderBy = filter.orderBy || 'lastDepositTimestamp';
|
|
554
|
-
|
|
555
|
-
const networkData = NETWORKS[filter.chainId];
|
|
556
|
-
if (!networkData) {
|
|
557
|
-
throw ErrorUnsupportedChainID;
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
const { stakers } = await customGqlFetch<{ stakers: StakerData[] }>(
|
|
561
|
-
getSubgraphUrl(networkData),
|
|
562
|
-
GET_STAKERS_QUERY(filter),
|
|
563
|
-
{
|
|
564
|
-
minStakedAmount: filter.minStakedAmount
|
|
565
|
-
? filter.minStakedAmount
|
|
566
|
-
: undefined,
|
|
567
|
-
maxStakedAmount: filter.maxStakedAmount
|
|
568
|
-
? filter.maxStakedAmount
|
|
569
|
-
: undefined,
|
|
570
|
-
minLockedAmount: filter.minLockedAmount
|
|
571
|
-
? filter.minLockedAmount
|
|
572
|
-
: undefined,
|
|
573
|
-
maxLockedAmount: filter.maxLockedAmount
|
|
574
|
-
? filter.maxLockedAmount
|
|
575
|
-
: undefined,
|
|
576
|
-
minWithdrawnAmount: filter.minWithdrawnAmount
|
|
577
|
-
? filter.minWithdrawnAmount
|
|
578
|
-
: undefined,
|
|
579
|
-
maxWithdrawnAmount: filter.maxWithdrawnAmount
|
|
580
|
-
? filter.maxWithdrawnAmount
|
|
581
|
-
: undefined,
|
|
582
|
-
minSlashedAmount: filter.minSlashedAmount
|
|
583
|
-
? filter.minSlashedAmount
|
|
584
|
-
: undefined,
|
|
585
|
-
maxSlashedAmount: filter.maxSlashedAmount
|
|
586
|
-
? filter.maxSlashedAmount
|
|
587
|
-
: undefined,
|
|
588
|
-
orderBy: orderBy,
|
|
589
|
-
orderDirection: orderDirection,
|
|
590
|
-
first: first,
|
|
591
|
-
skip: skip,
|
|
592
|
-
},
|
|
593
|
-
options
|
|
594
|
-
);
|
|
595
|
-
if (!stakers) {
|
|
596
|
-
return [];
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
return stakers.map((s) => mapStaker(s));
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
function mapStaker(s: StakerData): IStaker {
|
|
604
|
-
return {
|
|
605
|
-
address: s.address,
|
|
606
|
-
stakedAmount: BigInt(s.stakedAmount),
|
|
607
|
-
lockedAmount: BigInt(s.lockedAmount),
|
|
608
|
-
withdrawableAmount: BigInt(s.withdrawnAmount),
|
|
609
|
-
slashedAmount: BigInt(s.slashedAmount),
|
|
610
|
-
lockedUntil: Number(s.lockedUntilTimestamp) * 1000,
|
|
611
|
-
lastDepositTimestamp: Number(s.lastDepositTimestamp) * 1000,
|
|
612
|
-
};
|
|
613
|
-
}
|
|
@@ -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.
|
package/src/types.ts
CHANGED
|
@@ -76,6 +76,14 @@ export type NetworkData = {
|
|
|
76
76
|
* Subgraph URL API key
|
|
77
77
|
*/
|
|
78
78
|
subgraphUrlApiKey: string;
|
|
79
|
+
/**
|
|
80
|
+
* HMT statistics subgraph URL
|
|
81
|
+
*/
|
|
82
|
+
hmtSubgraphUrl?: string;
|
|
83
|
+
/**
|
|
84
|
+
* HMT statistics subgraph URL API key
|
|
85
|
+
*/
|
|
86
|
+
hmtSubgraphUrlApiKey?: string;
|
|
79
87
|
/**
|
|
80
88
|
* Old subgraph URL
|
|
81
89
|
*/
|
package/src/utils.ts
CHANGED
|
@@ -13,6 +13,8 @@ import {
|
|
|
13
13
|
NonceExpired,
|
|
14
14
|
NumericFault,
|
|
15
15
|
ReplacementUnderpriced,
|
|
16
|
+
SubgraphBadIndexerError,
|
|
17
|
+
SubgraphRequestError,
|
|
16
18
|
TransactionReplaced,
|
|
17
19
|
WarnSubgraphApiKeyNotProvided,
|
|
18
20
|
} from './error';
|
|
@@ -78,6 +80,24 @@ export const isValidJson = (input: string): boolean => {
|
|
|
78
80
|
}
|
|
79
81
|
};
|
|
80
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Extracts a readable message from unknown error values.
|
|
85
|
+
*
|
|
86
|
+
* @param error - Unknown error value
|
|
87
|
+
* @returns Human-readable error message
|
|
88
|
+
*/
|
|
89
|
+
export const getErrorMessage = (error: unknown): string => {
|
|
90
|
+
if (error instanceof Error) {
|
|
91
|
+
return error.message;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
return JSON.stringify(error);
|
|
96
|
+
} catch {
|
|
97
|
+
return String(error);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
81
101
|
/**
|
|
82
102
|
* Gets the subgraph URL for the given network, using API key if available.
|
|
83
103
|
*
|
|
@@ -96,6 +116,26 @@ export const getSubgraphUrl = (networkData: NetworkData) => {
|
|
|
96
116
|
return subgraphUrl;
|
|
97
117
|
};
|
|
98
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Gets the HMT statistics subgraph URL for the given network.
|
|
121
|
+
* Falls back to the default subgraph URL when a dedicated HMT endpoint is not configured.
|
|
122
|
+
*
|
|
123
|
+
* @param networkData - The network data containing subgraph URLs
|
|
124
|
+
* @returns The HMT statistics subgraph URL with API key if available
|
|
125
|
+
*/
|
|
126
|
+
export const getHMTSubgraphUrl = (networkData: NetworkData) => {
|
|
127
|
+
let subgraphUrl = networkData.hmtSubgraphUrl || networkData.subgraphUrl;
|
|
128
|
+
if (process.env.SUBGRAPH_API_KEY) {
|
|
129
|
+
subgraphUrl =
|
|
130
|
+
networkData.hmtSubgraphUrlApiKey || networkData.subgraphUrlApiKey;
|
|
131
|
+
} else if (networkData.chainId !== ChainId.LOCALHOST) {
|
|
132
|
+
// eslint-disable-next-line no-console
|
|
133
|
+
console.warn(WarnSubgraphApiKeyNotProvided);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return subgraphUrl;
|
|
137
|
+
};
|
|
138
|
+
|
|
99
139
|
/**
|
|
100
140
|
* Converts a Date object to Unix timestamp (seconds since epoch).
|
|
101
141
|
*
|
|
@@ -117,6 +157,38 @@ export const isIndexerError = (error: any): boolean => {
|
|
|
117
157
|
return errorMessage.toLowerCase().includes('bad indexers');
|
|
118
158
|
};
|
|
119
159
|
|
|
160
|
+
const getSubgraphErrorMessage = (error: any): string => {
|
|
161
|
+
return (
|
|
162
|
+
error?.response?.errors?.[0]?.message ||
|
|
163
|
+
error?.message ||
|
|
164
|
+
error?.toString?.() ||
|
|
165
|
+
'Subgraph request failed'
|
|
166
|
+
);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const getSubgraphStatusCode = (error: any): number | undefined => {
|
|
170
|
+
if (typeof error?.response?.status === 'number') {
|
|
171
|
+
return error.response.status;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (typeof error?.status === 'number') {
|
|
175
|
+
return error.status;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return undefined;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const toSubgraphError = (error: any, url: string): Error => {
|
|
182
|
+
const message = getSubgraphErrorMessage(error);
|
|
183
|
+
const statusCode = getSubgraphStatusCode(error);
|
|
184
|
+
|
|
185
|
+
if (isIndexerError(error)) {
|
|
186
|
+
return new SubgraphBadIndexerError(message, url, statusCode);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return new SubgraphRequestError(message, url, statusCode);
|
|
190
|
+
};
|
|
191
|
+
|
|
120
192
|
const sleep = (ms: number): Promise<void> => {
|
|
121
193
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
122
194
|
};
|
|
@@ -154,7 +226,11 @@ export const customGqlFetch = async <T = any>(
|
|
|
154
226
|
: undefined;
|
|
155
227
|
|
|
156
228
|
if (!options) {
|
|
157
|
-
|
|
229
|
+
try {
|
|
230
|
+
return await gqlFetch<T>(url, query, variables, headers);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
throw toSubgraphError(error, url);
|
|
233
|
+
}
|
|
158
234
|
}
|
|
159
235
|
|
|
160
236
|
const hasMaxRetries = options.maxRetries !== undefined;
|
|
@@ -177,10 +253,11 @@ export const customGqlFetch = async <T = any>(
|
|
|
177
253
|
try {
|
|
178
254
|
return await gqlFetch<T>(targetUrl, query, variables, headers);
|
|
179
255
|
} catch (error) {
|
|
180
|
-
|
|
256
|
+
const wrappedError = toSubgraphError(error, targetUrl);
|
|
257
|
+
lastError = wrappedError;
|
|
181
258
|
|
|
182
259
|
if (attempt === maxRetries || !isIndexerError(error)) {
|
|
183
|
-
throw
|
|
260
|
+
throw wrappedError;
|
|
184
261
|
}
|
|
185
262
|
|
|
186
263
|
const delay = baseDelay * attempt;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { WorkerUtils } from './worker_utils';
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { ethers } from 'ethers';
|
|
2
|
-
import { NETWORKS } from '
|
|
3
|
-
import { ChainId, OrderDirection } from '
|
|
4
|
-
import { ErrorInvalidAddress, ErrorUnsupportedChainID } from '
|
|
5
|
-
import { WorkerData } from '
|
|
6
|
-
import { GET_WORKER_QUERY, GET_WORKERS_QUERY } from '
|
|
7
|
-
import { IWorker, IWorkersFilter, SubgraphOptions } from '
|
|
8
|
-
import { getSubgraphUrl, customGqlFetch } from '
|
|
2
|
+
import { NETWORKS } from '../constants';
|
|
3
|
+
import { ChainId, OrderDirection } from '../enums';
|
|
4
|
+
import { ErrorInvalidAddress, ErrorUnsupportedChainID } from '../error';
|
|
5
|
+
import { WorkerData } from '../graphql';
|
|
6
|
+
import { GET_WORKER_QUERY, GET_WORKERS_QUERY } from '../graphql/queries/worker';
|
|
7
|
+
import { IWorker, IWorkersFilter, SubgraphOptions } from '../interfaces';
|
|
8
|
+
import { getSubgraphUrl, customGqlFetch } from '../utils';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Utility class for worker-related operations.
|