@human-protocol/sdk 1.1.18 → 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 -13
- package/dist/base.d.ts.map +1 -1
- package/dist/base.js +3 -18
- package/dist/constants.d.ts +7 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +18 -11
- package/dist/decorators.d.ts.map +1 -1
- package/dist/decorators.js +4 -2
- 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 -10
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +2 -18
- package/dist/escrow.d.ts +39 -33
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +121 -137
- 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 +14 -14
- package/dist/kvstore.d.ts.map +1 -1
- package/dist/kvstore.js +30 -48
- 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 +35 -95
- package/dist/staking.d.ts.map +1 -1
- package/dist/staking.js +73 -201
- 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 -15
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +9 -49
- package/package.json +4 -4
- package/src/base.ts +5 -30
- package/src/constants.ts +18 -10
- package/src/decorators.ts +3 -2
- package/src/encryption.ts +40 -0
- package/src/error.ts +0 -17
- package/src/escrow.ts +169 -178
- 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 +47 -59
- package/src/operator.ts +192 -0
- package/src/staking.ts +101 -213
- package/src/statistics.ts +8 -9
- package/src/types.ts +1 -3
- package/src/utils.ts +8 -58
- 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,71 +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
|
-
* @param {number | undefined} gasPriceMultiplier - The multiplier to apply to the gas price
|
|
118
110
|
*/
|
|
119
|
-
constructor(
|
|
120
|
-
|
|
121
|
-
networkData: NetworkData,
|
|
122
|
-
gasPriceMultiplier?: number
|
|
123
|
-
) {
|
|
124
|
-
super(signerOrProvider, networkData, gasPriceMultiplier);
|
|
111
|
+
constructor(runner: ContractRunner, networkData: NetworkData) {
|
|
112
|
+
super(runner, networkData);
|
|
125
113
|
|
|
126
114
|
this.stakingContract = Staking__factory.connect(
|
|
127
115
|
networkData.stakingAddress,
|
|
128
|
-
|
|
116
|
+
runner
|
|
129
117
|
);
|
|
130
118
|
|
|
131
119
|
this.escrowFactoryContract = EscrowFactory__factory.connect(
|
|
132
120
|
networkData.factoryAddress,
|
|
133
|
-
|
|
121
|
+
runner
|
|
134
122
|
);
|
|
135
123
|
|
|
136
124
|
this.tokenContract = HMToken__factory.connect(
|
|
137
125
|
networkData.hmtAddress,
|
|
138
|
-
|
|
126
|
+
runner
|
|
139
127
|
);
|
|
140
128
|
|
|
141
129
|
this.rewardPoolContract = RewardPool__factory.connect(
|
|
142
130
|
networkData.rewardPoolAddress,
|
|
143
|
-
this.
|
|
131
|
+
this.runner
|
|
144
132
|
);
|
|
145
133
|
}
|
|
146
134
|
|
|
147
135
|
/**
|
|
148
|
-
* Creates an instance of StakingClient from a
|
|
136
|
+
* Creates an instance of StakingClient from a Runner.
|
|
149
137
|
*
|
|
150
|
-
* @param {
|
|
138
|
+
* @param {ContractRunner} runner - The Runner object to interact with the Ethereum network
|
|
151
139
|
* @param {number | undefined} gasPriceMultiplier - The multiplier to apply to the gas price
|
|
152
140
|
*
|
|
153
141
|
* @returns {Promise<StakingClient>} - An instance of StakingClient
|
|
154
142
|
* @throws {ErrorProviderDoesNotExist} - Thrown if the provider does not exist for the provided Signer
|
|
155
143
|
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
156
144
|
*/
|
|
157
|
-
public static async build(
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
) {
|
|
161
|
-
let network: Network;
|
|
162
|
-
if (Signer.isSigner(signerOrProvider)) {
|
|
163
|
-
if (!signerOrProvider.provider) {
|
|
164
|
-
throw ErrorProviderDoesNotExist;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
network = await signerOrProvider.provider.getNetwork();
|
|
168
|
-
} else {
|
|
169
|
-
network = await signerOrProvider.getNetwork();
|
|
145
|
+
public static async build(runner: ContractRunner) {
|
|
146
|
+
if (!runner.provider) {
|
|
147
|
+
throw ErrorProviderDoesNotExist;
|
|
170
148
|
}
|
|
171
149
|
|
|
172
|
-
const
|
|
150
|
+
const network = await runner.provider?.getNetwork();
|
|
151
|
+
|
|
152
|
+
const chainId: ChainId = Number(network?.chainId);
|
|
173
153
|
const networkData = NETWORKS[chainId];
|
|
174
154
|
|
|
175
155
|
if (!networkData) {
|
|
176
156
|
throw ErrorUnsupportedChainID;
|
|
177
157
|
}
|
|
178
158
|
|
|
179
|
-
return new StakingClient(
|
|
159
|
+
return new StakingClient(runner, networkData);
|
|
180
160
|
}
|
|
181
161
|
|
|
182
162
|
/**
|
|
@@ -185,7 +165,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
185
165
|
* @param escrowAddress Escrow address to check against
|
|
186
166
|
*/
|
|
187
167
|
private async checkValidEscrow(escrowAddress: string) {
|
|
188
|
-
if (!ethers.
|
|
168
|
+
if (!ethers.isAddress(escrowAddress)) {
|
|
189
169
|
throw ErrorInvalidEscrowAddressProvided;
|
|
190
170
|
}
|
|
191
171
|
|
|
@@ -197,7 +177,8 @@ export class StakingClient extends BaseEthersClient {
|
|
|
197
177
|
/**
|
|
198
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.
|
|
199
179
|
*
|
|
200
|
-
* @param {
|
|
180
|
+
* @param {bigint} amount Amount in WEI of tokens to approve for stake.
|
|
181
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
201
182
|
* @returns Returns void if successful. Throws error if any.
|
|
202
183
|
*
|
|
203
184
|
*
|
|
@@ -214,24 +195,31 @@ export class StakingClient extends BaseEthersClient {
|
|
|
214
195
|
* const signer = new Wallet(privateKey, provider);
|
|
215
196
|
* const stakingClient = await StakingClient.build(signer);
|
|
216
197
|
*
|
|
217
|
-
* const amount = ethers.
|
|
198
|
+
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
218
199
|
* await stakingClient.approveStake(amount);
|
|
219
200
|
* ```
|
|
220
201
|
*/
|
|
221
202
|
@requiresSigner
|
|
222
|
-
public async approveStake(
|
|
223
|
-
|
|
203
|
+
public async approveStake(
|
|
204
|
+
amount: bigint,
|
|
205
|
+
txOptions: Overrides = {}
|
|
206
|
+
): Promise<void> {
|
|
207
|
+
if (typeof amount !== 'bigint') {
|
|
224
208
|
throw ErrorInvalidStakingValueType;
|
|
225
209
|
}
|
|
226
210
|
|
|
227
|
-
if (amount
|
|
211
|
+
if (amount < 0n) {
|
|
228
212
|
throw ErrorInvalidStakingValueSign;
|
|
229
213
|
}
|
|
230
214
|
|
|
231
215
|
try {
|
|
232
|
-
await
|
|
233
|
-
|
|
234
|
-
|
|
216
|
+
await (
|
|
217
|
+
await this.tokenContract.approve(
|
|
218
|
+
await this.stakingContract.getAddress(),
|
|
219
|
+
amount,
|
|
220
|
+
txOptions
|
|
221
|
+
)
|
|
222
|
+
).wait();
|
|
235
223
|
return;
|
|
236
224
|
} catch (e) {
|
|
237
225
|
return throwError(e);
|
|
@@ -243,7 +231,8 @@ 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.
|
|
235
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
247
236
|
* @returns Returns void if successful. Throws error if any.
|
|
248
237
|
*
|
|
249
238
|
*
|
|
@@ -260,25 +249,23 @@ export class StakingClient extends BaseEthersClient {
|
|
|
260
249
|
* const signer = new Wallet(privateKey, provider);
|
|
261
250
|
* const stakingClient = await StakingClient.build(signer);
|
|
262
251
|
*
|
|
263
|
-
* const amount = ethers.
|
|
252
|
+
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
264
253
|
* await stakingClient.approveStake(amount); // if it was already approved before, this is not necessary
|
|
265
254
|
* await stakingClient.approveStake(amount);
|
|
266
255
|
* ```
|
|
267
256
|
*/
|
|
268
257
|
@requiresSigner
|
|
269
|
-
public async stake(amount:
|
|
270
|
-
if (
|
|
258
|
+
public async stake(amount: bigint, txOptions: Overrides = {}): Promise<void> {
|
|
259
|
+
if (typeof amount !== 'bigint') {
|
|
271
260
|
throw ErrorInvalidStakingValueType;
|
|
272
261
|
}
|
|
273
262
|
|
|
274
|
-
if (amount
|
|
263
|
+
if (amount < 0n) {
|
|
275
264
|
throw ErrorInvalidStakingValueSign;
|
|
276
265
|
}
|
|
277
266
|
|
|
278
267
|
try {
|
|
279
|
-
await this.stakingContract.stake(amount,
|
|
280
|
-
...(await this.gasPriceOptions()),
|
|
281
|
-
});
|
|
268
|
+
await (await this.stakingContract.stake(amount, txOptions)).wait();
|
|
282
269
|
return;
|
|
283
270
|
} catch (e) {
|
|
284
271
|
return throwError(e);
|
|
@@ -290,7 +277,8 @@ export class StakingClient extends BaseEthersClient {
|
|
|
290
277
|
*
|
|
291
278
|
* > Must have tokens available to unstake
|
|
292
279
|
*
|
|
293
|
-
* @param {
|
|
280
|
+
* @param {bigint} amount Amount in WEI of tokens to unstake.
|
|
281
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
294
282
|
* @returns Returns void if successful. Throws error if any.
|
|
295
283
|
*
|
|
296
284
|
*
|
|
@@ -307,24 +295,25 @@ export class StakingClient extends BaseEthersClient {
|
|
|
307
295
|
* const signer = new Wallet(privateKey, provider);
|
|
308
296
|
* const stakingClient = await StakingClient.build(signer);
|
|
309
297
|
*
|
|
310
|
-
* const amount = ethers.
|
|
298
|
+
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
311
299
|
* await stakingClient.unstake(amount);
|
|
312
300
|
* ```
|
|
313
301
|
*/
|
|
314
302
|
@requiresSigner
|
|
315
|
-
public async unstake(
|
|
316
|
-
|
|
303
|
+
public async unstake(
|
|
304
|
+
amount: bigint,
|
|
305
|
+
txOptions: Overrides = {}
|
|
306
|
+
): Promise<void> {
|
|
307
|
+
if (typeof amount !== 'bigint') {
|
|
317
308
|
throw ErrorInvalidStakingValueType;
|
|
318
309
|
}
|
|
319
310
|
|
|
320
|
-
if (amount
|
|
311
|
+
if (amount < 0n) {
|
|
321
312
|
throw ErrorInvalidStakingValueSign;
|
|
322
313
|
}
|
|
323
314
|
|
|
324
315
|
try {
|
|
325
|
-
await this.stakingContract.unstake(amount,
|
|
326
|
-
...(await this.gasPriceOptions()),
|
|
327
|
-
});
|
|
316
|
+
await (await this.stakingContract.unstake(amount, txOptions)).wait();
|
|
328
317
|
return;
|
|
329
318
|
} catch (e) {
|
|
330
319
|
return throwError(e);
|
|
@@ -336,6 +325,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
336
325
|
*
|
|
337
326
|
* > Must have tokens available to withdraw
|
|
338
327
|
*
|
|
328
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
339
329
|
* @returns Returns void if successful. Throws error if any.
|
|
340
330
|
*
|
|
341
331
|
*
|
|
@@ -356,11 +346,9 @@ export class StakingClient extends BaseEthersClient {
|
|
|
356
346
|
* ```
|
|
357
347
|
*/
|
|
358
348
|
@requiresSigner
|
|
359
|
-
public async withdraw(): Promise<void> {
|
|
349
|
+
public async withdraw(txOptions: Overrides = {}): Promise<void> {
|
|
360
350
|
try {
|
|
361
|
-
await this.stakingContract.withdraw(
|
|
362
|
-
...(await this.gasPriceOptions()),
|
|
363
|
-
});
|
|
351
|
+
await (await this.stakingContract.withdraw(txOptions)).wait();
|
|
364
352
|
return;
|
|
365
353
|
} catch (e) {
|
|
366
354
|
return throwError(e);
|
|
@@ -373,7 +361,8 @@ export class StakingClient extends BaseEthersClient {
|
|
|
373
361
|
* @param {string} slasher Wallet address from who requested the slash
|
|
374
362
|
* @param {string} staker Wallet address from who is going to be slashed
|
|
375
363
|
* @param {string} escrowAddress Address of the escrow which allocation will be slashed
|
|
376
|
-
* @param {
|
|
364
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
365
|
+
* @param {bigint} amount Amount in WEI of tokens to unstake.
|
|
377
366
|
* @returns Returns void if successful. Throws error if any.
|
|
378
367
|
*
|
|
379
368
|
*
|
|
@@ -390,7 +379,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
390
379
|
* const signer = new Wallet(privateKey, provider);
|
|
391
380
|
* const stakingClient = await StakingClient.build(signer);
|
|
392
381
|
*
|
|
393
|
-
* const amount = ethers.
|
|
382
|
+
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
394
383
|
* await stakingClient.slash('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);
|
|
395
384
|
* ```
|
|
396
385
|
*/
|
|
@@ -399,30 +388,37 @@ export class StakingClient extends BaseEthersClient {
|
|
|
399
388
|
slasher: string,
|
|
400
389
|
staker: string,
|
|
401
390
|
escrowAddress: string,
|
|
402
|
-
amount:
|
|
391
|
+
amount: bigint,
|
|
392
|
+
txOptions: Overrides = {}
|
|
403
393
|
): Promise<void> {
|
|
404
|
-
if (
|
|
394
|
+
if (typeof amount !== 'bigint') {
|
|
405
395
|
throw ErrorInvalidStakingValueType;
|
|
406
396
|
}
|
|
407
397
|
|
|
408
|
-
if (amount
|
|
398
|
+
if (amount < 0n) {
|
|
409
399
|
throw ErrorInvalidStakingValueSign;
|
|
410
400
|
}
|
|
411
401
|
|
|
412
|
-
if (!ethers.
|
|
402
|
+
if (!ethers.isAddress(slasher)) {
|
|
413
403
|
throw ErrorInvalidSlasherAddressProvided;
|
|
414
404
|
}
|
|
415
405
|
|
|
416
|
-
if (!ethers.
|
|
406
|
+
if (!ethers.isAddress(staker)) {
|
|
417
407
|
throw ErrorInvalidStakerAddressProvided;
|
|
418
408
|
}
|
|
419
409
|
|
|
420
410
|
await this.checkValidEscrow(escrowAddress);
|
|
421
411
|
|
|
422
412
|
try {
|
|
423
|
-
await
|
|
424
|
-
|
|
425
|
-
|
|
413
|
+
await (
|
|
414
|
+
await this.stakingContract.slash(
|
|
415
|
+
slasher,
|
|
416
|
+
staker,
|
|
417
|
+
escrowAddress,
|
|
418
|
+
amount,
|
|
419
|
+
txOptions
|
|
420
|
+
)
|
|
421
|
+
).wait();
|
|
426
422
|
|
|
427
423
|
return;
|
|
428
424
|
} catch (e) {
|
|
@@ -436,7 +432,8 @@ export class StakingClient extends BaseEthersClient {
|
|
|
436
432
|
* > Must have tokens staked
|
|
437
433
|
*
|
|
438
434
|
* @param {string} escrowAddress Address of the escrow contract to allocate in.
|
|
439
|
-
* @param {
|
|
435
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
436
|
+
* @param {bigint} amount Amount in WEI of tokens to allocate.
|
|
440
437
|
* @returns Returns void if successful. Throws error if any.
|
|
441
438
|
*
|
|
442
439
|
*
|
|
@@ -453,29 +450,30 @@ export class StakingClient extends BaseEthersClient {
|
|
|
453
450
|
* const signer = new Wallet(privateKey, provider);
|
|
454
451
|
* const stakingClient = await StakingClient.build(signer);
|
|
455
452
|
*
|
|
456
|
-
* const amount = ethers.
|
|
453
|
+
* const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
|
|
457
454
|
* await stakingClient.allocate('0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);
|
|
458
455
|
* ```
|
|
459
456
|
*/
|
|
460
457
|
@requiresSigner
|
|
461
458
|
public async allocate(
|
|
462
459
|
escrowAddress: string,
|
|
463
|
-
amount:
|
|
460
|
+
amount: bigint,
|
|
461
|
+
txOptions: Overrides = {}
|
|
464
462
|
): Promise<void> {
|
|
465
|
-
if (
|
|
463
|
+
if (typeof amount !== 'bigint') {
|
|
466
464
|
throw ErrorInvalidStakingValueType;
|
|
467
465
|
}
|
|
468
466
|
|
|
469
|
-
if (amount
|
|
467
|
+
if (amount < 0n) {
|
|
470
468
|
throw ErrorInvalidStakingValueSign;
|
|
471
469
|
}
|
|
472
470
|
|
|
473
471
|
await this.checkValidEscrow(escrowAddress);
|
|
474
472
|
|
|
475
473
|
try {
|
|
476
|
-
await
|
|
477
|
-
|
|
478
|
-
|
|
474
|
+
await (
|
|
475
|
+
await this.stakingContract.allocate(escrowAddress, amount, txOptions)
|
|
476
|
+
).wait();
|
|
479
477
|
return;
|
|
480
478
|
} catch (e) {
|
|
481
479
|
return throwError(e);
|
|
@@ -489,6 +487,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
489
487
|
* > The escrow must be cancelled or completed.
|
|
490
488
|
*
|
|
491
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).
|
|
492
491
|
* @returns Returns void if successful. Throws error if any.
|
|
493
492
|
*
|
|
494
493
|
*
|
|
@@ -509,13 +508,16 @@ export class StakingClient extends BaseEthersClient {
|
|
|
509
508
|
* ```
|
|
510
509
|
*/
|
|
511
510
|
@requiresSigner
|
|
512
|
-
public async closeAllocation(
|
|
511
|
+
public async closeAllocation(
|
|
512
|
+
escrowAddress: string,
|
|
513
|
+
txOptions: Overrides = {}
|
|
514
|
+
): Promise<void> {
|
|
513
515
|
await this.checkValidEscrow(escrowAddress);
|
|
514
516
|
|
|
515
517
|
try {
|
|
516
|
-
await
|
|
517
|
-
|
|
518
|
-
|
|
518
|
+
await (
|
|
519
|
+
await this.stakingContract.closeAllocation(escrowAddress, txOptions)
|
|
520
|
+
).wait();
|
|
519
521
|
return;
|
|
520
522
|
} catch (e) {
|
|
521
523
|
return throwError(e);
|
|
@@ -528,6 +530,7 @@ export class StakingClient extends BaseEthersClient {
|
|
|
528
530
|
* > The escrow must have rewards added
|
|
529
531
|
*
|
|
530
532
|
* @param {string} escrowAddress Escrow address from which rewards are distributed.
|
|
533
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
531
534
|
* @returns Returns void if successful. Throws error if any.
|
|
532
535
|
*
|
|
533
536
|
*
|
|
@@ -548,93 +551,22 @@ export class StakingClient extends BaseEthersClient {
|
|
|
548
551
|
* ```
|
|
549
552
|
*/
|
|
550
553
|
@requiresSigner
|
|
551
|
-
public async distributeReward(
|
|
554
|
+
public async distributeReward(
|
|
555
|
+
escrowAddress: string,
|
|
556
|
+
txOptions: Overrides = {}
|
|
557
|
+
): Promise<void> {
|
|
552
558
|
await this.checkValidEscrow(escrowAddress);
|
|
553
559
|
|
|
554
560
|
try {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
561
|
+
(
|
|
562
|
+
await this.rewardPoolContract.distributeReward(escrowAddress, txOptions)
|
|
563
|
+
).wait();
|
|
558
564
|
return;
|
|
559
565
|
} catch (e) {
|
|
560
566
|
return throwError(e);
|
|
561
567
|
}
|
|
562
568
|
}
|
|
563
569
|
|
|
564
|
-
/**
|
|
565
|
-
* This function returns all the leader details of the protocol.
|
|
566
|
-
*
|
|
567
|
-
* @param {ILeadersFilter} filter Filter for the leaders.
|
|
568
|
-
* @returns {ILeader[]} Returns an array with all the leader details.
|
|
569
|
-
*
|
|
570
|
-
*
|
|
571
|
-
* **Code example**
|
|
572
|
-
*
|
|
573
|
-
* ```ts
|
|
574
|
-
* import { StakingClient } from '@human-protocol/sdk';
|
|
575
|
-
* import { providers } from 'ethers';
|
|
576
|
-
*
|
|
577
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
578
|
-
*
|
|
579
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
580
|
-
* const stakingClient = await StakingClient.build(provider);
|
|
581
|
-
*
|
|
582
|
-
* const leaders = await stakingClient.getLeaders();
|
|
583
|
-
* ```
|
|
584
|
-
*/
|
|
585
|
-
public async getLeader(address: string): Promise<ILeader> {
|
|
586
|
-
if (!ethers.utils.isAddress(address)) {
|
|
587
|
-
throw ErrorInvalidStakerAddressProvided;
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
try {
|
|
591
|
-
const { leader } = await gqlFetch<{
|
|
592
|
-
leader: ILeader;
|
|
593
|
-
}>(this.networkData.subgraphUrl, GET_LEADER_QUERY, {
|
|
594
|
-
address: address.toLowerCase(),
|
|
595
|
-
});
|
|
596
|
-
|
|
597
|
-
return leader;
|
|
598
|
-
} catch (e) {
|
|
599
|
-
return throwError(e);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
/**
|
|
604
|
-
* This function returns the leader data for the given address.
|
|
605
|
-
*
|
|
606
|
-
* @param {string} address Leader address.
|
|
607
|
-
* @returns {ILeader} Returns the leader details.
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
* **Code example**
|
|
611
|
-
*
|
|
612
|
-
* ```ts
|
|
613
|
-
* import { StakingClient } from '@human-protocol/sdk';
|
|
614
|
-
* import { providers } from 'ethers';
|
|
615
|
-
*
|
|
616
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
617
|
-
*
|
|
618
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
619
|
-
* const stakingClient = await StakingClient.build(provider);
|
|
620
|
-
*
|
|
621
|
-
* const leader = await stakingClient.getLeader('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
622
|
-
* ```
|
|
623
|
-
*/
|
|
624
|
-
public async getLeaders(filter: ILeadersFilter = {}): Promise<ILeader[]> {
|
|
625
|
-
try {
|
|
626
|
-
const { leaders } = await gqlFetch<{
|
|
627
|
-
leaders: ILeader[];
|
|
628
|
-
}>(this.networkData.subgraphUrl, GET_LEADERS_QUERY(filter), {
|
|
629
|
-
role: filter.role,
|
|
630
|
-
});
|
|
631
|
-
|
|
632
|
-
return leaders;
|
|
633
|
-
} catch (e) {
|
|
634
|
-
return throwError(e);
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
|
|
638
570
|
/**
|
|
639
571
|
* This function returns information about the allocation of the specified escrow.
|
|
640
572
|
*
|
|
@@ -666,48 +598,4 @@ export class StakingClient extends BaseEthersClient {
|
|
|
666
598
|
return throwError(e);
|
|
667
599
|
}
|
|
668
600
|
}
|
|
669
|
-
|
|
670
|
-
/**
|
|
671
|
-
* This function returns information about the rewards for a given slasher address.
|
|
672
|
-
*
|
|
673
|
-
* @param {string} slasherAddress Slasher address.
|
|
674
|
-
* @returns {IReward[]} Returns an array of Reward objects that contain the rewards earned by the user through slashing other users.
|
|
675
|
-
*
|
|
676
|
-
*
|
|
677
|
-
* **Code example**
|
|
678
|
-
*
|
|
679
|
-
* ```ts
|
|
680
|
-
* import { StakingClient } from '@human-protocol/sdk';
|
|
681
|
-
* import { providers } from 'ethers';
|
|
682
|
-
*
|
|
683
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
684
|
-
*
|
|
685
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
686
|
-
* const stakingClient = await StakingClient.build(provider);
|
|
687
|
-
*
|
|
688
|
-
* const rewards = await stakingClient.getRewards('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
689
|
-
* ```
|
|
690
|
-
*/
|
|
691
|
-
public async getRewards(slasherAddress: string): Promise<IReward[]> {
|
|
692
|
-
if (!ethers.utils.isAddress(slasherAddress)) {
|
|
693
|
-
throw ErrorInvalidSlasherAddressProvided;
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
try {
|
|
697
|
-
const { rewardAddedEvents } = await gqlFetch<{
|
|
698
|
-
rewardAddedEvents: RewardAddedEventData[];
|
|
699
|
-
}>(this.networkData.subgraphUrl, GET_REWARD_ADDED_EVENTS_QUERY, {
|
|
700
|
-
slasherAddress: slasherAddress.toLowerCase(),
|
|
701
|
-
});
|
|
702
|
-
|
|
703
|
-
return rewardAddedEvents.map((reward: any) => {
|
|
704
|
-
return {
|
|
705
|
-
escrowAddress: reward.escrow,
|
|
706
|
-
amount: reward.amount,
|
|
707
|
-
};
|
|
708
|
-
});
|
|
709
|
-
} catch (e) {
|
|
710
|
-
return throwError(e);
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
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
|
};
|