@human-protocol/sdk 1.1.19 → 2.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/dist/base.d.ts +4 -5
- package/dist/base.d.ts.map +1 -1
- package/dist/base.js +3 -3
- package/dist/decorators.d.ts.map +1 -1
- package/dist/decorators.js +4 -2
- package/dist/error.d.ts +0 -6
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +2 -14
- package/dist/escrow.d.ts +23 -24
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +86 -90
- package/dist/graphql/queries/{staking.d.ts → operator.d.ts} +2 -1
- package/dist/graphql/queries/operator.d.ts.map +1 -0
- package/dist/graphql/queries/{staking.js → operator.js} +24 -1
- package/dist/graphql/types.d.ts +5 -6
- package/dist/graphql/types.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/interfaces.d.ts +28 -18
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/kvstore.d.ts +8 -9
- package/dist/kvstore.d.ts.map +1 -1
- package/dist/kvstore.js +18 -31
- package/dist/operator.d.ts +68 -0
- package/dist/operator.d.ts.map +1 -0
- package/dist/operator.js +153 -0
- package/dist/staking.d.ts +24 -91
- package/dist/staking.d.ts.map +1 -1
- package/dist/staking.js +47 -166
- package/dist/statistics.d.ts.map +1 -1
- package/dist/statistics.js +7 -6
- package/dist/types.d.ts +1 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +0 -7
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +8 -28
- package/package.json +4 -4
- package/src/base.ts +5 -6
- package/src/decorators.ts +3 -2
- package/src/error.ts +0 -12
- package/src/escrow.ts +101 -114
- package/src/graphql/queries/{staking.ts → operator.ts} +24 -0
- package/src/graphql/types.ts +5 -7
- package/src/index.ts +2 -0
- package/src/interfaces.ts +30 -18
- package/src/kvstore.ts +20 -38
- package/src/operator.ts +192 -0
- package/src/staking.ts +49 -182
- package/src/statistics.ts +8 -9
- package/src/types.ts +1 -3
- package/src/utils.ts +7 -29
- package/dist/graphql/queries/staking.d.ts.map +0 -1
package/src/staking.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { Provider } from '@ethersproject/abstract-provider';
|
|
3
|
-
import { Network } from '@ethersproject/networks';
|
|
4
1
|
import {
|
|
5
2
|
EscrowFactory,
|
|
6
3
|
EscrowFactory__factory,
|
|
@@ -11,8 +8,7 @@ import {
|
|
|
11
8
|
Staking,
|
|
12
9
|
Staking__factory,
|
|
13
10
|
} from '@human-protocol/core/typechain-types';
|
|
14
|
-
import {
|
|
15
|
-
import gqlFetch from 'graphql-request';
|
|
11
|
+
import { ContractRunner, Overrides, ethers } from 'ethers';
|
|
16
12
|
import { BaseEthersClient } from './base';
|
|
17
13
|
import { NETWORKS } from './constants';
|
|
18
14
|
import { requiresSigner } from './decorators';
|
|
@@ -27,23 +23,20 @@ import {
|
|
|
27
23
|
ErrorProviderDoesNotExist,
|
|
28
24
|
ErrorUnsupportedChainID,
|
|
29
25
|
} from './error';
|
|
30
|
-
import { IAllocation
|
|
26
|
+
import { IAllocation } from './interfaces';
|
|
31
27
|
import { NetworkData } from './types';
|
|
32
28
|
import { throwError } from './utils';
|
|
33
|
-
import { GET_REWARD_ADDED_EVENTS_QUERY } from './graphql/queries/reward';
|
|
34
|
-
import { RewardAddedEventData } from './graphql';
|
|
35
|
-
import { GET_LEADER_QUERY, GET_LEADERS_QUERY } from './graphql/queries/staking';
|
|
36
29
|
|
|
37
30
|
/**
|
|
38
31
|
* ## Introduction
|
|
39
32
|
*
|
|
40
33
|
* This client enables to perform actions on staking contracts and obtain staking information from both the contracts and subgraph.
|
|
41
34
|
*
|
|
42
|
-
* Internally, the SDK will use one network or another according to the network ID of the `
|
|
35
|
+
* Internally, the SDK will use one network or another according to the network ID of the `runner`.
|
|
43
36
|
* To use this client, it is recommended to initialize it using the static `build` method.
|
|
44
37
|
*
|
|
45
38
|
* ```ts
|
|
46
|
-
* static async build(
|
|
39
|
+
* static async build(runner: ContractRunner);
|
|
47
40
|
* ```
|
|
48
41
|
*
|
|
49
42
|
* A `Signer` or a `Provider` should be passed depending on the use case of this module:
|
|
@@ -112,63 +105,58 @@ export class StakingClient extends BaseEthersClient {
|
|
|
112
105
|
/**
|
|
113
106
|
* **StakingClient constructor**
|
|
114
107
|
*
|
|
115
|
-
* @param {
|
|
108
|
+
* @param {ContractRunner} runner - The Runner object to interact with the Ethereum network
|
|
116
109
|
* @param {NetworkData} network - The network information required to connect to the Staking contract
|
|
117
110
|
*/
|
|
118
|
-
constructor(
|
|
119
|
-
super(
|
|
111
|
+
constructor(runner: ContractRunner, networkData: NetworkData) {
|
|
112
|
+
super(runner, networkData);
|
|
120
113
|
|
|
121
114
|
this.stakingContract = Staking__factory.connect(
|
|
122
115
|
networkData.stakingAddress,
|
|
123
|
-
|
|
116
|
+
runner
|
|
124
117
|
);
|
|
125
118
|
|
|
126
119
|
this.escrowFactoryContract = EscrowFactory__factory.connect(
|
|
127
120
|
networkData.factoryAddress,
|
|
128
|
-
|
|
121
|
+
runner
|
|
129
122
|
);
|
|
130
123
|
|
|
131
124
|
this.tokenContract = HMToken__factory.connect(
|
|
132
125
|
networkData.hmtAddress,
|
|
133
|
-
|
|
126
|
+
runner
|
|
134
127
|
);
|
|
135
128
|
|
|
136
129
|
this.rewardPoolContract = RewardPool__factory.connect(
|
|
137
130
|
networkData.rewardPoolAddress,
|
|
138
|
-
this.
|
|
131
|
+
this.runner
|
|
139
132
|
);
|
|
140
133
|
}
|
|
141
134
|
|
|
142
135
|
/**
|
|
143
|
-
* Creates an instance of StakingClient from a
|
|
136
|
+
* Creates an instance of StakingClient from a Runner.
|
|
144
137
|
*
|
|
145
|
-
* @param {
|
|
138
|
+
* @param {ContractRunner} runner - The Runner object to interact with the Ethereum network
|
|
146
139
|
* @param {number | undefined} gasPriceMultiplier - The multiplier to apply to the gas price
|
|
147
140
|
*
|
|
148
141
|
* @returns {Promise<StakingClient>} - An instance of StakingClient
|
|
149
142
|
* @throws {ErrorProviderDoesNotExist} - Thrown if the provider does not exist for the provided Signer
|
|
150
143
|
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
151
144
|
*/
|
|
152
|
-
public static async build(
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
if (!signerOrProvider.provider) {
|
|
156
|
-
throw ErrorProviderDoesNotExist;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
network = await signerOrProvider.provider.getNetwork();
|
|
160
|
-
} else {
|
|
161
|
-
network = await signerOrProvider.getNetwork();
|
|
145
|
+
public static async build(runner: ContractRunner) {
|
|
146
|
+
if (!runner.provider) {
|
|
147
|
+
throw ErrorProviderDoesNotExist;
|
|
162
148
|
}
|
|
163
149
|
|
|
164
|
-
const
|
|
150
|
+
const network = await runner.provider?.getNetwork();
|
|
151
|
+
|
|
152
|
+
const chainId: ChainId = Number(network?.chainId);
|
|
165
153
|
const networkData = NETWORKS[chainId];
|
|
166
154
|
|
|
167
155
|
if (!networkData) {
|
|
168
156
|
throw ErrorUnsupportedChainID;
|
|
169
157
|
}
|
|
170
158
|
|
|
171
|
-
return new StakingClient(
|
|
159
|
+
return new StakingClient(runner, networkData);
|
|
172
160
|
}
|
|
173
161
|
|
|
174
162
|
/**
|
|
@@ -177,7 +165,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
177
165
|
* @param escrowAddress Escrow address to check against
|
|
178
166
|
*/
|
|
179
167
|
private async checkValidEscrow(escrowAddress: string) {
|
|
180
|
-
if (!ethers.
|
|
168
|
+
if (!ethers.isAddress(escrowAddress)) {
|
|
181
169
|
throw ErrorInvalidEscrowAddressProvided;
|
|
182
170
|
}
|
|
183
171
|
|
|
@@ -189,7 +177,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
189
177
|
/**
|
|
190
178
|
* This function approves the staking contract to transfer a specified amount of tokens when the user stakes. It increases the allowance for the staking contract.
|
|
191
179
|
*
|
|
192
|
-
* @param {
|
|
180
|
+
* @param {bigint} amount Amount in WEI of tokens to approve for stake.
|
|
193
181
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
194
182
|
* @returns Returns void if successful. Throws error if any.
|
|
195
183
|
*
|
|
@@ -207,27 +195,27 @@ export class StakingClient extends BaseEthersClient {
|
|
|
207
195
|
* const signer = new Wallet(privateKey, provider);
|
|
208
196
|
* const stakingClient = await StakingClient.build(signer);
|
|
209
197
|
*
|
|
210
|
-
* const amount = ethers.
|
|
198
|
+
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
211
199
|
* await stakingClient.approveStake(amount);
|
|
212
200
|
* ```
|
|
213
201
|
*/
|
|
214
202
|
@requiresSigner
|
|
215
203
|
public async approveStake(
|
|
216
|
-
amount:
|
|
204
|
+
amount: bigint,
|
|
217
205
|
txOptions: Overrides = {}
|
|
218
206
|
): Promise<void> {
|
|
219
|
-
if (
|
|
207
|
+
if (typeof amount !== 'bigint') {
|
|
220
208
|
throw ErrorInvalidStakingValueType;
|
|
221
209
|
}
|
|
222
210
|
|
|
223
|
-
if (amount
|
|
211
|
+
if (amount < 0n) {
|
|
224
212
|
throw ErrorInvalidStakingValueSign;
|
|
225
213
|
}
|
|
226
214
|
|
|
227
215
|
try {
|
|
228
216
|
await (
|
|
229
217
|
await this.tokenContract.approve(
|
|
230
|
-
this.stakingContract.
|
|
218
|
+
await this.stakingContract.getAddress(),
|
|
231
219
|
amount,
|
|
232
220
|
txOptions
|
|
233
221
|
)
|
|
@@ -243,7 +231,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
243
231
|
*
|
|
244
232
|
* > `approveStake` must be called before
|
|
245
233
|
*
|
|
246
|
-
* @param {
|
|
234
|
+
* @param {bigint} amount Amount in WEI of tokens to stake.
|
|
247
235
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
248
236
|
* @returns Returns void if successful. Throws error if any.
|
|
249
237
|
*
|
|
@@ -261,21 +249,18 @@ export class StakingClient extends BaseEthersClient {
|
|
|
261
249
|
* const signer = new Wallet(privateKey, provider);
|
|
262
250
|
* const stakingClient = await StakingClient.build(signer);
|
|
263
251
|
*
|
|
264
|
-
* const amount = ethers.
|
|
252
|
+
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
265
253
|
* await stakingClient.approveStake(amount); // if it was already approved before, this is not necessary
|
|
266
254
|
* await stakingClient.approveStake(amount);
|
|
267
255
|
* ```
|
|
268
256
|
*/
|
|
269
257
|
@requiresSigner
|
|
270
|
-
public async stake(
|
|
271
|
-
amount
|
|
272
|
-
txOptions: Overrides = {}
|
|
273
|
-
): Promise<void> {
|
|
274
|
-
if (!BigNumber.isBigNumber(amount)) {
|
|
258
|
+
public async stake(amount: bigint, txOptions: Overrides = {}): Promise<void> {
|
|
259
|
+
if (typeof amount !== 'bigint') {
|
|
275
260
|
throw ErrorInvalidStakingValueType;
|
|
276
261
|
}
|
|
277
262
|
|
|
278
|
-
if (amount
|
|
263
|
+
if (amount < 0n) {
|
|
279
264
|
throw ErrorInvalidStakingValueSign;
|
|
280
265
|
}
|
|
281
266
|
|
|
@@ -292,7 +277,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
292
277
|
*
|
|
293
278
|
* > Must have tokens available to unstake
|
|
294
279
|
*
|
|
295
|
-
* @param {
|
|
280
|
+
* @param {bigint} amount Amount in WEI of tokens to unstake.
|
|
296
281
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
297
282
|
* @returns Returns void if successful. Throws error if any.
|
|
298
283
|
*
|
|
@@ -310,20 +295,20 @@ export class StakingClient extends BaseEthersClient {
|
|
|
310
295
|
* const signer = new Wallet(privateKey, provider);
|
|
311
296
|
* const stakingClient = await StakingClient.build(signer);
|
|
312
297
|
*
|
|
313
|
-
* const amount = ethers.
|
|
298
|
+
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
314
299
|
* await stakingClient.unstake(amount);
|
|
315
300
|
* ```
|
|
316
301
|
*/
|
|
317
302
|
@requiresSigner
|
|
318
303
|
public async unstake(
|
|
319
|
-
amount:
|
|
304
|
+
amount: bigint,
|
|
320
305
|
txOptions: Overrides = {}
|
|
321
306
|
): Promise<void> {
|
|
322
|
-
if (
|
|
307
|
+
if (typeof amount !== 'bigint') {
|
|
323
308
|
throw ErrorInvalidStakingValueType;
|
|
324
309
|
}
|
|
325
310
|
|
|
326
|
-
if (amount
|
|
311
|
+
if (amount < 0n) {
|
|
327
312
|
throw ErrorInvalidStakingValueSign;
|
|
328
313
|
}
|
|
329
314
|
|
|
@@ -377,7 +362,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
377
362
|
* @param {string} staker Wallet address from who is going to be slashed
|
|
378
363
|
* @param {string} escrowAddress Address of the escrow which allocation will be slashed
|
|
379
364
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
380
|
-
* @param {
|
|
365
|
+
* @param {bigint} amount Amount in WEI of tokens to unstake.
|
|
381
366
|
* @returns Returns void if successful. Throws error if any.
|
|
382
367
|
*
|
|
383
368
|
*
|
|
@@ -394,7 +379,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
394
379
|
* const signer = new Wallet(privateKey, provider);
|
|
395
380
|
* const stakingClient = await StakingClient.build(signer);
|
|
396
381
|
*
|
|
397
|
-
* const amount = ethers.
|
|
382
|
+
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
398
383
|
* await stakingClient.slash('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);
|
|
399
384
|
* ```
|
|
400
385
|
*/
|
|
@@ -403,22 +388,22 @@ export class StakingClient extends BaseEthersClient {
|
|
|
403
388
|
slasher: string,
|
|
404
389
|
staker: string,
|
|
405
390
|
escrowAddress: string,
|
|
406
|
-
amount:
|
|
391
|
+
amount: bigint,
|
|
407
392
|
txOptions: Overrides = {}
|
|
408
393
|
): Promise<void> {
|
|
409
|
-
if (
|
|
394
|
+
if (typeof amount !== 'bigint') {
|
|
410
395
|
throw ErrorInvalidStakingValueType;
|
|
411
396
|
}
|
|
412
397
|
|
|
413
|
-
if (amount
|
|
398
|
+
if (amount < 0n) {
|
|
414
399
|
throw ErrorInvalidStakingValueSign;
|
|
415
400
|
}
|
|
416
401
|
|
|
417
|
-
if (!ethers.
|
|
402
|
+
if (!ethers.isAddress(slasher)) {
|
|
418
403
|
throw ErrorInvalidSlasherAddressProvided;
|
|
419
404
|
}
|
|
420
405
|
|
|
421
|
-
if (!ethers.
|
|
406
|
+
if (!ethers.isAddress(staker)) {
|
|
422
407
|
throw ErrorInvalidStakerAddressProvided;
|
|
423
408
|
}
|
|
424
409
|
|
|
@@ -448,7 +433,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
448
433
|
*
|
|
449
434
|
* @param {string} escrowAddress Address of the escrow contract to allocate in.
|
|
450
435
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
451
|
-
* @param {
|
|
436
|
+
* @param {bigint} amount Amount in WEI of tokens to allocate.
|
|
452
437
|
* @returns Returns void if successful. Throws error if any.
|
|
453
438
|
*
|
|
454
439
|
*
|
|
@@ -465,21 +450,21 @@ export class StakingClient extends BaseEthersClient {
|
|
|
465
450
|
* const signer = new Wallet(privateKey, provider);
|
|
466
451
|
* const stakingClient = await StakingClient.build(signer);
|
|
467
452
|
*
|
|
468
|
-
* const amount = ethers.
|
|
453
|
+
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
469
454
|
* await stakingClient.allocate('0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);
|
|
470
455
|
* ```
|
|
471
456
|
*/
|
|
472
457
|
@requiresSigner
|
|
473
458
|
public async allocate(
|
|
474
459
|
escrowAddress: string,
|
|
475
|
-
amount:
|
|
460
|
+
amount: bigint,
|
|
476
461
|
txOptions: Overrides = {}
|
|
477
462
|
): Promise<void> {
|
|
478
|
-
if (
|
|
463
|
+
if (typeof amount !== 'bigint') {
|
|
479
464
|
throw ErrorInvalidStakingValueType;
|
|
480
465
|
}
|
|
481
466
|
|
|
482
|
-
if (amount
|
|
467
|
+
if (amount < 0n) {
|
|
483
468
|
throw ErrorInvalidStakingValueSign;
|
|
484
469
|
}
|
|
485
470
|
|
|
@@ -582,80 +567,6 @@ export class StakingClient extends BaseEthersClient {
|
|
|
582
567
|
}
|
|
583
568
|
}
|
|
584
569
|
|
|
585
|
-
/**
|
|
586
|
-
* This function returns all the leader details of the protocol.
|
|
587
|
-
*
|
|
588
|
-
* @param {ILeadersFilter} filter Filter for the leaders.
|
|
589
|
-
* @returns {ILeader[]} Returns an array with all the leader details.
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
* **Code example**
|
|
593
|
-
*
|
|
594
|
-
* ```ts
|
|
595
|
-
* import { StakingClient } from '@human-protocol/sdk';
|
|
596
|
-
* import { providers } from 'ethers';
|
|
597
|
-
*
|
|
598
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
599
|
-
*
|
|
600
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
601
|
-
* const stakingClient = await StakingClient.build(provider);
|
|
602
|
-
*
|
|
603
|
-
* const leaders = await stakingClient.getLeaders();
|
|
604
|
-
* ```
|
|
605
|
-
*/
|
|
606
|
-
public async getLeader(address: string): Promise<ILeader> {
|
|
607
|
-
if (!ethers.utils.isAddress(address)) {
|
|
608
|
-
throw ErrorInvalidStakerAddressProvided;
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
try {
|
|
612
|
-
const { leader } = await gqlFetch<{
|
|
613
|
-
leader: ILeader;
|
|
614
|
-
}>(this.networkData.subgraphUrl, GET_LEADER_QUERY, {
|
|
615
|
-
address: address.toLowerCase(),
|
|
616
|
-
});
|
|
617
|
-
|
|
618
|
-
return leader;
|
|
619
|
-
} catch (e) {
|
|
620
|
-
return throwError(e);
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
/**
|
|
625
|
-
* This function returns the leader data for the given address.
|
|
626
|
-
*
|
|
627
|
-
* @param {string} address Leader address.
|
|
628
|
-
* @returns {ILeader} Returns the leader details.
|
|
629
|
-
*
|
|
630
|
-
*
|
|
631
|
-
* **Code example**
|
|
632
|
-
*
|
|
633
|
-
* ```ts
|
|
634
|
-
* import { StakingClient } from '@human-protocol/sdk';
|
|
635
|
-
* import { providers } from 'ethers';
|
|
636
|
-
*
|
|
637
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
638
|
-
*
|
|
639
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
640
|
-
* const stakingClient = await StakingClient.build(provider);
|
|
641
|
-
*
|
|
642
|
-
* const leader = await stakingClient.getLeader('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
643
|
-
* ```
|
|
644
|
-
*/
|
|
645
|
-
public async getLeaders(filter: ILeadersFilter = {}): Promise<ILeader[]> {
|
|
646
|
-
try {
|
|
647
|
-
const { leaders } = await gqlFetch<{
|
|
648
|
-
leaders: ILeader[];
|
|
649
|
-
}>(this.networkData.subgraphUrl, GET_LEADERS_QUERY(filter), {
|
|
650
|
-
role: filter.role,
|
|
651
|
-
});
|
|
652
|
-
|
|
653
|
-
return leaders;
|
|
654
|
-
} catch (e) {
|
|
655
|
-
return throwError(e);
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
|
|
659
570
|
/**
|
|
660
571
|
* This function returns information about the allocation of the specified escrow.
|
|
661
572
|
*
|
|
@@ -687,48 +598,4 @@ export class StakingClient extends BaseEthersClient {
|
|
|
687
598
|
return throwError(e);
|
|
688
599
|
}
|
|
689
600
|
}
|
|
690
|
-
|
|
691
|
-
/**
|
|
692
|
-
* This function returns information about the rewards for a given slasher address.
|
|
693
|
-
*
|
|
694
|
-
* @param {string} slasherAddress Slasher address.
|
|
695
|
-
* @returns {IReward[]} Returns an array of Reward objects that contain the rewards earned by the user through slashing other users.
|
|
696
|
-
*
|
|
697
|
-
*
|
|
698
|
-
* **Code example**
|
|
699
|
-
*
|
|
700
|
-
* ```ts
|
|
701
|
-
* import { StakingClient } from '@human-protocol/sdk';
|
|
702
|
-
* import { providers } from 'ethers';
|
|
703
|
-
*
|
|
704
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
705
|
-
*
|
|
706
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
707
|
-
* const stakingClient = await StakingClient.build(provider);
|
|
708
|
-
*
|
|
709
|
-
* const rewards = await stakingClient.getRewards('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
710
|
-
* ```
|
|
711
|
-
*/
|
|
712
|
-
public async getRewards(slasherAddress: string): Promise<IReward[]> {
|
|
713
|
-
if (!ethers.utils.isAddress(slasherAddress)) {
|
|
714
|
-
throw ErrorInvalidSlasherAddressProvided;
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
try {
|
|
718
|
-
const { rewardAddedEvents } = await gqlFetch<{
|
|
719
|
-
rewardAddedEvents: RewardAddedEventData[];
|
|
720
|
-
}>(this.networkData.subgraphUrl, GET_REWARD_ADDED_EVENTS_QUERY, {
|
|
721
|
-
slasherAddress: slasherAddress.toLowerCase(),
|
|
722
|
-
});
|
|
723
|
-
|
|
724
|
-
return rewardAddedEvents.map((reward: any) => {
|
|
725
|
-
return {
|
|
726
|
-
escrowAddress: reward.escrow,
|
|
727
|
-
amount: reward.amount,
|
|
728
|
-
};
|
|
729
|
-
});
|
|
730
|
-
} catch (e) {
|
|
731
|
-
return throwError(e);
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
601
|
}
|
package/src/statistics.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import {
|
|
2
|
+
import { ethers } from 'ethers';
|
|
3
3
|
import gqlFetch from 'graphql-request';
|
|
4
4
|
|
|
5
5
|
import {
|
|
@@ -296,14 +296,13 @@ export class StatisticsClient {
|
|
|
296
296
|
return {
|
|
297
297
|
dailyPaymentsData: eventDayDatas.map((eventDayData) => ({
|
|
298
298
|
timestamp: new Date(+eventDayData.timestamp * 1000),
|
|
299
|
-
totalAmountPaid:
|
|
299
|
+
totalAmountPaid: ethers.toBigInt(eventDayData.dailyPayoutAmount),
|
|
300
300
|
totalCount: +eventDayData.dailyPayoutCount,
|
|
301
301
|
averageAmountPerWorker:
|
|
302
302
|
eventDayData.dailyWorkerCount === '0'
|
|
303
|
-
?
|
|
304
|
-
:
|
|
305
|
-
|
|
306
|
-
),
|
|
303
|
+
? ethers.toBigInt(0)
|
|
304
|
+
: ethers.toBigInt(eventDayData.dailyPayoutAmount) /
|
|
305
|
+
ethers.toBigInt(eventDayData.dailyWorkerCount),
|
|
307
306
|
})),
|
|
308
307
|
};
|
|
309
308
|
} catch (e: any) {
|
|
@@ -412,18 +411,18 @@ export class StatisticsClient {
|
|
|
412
411
|
});
|
|
413
412
|
|
|
414
413
|
return {
|
|
415
|
-
totalTransferAmount:
|
|
414
|
+
totalTransferAmount: ethers.toBigInt(
|
|
416
415
|
hmtokenStatistics.totalValueTransfered
|
|
417
416
|
),
|
|
418
417
|
totalTransferCount: Number(hmtokenStatistics.totalTransferEventCount),
|
|
419
418
|
totalHolders: +hmtokenStatistics.holders,
|
|
420
419
|
holders: holders.map((holder) => ({
|
|
421
420
|
address: holder.address,
|
|
422
|
-
balance:
|
|
421
|
+
balance: ethers.toBigInt(holder.balance),
|
|
423
422
|
})),
|
|
424
423
|
dailyHMTData: eventDayDatas.map((eventDayData) => ({
|
|
425
424
|
timestamp: new Date(+eventDayData.timestamp * 1000),
|
|
426
|
-
totalTransactionAmount:
|
|
425
|
+
totalTransactionAmount: ethers.toBigInt(
|
|
427
426
|
eventDayData.dailyHMTTransferAmount
|
|
428
427
|
),
|
|
429
428
|
totalTransactionCount: +eventDayData.dailyHMTTransferCount,
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { BigNumber } from 'ethers';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Enum for escrow statuses.
|
|
5
3
|
* @readonly
|
|
@@ -150,5 +148,5 @@ export type EscrowCancel = {
|
|
|
150
148
|
/**
|
|
151
149
|
* The amount refunded in the escrow cancellation.
|
|
152
150
|
*/
|
|
153
|
-
amountRefunded:
|
|
151
|
+
amountRefunded: bigint;
|
|
154
152
|
};
|
package/src/utils.ts
CHANGED
|
@@ -7,27 +7,10 @@ import {
|
|
|
7
7
|
InvalidArgumentError,
|
|
8
8
|
NonceExpired,
|
|
9
9
|
NumericFault,
|
|
10
|
-
OutOfGasError,
|
|
11
10
|
ReplacementUnderpriced,
|
|
12
11
|
TransactionReplaced,
|
|
13
|
-
UnpredictableGasLimit,
|
|
14
12
|
} from './error';
|
|
15
13
|
|
|
16
|
-
/**
|
|
17
|
-
* **Get specific error text.*
|
|
18
|
-
*
|
|
19
|
-
* @param {any} error - An error message.
|
|
20
|
-
* @returns
|
|
21
|
-
*/
|
|
22
|
-
export const getRevertReason = (error: any): string => {
|
|
23
|
-
const prefix = "reverted with reason string '";
|
|
24
|
-
const suffix = "'";
|
|
25
|
-
const message = error.data.substring(
|
|
26
|
-
error.data.indexOf(prefix) + prefix.length
|
|
27
|
-
);
|
|
28
|
-
return message.substring(0, message.indexOf(suffix));
|
|
29
|
-
};
|
|
30
|
-
|
|
31
14
|
/**
|
|
32
15
|
* **Handle and throw the error.*
|
|
33
16
|
*
|
|
@@ -35,22 +18,17 @@ export const getRevertReason = (error: any): string => {
|
|
|
35
18
|
* @returns
|
|
36
19
|
*/
|
|
37
20
|
export const throwError = (e: any) => {
|
|
38
|
-
if (e
|
|
21
|
+
if (ethers.isError(e, 'INVALID_ARGUMENT')) {
|
|
39
22
|
throw new InvalidArgumentError(e.message);
|
|
40
|
-
} else if (e
|
|
41
|
-
throw new
|
|
42
|
-
} else if (e
|
|
43
|
-
const reason = getRevertReason(e.data);
|
|
44
|
-
throw new ContractExecutionError(reason);
|
|
45
|
-
} else if (e.code === ethers.utils.Logger.errors.UNPREDICTABLE_GAS_LIMIT) {
|
|
46
|
-
throw new UnpredictableGasLimit(e.message);
|
|
47
|
-
} else if (e.code === ethers.utils.Logger.errors.TRANSACTION_REPLACED) {
|
|
23
|
+
} else if (ethers.isError(e, 'CALL_EXCEPTION')) {
|
|
24
|
+
throw new ContractExecutionError(e.reason as string);
|
|
25
|
+
} else if (ethers.isError(e, 'TRANSACTION_REPLACED')) {
|
|
48
26
|
throw new TransactionReplaced(e.message);
|
|
49
|
-
} else if (e
|
|
27
|
+
} else if (ethers.isError(e, 'REPLACEMENT_UNDERPRICED')) {
|
|
50
28
|
throw new ReplacementUnderpriced(e.message);
|
|
51
|
-
} else if (e
|
|
29
|
+
} else if (ethers.isError(e, 'NUMERIC_FAULT')) {
|
|
52
30
|
throw new NumericFault(e.message);
|
|
53
|
-
} else if (e
|
|
31
|
+
} else if (ethers.isError(e, 'NONCE_EXPIRED')) {
|
|
54
32
|
throw new NonceExpired(e.message);
|
|
55
33
|
} else {
|
|
56
34
|
throw new EthereumError(e.message);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"staking.d.ts","sourceRoot":"","sources":["../../../src/graphql/queries/staking.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAuBhD,eAAO,MAAM,iBAAiB,WAAY,cAAc,mCAuBvD,CAAC;AAEF,eAAO,MAAM,gBAAgB,gCAO5B,CAAC"}
|