@human-protocol/sdk 5.2.0 → 6.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.
Files changed (63) hide show
  1. package/dist/base.d.ts +4 -5
  2. package/dist/base.d.ts.map +1 -1
  3. package/dist/base.js +4 -5
  4. package/dist/constants.js +6 -6
  5. package/dist/encryption.d.ts +68 -203
  6. package/dist/encryption.d.ts.map +1 -1
  7. package/dist/encryption.js +66 -202
  8. package/dist/error.d.ts +0 -24
  9. package/dist/error.d.ts.map +1 -1
  10. package/dist/error.js +2 -26
  11. package/dist/escrow.d.ts +427 -780
  12. package/dist/escrow.d.ts.map +1 -1
  13. package/dist/escrow.js +314 -684
  14. package/dist/graphql/queries/operator.d.ts.map +1 -1
  15. package/dist/graphql/queries/operator.js +3 -1
  16. package/dist/graphql/types.d.ts.map +1 -1
  17. package/dist/index.d.ts +3 -4
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +2 -4
  20. package/dist/kvstore.d.ts +119 -181
  21. package/dist/kvstore.d.ts.map +1 -1
  22. package/dist/kvstore.js +119 -182
  23. package/dist/operator.d.ts +59 -30
  24. package/dist/operator.d.ts.map +1 -1
  25. package/dist/operator.js +59 -30
  26. package/dist/staking.d.ts +135 -134
  27. package/dist/staking.d.ts.map +1 -1
  28. package/dist/staking.js +135 -134
  29. package/dist/statistics.d.ts +104 -134
  30. package/dist/statistics.d.ts.map +1 -1
  31. package/dist/statistics.js +119 -144
  32. package/dist/transaction.d.ts +36 -15
  33. package/dist/transaction.d.ts.map +1 -1
  34. package/dist/transaction.js +36 -15
  35. package/dist/types.d.ts +0 -54
  36. package/dist/types.d.ts.map +1 -1
  37. package/dist/utils.d.ts +31 -17
  38. package/dist/utils.d.ts.map +1 -1
  39. package/dist/utils.js +31 -17
  40. package/dist/worker.d.ts +35 -14
  41. package/dist/worker.d.ts.map +1 -1
  42. package/dist/worker.js +35 -14
  43. package/package.json +7 -24
  44. package/src/base.ts +4 -5
  45. package/src/constants.ts +6 -6
  46. package/src/encryption.ts +69 -203
  47. package/src/error.ts +0 -36
  48. package/src/escrow.ts +425 -780
  49. package/src/graphql/queries/operator.ts +3 -1
  50. package/src/graphql/types.ts +4 -2
  51. package/src/index.ts +4 -5
  52. package/src/kvstore.ts +120 -183
  53. package/src/operator.ts +59 -30
  54. package/src/staking.ts +135 -134
  55. package/src/statistics.ts +125 -146
  56. package/src/transaction.ts +36 -15
  57. package/src/types.ts +0 -57
  58. package/src/utils.ts +31 -17
  59. package/src/worker.ts +35 -14
  60. package/dist/storage.d.ts +0 -186
  61. package/dist/storage.d.ts.map +0 -1
  62. package/dist/storage.js +0 -319
  63. package/src/storage.ts +0 -313
package/src/staking.ts CHANGED
@@ -37,9 +37,7 @@ import {
37
37
  } from './graphql/queries/staking';
38
38
 
