@human-protocol/sdk 3.0.7 → 4.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/constants.d.ts +2 -25
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +25 -66
- package/dist/decorators.js +1 -1
- package/dist/encryption.d.ts +21 -29
- package/dist/encryption.d.ts.map +1 -1
- package/dist/encryption.js +34 -36
- package/dist/error.d.ts +31 -28
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +36 -33
- package/dist/escrow.d.ts +118 -112
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +254 -180
- package/dist/graphql/queries/operator.d.ts.map +1 -1
- package/dist/graphql/queries/operator.js +15 -7
- package/dist/graphql/queries/statistics.d.ts.map +1 -1
- package/dist/graphql/queries/statistics.js +0 -2
- package/dist/graphql/queries/transaction.d.ts.map +1 -1
- package/dist/graphql/queries/transaction.js +23 -10
- package/dist/graphql/types.d.ts +0 -2
- package/dist/graphql/types.d.ts.map +1 -1
- package/dist/interfaces.d.ts +29 -12
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/kvstore.d.ts +16 -16
- package/dist/kvstore.d.ts.map +1 -1
- package/dist/kvstore.js +16 -16
- package/dist/operator.d.ts +11 -10
- package/dist/operator.d.ts.map +1 -1
- package/dist/operator.js +36 -11
- package/dist/staking.d.ts +26 -118
- package/dist/staking.d.ts.map +1 -1
- package/dist/staking.js +46 -173
- package/dist/statistics.d.ts +10 -29
- package/dist/statistics.d.ts.map +1 -1
- package/dist/statistics.js +13 -30
- package/dist/storage.d.ts +13 -18
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +30 -25
- package/dist/transaction.js +1 -1
- package/dist/types.d.ts +23 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/utils.d.ts +0 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +0 -1
- package/package.json +8 -4
- package/src/constants.ts +25 -66
- package/src/decorators.ts +1 -1
- package/src/encryption.ts +21 -29
- package/src/error.ts +39 -37
- package/src/escrow.ts +360 -216
- package/src/graphql/queries/operator.ts +15 -7
- package/src/graphql/queries/statistics.ts +0 -2
- package/src/graphql/queries/transaction.ts +23 -13
- package/src/graphql/types.ts +0 -2
- package/src/interfaces.ts +30 -13
- package/src/kvstore.ts +17 -17
- package/src/operator.ts +47 -12
- package/src/staking.ts +53 -187
- package/src/statistics.ts +13 -30
- package/src/storage.ts +13 -18
- package/src/transaction.ts +2 -2
- package/src/types.ts +24 -6
- package/src/utils.ts +0 -1
package/src/staking.ts
CHANGED
|
@@ -3,8 +3,6 @@ import {
|
|
|
3
3
|
EscrowFactory__factory,
|
|
4
4
|
HMToken,
|
|
5
5
|
HMToken__factory,
|
|
6
|
-
RewardPool,
|
|
7
|
-
RewardPool__factory,
|
|
8
6
|
Staking,
|
|
9
7
|
Staking__factory,
|
|
10
8
|
} from '@human-protocol/core/typechain-types';
|
|
@@ -23,26 +21,26 @@ import {
|
|
|
23
21
|
ErrorProviderDoesNotExist,
|
|
24
22
|
ErrorUnsupportedChainID,
|
|
25
23
|
} from './error';
|
|
26
|
-
import { IAllocation } from './interfaces';
|
|
27
24
|
import { NetworkData } from './types';
|
|
28
25
|
import { throwError } from './utils';
|
|
26
|
+
import { StakerInfo } from './interfaces';
|
|
29
27
|
|
|
30
28
|
/**
|
|
31
29
|
* ## Introduction
|
|
32
30
|
*
|
|
33
|
-
* This client enables
|
|
31
|
+
* This client enables performing actions on staking contracts and obtaining staking information from both the contracts and subgraph.
|
|
34
32
|
*
|
|
35
33
|
* Internally, the SDK will use one network or another according to the network ID of the `runner`.
|
|
36
34
|
* To use this client, it is recommended to initialize it using the static `build` method.
|
|
37
35
|
*
|
|
38
36
|
* ```ts
|
|
39
|
-
* static async build(runner: ContractRunner)
|
|
37
|
+
* static async build(runner: ContractRunner): Promise<StakingClient>;
|
|
40
38
|
* ```
|
|
41
39
|
*
|
|
42
40
|
* A `Signer` or a `Provider` should be passed depending on the use case of this module:
|
|
43
41
|
*
|
|
44
|
-
* - **Signer**: when the user wants to use this model
|
|
45
|
-
* - **Provider**: when the user wants to use this model
|
|
42
|
+
* - **Signer**: when the user wants to use this model to send transactions calling the contract functions.
|
|
43
|
+
* - **Provider**: when the user wants to use this model to get information from the contracts or subgraph.
|
|
46
44
|
*
|
|
47
45
|
* ## Installation
|
|
48
46
|
*
|
|
@@ -60,21 +58,21 @@ import { throwError } from './utils';
|
|
|
60
58
|
*
|
|
61
59
|
* ### Signer
|
|
62
60
|
*
|
|
63
|
-
* **Using private key(backend)**
|
|
61
|
+
* **Using private key (backend)**
|
|
64
62
|
*
|
|
65
63
|
* ```ts
|
|
66
64
|
* import { StakingClient } from '@human-protocol/sdk';
|
|
67
65
|
* import { Wallet, providers } from 'ethers';
|
|
68
66
|
*
|
|
69
67
|
* const rpcUrl = 'YOUR_RPC_URL';
|
|
70
|
-
* const privateKey = 'YOUR_PRIVATE_KEY'
|
|
68
|
+
* const privateKey = 'YOUR_PRIVATE_KEY';
|
|
71
69
|
*
|
|
72
70
|
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
73
71
|
* const signer = new Wallet(privateKey, provider);
|
|
74
72
|
* const stakingClient = await StakingClient.build(signer);
|
|
75
73
|
* ```
|
|
76
74
|
*
|
|
77
|
-
* **Using Wagmi(frontend)**
|
|
75
|
+
* **Using Wagmi (frontend)**
|
|
78
76
|
*
|
|
79
77
|
* ```ts
|
|
80
78
|
* import { useSigner, useChainId } from 'wagmi';
|
|
@@ -100,13 +98,12 @@ export class StakingClient extends BaseEthersClient {
|
|
|
100
98
|
public tokenContract: HMToken;
|
|
101
99
|
public stakingContract: Staking;
|
|
102
100
|
public escrowFactoryContract: EscrowFactory;
|
|
103
|
-
public rewardPoolContract: RewardPool;
|
|
104
101
|
|
|
105
102
|
/**
|
|
106
103
|
* **StakingClient constructor**
|
|
107
104
|
*
|
|
108
105
|
* @param {ContractRunner} runner - The Runner object to interact with the Ethereum network
|
|
109
|
-
* @param {NetworkData}
|
|
106
|
+
* @param {NetworkData} networkData - The network information required to connect to the Staking contract
|
|
110
107
|
*/
|
|
111
108
|
constructor(runner: ContractRunner, networkData: NetworkData) {
|
|
112
109
|
super(runner, networkData);
|
|
@@ -125,24 +122,18 @@ export class StakingClient extends BaseEthersClient {
|
|
|
125
122
|
networkData.hmtAddress,
|
|
126
123
|
runner
|
|
127
124
|
);
|
|
128
|
-
|
|
129
|
-
this.rewardPoolContract = RewardPool__factory.connect(
|
|
130
|
-
networkData.rewardPoolAddress,
|
|
131
|
-
this.runner
|
|
132
|
-
);
|
|
133
125
|
}
|
|
134
126
|
|
|
135
127
|
/**
|
|
136
128
|
* Creates an instance of StakingClient from a Runner.
|
|
137
129
|
*
|
|
138
130
|
* @param {ContractRunner} runner - The Runner object to interact with the Ethereum network
|
|
139
|
-
* @param {number | undefined} gasPriceMultiplier - The multiplier to apply to the gas price
|
|
140
131
|
*
|
|
141
132
|
* @returns {Promise<StakingClient>} - An instance of StakingClient
|
|
142
133
|
* @throws {ErrorProviderDoesNotExist} - Thrown if the provider does not exist for the provided Signer
|
|
143
134
|
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
144
135
|
*/
|
|
145
|
-
public static async build(runner: ContractRunner) {
|
|
136
|
+
public static async build(runner: ContractRunner): Promise<StakingClient> {
|
|
146
137
|
if (!runner.provider) {
|
|
147
138
|
throw ErrorProviderDoesNotExist;
|
|
148
139
|
}
|
|
@@ -181,7 +172,6 @@ export class StakingClient extends BaseEthersClient {
|
|
|
181
172
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
182
173
|
* @returns Returns void if successful. Throws error if any.
|
|
183
174
|
*
|
|
184
|
-
*
|
|
185
175
|
* **Code example**
|
|
186
176
|
*
|
|
187
177
|
* ```ts
|
|
@@ -189,7 +179,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
189
179
|
* import { StakingClient } from '@human-protocol/sdk';
|
|
190
180
|
*
|
|
191
181
|
* const rpcUrl = 'YOUR_RPC_URL';
|
|
192
|
-
* const privateKey = 'YOUR_PRIVATE_KEY'
|
|
182
|
+
* const privateKey = 'YOUR_PRIVATE_KEY';
|
|
193
183
|
*
|
|
194
184
|
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
195
185
|
* const signer = new Wallet(privateKey, provider);
|
|
@@ -235,7 +225,6 @@ export class StakingClient extends BaseEthersClient {
|
|
|
235
225
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
236
226
|
* @returns Returns void if successful. Throws error if any.
|
|
237
227
|
*
|
|
238
|
-
*
|
|
239
228
|
* **Code example**
|
|
240
229
|
*
|
|
241
230
|
* ```ts
|
|
@@ -243,7 +232,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
243
232
|
* import { StakingClient } from '@human-protocol/sdk';
|
|
244
233
|
*
|
|
245
234
|
* const rpcUrl = 'YOUR_RPC_URL';
|
|
246
|
-
* const privateKey = 'YOUR_PRIVATE_KEY'
|
|
235
|
+
* const privateKey = 'YOUR_PRIVATE_KEY';
|
|
247
236
|
*
|
|
248
237
|
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
249
238
|
* const signer = new Wallet(privateKey, provider);
|
|
@@ -251,7 +240,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
251
240
|
*
|
|
252
241
|
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
253
242
|
* await stakingClient.approveStake(amount); // if it was already approved before, this is not necessary
|
|
254
|
-
* await stakingClient.
|
|
243
|
+
* await stakingClient.stake(amount);
|
|
255
244
|
* ```
|
|
256
245
|
*/
|
|
257
246
|
@requiresSigner
|
|
@@ -281,7 +270,6 @@ export class StakingClient extends BaseEthersClient {
|
|
|
281
270
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
282
271
|
* @returns Returns void if successful. Throws error if any.
|
|
283
272
|
*
|
|
284
|
-
*
|
|
285
273
|
* **Code example**
|
|
286
274
|
*
|
|
287
275
|
* ```ts
|
|
@@ -289,7 +277,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
289
277
|
* import { StakingClient } from '@human-protocol/sdk';
|
|
290
278
|
*
|
|
291
279
|
* const rpcUrl = 'YOUR_RPC_URL';
|
|
292
|
-
* const privateKey = 'YOUR_PRIVATE_KEY'
|
|
280
|
+
* const privateKey = 'YOUR_PRIVATE_KEY';
|
|
293
281
|
*
|
|
294
282
|
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
295
283
|
* const signer = new Wallet(privateKey, provider);
|
|
@@ -321,14 +309,13 @@ export class StakingClient extends BaseEthersClient {
|
|
|
321
309
|
}
|
|
322
310
|
|
|
323
311
|
/**
|
|
324
|
-
* This function withdraws unstaked and non
|
|
312
|
+
* This function withdraws unstaked and non-locked tokens from staking contract to the user wallet.
|
|
325
313
|
*
|
|
326
314
|
* > Must have tokens available to withdraw
|
|
327
315
|
*
|
|
328
316
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
329
317
|
* @returns Returns void if successful. Throws error if any.
|
|
330
318
|
*
|
|
331
|
-
*
|
|
332
319
|
* **Code example**
|
|
333
320
|
*
|
|
334
321
|
* ```ts
|
|
@@ -336,7 +323,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
336
323
|
* import { StakingClient } from '@human-protocol/sdk';
|
|
337
324
|
*
|
|
338
325
|
* const rpcUrl = 'YOUR_RPC_URL';
|
|
339
|
-
* const privateKey = 'YOUR_PRIVATE_KEY'
|
|
326
|
+
* const privateKey = 'YOUR_PRIVATE_KEY';
|
|
340
327
|
*
|
|
341
328
|
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
342
329
|
* const signer = new Wallet(privateKey, provider);
|
|
@@ -356,16 +343,15 @@ export class StakingClient extends BaseEthersClient {
|
|
|
356
343
|
}
|
|
357
344
|
|
|
358
345
|
/**
|
|
359
|
-
* This function reduces the allocated amount by
|
|
346
|
+
* This function reduces the allocated amount by a staker in an escrow and transfers those tokens to the reward pool. This allows the slasher to claim them later.
|
|
360
347
|
*
|
|
361
348
|
* @param {string} slasher Wallet address from who requested the slash
|
|
362
349
|
* @param {string} staker Wallet address from who is going to be slashed
|
|
363
|
-
* @param {string} escrowAddress Address of the escrow
|
|
350
|
+
* @param {string} escrowAddress Address of the escrow that the slash is made
|
|
351
|
+
* @param {bigint} amount Amount in WEI of tokens to slash.
|
|
364
352
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
365
|
-
* @param {bigint} amount Amount in WEI of tokens to unstake.
|
|
366
353
|
* @returns Returns void if successful. Throws error if any.
|
|
367
354
|
*
|
|
368
|
-
*
|
|
369
355
|
* **Code example**
|
|
370
356
|
*
|
|
371
357
|
* ```ts
|
|
@@ -373,7 +359,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
373
359
|
* import { StakingClient } from '@human-protocol/sdk';
|
|
374
360
|
*
|
|
375
361
|
* const rpcUrl = 'YOUR_RPC_URL';
|
|
376
|
-
* const privateKey = 'YOUR_PRIVATE_KEY'
|
|
362
|
+
* const privateKey = 'YOUR_PRIVATE_KEY';
|
|
377
363
|
*
|
|
378
364
|
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
379
365
|
* const signer = new Wallet(privateKey, provider);
|
|
@@ -427,175 +413,55 @@ export class StakingClient extends BaseEthersClient {
|
|
|
427
413
|
}
|
|
428
414
|
|
|
429
415
|
/**
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
* > Must have tokens staked
|
|
433
|
-
*
|
|
434
|
-
* @param {string} escrowAddress Address of the escrow contract to allocate in.
|
|
435
|
-
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
436
|
-
* @param {bigint} amount Amount in WEI of tokens to allocate.
|
|
437
|
-
* @returns Returns void if successful. Throws error if any.
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
* **Code example**
|
|
441
|
-
*
|
|
442
|
-
* ```ts
|
|
443
|
-
* import { ethers, Wallet, providers } from 'ethers';
|
|
444
|
-
* import { StakingClient } from '@human-protocol/sdk';
|
|
445
|
-
*
|
|
446
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
447
|
-
* const privateKey = 'YOUR_PRIVATE_KEY'
|
|
448
|
-
*
|
|
449
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
450
|
-
* const signer = new Wallet(privateKey, provider);
|
|
451
|
-
* const stakingClient = await StakingClient.build(signer);
|
|
452
|
-
*
|
|
453
|
-
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
454
|
-
* await stakingClient.allocate('0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);
|
|
455
|
-
* ```
|
|
456
|
-
*/
|
|
457
|
-
@requiresSigner
|
|
458
|
-
public async allocate(
|
|
459
|
-
escrowAddress: string,
|
|
460
|
-
amount: bigint,
|
|
461
|
-
txOptions: Overrides = {}
|
|
462
|
-
): Promise<void> {
|
|
463
|
-
if (typeof amount !== 'bigint') {
|
|
464
|
-
throw ErrorInvalidStakingValueType;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
if (amount < 0n) {
|
|
468
|
-
throw ErrorInvalidStakingValueSign;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
await this.checkValidEscrow(escrowAddress);
|
|
472
|
-
|
|
473
|
-
try {
|
|
474
|
-
await (
|
|
475
|
-
await this.stakingContract.allocate(escrowAddress, amount, txOptions)
|
|
476
|
-
).wait();
|
|
477
|
-
return;
|
|
478
|
-
} catch (e) {
|
|
479
|
-
return throwError(e);
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
/**
|
|
484
|
-
* This function drops the allocation from a specific escrow.
|
|
485
|
-
*
|
|
486
|
-
* > The escrow must have allocation
|
|
487
|
-
* > The escrow must be cancelled or completed.
|
|
488
|
-
*
|
|
489
|
-
* @param {string} escrowAddress Address of the escrow contract to close allocation from.
|
|
490
|
-
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
491
|
-
* @returns Returns void if successful. Throws error if any.
|
|
492
|
-
*
|
|
493
|
-
*
|
|
494
|
-
* **Code example**
|
|
495
|
-
*
|
|
496
|
-
* ```ts
|
|
497
|
-
* import { Wallet, providers } from 'ethers';
|
|
498
|
-
* import { StakingClient } from '@human-protocol/sdk';
|
|
499
|
-
*
|
|
500
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
501
|
-
* const privateKey = 'YOUR_PRIVATE_KEY'
|
|
502
|
-
*
|
|
503
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
504
|
-
* const signer = new Wallet(privateKey, provider);
|
|
505
|
-
* const stakingClient = await StakingClient.build(signer);
|
|
506
|
-
*
|
|
507
|
-
* await stakingClient.closeAllocation('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
508
|
-
* ```
|
|
509
|
-
*/
|
|
510
|
-
@requiresSigner
|
|
511
|
-
public async closeAllocation(
|
|
512
|
-
escrowAddress: string,
|
|
513
|
-
txOptions: Overrides = {}
|
|
514
|
-
): Promise<void> {
|
|
515
|
-
await this.checkValidEscrow(escrowAddress);
|
|
516
|
-
|
|
517
|
-
try {
|
|
518
|
-
await (
|
|
519
|
-
await this.stakingContract.closeAllocation(escrowAddress, txOptions)
|
|
520
|
-
).wait();
|
|
521
|
-
return;
|
|
522
|
-
} catch (e) {
|
|
523
|
-
return throwError(e);
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
/**
|
|
528
|
-
* This function drops the allocation from a specific escrow.
|
|
529
|
-
*
|
|
530
|
-
* > The escrow must have rewards added
|
|
531
|
-
*
|
|
532
|
-
* @param {string} escrowAddress Escrow address from which rewards are distributed.
|
|
533
|
-
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
534
|
-
* @returns Returns void if successful. Throws error if any.
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
* **Code example**
|
|
538
|
-
*
|
|
539
|
-
* ```ts
|
|
540
|
-
* import { Wallet, providers } from 'ethers';
|
|
541
|
-
* import { StakingClient } from '@human-protocol/sdk';
|
|
542
|
-
*
|
|
543
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
544
|
-
* const privateKey = 'YOUR_PRIVATE_KEY'
|
|
545
|
-
*
|
|
546
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
547
|
-
* const signer = new Wallet(privateKey, provider);
|
|
548
|
-
* const stakingClient = await StakingClient.build(signer);
|
|
549
|
-
*
|
|
550
|
-
* await stakingClient.distributeReward('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
551
|
-
* ```
|
|
552
|
-
*/
|
|
553
|
-
@requiresSigner
|
|
554
|
-
public async distributeReward(
|
|
555
|
-
escrowAddress: string,
|
|
556
|
-
txOptions: Overrides = {}
|
|
557
|
-
): Promise<void> {
|
|
558
|
-
await this.checkValidEscrow(escrowAddress);
|
|
559
|
-
|
|
560
|
-
try {
|
|
561
|
-
(
|
|
562
|
-
await this.rewardPoolContract.distributeReward(escrowAddress, txOptions)
|
|
563
|
-
).wait();
|
|
564
|
-
return;
|
|
565
|
-
} catch (e) {
|
|
566
|
-
return throwError(e);
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
/**
|
|
571
|
-
* This function returns information about the allocation of the specified escrow.
|
|
572
|
-
*
|
|
573
|
-
* @param {string} escrowAddress Escrow address from which we want to get allocation information.
|
|
574
|
-
* @returns {IAllocation} Returns allocation info if exists.
|
|
416
|
+
* Retrieves comprehensive staking information for a staker.
|
|
575
417
|
*
|
|
418
|
+
* @param {string} stakerAddress - The address of the staker.
|
|
419
|
+
* @returns {Promise<StakerInfo>}
|
|
576
420
|
*
|
|
577
421
|
* **Code example**
|
|
578
422
|
*
|
|
579
423
|
* ```ts
|
|
580
424
|
* import { StakingClient } from '@human-protocol/sdk';
|
|
581
|
-
* import { providers } from 'ethers';
|
|
582
425
|
*
|
|
583
426
|
* const rpcUrl = 'YOUR_RPC_URL';
|
|
584
427
|
*
|
|
585
428
|
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
586
429
|
* const stakingClient = await StakingClient.build(provider);
|
|
587
430
|
*
|
|
588
|
-
* const
|
|
431
|
+
* const stakingInfo = await stakingClient.getStakerInfo('0xYourStakerAddress');
|
|
432
|
+
* console.log(stakingInfo.tokensStaked);
|
|
589
433
|
* ```
|
|
590
434
|
*/
|
|
591
|
-
public async
|
|
592
|
-
|
|
435
|
+
public async getStakerInfo(stakerAddress: string): Promise<StakerInfo> {
|
|
436
|
+
if (!ethers.isAddress(stakerAddress)) {
|
|
437
|
+
throw ErrorInvalidStakerAddressProvided;
|
|
438
|
+
}
|
|
593
439
|
|
|
594
440
|
try {
|
|
595
|
-
const
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
441
|
+
const stakerInfo = await this.stakingContract.stakes(stakerAddress);
|
|
442
|
+
const currentBlock = await this.runner.provider!.getBlockNumber();
|
|
443
|
+
|
|
444
|
+
const tokensWithdrawable =
|
|
445
|
+
stakerInfo.tokensLockedUntil !== 0n &&
|
|
446
|
+
currentBlock >= stakerInfo.tokensLockedUntil
|
|
447
|
+
? stakerInfo.tokensLocked
|
|
448
|
+
: 0n;
|
|
449
|
+
|
|
450
|
+
const adjustedLockedAmount =
|
|
451
|
+
stakerInfo.tokensLockedUntil !== 0n &&
|
|
452
|
+
currentBlock >= stakerInfo.tokensLockedUntil
|
|
453
|
+
? 0n
|
|
454
|
+
: stakerInfo.tokensLocked;
|
|
455
|
+
|
|
456
|
+
return {
|
|
457
|
+
stakedAmount: stakerInfo.tokensStaked,
|
|
458
|
+
lockedAmount: adjustedLockedAmount,
|
|
459
|
+
lockedUntil:
|
|
460
|
+
adjustedLockedAmount === 0n ? 0n : stakerInfo.tokensLockedUntil,
|
|
461
|
+
withdrawableAmount: tokensWithdrawable,
|
|
462
|
+
};
|
|
463
|
+
} catch (error) {
|
|
464
|
+
return throwError(error);
|
|
599
465
|
}
|
|
600
466
|
}
|
|
601
467
|
}
|
package/src/statistics.ts
CHANGED
|
@@ -26,20 +26,15 @@ import { OrderDirection } from './enums';
|
|
|
26
26
|
/**
|
|
27
27
|
* ## Introduction
|
|
28
28
|
*
|
|
29
|
-
* This client enables
|
|
29
|
+
* This client enables obtaining statistical information from the subgraph.
|
|
30
30
|
*
|
|
31
|
-
*
|
|
32
|
-
* We just need to create client object using relevant network data.
|
|
31
|
+
* Unlike other SDK clients, `StatisticsClient` does not require `signer` or `provider` to be provided.
|
|
32
|
+
* We just need to create a client object using relevant network data.
|
|
33
33
|
*
|
|
34
34
|
* ```ts
|
|
35
35
|
* constructor(network: NetworkData)
|
|
36
36
|
* ```
|
|
37
37
|
*
|
|
38
|
-
* A `Signer` or a `Provider` should be passed depending on the use case of this module:
|
|
39
|
-
*
|
|
40
|
-
* - **Signer**: when the user wants to use this model in order to send transactions caling the contract functions.
|
|
41
|
-
* - **Provider**: when the user wants to use this model in order to get information from the contracts or subgraph.
|
|
42
|
-
*
|
|
43
38
|
* ## Installation
|
|
44
39
|
*
|
|
45
40
|
* ### npm
|
|
@@ -77,7 +72,6 @@ export class StatisticsClient {
|
|
|
77
72
|
/**
|
|
78
73
|
* This function returns the statistical data of escrows.
|
|
79
74
|
*
|
|
80
|
-
*
|
|
81
75
|
* **Input parameters**
|
|
82
76
|
*
|
|
83
77
|
* ```ts
|
|
@@ -106,10 +100,8 @@ export class StatisticsClient {
|
|
|
106
100
|
* };
|
|
107
101
|
* ```
|
|
108
102
|
*
|
|
109
|
-
*
|
|
110
103
|
* @param {IStatisticsFilter} filter Statistics params with duration data
|
|
111
|
-
* @returns {EscrowStatistics} Escrow statistics data.
|
|
112
|
-
*
|
|
104
|
+
* @returns {Promise<EscrowStatistics>} Escrow statistics data.
|
|
113
105
|
*
|
|
114
106
|
* **Code example**
|
|
115
107
|
*
|
|
@@ -149,7 +141,9 @@ export class StatisticsClient {
|
|
|
149
141
|
});
|
|
150
142
|
|
|
151
143
|
return {
|
|
152
|
-
totalEscrows:
|
|
144
|
+
totalEscrows: escrowStatistics?.totalEscrowCount
|
|
145
|
+
? +escrowStatistics.totalEscrowCount
|
|
146
|
+
: 0,
|
|
153
147
|
dailyEscrowsData: eventDayDatas.map((eventDayData) => ({
|
|
154
148
|
timestamp: new Date(+eventDayData.timestamp * 1000),
|
|
155
149
|
escrowsTotal: +eventDayData.dailyEscrowCount,
|
|
@@ -167,7 +161,6 @@ export class StatisticsClient {
|
|
|
167
161
|
/**
|
|
168
162
|
* This function returns the statistical data of workers.
|
|
169
163
|
*
|
|
170
|
-
*
|
|
171
164
|
* **Input parameters**
|
|
172
165
|
*
|
|
173
166
|
* ```ts
|
|
@@ -191,10 +184,8 @@ export class StatisticsClient {
|
|
|
191
184
|
* };
|
|
192
185
|
* ```
|
|
193
186
|
*
|
|
194
|
-
*
|
|
195
187
|
* @param {IStatisticsFilter} filter Statistics params with duration data
|
|
196
|
-
* @returns {WorkerStatistics} Worker statistics data.
|
|
197
|
-
*
|
|
188
|
+
* @returns {Promise<WorkerStatistics>} Worker statistics data.
|
|
198
189
|
*
|
|
199
190
|
* **Code example**
|
|
200
191
|
*
|
|
@@ -243,7 +234,6 @@ export class StatisticsClient {
|
|
|
243
234
|
/**
|
|
244
235
|
* This function returns the statistical data of payments.
|
|
245
236
|
*
|
|
246
|
-
*
|
|
247
237
|
* **Input parameters**
|
|
248
238
|
*
|
|
249
239
|
* ```ts
|
|
@@ -269,10 +259,8 @@ export class StatisticsClient {
|
|
|
269
259
|
* };
|
|
270
260
|
* ```
|
|
271
261
|
*
|
|
272
|
-
*
|
|
273
262
|
* @param {IStatisticsFilter} filter Statistics params with duration data
|
|
274
|
-
* @returns {PaymentStatistics} Payment statistics data.
|
|
275
|
-
*
|
|
263
|
+
* @returns {Promise<PaymentStatistics>} Payment statistics data.
|
|
276
264
|
*
|
|
277
265
|
* **Code example**
|
|
278
266
|
*
|
|
@@ -348,7 +336,7 @@ export class StatisticsClient {
|
|
|
348
336
|
/**
|
|
349
337
|
* This function returns the statistical data of HMToken.
|
|
350
338
|
*
|
|
351
|
-
*
|
|
339
|
+
* ```ts
|
|
352
340
|
* type HMTStatistics = {
|
|
353
341
|
* totalTransferAmount: BigNumber;
|
|
354
342
|
* totalTransferCount: BigNumber;
|
|
@@ -356,9 +344,7 @@ export class StatisticsClient {
|
|
|
356
344
|
* };
|
|
357
345
|
* ```
|
|
358
346
|
*
|
|
359
|
-
*
|
|
360
|
-
* @returns {HMTStatistics} HMToken statistics data.
|
|
361
|
-
*
|
|
347
|
+
* @returns {Promise<HMTStatistics>} HMToken statistics data.
|
|
362
348
|
*
|
|
363
349
|
* **Code example**
|
|
364
350
|
*
|
|
@@ -399,7 +385,7 @@ export class StatisticsClient {
|
|
|
399
385
|
* **Input parameters**
|
|
400
386
|
*
|
|
401
387
|
* @param {IHMTHoldersParams} params HMT Holders params with filters and ordering
|
|
402
|
-
* @returns {HMTHolder[]} List of HMToken holders.
|
|
388
|
+
* @returns {Promise<HMTHolder[]>} List of HMToken holders.
|
|
403
389
|
*
|
|
404
390
|
* **Code example**
|
|
405
391
|
*
|
|
@@ -445,7 +431,6 @@ export class StatisticsClient {
|
|
|
445
431
|
/**
|
|
446
432
|
* This function returns the statistical data of HMToken day by day.
|
|
447
433
|
*
|
|
448
|
-
*
|
|
449
434
|
* **Input parameters**
|
|
450
435
|
*
|
|
451
436
|
* ```ts
|
|
@@ -468,10 +453,8 @@ export class StatisticsClient {
|
|
|
468
453
|
* }
|
|
469
454
|
* ```
|
|
470
455
|
*
|
|
471
|
-
*
|
|
472
456
|
* @param {IStatisticsFilter} filter Statistics params with duration data
|
|
473
|
-
* @returns {DailyHMTData[]} Daily HMToken statistics data.
|
|
474
|
-
*
|
|
457
|
+
* @returns {Promise<DailyHMTData[]>} Daily HMToken statistics data.
|
|
475
458
|
*
|
|
476
459
|
* **Code example**
|
|
477
460
|
*
|
package/src/storage.ts
CHANGED
|
@@ -19,7 +19,7 @@ import { HttpStatus } from './constants';
|
|
|
19
19
|
*
|
|
20
20
|
* ## Introduction
|
|
21
21
|
*
|
|
22
|
-
* This client enables
|
|
22
|
+
* This client enables interacting with S3 cloud storage services like Amazon S3 Bucket, Google Cloud Storage, and others.
|
|
23
23
|
*
|
|
24
24
|
* The instance creation of `StorageClient` should be made using its constructor:
|
|
25
25
|
*
|
|
@@ -27,7 +27,7 @@ import { HttpStatus } from './constants';
|
|
|
27
27
|
* constructor(params: StorageParams, credentials?: StorageCredentials)
|
|
28
28
|
* ```
|
|
29
29
|
*
|
|
30
|
-
* > If credentials
|
|
30
|
+
* > If credentials are not provided, it uses anonymous access to the bucket for downloading files.
|
|
31
31
|
*
|
|
32
32
|
* ## Installation
|
|
33
33
|
*
|
|
@@ -68,7 +68,7 @@ export class StorageClient {
|
|
|
68
68
|
* **Storage client constructor**
|
|
69
69
|
*
|
|
70
70
|
* @param {StorageParams} params - Cloud storage params
|
|
71
|
-
* @param {StorageCredentials} credentials - Optional. Cloud storage access data. If credentials
|
|
71
|
+
* @param {StorageCredentials} credentials - Optional. Cloud storage access data. If credentials are not provided - use anonymous access to the bucket
|
|
72
72
|
*/
|
|
73
73
|
constructor(params: StorageParams, credentials?: StorageCredentials) {
|
|
74
74
|
try {
|
|
@@ -89,8 +89,7 @@ export class StorageClient {
|
|
|
89
89
|
*
|
|
90
90
|
* @param {string[]} keys Array of filenames to download.
|
|
91
91
|
* @param {string} bucket Bucket name.
|
|
92
|
-
* @returns {any[]} Returns an array of
|
|
93
|
-
*
|
|
92
|
+
* @returns {Promise<any[]>} Returns an array of JSON files downloaded and parsed into objects.
|
|
94
93
|
*
|
|
95
94
|
* **Code example**
|
|
96
95
|
*
|
|
@@ -131,18 +130,17 @@ export class StorageClient {
|
|
|
131
130
|
}
|
|
132
131
|
|
|
133
132
|
/**
|
|
134
|
-
* This function downloads files from a
|
|
135
|
-
*
|
|
136
|
-
* @param {string} url Url of the file to download.
|
|
137
|
-
* @returns {any} Returns the JSON file downloaded and parsed into object.
|
|
133
|
+
* This function downloads files from a URL.
|
|
138
134
|
*
|
|
135
|
+
* @param {string} url URL of the file to download.
|
|
136
|
+
* @returns {Promise<any>} Returns the JSON file downloaded and parsed into an object.
|
|
139
137
|
*
|
|
140
138
|
* **Code example**
|
|
141
139
|
*
|
|
142
140
|
* ```ts
|
|
143
141
|
* import { StorageClient } from '@human-protocol/sdk';
|
|
144
142
|
*
|
|
145
|
-
* const file = await
|
|
143
|
+
* const file = await StorageClient.downloadFileFromUrl('http://localhost/file.json');
|
|
146
144
|
* ```
|
|
147
145
|
*/
|
|
148
146
|
public static async downloadFileFromUrl(url: string): Promise<any> {
|
|
@@ -170,10 +168,9 @@ export class StorageClient {
|
|
|
170
168
|
/**
|
|
171
169
|
* This function uploads files to a bucket.
|
|
172
170
|
*
|
|
173
|
-
* @param {any[]} files Array of objects to upload serialized into
|
|
171
|
+
* @param {any[]} files Array of objects to upload serialized into JSON.
|
|
174
172
|
* @param {string} bucket Bucket name.
|
|
175
|
-
* @returns {UploadFile[]} Returns an array of
|
|
176
|
-
*
|
|
173
|
+
* @returns {Promise<UploadFile[]>} Returns an array of uploaded file metadata.
|
|
177
174
|
*
|
|
178
175
|
* **Code example**
|
|
179
176
|
*
|
|
@@ -240,8 +237,7 @@ export class StorageClient {
|
|
|
240
237
|
* This function checks if a bucket exists.
|
|
241
238
|
*
|
|
242
239
|
* @param {string} bucket Bucket name.
|
|
243
|
-
* @returns {boolean} Returns `true` if exists, `false` if it doesn't.
|
|
244
|
-
*
|
|
240
|
+
* @returns {Promise<boolean>} Returns `true` if exists, `false` if it doesn't.
|
|
245
241
|
*
|
|
246
242
|
* **Code example**
|
|
247
243
|
*
|
|
@@ -268,11 +264,10 @@ export class StorageClient {
|
|
|
268
264
|
}
|
|
269
265
|
|
|
270
266
|
/**
|
|
271
|
-
* This function
|
|
267
|
+
* This function lists all file names contained in the bucket.
|
|
272
268
|
*
|
|
273
269
|
* @param {string} bucket Bucket name.
|
|
274
|
-
* @returns {
|
|
275
|
-
*
|
|
270
|
+
* @returns {Promise<string[]>} Returns the list of file names contained in the bucket.
|
|
276
271
|
*
|
|
277
272
|
* **Code example**
|
|
278
273
|
*
|
package/src/transaction.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { NETWORKS } from './constants';
|
|
|
5
5
|
import { ChainId, OrderDirection } from './enums';
|
|
6
6
|
import {
|
|
7
7
|
ErrorCannotUseDateAndBlockSimultaneously,
|
|
8
|
-
|
|
8
|
+
ErrorInvalidHashProvided,
|
|
9
9
|
ErrorUnsupportedChainID,
|
|
10
10
|
} from './error';
|
|
11
11
|
import {
|
|
@@ -36,7 +36,7 @@ export class TransactionUtils {
|
|
|
36
36
|
hash: string
|
|
37
37
|
): Promise<ITransaction> {
|
|
38
38
|
if (!ethers.isHexString(hash)) {
|
|
39
|
-
throw
|
|
39
|
+
throw ErrorInvalidHashProvided;
|
|
40
40
|
}
|
|
41
41
|
const networkData = NETWORKS[chainId];
|
|
42
42
|
|