@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/dist/staking.js CHANGED
@@ -20,9 +20,7 @@ const error_1 = require("./error");
20
20
  const utils_1 = require("./utils");
21
21
  const staking_1 = require("./graphql/queries/staking");
22
22
  /**
23
- * ## Introduction
24
- *
25
- * This client enables performing actions on staking contracts and obtaining staking information from both the contracts and subgraph.
23
+ * Client for staking actions on HUMAN Protocol.
26
24
  *
27
25
  * Internally, the SDK will use one network or another according to the network ID of the `runner`.
28
26
  * To use this client, it is recommended to initialize it using the static `build` method.
@@ -36,37 +34,25 @@ const staking_1 = require("./graphql/queries/staking");
36
34
  * - **Signer**: when the user wants to use this model to send transactions calling the contract functions.
37
35
  * - **Provider**: when the user wants to use this model to get information from the contracts or subgraph.
38
36
  *
39
- * ## Installation
40
- *
41
- * ### npm
42
- * ```bash
43
- * npm install @human-protocol/sdk
44
- * ```
45
- *
46
- * ### yarn
47
- * ```bash
48
- * yarn install @human-protocol/sdk
49
- * ```
50
- *
51
- * ## Code example
37
+ * @example
52
38
  *
53
- * ### Signer
39
+ * ###Using Signer
54
40
  *
55
- * **Using private key (backend)**
41
+ * ####Using private key (backend)
56
42
  *
57
43
  * ```ts
58
44
  * import { StakingClient } from '@human-protocol/sdk';
59
- * import { Wallet, providers } from 'ethers';
45
+ * import { Wallet, JsonRpcProvider } from 'ethers';
60
46
  *
61
47
  * const rpcUrl = 'YOUR_RPC_URL';
62
48
  * const privateKey = 'YOUR_PRIVATE_KEY';
63
49
  *
64
- * const provider = new providers.JsonRpcProvider(rpcUrl);
50
+ * const provider = new JsonRpcProvider(rpcUrl);
65
51
  * const signer = new Wallet(privateKey, provider);
66
52
  * const stakingClient = await StakingClient.build(signer);
67
53
  * ```
68
54
  *
69
- * **Using Wagmi (frontend)**
55
+ * ####Using Wagmi (frontend)
70
56
  *
71
57
  * ```ts
72
58
  * import { useSigner, useChainId } from 'wagmi';
@@ -76,15 +62,15 @@ const staking_1 = require("./graphql/queries/staking");
76
62
  * const stakingClient = await StakingClient.build(signer);
77
63
  * ```
78
64
  *
79
- * ### Provider
65
+ * ###Using Provider
80
66
  *
81
67
  * ```ts
82
68
  * import { StakingClient } from '@human-protocol/sdk';
83
- * import { providers } from 'ethers';
69
+ * import { JsonRpcProvider } from 'ethers';
84
70
  *
85
71
  * const rpcUrl = 'YOUR_RPC_URL';
86
72
  *
87
- * const provider = new providers.JsonRpcProvider(rpcUrl);
73
+ * const provider = new JsonRpcProvider(rpcUrl);
88
74
  * const stakingClient = await StakingClient.build(provider);
89
75
  * ```
90
76
  */