39
39
  /**
40
- * ## Introduction
41
- *
42
- * This client enables performing actions on staking contracts and obtaining staking information from both the contracts and subgraph.
40
+ * Client for staking actions on HUMAN Protocol.
43
41
  *
44
42
  * Internally, the SDK will use one network or another according to the network ID of the `runner`.
45
43
  * To use this client, it is recommended to initialize it using the static `build` method.
@@ -53,37 +51,25 @@ import {
53
51
  * - **Signer**: when the user wants to use this model to send transactions calling the contract functions.
54
52
  * - **Provider**: when the user wants to use this model to get information from the contracts or subgraph.
55
53
  *
56
- * ## Installation
57
- *
58
- * ### npm
59
- * ```bash
60
- * npm install @human-protocol/sdk
61
- * ```
62
- *
63
- * ### yarn
64
- * ```bash
65
- * yarn install @human-protocol/sdk
66
- * ```
67
- *
68
- * ## Code example
54
+ * @example
69
55
  *
70
- * ### Signer
56
+ * ###Using Signer
71
57
  *
72
- * **Using private key (backend)**
58
+ * ####Using private key (backend)
73
59
  *
74
60
  * ```ts
75
61
  * import { StakingClient } from '@human-protocol/sdk';
76
- * import { Wallet, providers } from 'ethers';
62
+ * import { Wallet, JsonRpcProvider } from 'ethers';
77
63
  *
78
64
  * const rpcUrl = 'YOUR_RPC_URL';
79
65
  * const privateKey = 'YOUR_PRIVATE_KEY';
80
66
  *
81
- * const provider = new providers.JsonRpcProvider(rpcUrl);
67
+ * const provider = new JsonRpcProvider(rpcUrl);
82
68
  * const signer = new Wallet(privateKey, provider);
83
69
  * const stakingClient = await StakingClient.build(signer);
84
70
  * ```
85
71
  *
86
- * **Using Wagmi (frontend)**
72
+ * ####Using Wagmi (frontend)
87
73
  *
88
74
  * ```ts
89
75
  * import { useSigner, useChainId } from 'wagmi';
@@ -93,15 +79,15 @@ import {
93
79
  * const stakingClient = await StakingClient.build(signer);
94
80
  * ```
95
81
  *
96
- * ### Provider
82
+ * ###Using Provider
97
83
  *
98
84
  * ```ts
99
85
  * import { StakingClient } from '@human-protocol/sdk';
100
- * import { providers } from 'ethers';
86
+ * import { JsonRpcProvider } from 'ethers';
101
87
  *
102
88
  * const rpcUrl = 'YOUR_RPC_URL';
103
89
  *
104
- * const provider = new providers.JsonRpcProvider(rpcUrl);
90
+ * const provider = new JsonRpcProvider(rpcUrl);
105
91
  * const stakingClient = await StakingClient.build(provider);
106
92
  * ```
107
93
  */
