@human-protocol/sdk 1.1.18 → 1.1.19
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 +2 -10
- package/dist/base.d.ts.map +1 -1
- package/dist/base.js +1 -16
- package/dist/constants.d.ts +7 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +18 -11
- package/dist/encryption.d.ts +31 -0
- package/dist/encryption.d.ts.map +1 -1
- package/dist/encryption.js +37 -0
- package/dist/error.d.ts +0 -4
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +1 -5
- package/dist/escrow.d.ts +21 -14
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +40 -52
- package/dist/kvstore.d.ts +9 -8
- package/dist/kvstore.d.ts.map +1 -1
- package/dist/kvstore.js +16 -21
- package/dist/staking.d.ts +19 -12
- package/dist/staking.d.ts.map +1 -1
- package/dist/staking.js +36 -45
- package/dist/utils.d.ts +0 -8
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +2 -22
- package/package.json +1 -1
- package/src/base.ts +2 -26
- package/src/constants.ts +18 -10
- package/src/encryption.ts +40 -0
- package/src/error.ts +0 -5
- package/src/escrow.ts +74 -70
- package/src/kvstore.ts +32 -26
- package/src/staking.ts +66 -45
- package/src/utils.ts +1 -29
package/src/staking.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
Staking,
|
|
12
12
|
Staking__factory,
|
|
13
13
|
} from '@human-protocol/core/typechain-types';
|
|
14
|
-
import { BigNumber, Signer, ethers } from 'ethers';
|
|
14
|
+
import { BigNumber, Overrides, Signer, ethers } from 'ethers';
|
|
15
15
|
import gqlFetch from 'graphql-request';
|
|
16
16
|
import { BaseEthersClient } from './base';
|
|
17
17
|
import { NETWORKS } from './constants';
|
|
@@ -114,14 +114,9 @@ export class StakingClient extends BaseEthersClient {
|
|
|
114
114
|
*
|
|
115
115
|
* @param {Signer | Provider} signerOrProvider - The Signer or Provider object to interact with the Ethereum network
|
|
116
116
|
* @param {NetworkData} network - The network information required to connect to the Staking contract
|
|
117
|
-
* @param {number | undefined} gasPriceMultiplier - The multiplier to apply to the gas price
|
|
118
117
|
*/
|
|
119
|
-
constructor(
|
|
120
|
-
signerOrProvider
|
|
121
|
-
networkData: NetworkData,
|
|
122
|
-
gasPriceMultiplier?: number
|
|
123
|
-
) {
|
|
124
|
-
super(signerOrProvider, networkData, gasPriceMultiplier);
|
|
118
|
+
constructor(signerOrProvider: Signer | Provider, networkData: NetworkData) {
|
|
119
|
+
super(signerOrProvider, networkData);
|
|
125
120
|
|
|
126
121
|
this.stakingContract = Staking__factory.connect(
|
|
127
122
|
networkData.stakingAddress,
|
|
@@ -154,10 +149,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
154
149
|
* @throws {ErrorProviderDoesNotExist} - Thrown if the provider does not exist for the provided Signer
|
|
155
150
|
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
156
151
|
*/
|
|
157
|
-
public static async build(
|
|
158
|
-
signerOrProvider: Signer | Provider,
|
|
159
|
-
gasPriceMultiplier?: number
|
|
160
|
-
) {
|
|
152
|
+
public static async build(signerOrProvider: Signer | Provider) {
|
|
161
153
|
let network: Network;
|
|
162
154
|
if (Signer.isSigner(signerOrProvider)) {
|
|
163
155
|
if (!signerOrProvider.provider) {
|
|
@@ -176,7 +168,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
176
168
|
throw ErrorUnsupportedChainID;
|
|
177
169
|
}
|
|
178
170
|
|
|
179
|
-
return new StakingClient(signerOrProvider, networkData
|
|
171
|
+
return new StakingClient(signerOrProvider, networkData);
|
|
180
172
|
}
|
|
181
173
|
|
|
182
174
|
/**
|
|
@@ -198,6 +190,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
198
190
|
* 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.
|
|
199
191
|
*
|
|
200
192
|
* @param {BigNumber} amount Amount in WEI of tokens to approve for stake.
|
|
193
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
201
194
|
* @returns Returns void if successful. Throws error if any.
|
|
202
195
|
*
|
|
203
196
|
*
|
|
@@ -219,7 +212,10 @@ export class StakingClient extends BaseEthersClient {
|
|
|
219
212
|
* ```
|
|
220
213
|
*/
|
|
221
214
|
@requiresSigner
|
|
222
|
-
public async approveStake(
|
|
215
|
+
public async approveStake(
|
|
216
|
+
amount: BigNumber,
|
|
217
|
+
txOptions: Overrides = {}
|
|
218
|
+
): Promise<void> {
|
|
223
219
|
if (!BigNumber.isBigNumber(amount)) {
|
|
224
220
|
throw ErrorInvalidStakingValueType;
|
|
225
221
|
}
|
|
@@ -229,9 +225,13 @@ export class StakingClient extends BaseEthersClient {
|
|
|
229
225
|
}
|
|
230
226
|
|
|
231
227
|
try {
|
|
232
|
-
await
|
|
233
|
-
|
|
234
|
-
|
|
228
|
+
await (
|
|
229
|
+
await this.tokenContract.approve(
|
|
230
|
+
this.stakingContract.address,
|
|
231
|
+
amount,
|
|
232
|
+
txOptions
|
|
233
|
+
)
|
|
234
|
+
).wait();
|
|
235
235
|
return;
|
|
236
236
|
} catch (e) {
|
|
237
237
|
return throwError(e);
|
|
@@ -244,6 +244,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
244
244
|
* > `approveStake` must be called before
|
|
245
245
|
*
|
|
246
246
|
* @param {BigNumber} amount Amount in WEI of tokens to stake.
|
|
247
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
247
248
|
* @returns Returns void if successful. Throws error if any.
|
|
248
249
|
*
|
|
249
250
|
*
|
|
@@ -266,7 +267,10 @@ export class StakingClient extends BaseEthersClient {
|
|
|
266
267
|
* ```
|
|
267
268
|
*/
|
|
268
269
|
@requiresSigner
|
|
269
|
-
public async stake(
|
|
270
|
+
public async stake(
|
|
271
|
+
amount: BigNumber,
|
|
272
|
+
txOptions: Overrides = {}
|
|
273
|
+
): Promise<void> {
|
|
270
274
|
if (!BigNumber.isBigNumber(amount)) {
|
|
271
275
|
throw ErrorInvalidStakingValueType;
|
|
272
276
|
}
|
|
@@ -276,9 +280,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
276
280
|
}
|
|
277
281
|
|
|
278
282
|
try {
|
|
279
|
-
await this.stakingContract.stake(amount,
|
|
280
|
-
...(await this.gasPriceOptions()),
|
|
281
|
-
});
|
|
283
|
+
await (await this.stakingContract.stake(amount, txOptions)).wait();
|
|
282
284
|
return;
|
|
283
285
|
} catch (e) {
|
|
284
286
|
return throwError(e);
|
|
@@ -291,6 +293,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
291
293
|
* > Must have tokens available to unstake
|
|
292
294
|
*
|
|
293
295
|
* @param {BigNumber} amount Amount in WEI of tokens to unstake.
|
|
296
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
294
297
|
* @returns Returns void if successful. Throws error if any.
|
|
295
298
|
*
|
|
296
299
|
*
|
|
@@ -312,7 +315,10 @@ export class StakingClient extends BaseEthersClient {
|
|
|
312
315
|
* ```
|
|
313
316
|
*/
|
|
314
317
|
@requiresSigner
|
|
315
|
-
public async unstake(
|
|
318
|
+
public async unstake(
|
|
319
|
+
amount: BigNumber,
|
|
320
|
+
txOptions: Overrides = {}
|
|
321
|
+
): Promise<void> {
|
|
316
322
|
if (!BigNumber.isBigNumber(amount)) {
|
|
317
323
|
throw ErrorInvalidStakingValueType;
|
|
318
324
|
}
|
|
@@ -322,9 +328,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
322
328
|
}
|
|
323
329
|
|
|
324
330
|
try {
|
|
325
|
-
await this.stakingContract.unstake(amount,
|
|
326
|
-
...(await this.gasPriceOptions()),
|
|
327
|
-
});
|
|
331
|
+
await (await this.stakingContract.unstake(amount, txOptions)).wait();
|
|
328
332
|
return;
|
|
329
333
|
} catch (e) {
|
|
330
334
|
return throwError(e);
|
|
@@ -336,6 +340,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
336
340
|
*
|
|
337
341
|
* > Must have tokens available to withdraw
|
|
338
342
|
*
|
|
343
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
339
344
|
* @returns Returns void if successful. Throws error if any.
|
|
340
345
|
*
|
|
341
346
|
*
|
|
@@ -356,11 +361,9 @@ export class StakingClient extends BaseEthersClient {
|
|
|
356
361
|
* ```
|
|
357
362
|
*/
|
|
358
363
|
@requiresSigner
|
|
359
|
-
public async withdraw(): Promise<void> {
|
|
364
|
+
public async withdraw(txOptions: Overrides = {}): Promise<void> {
|
|
360
365
|
try {
|
|
361
|
-
await this.stakingContract.withdraw(
|
|
362
|
-
...(await this.gasPriceOptions()),
|
|
363
|
-
});
|
|
366
|
+
await (await this.stakingContract.withdraw(txOptions)).wait();
|
|
364
367
|
return;
|
|
365
368
|
} catch (e) {
|
|
366
369
|
return throwError(e);
|
|
@@ -373,6 +376,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
373
376
|
* @param {string} slasher Wallet address from who requested the slash
|
|
374
377
|
* @param {string} staker Wallet address from who is going to be slashed
|
|
375
378
|
* @param {string} escrowAddress Address of the escrow which allocation will be slashed
|
|
379
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
376
380
|
* @param {BigNumber} amount Amount in WEI of tokens to unstake.
|
|
377
381
|
* @returns Returns void if successful. Throws error if any.
|
|
378
382
|
*
|
|
@@ -399,7 +403,8 @@ export class StakingClient extends BaseEthersClient {
|
|
|
399
403
|
slasher: string,
|
|
400
404
|
staker: string,
|
|
401
405
|
escrowAddress: string,
|
|
402
|
-
amount: BigNumber
|
|
406
|
+
amount: BigNumber,
|
|
407
|
+
txOptions: Overrides = {}
|
|
403
408
|
): Promise<void> {
|
|
404
409
|
if (!BigNumber.isBigNumber(amount)) {
|
|
405
410
|
throw ErrorInvalidStakingValueType;
|
|
@@ -420,9 +425,15 @@ export class StakingClient extends BaseEthersClient {
|
|
|
420
425
|
await this.checkValidEscrow(escrowAddress);
|
|
421
426
|
|
|
422
427
|
try {
|
|
423
|
-
await
|
|
424
|
-
|
|
425
|
-
|
|
428
|
+
await (
|
|
429
|
+
await this.stakingContract.slash(
|
|
430
|
+
slasher,
|
|
431
|
+
staker,
|
|
432
|
+
escrowAddress,
|
|
433
|
+
amount,
|
|
434
|
+
txOptions
|
|
435
|
+
)
|
|
436
|
+
).wait();
|
|
426
437
|
|
|
427
438
|
return;
|
|
428
439
|
} catch (e) {
|
|
@@ -436,6 +447,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
436
447
|
* > Must have tokens staked
|
|
437
448
|
*
|
|
438
449
|
* @param {string} escrowAddress Address of the escrow contract to allocate in.
|
|
450
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
439
451
|
* @param {BigNumber} amount Amount in WEI of tokens to allocate.
|
|
440
452
|
* @returns Returns void if successful. Throws error if any.
|
|
441
453
|
*
|
|
@@ -460,7 +472,8 @@ export class StakingClient extends BaseEthersClient {
|
|
|
460
472
|
@requiresSigner
|
|
461
473
|
public async allocate(
|
|
462
474
|
escrowAddress: string,
|
|
463
|
-
amount: BigNumber
|
|
475
|
+
amount: BigNumber,
|
|
476
|
+
txOptions: Overrides = {}
|
|
464
477
|
): Promise<void> {
|
|
465
478
|
if (!BigNumber.isBigNumber(amount)) {
|
|
466
479
|
throw ErrorInvalidStakingValueType;
|
|
@@ -473,9 +486,9 @@ export class StakingClient extends BaseEthersClient {
|
|
|
473
486
|
await this.checkValidEscrow(escrowAddress);
|
|
474
487
|
|
|
475
488
|
try {
|
|
476
|
-
await
|
|
477
|
-
|
|
478
|
-
|
|
489
|
+
await (
|
|
490
|
+
await this.stakingContract.allocate(escrowAddress, amount, txOptions)
|
|
491
|
+
).wait();
|
|
479
492
|
return;
|
|
480
493
|
} catch (e) {
|
|
481
494
|
return throwError(e);
|
|
@@ -489,6 +502,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
489
502
|
* > The escrow must be cancelled or completed.
|
|
490
503
|
*
|
|
491
504
|
* @param {string} escrowAddress Address of the escrow contract to close allocation from.
|
|
505
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
492
506
|
* @returns Returns void if successful. Throws error if any.
|
|
493
507
|
*
|
|
494
508
|
*
|
|
@@ -509,13 +523,16 @@ export class StakingClient extends BaseEthersClient {
|
|
|
509
523
|
* ```
|
|
510
524
|
*/
|
|
511
525
|
@requiresSigner
|
|
512
|
-
public async closeAllocation(
|
|
526
|
+
public async closeAllocation(
|
|
527
|
+
escrowAddress: string,
|
|
528
|
+
txOptions: Overrides = {}
|
|
529
|
+
): Promise<void> {
|
|
513
530
|
await this.checkValidEscrow(escrowAddress);
|
|
514
531
|
|
|
515
532
|
try {
|
|
516
|
-
await
|
|
517
|
-
|
|
518
|
-
|
|
533
|
+
await (
|
|
534
|
+
await this.stakingContract.closeAllocation(escrowAddress, txOptions)
|
|
535
|
+
).wait();
|
|
519
536
|
return;
|
|
520
537
|
} catch (e) {
|
|
521
538
|
return throwError(e);
|
|
@@ -528,6 +545,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
528
545
|
* > The escrow must have rewards added
|
|
529
546
|
*
|
|
530
547
|
* @param {string} escrowAddress Escrow address from which rewards are distributed.
|
|
548
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
531
549
|
* @returns Returns void if successful. Throws error if any.
|
|
532
550
|
*
|
|
533
551
|
*
|
|
@@ -548,13 +566,16 @@ export class StakingClient extends BaseEthersClient {
|
|
|
548
566
|
* ```
|
|
549
567
|
*/
|
|
550
568
|
@requiresSigner
|
|
551
|
-
public async distributeReward(
|
|
569
|
+
public async distributeReward(
|
|
570
|
+
escrowAddress: string,
|
|
571
|
+
txOptions: Overrides = {}
|
|
572
|
+
): Promise<void> {
|
|
552
573
|
await this.checkValidEscrow(escrowAddress);
|
|
553
574
|
|
|
554
575
|
try {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
576
|
+
(
|
|
577
|
+
await this.rewardPoolContract.distributeReward(escrowAddress, txOptions)
|
|
578
|
+
).wait();
|
|
558
579
|
return;
|
|
559
580
|
} catch (e) {
|
|
560
581
|
return throwError(e);
|
package/src/utils.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import {
|
|
3
|
-
import { BigNumber, Signer, ethers } from 'ethers';
|
|
2
|
+
import { ethers } from 'ethers';
|
|
4
3
|
|
|
5
4
|
import {
|
|
6
5
|
ContractExecutionError,
|
|
7
|
-
ErrorMissingGasPrice,
|
|
8
6
|
EthereumError,
|
|
9
7
|
InvalidArgumentError,
|
|
10
8
|
NonceExpired,
|
|
@@ -73,29 +71,3 @@ export const isValidUrl = (url: string) => {
|
|
|
73
71
|
return false;
|
|
74
72
|
}
|
|
75
73
|
};
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Increase/Decrease gas price
|
|
79
|
-
*
|
|
80
|
-
* @returns {Promise<BigNumber>} Returns the adjusted gas price
|
|
81
|
-
*/
|
|
82
|
-
export const gasPriceAdjusted = async (
|
|
83
|
-
signerOrProvider: Signer | Provider,
|
|
84
|
-
gasPriceMultiplier: number
|
|
85
|
-
): Promise<BigNumber> => {
|
|
86
|
-
let gasPrice;
|
|
87
|
-
|
|
88
|
-
if (Signer.isSigner(signerOrProvider)) {
|
|
89
|
-
gasPrice = (await signerOrProvider.provider?.getFeeData())?.gasPrice;
|
|
90
|
-
} else {
|
|
91
|
-
gasPrice = (await signerOrProvider.getFeeData()).gasPrice;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (!gasPrice) {
|
|
95
|
-
throw ErrorMissingGasPrice;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return gasPrice
|
|
99
|
-
.mul(ethers.utils.parseEther(gasPriceMultiplier.toString()))
|
|
100
|
-
.div(ethers.utils.parseEther('1'));
|
|
101
|
-
};
|