@@ -92,8 +78,8 @@ class StakingClient extends base_1.BaseEthersClient {
92
78
  /**
93
79
  * **StakingClient constructor**
94
80
  *
95
- * @param {ContractRunner} runner - The Runner object to interact with the Ethereum network
96
- * @param {NetworkData} networkData - The network information required to connect to the Staking contract
81
+ * @param runner - The Runner object to interact with the Ethereum network
82
+ * @param networkData - The network information required to connect to the Staking contract
97
83
  */
98
84
  constructor(runner, networkData) {
99
85
  super(runner, networkData);
@@ -104,11 +90,23 @@ class StakingClient extends base_1.BaseEthersClient {
104
90
  /**
105
91
  * Creates an instance of StakingClient from a Runner.
106
92
  *
107
- * @param {ContractRunner} runner - The Runner object to interact with the Ethereum network
93
+ * @param runner - The Runner object to interact with the Ethereum network
94
+ * @returns An instance of StakingClient
95
+ * @throws ErrorProviderDoesNotExist If the provider does not exist for the provided Signer
96
+ * @throws ErrorUnsupportedChainID If the network's chainId is not supported
108
97
  *
109
- * @returns {Promise<StakingClient>} - An instance of StakingClient
110
- * @throws {ErrorProviderDoesNotExist} - Thrown if the provider does not exist for the provided Signer
111
- * @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
98
+ * @example
99
+ * ```ts
100
+ * import { StakingClient } from '@human-protocol/sdk';
101
+ * import { Wallet, JsonRpcProvider } from 'ethers';
102
+ *
103
+ * const rpcUrl = 'YOUR_RPC_URL';
104
+ * const privateKey = 'YOUR_PRIVATE_KEY';
105
+ *
106
+ * const provider = new JsonRpcProvider(rpcUrl);
107
+ * const signer = new Wallet(privateKey, provider);
108
+ * const stakingClient = await StakingClient.build(signer);
109
+ * ```
112
110
  */
113
111
  static async build(runner) {
114
112
  if (!runner.provider) {
@@ -138,24 +136,17 @@ class StakingClient extends base_1.BaseEthersClient {
138
136
  /**
139
137
  * 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.
140
138
  *
141
- * @param {bigint} amount Amount in WEI of tokens to approve for stake.
142
- * @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
143
- * @returns Returns void if successful. Throws error if any.
144
- *
145
- * **Code example**
139
+ * @param amount - Amount in WEI of tokens to approve for stake.
140
+ * @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
141
+ * @returns -
142
+ * @throws ErrorInvalidStakingValueType If the amount is not a bigint
143
+ * @throws ErrorInvalidStakingValueSign If the amount is negative
146
144
  *
145
+ * @example
147
146
  * ```ts
148
- * import { ethers, Wallet, providers } from 'ethers';
149
- * import { StakingClient } from '@human-protocol/sdk';
150
- *
151
- * const rpcUrl = 'YOUR_RPC_URL';
152
- * const privateKey = 'YOUR_PRIVATE_KEY';
147
+ * import { ethers } from 'ethers';
153
148
  *
154
- * const provider = new providers.JsonRpcProvider(rpcUrl);
155
- * const signer = new Wallet(privateKey, provider);
156
- * const stakingClient = await StakingClient.build(signer);
157
- *
158
- * const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
149
+ * const amount = ethers.parseUnits('5', 'ether'); //convert from ETH to WEI
159
150
  * await stakingClient.approveStake(amount);
160
151
  * ```
161
152
  */
@@ -177,26 +168,20 @@ class StakingClient extends base_1.BaseEthersClient {
177
168
  /**
178
169
  * This function stakes a specified amount of tokens on a specific network.
179
170
  *
180
- * > `approveStake` must be called before
181
- *
182
- * @param {bigint} amount Amount in WEI of tokens to stake.
183
- * @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
184
- * @returns Returns void if successful. Throws error if any.
171
+ * !!! note
172
+ * `approveStake` must be called before
185
173
  *
186
- * **Code example**
174
+ * @param amount - Amount in WEI of tokens to stake.
175
+ * @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
176
+ * @returns -
177
+ * @throws ErrorInvalidStakingValueType If the amount is not a bigint
178
+ * @throws ErrorInvalidStakingValueSign If the amount is negative
187
179
  *
180
+ * @example
188
181
  * ```ts
189
- * import { ethers, Wallet, providers } from 'ethers';
190
- * import { StakingClient } from '@human-protocol/sdk';
182
+ * import { ethers } from 'ethers';
191
183
  *
192
- * const rpcUrl = 'YOUR_RPC_URL';
193
- * const privateKey = 'YOUR_PRIVATE_KEY';
194
- *
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
184
+ * const amount = ethers.parseUnits('5', 'ether'); //convert from ETH to WEI
200
185
  * await stakingClient.approveStake(amount); // if it was already approved before, this is not necessary
201
186
  * await stakingClient.stake(amount);
202
187
  * ```
@@ -219,26 +204,20 @@ class StakingClient extends base_1.BaseEthersClient {
219
204
  /**
220
205
  * This function unstakes tokens from staking contract. The unstaked tokens stay locked for a period of time.
221
206
  *
222
- * > Must have tokens available to unstake
223
- *
224
- * @param {bigint} amount Amount in WEI of tokens to unstake.
225
- * @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
226
- * @returns Returns void if successful. Throws error if any.
207
+ * !!! note
208
+ * Must have tokens available to unstake
227
209
  *
228
- * **Code example**
210
+ * @param amount - Amount in WEI of tokens to unstake.
211
+ * @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
212
+ * @returns -
213
+ * @throws ErrorInvalidStakingValueType If the amount is not a bigint
214
+ * @throws ErrorInvalidStakingValueSign If the amount is negative
229
215
  *
216
+ * @example
230
217
  * ```ts
231
- * import { ethers, Wallet, providers } from 'ethers';
232
- * import { StakingClient } from '@human-protocol/sdk';
233
- *
234
- * const rpcUrl = 'YOUR_RPC_URL';
235
- * const privateKey = 'YOUR_PRIVATE_KEY';
236
- *
237
- * const provider = new providers.JsonRpcProvider(rpcUrl);
238
- * const signer = new Wallet(privateKey, provider);
239
- * const stakingClient = await StakingClient.build(signer);
218
+ * import { ethers } from 'ethers';
240
219
  *
241
- * const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
220
+ * const amount = ethers.parseUnits('5', 'ether'); //convert from ETH to WEI
242
221
  * await stakingClient.unstake(amount);
243
222
  * ```
244
223
  */
@@ -259,25 +238,14 @@ class StakingClient extends base_1.BaseEthersClient {
259
238
  }
260
239
  /**
261
240
  * This function withdraws unstaked and non-locked tokens from staking contract to the user wallet.
241
+ * !!! note
242
+ * Must have tokens available to withdraw
262
243
  *
263
- * > Must have tokens available to withdraw
264
- *
265
- * @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
266
- * @returns Returns void if successful. Throws error if any.
267
- *
268
- * **Code example**
244
+ * @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
245
+ * @returns -
269
246
  *
247
+ * @example
270
248
  * ```ts
271
- * import { Wallet, providers } from 'ethers';
272
- * import { StakingClient } from '@human-protocol/sdk';
273
- *
274
- * const rpcUrl = 'YOUR_RPC_URL';
275
- * const privateKey = 'YOUR_PRIVATE_KEY';
276
- *
277
- * const provider = new providers.JsonRpcProvider(rpcUrl);
278
- * const signer = new Wallet(privateKey, provider);
279
- * const stakingClient = await StakingClient.build(signer);
280
- *
281
249
  * await stakingClient.withdraw();
282
250
  * ```
283
251
  */
@@ -293,28 +261,30 @@ class StakingClient extends base_1.BaseEthersClient {
293
261
  /**
294
262
  * 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.
295
263
  *
296
- * @param {string} slasher Wallet address from who requested the slash
297
- * @param {string} staker Wallet address from who is going to be slashed
298
- * @param {string} escrowAddress Address of the escrow that the slash is made
299
- * @param {bigint} amount Amount in WEI of tokens to slash.
300
- * @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
301
- * @returns Returns void if successful. Throws error if any.
302
- *
303
- * **Code example**
304
- *
264
+ * @param slasher - Wallet address from who requested the slash
265
+ * @param staker - Wallet address from who is going to be slashed
266
+ * @param escrowAddress - Address of the escrow that the slash is made
267
+ * @param amount - Amount in WEI of tokens to slash.
268
+ * @param txOptions - Additional transaction parameters (optional, defaults to an empty object).
269
+ * @returns -
270
+ * @throws ErrorInvalidStakingValueType If the amount is not a bigint
271
+ * @throws ErrorInvalidStakingValueSign If the amount is negative
272
+ * @throws ErrorInvalidSlasherAddressProvided If the slasher address is invalid
273
+ * @throws ErrorInvalidStakerAddressProvided If the staker address is invalid
274
+ * @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
275
+ * @throws ErrorEscrowAddressIsNotProvidedByFactory If the escrow is not provided by the factory
276
+ *
277
+ * @example
305
278
  * ```ts
306
- * import { ethers, Wallet, providers } from 'ethers';
307
- * import { StakingClient } from '@human-protocol/sdk';
308
- *
309
- * const rpcUrl = 'YOUR_RPC_URL';
310
- * const privateKey = 'YOUR_PRIVATE_KEY';
311
- *
312
- * const provider = new providers.JsonRpcProvider(rpcUrl);
313
- * const signer = new Wallet(privateKey, provider);
314
- * const stakingClient = await StakingClient.build(signer);
315
- *
316
- * const amount = ethers.parseUnits(5, 'ether'); //convert from ETH to WEI
317
- * await stakingClient.slash('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);
279
+ * import { ethers } from 'ethers';
280
+ *
281
+ * const amount = ethers.parseUnits('5', 'ether'); //convert from ETH to WEI
282
+ * await stakingClient.slash(
283
+ * '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
284
+ * '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
285
+ * '0x62dD51230A30401C455c8398d06F85e4EaB6309f',
286
+ * amount
287
+ * );
318
288
  * ```
319
289
  */
320
290
  async slash(slasher, staker, escrowAddress, amount, txOptions = {}) {
@@ -342,21 +312,14 @@ class StakingClient extends base_1.BaseEthersClient {
342
312
  /**
343
313
  * Retrieves comprehensive staking information for a staker.
344
314
  *
345
- * @param {string} stakerAddress - The address of the staker.
346
- * @returns {Promise<StakerInfo>}
347
- *
348
- * **Code example**
315
+ * @param stakerAddress - The address of the staker.
316
+ * @returns Staking information for the staker
317
+ * @throws ErrorInvalidStakerAddressProvided If the staker address is invalid
349
318
  *
319
+ * @example
350
320
  * ```ts
351
- * import { StakingClient } from '@human-protocol/sdk';
352
- *
353
- * const rpcUrl = 'YOUR_RPC_URL';
354
- *
355
- * const provider = new providers.JsonRpcProvider(rpcUrl);
356
- * const stakingClient = await StakingClient.build(provider);
357
- *
358
321
  * const stakingInfo = await stakingClient.getStakerInfo('0xYourStakerAddress');
359
- * console.log(stakingInfo.tokensStaked);
322
+ * console.log('Tokens staked:', stakingInfo.stakedAmount);
360
323
  * ```
361
324
  */
362
325
  async getStakerInfo(stakerAddress) {
@@ -418,16 +381,41 @@ __decorate([
418
381
  __metadata("design:returntype", Promise)
419
382
  ], StakingClient.prototype, "slash", null);
420
383
  /**
421
- * Utility class for Staking-related subgraph queries.
384
+ * Utility helpers for Staking-related queries.
385
+ *
386
+ * @example
387
+ * ```ts
388
+ * import { StakingUtils, ChainId } from '@human-protocol/sdk';
389
+ *
390
+ * const staker = await StakingUtils.getStaker(
391
+ * ChainId.POLYGON_AMOY,
392
+ * '0xYourStakerAddress'
393
+ * );
394
+ * console.log('Staked amount:', staker.stakedAmount);
395
+ * ```
422
396
  */
423
397
  class StakingUtils {
424
398
  /**
425
399
  * Gets staking info for a staker from the subgraph.
426
400
  *
427
- * @param {ChainId} chainId Network in which the staking contract is deployed
428
- * @param {string} stakerAddress Address of the staker
429
- * @param {SubgraphOptions} options Optional configuration for subgraph requests.
430
- * @returns {Promise<IStaker>} Staker info from subgraph
401
+ * @param chainId - Network in which the staking contract is deployed
402
+ * @param stakerAddress - Address of the staker
403
+ * @param options - Optional configuration for subgraph requests.
404
+ * @returns Staker info from subgraph
405
+ * @throws ErrorInvalidStakerAddressProvided If the staker address is invalid
406
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
407
+ * @throws ErrorStakerNotFound If the staker is not found
408
+ *
409
+ * @example
410
+ * ```ts
411
+ * import { StakingUtils, ChainId } from '@human-protocol/sdk';
412
+ *
413
+ * const staker = await StakingUtils.getStaker(
414
+ * ChainId.POLYGON_AMOY,
415
+ * '0xYourStakerAddress'
416
+ * );
417
+ * console.log('Staked amount:', staker.stakedAmount);
418
+ * ```
431
419
  */
432
420
  static async getStaker(chainId, stakerAddress, options) {
433
421
  if (!ethers_1.ethers.isAddress(stakerAddress)) {
@@ -446,9 +434,22 @@ class StakingUtils {
446
434
  /**
447
435
  * Gets all stakers from the subgraph with filters, pagination and ordering.
448
436
  *
449
- * @param {IStakersFilter} filter Stakers filter with pagination and ordering
450
- * @param {SubgraphOptions} options Optional configuration for subgraph requests.
451
- * @returns {Promise<IStaker[]>} Array of stakers
437
+ * @param filter - Stakers filter with pagination and ordering
438
+ * @param options - Optional configuration for subgraph requests.
439
+ * @returns Array of stakers
440
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
441
+ *
442
+ * @example
443
+ * ```ts
444
+ * import { ChainId } from '@human-protocol/sdk';
445
+ *
446
+ * const filter = {
447
+ * chainId: ChainId.POLYGON_AMOY,
448
+ * minStakedAmount: '1000000000000000000', // 1 token in WEI
449
+ * };
450
+ * const stakers = await StakingUtils.getStakers(filter);
451
+ * console.log('Stakers:', stakers.length);
452
+ * ```
452
453
  */
453
454
  static async getStakers(filter, options) {
454
455
  const first = filter.first !== undefined ? Math.min(filter.first, 1000) : 10;