@@ -113,8 +99,8 @@ export class StakingClient extends BaseEthersClient {
113
99
  /**
114
100
  * **StakingClient constructor**
115
101
  *
116
- * @param {ContractRunner} runner - The Runner object to interact with the Ethereum network
117
- * @param {NetworkData} networkData - The network information required to connect to the Staking contract
102
+ * @param runner - The Runner object to interact with the Ethereum network
103
+ * @param networkData - The network information required to connect to the Staking contract
118
104
  */
119
105
  constructor(runner: ContractRunner, networkData: NetworkData) {
120
106
  super(runner, networkData);
@@ -138,11 +124,23 @@ export class StakingClient extends BaseEthersClient {
138
124
  /**
139
125
  * Creates an instance of StakingClient from a Runner.
140
126
  *
141
- * @param {ContractRunner} runner - The Runner object to interact with the Ethereum network
127
+ * @param runner - The Runner object to interact with the Ethereum network
128
+ * @returns An instance of StakingClient
129
+ * @throws ErrorProviderDoesNotExist If the provider does not exist for the provided Signer
130
+ * @throws ErrorUnsupportedChainID If the network's chainId is not supported
142
131
  *
143
- * @returns {Promise<StakingClient>} - An instance of StakingClient
144
- * @throws {ErrorProviderDoesNotExist} - Thrown if the provider does not exist for the provided Signer
145
- * @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
132
+ * @example
133
+ * ```ts
134
+ * import { StakingClient } from '@human-protocol/sdk';
135
+ * import { Wallet, JsonRpcProvider } from 'ethers';
136
+ *
137
+ * const rpcUrl = 'YOUR_RPC_URL';
138
+ * const privateKey = 'YOUR_PRIVATE_KEY';
139
+ *
140
+ * const provider = new JsonRpcProvider(rpcUrl);
141
+ * const signer = new Wallet(privateKey, provider);
142
+ * const stakingClient = await StakingClient.build(signer);
143
+ * ```
146
144
  */
147
145
  public static async build(runner: ContractRunner): Promise<StakingClient> {
148
146
  if (!runner.provider) {
@@ -179,24 +177,17 @@ export class StakingClient extends BaseEthersClient {
179
177
  /**
180
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.
181
179
  *
182
- * @param {bigint} amount Amount in WEI of tokens to approve for stake.
183
- * @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
184
- * @returns Returns void if successful. Throws error if any.
185
- *
186
- * **Code example**
180
+ * @param amount - Amount in WEI of tokens to approve for stake.
181
+ * @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
182
+ * @returns -
183
+ * @throws ErrorInvalidStakingValueType If the amount is not a bigint
184
+ * @throws ErrorInvalidStakingValueSign If the amount is negative
187
185
  *
186
+ * @example
188
187
  * ```ts
189
- * import { ethers, Wallet, providers } from 'ethers';
190
- * import { StakingClient } from '@human-protocol/sdk';
191
- *
192
- * const rpcUrl = 'YOUR_RPC_URL';
193
- * const privateKey = 'YOUR_PRIVATE_KEY';
188
+ * import { ethers } from 'ethers';
194
189
  *
195
- * const provider = new providers.JsonRpcProvider(rpcUrl);
196
- * const signer = new Wallet(privateKey, provider);
197
- * const stakingClient = await StakingClient.build(signer);
198
- *
199
- * const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
190
+ * const amount = ethers.parseUnits('5', 'ether'); //convert from ETH to WEI
200
191
  * await stakingClient.approveStake(amount);
201
192
  * ```
202
193
  */
@@ -230,26 +221,20 @@ export class StakingClient extends BaseEthersClient {
230
221
  /**
231
222
  * This function stakes a specified amount of tokens on a specific network.
232
223
  *
233
- * > `approveStake` must be called before
234
- *
235
- * @param {bigint} amount Amount in WEI of tokens to stake.
236
- * @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
237
- * @returns Returns void if successful. Throws error if any.
224
+ * !!! note
225
+ * `approveStake` must be called before
238
226
  *
239
- * **Code example**
227
+ * @param amount - Amount in WEI of tokens to stake.
228
+ * @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
229
+ * @returns -
230
+ * @throws ErrorInvalidStakingValueType If the amount is not a bigint
231
+ * @throws ErrorInvalidStakingValueSign If the amount is negative
240
232
  *
233
+ * @example
241
234
  * ```ts
242
- * import { ethers, Wallet, providers } from 'ethers';
243
- * import { StakingClient } from '@human-protocol/sdk';
235
+ * import { ethers } from 'ethers';
244
236
  *
245
- * const rpcUrl = 'YOUR_RPC_URL';
246
- * const privateKey = 'YOUR_PRIVATE_KEY';
247
- *
248
- * const provider = new providers.JsonRpcProvider(rpcUrl);
249
- * const signer = new Wallet(privateKey, provider);
250
- * const stakingClient = await StakingClient.build(signer);
251
- *
252
- * const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
237
+ * const amount = ethers.parseUnits('5', 'ether'); //convert from ETH to WEI
253
238
  * await stakingClient.approveStake(amount); // if it was already approved before, this is not necessary
254
239
  * await stakingClient.stake(amount);
255
240
  * ```
@@ -275,26 +260,20 @@ export class StakingClient extends BaseEthersClient {
275
260
  /**
276
261
  * This function unstakes tokens from staking contract. The unstaked tokens stay locked for a period of time.
277
262
  *
278
- * > Must have tokens available to unstake
279
- *
280
- * @param {bigint} amount Amount in WEI of tokens to unstake.
281
- * @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
282
- * @returns Returns void if successful. Throws error if any.
263
+ * !!! note
264
+ * Must have tokens available to unstake
283
265
  *
284
- * **Code example**
266
+ * @param amount - Amount in WEI of tokens to unstake.
267
+ * @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
268
+ * @returns -
269
+ * @throws ErrorInvalidStakingValueType If the amount is not a bigint
270
+ * @throws ErrorInvalidStakingValueSign If the amount is negative
285
271
  *
272
+ * @example
286
273
  * ```ts
287
- * import { ethers, Wallet, providers } from 'ethers';
288
- * import { StakingClient } from '@human-protocol/sdk';
289
- *
290
- * const rpcUrl = 'YOUR_RPC_URL';
291
- * const privateKey = 'YOUR_PRIVATE_KEY';
292
- *
293
- * const provider = new providers.JsonRpcProvider(rpcUrl);
294
- * const signer = new Wallet(privateKey, provider);
295
- * const stakingClient = await StakingClient.build(signer);
274
+ * import { ethers } from 'ethers';
296
275
  *
297
- * const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
276
+ * const amount = ethers.parseUnits('5', 'ether'); //convert from ETH to WEI
298
277
  * await stakingClient.unstake(amount);
299
278
  * ```
300
279
  */
@@ -321,25 +300,14 @@ export class StakingClient extends BaseEthersClient {
321
300
 
322
301
  /**
323
302
  * This function withdraws unstaked and non-locked tokens from staking contract to the user wallet.
303
+ * !!! note
304
+ * Must have tokens available to withdraw
324
305
  *
325
- * > Must have tokens available to withdraw
326
- *
327
- * @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
328
- * @returns Returns void if successful. Throws error if any.
329
- *
330
- * **Code example**
306
+ * @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
307
+ * @returns -
331
308
  *
309
+ * @example
332
310
  * ```ts
333
- * import { Wallet, providers } from 'ethers';
334
- * import { StakingClient } from '@human-protocol/sdk';
335
- *
336
- * const rpcUrl = 'YOUR_RPC_URL';
337
- * const privateKey = 'YOUR_PRIVATE_KEY';
338
- *
339
- * const provider = new providers.JsonRpcProvider(rpcUrl);
340
- * const signer = new Wallet(privateKey, provider);
341
- * const stakingClient = await StakingClient.build(signer);
342
- *
343
311
  * await stakingClient.withdraw();
344
312
  * ```
345
313
  */
@@ -356,28 +324,30 @@ export class StakingClient extends BaseEthersClient {
356
324
  /**
357
325
  * 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.
358
326
  *
359
- * @param {string} slasher Wallet address from who requested the slash
360
- * @param {string} staker Wallet address from who is going to be slashed
361
- * @param {string} escrowAddress Address of the escrow that the slash is made
362
- * @param {bigint} amount Amount in WEI of tokens to slash.
363
- * @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
364
- * @returns Returns void if successful. Throws error if any.
365
- *
366
- * **Code example**
367
- *
327
+ * @param slasher - Wallet address from who requested the slash
328
+ * @param staker - Wallet address from who is going to be slashed
329
+ * @param escrowAddress - Address of the escrow that the slash is made
330
+ * @param amount - Amount in WEI of tokens to slash.
331
+ * @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
332
+ * @returns -
333
+ * @throws ErrorInvalidStakingValueType If the amount is not a bigint
334
+ * @throws ErrorInvalidStakingValueSign If the amount is negative
335
+ * @throws ErrorInvalidSlasherAddressProvided If the slasher address is invalid
336
+ * @throws ErrorInvalidStakerAddressProvided If the staker address is invalid
337
+ * @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
338
+ * @throws ErrorEscrowAddressIsNotProvidedByFactory If the escrow is not provided by the factory
339
+ *
340
+ * @example
368
341
  * ```ts
369
- * import { ethers, Wallet, providers } from 'ethers';
370
- * import { StakingClient } from '@human-protocol/sdk';
371
- *
372
- * const rpcUrl = 'YOUR_RPC_URL';
373
- * const privateKey = 'YOUR_PRIVATE_KEY';
374
- *
375
- * const provider = new providers.JsonRpcProvider(rpcUrl);
376
- * const signer = new Wallet(privateKey, provider);
377
- * const stakingClient = await StakingClient.build(signer);
378
- *
379
- * const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
380
- * await stakingClient.slash('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);
342
+ * import { ethers } from 'ethers';
343
+ *
344
+ * const amount = ethers.parseUnits('5', 'ether'); //convert from ETH to WEI
345
+ * await stakingClient.slash(
346
+ * '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
347
+ * '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
348
+ * '0x62dD51230A30401C455c8398d06F85e4EaB6309f',
349
+ * amount
350
+ * );
381
351
  * ```
382
352
  */
383
353
  @requiresSigner
@@ -426,21 +396,14 @@ export class StakingClient extends BaseEthersClient {
426
396
  /**
427
397
  * Retrieves comprehensive staking information for a staker.
428
398
  *
429
- * @param {string} stakerAddress - The address of the staker.
430
- * @returns {Promise<StakerInfo>}
431
- *
432
- * **Code example**
399
+ * @param stakerAddress - The address of the staker.
400
+ * @returns Staking information for the staker
401
+ * @throws ErrorInvalidStakerAddressProvided If the staker address is invalid
433
402
  *
403
+ * @example
434
404
  * ```ts
435
- * import { StakingClient } from '@human-protocol/sdk';
436
- *
437
- * const rpcUrl = 'YOUR_RPC_URL';
438
- *
439
- * const provider = new providers.JsonRpcProvider(rpcUrl);
440
- * const stakingClient = await StakingClient.build(provider);
441
- *
442
405
  * const stakingInfo = await stakingClient.getStakerInfo('0xYourStakerAddress');
443
- * console.log(stakingInfo.tokensStaked);
406
+ * console.log('Tokens staked:', stakingInfo.stakedAmount);
444
407
  * ```
445
408
  */
446
409
  public async getStakerInfo(stakerAddress: string): Promise<StakerInfo> {
@@ -479,16 +442,41 @@ export class StakingClient extends BaseEthersClient {
479
442
  }
480
443
 
481
444
  /**
482
- * Utility class for Staking-related subgraph queries.
445
+ * Utility helpers for Staking-related queries.
446
+ *
447
+ * @example
448
+ * ```ts
449
+ * import { StakingUtils, ChainId } from '@human-protocol/sdk';
450
+ *
451
+ * const staker = await StakingUtils.getStaker(
452
+ * ChainId.POLYGON_AMOY,
453
+ * '0xYourStakerAddress'
454
+ * );
455
+ * console.log('Staked amount:', staker.stakedAmount);
456
+ * ```
483
457
  */
484
458
  export class StakingUtils {
485
459
  /**
486
460
  * Gets staking info for a staker from the subgraph.
487
461
  *
488
- * @param {ChainId} chainId Network in which the staking contract is deployed
489
- * @param {string} stakerAddress Address of the staker
490
- * @param {SubgraphOptions} options Optional configuration for subgraph requests.
491
- * @returns {Promise<IStaker>} Staker info from subgraph
462
+ * @param chainId - Network in which the staking contract is deployed
463
+ * @param stakerAddress - Address of the staker
464
+ * @param options - Optional configuration for subgraph requests.
465
+ * @returns Staker info from subgraph
466
+ * @throws ErrorInvalidStakerAddressProvided If the staker address is invalid
467
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
468
+ * @throws ErrorStakerNotFound If the staker is not found
469
+ *
470
+ * @example
471
+ * ```ts
472
+ * import { StakingUtils, ChainId } from '@human-protocol/sdk';
473
+ *
474
+ * const staker = await StakingUtils.getStaker(
475
+ * ChainId.POLYGON_AMOY,
476
+ * '0xYourStakerAddress'
477
+ * );
478
+ * console.log('Staked amount:', staker.stakedAmount);
479
+ * ```
492
480
  */
493
481
  public static async getStaker(
494
482
  chainId: ChainId,
@@ -521,9 +509,22 @@ export class StakingUtils {
521
509
  /**
522
510
  * Gets all stakers from the subgraph with filters, pagination and ordering.
523
511
  *
524
- * @param {IStakersFilter} filter Stakers filter with pagination and ordering
525
- * @param {SubgraphOptions} options Optional configuration for subgraph requests.
526
- * @returns {Promise<IStaker[]>} Array of stakers
512
+ * @param filter - Stakers filter with pagination and ordering
513
+ * @param options - Optional configuration for subgraph requests.
514
+ * @returns Array of stakers
515
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
516
+ *
517
+ * @example
518
+ * ```ts
519
+ * import { ChainId } from '@human-protocol/sdk';
520
+ *
521
+ * const filter = {
522
+ * chainId: ChainId.POLYGON_AMOY,
523
+ * minStakedAmount: '1000000000000000000', // 1 token in WEI
524
+ * };
525
+ * const stakers = await StakingUtils.getStakers(filter);
526
+ * console.log('Stakers:', stakers.length);
527
+ * ```
527
528
  */
528
529
  public static async getStakers(
529
530
  filter: IStakersFilter,