@human-protocol/sdk 1.1.13 → 1.1.15

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/staking.js CHANGED
@@ -22,6 +22,75 @@ const error_1 = require("./error");
22
22
  const utils_1 = require("./utils");
23
23
  const reward_1 = require("./graphql/queries/reward");
24
24
  const staking_1 = require("./graphql/queries/staking");
25
+ /**
26
+ * ## Introduction
27
+ *
28
+ * This client enables to perform actions on staking contracts and obtain staking information from both the contracts and subgraph.
29
+ *
30
+ * Internally, the SDK will use one network or another according to the network ID of the `signerOrProvider`.
31
+ * To use this client, it is recommended to initialize it using the static `build` method.
32
+ *
33
+ * ```ts
34
+ * static async build(signerOrProvider: Signer | Provider);
35
+ * ```
36
+ *
37
+ * A `Signer` or a `Provider` should be passed depending on the use case of this module:
38
+ *
39
+ * - **Signer**: when the user wants to use this model in order to send transactions caling the contract functions.
40
+ * - **Provider**: when the user wants to use this model in order to get information from the contracts or subgraph.
41
+ *
42
+ * ## Installation
43
+ *
44
+ * ### npm
45
+ * ```bash
46
+ * npm install @human-protocol/sdk
47
+ * ```
48
+ *
49
+ * ### yarn
50
+ * ```bash
51
+ * yarn install @human-protocol/sdk
52
+ * ```
53
+ *
54
+ * ## Code example
55
+ *
56
+ * ### Signer
57
+ *
58
+ * **Using private key(backend)**
59
+ *
60
+ * ```ts
61
+ * import { StakingClient } from '@human-protocol/sdk';
62
+ * import { Wallet, providers } from 'ethers';
63
+ *
64
+ * const rpcUrl = 'YOUR_RPC_URL';
65
+ * const privateKey = 'YOUR_PRIVATE_KEY'
66
+ *
67
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
68
+ * const signer = new Wallet(privateKey, provider);
69
+ * const stakingClient = await StakingClient.build(signer);
70
+ * ```
71
+ *
72
+ * **Using Wagmi(frontend)**
73
+ *
74
+ * ```ts
75
+ * import { useSigner, useChainId } from 'wagmi';
76
+ * import { StakingClient } from '@human-protocol/sdk';
77
+ *
78
+ * const { data: signer } = useSigner();
79
+ * const stakingClient = await StakingClient.build(signer);
80
+ * ```
81
+ *
82
+ * ### Provider
83
+ *
84
+ * ```ts
85
+ * import { StakingClient } from '@human-protocol/sdk';
86
+ * import { providers } from 'ethers';
87
+ *
88
+ * const rpcUrl = 'YOUR_RPC_URL';
89
+ *
90
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
91
+ * const stakingClient = await StakingClient.build(provider);
92
+ * ```
93
+ */
25
94
  class StakingClient {
26
95
  /**
27
96
  * **StakingClient constructor**
@@ -63,12 +132,28 @@ class StakingClient {
63
132
  return new StakingClient(signerOrProvider, networkData);
64
133
  }
65
134
  /**
66
- * **Approves the staking contract to transfer a specified amount of tokens when the user stakes.
67
- * **It increases the allowance for the staking contract.*
135
+ * 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.
68
136
  *
69
- * @param {BigNumber} amount - Amount of tokens to approve for stake
70
- * @returns {Promise<void>}
71
- * @throws {Error} - An error object if an error occurred, void otherwise
137
+ * @param {BigNumber} amount Amount in WEI of tokens to approve for stake.
138
+ * @returns Returns void if successful. Throws error if any.
139
+ *
140
+ *
141
+ * **Code example**
142
+ *
143
+ * ```ts
144
+ * import { ethers, Wallet, providers } from 'ethers';
145
+ * import { StakingClient } from '@human-protocol/sdk';
146
+ *
147
+ * const rpcUrl = 'YOUR_RPC_URL';
148
+ * const privateKey = 'YOUR_PRIVATE_KEY'
149
+ *
150
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
151
+ * const signer = new Wallet(privateKey, provider);
152
+ * const stakingClient = await StakingClient.build(signer);
153
+ *
154
+ * const amount = ethers.utils.parseUnits(5, 'ether'); //convert from ETH to WEI
155
+ * await stakingClient.approveStake(amount);
156
+ * ```
72
157
  */
73
158
  async approveStake(amount) {
74
159
  if (!ethers_1.BigNumber.isBigNumber(amount)) {
@@ -86,11 +171,31 @@ class StakingClient {
86
171
  }
87
172
  }
88
173
  /**
89
- * **Stakes a specified amount of tokens on a specific network.*
174
+ * This function stakes a specified amount of tokens on a specific network.
175
+ *
176
+ * > `approveStake` must be called before
177
+ *
178
+ * @param {BigNumber} amount Amount in WEI of tokens to stake.
179
+ * @returns Returns void if successful. Throws error if any.
180
+ *
181
+ *
182
+ * **Code example**
90
183
  *
91
- * @param {BigNumber} amount - Amount of tokens to stake
92
- * @returns {Promise<void>}
93
- * @throws {Error} - An error object if an error occurred, void otherwise
184
+ * ```ts
185
+ * import { ethers, Wallet, providers } from 'ethers';
186
+ * import { StakingClient } from '@human-protocol/sdk';
187
+ *
188
+ * const rpcUrl = 'YOUR_RPC_URL';
189
+ * const privateKey = 'YOUR_PRIVATE_KEY'
190
+ *
191
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
192
+ * const signer = new Wallet(privateKey, provider);
193
+ * const stakingClient = await StakingClient.build(signer);
194
+ *
195
+ * const amount = ethers.utils.parseUnits(5, 'ether'); //convert from ETH to WEI
196
+ * await stakingClient.approveStake(amount); // if it was already approved before, this is not necessary
197
+ * await stakingClient.approveStake(amount);
198
+ * ```
94
199
  */
95
200
  async stake(amount) {
96
201
  if (!ethers_1.BigNumber.isBigNumber(amount)) {
@@ -108,12 +213,30 @@ class StakingClient {
108
213
  }
109
214
  }
110
215
  /**
111
- * **Unstakes tokens from staking contract.
112
- * **The unstaked tokens stay locked for a period of time.*
216
+ * This function unstakes tokens from staking contract. The unstaked tokens stay locked for a period of time.
217
+ *
218
+ * > Must have tokens available to unstake
219
+ *
220
+ * @param {BigNumber} amount Amount in WEI of tokens to unstake.
221
+ * @returns Returns void if successful. Throws error if any.
222
+ *
223
+ *
224
+ * **Code example**
225
+ *
226
+ * ```ts
227
+ * import { ethers, Wallet, providers } from 'ethers';
228
+ * import { StakingClient } from '@human-protocol/sdk';
229
+ *
230
+ * const rpcUrl = 'YOUR_RPC_URL';
231
+ * const privateKey = 'YOUR_PRIVATE_KEY'
232
+ *
233
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
234
+ * const signer = new Wallet(privateKey, provider);
235
+ * const stakingClient = await StakingClient.build(signer);
113
236
  *
114
- * @param {BigNumber} amount - Amount of tokens to unstake
115
- * @returns {Promise<void>}
116
- * @throws {Error} - An error object if an error occurred, void otherwise
237
+ * const amount = ethers.utils.parseUnits(5, 'ether'); //convert from ETH to WEI
238
+ * await stakingClient.unstake(amount);
239
+ * ```
117
240
  */
118
241
  async unstake(amount) {
119
242
  if (!ethers_1.BigNumber.isBigNumber(amount)) {
@@ -131,10 +254,28 @@ class StakingClient {
131
254
  }
132
255
  }
133
256
  /**
134
- * **Withdraws unstaked and non locked tokens form staking contract to the user wallet.*
257
+ * This function withdraws unstaked and non locked tokens form staking contract to the user wallet.
135
258
  *
136
- * @returns {Promise<void>}
137
- * @throws {Error} - An error object if an error occurred, void otherwise
259
+ * > Must have tokens available to withdraw
260
+ *
261
+ * @returns Returns void if successful. Throws error if any.
262
+ *
263
+ *
264
+ * **Code example**
265
+ *
266
+ * ```ts
267
+ * import { Wallet, providers } from 'ethers';
268
+ * import { StakingClient } from '@human-protocol/sdk';
269
+ *
270
+ * const rpcUrl = 'YOUR_RPC_URL';
271
+ * const privateKey = 'YOUR_PRIVATE_KEY'
272
+ *
273
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
274
+ * const signer = new Wallet(privateKey, provider);
275
+ * const stakingClient = await StakingClient.build(signer);
276
+ *
277
+ * await stakingClient.withdraw();
278
+ * ```
138
279
  */
139
280
  async withdraw() {
140
281
  try {
@@ -146,15 +287,31 @@ class StakingClient {
146
287
  }
147
288
  }
148
289
  /**
149
- * **Slash the allocated amount by an staker in an escrow and transfers those tokens to the reward pool.
150
- * **This allows the slasher to claim them later.*
151
- *
152
- * @param {string} slasher - Wallet address from who requested the slash
153
- * @param {string} staker - Wallet address from who is going to be slashed
154
- * @param {string} escrowAddress - Address of the escrow which allocation will be slashed
155
- * @param {BigNumber} amount - Amount of tokens to slash
156
- * @returns {Promise<void>}
157
- * @throws {Error} - An error object if an error occurred, void otherwise
290
+ * This function reduces the allocated amount by an staker in an escrow and transfers those tokens to the reward pool. This allows the slasher to claim them later.
291
+ *
292
+ * @param {string} slasher Wallet address from who requested the slash
293
+ * @param {string} staker Wallet address from who is going to be slashed
294
+ * @param {string} escrowAddress Address of the escrow which allocation will be slashed
295
+ * @param {BigNumber} amount Amount in WEI of tokens to unstake.
296
+ * @returns Returns void if successful. Throws error if any.
297
+ *
298
+ *
299
+ * **Code example**
300
+ *
301
+ * ```ts
302
+ * import { ethers, Wallet, providers } from 'ethers';
303
+ * import { StakingClient } from '@human-protocol/sdk';
304
+ *
305
+ * const rpcUrl = 'YOUR_RPC_URL';
306
+ * const privateKey = 'YOUR_PRIVATE_KEY'
307
+ *
308
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
309
+ * const signer = new Wallet(privateKey, provider);
310
+ * const stakingClient = await StakingClient.build(signer);
311
+ *
312
+ * const amount = ethers.utils.parseUnits(5, 'ether'); //convert from ETH to WEI
313
+ * await stakingClient.slash('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);
314
+ * ```
158
315
  */
159
316
  async slash(slasher, staker, escrowAddress, amount) {
160
317
  if (!ethers_1.BigNumber.isBigNumber(amount)) {
@@ -184,12 +341,31 @@ class StakingClient {
184
341
  }
185
342
  }
186
343
  /**
187
- * **Allocates a portion of the staked tokens to a specific escrow.*
344
+ * This function allocates a portion of the staked tokens to a specific escrow.
345
+ *
346
+ * > Must have tokens staked
347
+ *
348
+ * @param {string} escrowAddress Address of the escrow contract to allocate in.
349
+ * @param {BigNumber} amount Amount in WEI of tokens to allocate.
350
+ * @returns Returns void if successful. Throws error if any.
351
+ *
352
+ *
353
+ * **Code example**
354
+ *
355
+ * ```ts
356
+ * import { ethers, Wallet, providers } from 'ethers';
357
+ * import { StakingClient } from '@human-protocol/sdk';
358
+ *
359
+ * const rpcUrl = 'YOUR_RPC_URL';
360
+ * const privateKey = 'YOUR_PRIVATE_KEY'
188
361
  *
189
- * @param {string} escrowAddress - Address of the escrow contract
190
- * @param {BigNumber} amount - Amount of tokens to allocate
191
- * @returns {Promise<void>}
192
- * @throws {Error} - An error object if an error occurred, void otherwise
362
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
363
+ * const signer = new Wallet(privateKey, provider);
364
+ * const stakingClient = await StakingClient.build(signer);
365
+ *
366
+ * const amount = ethers.utils.parseUnits(5, 'ether'); //convert from ETH to WEI
367
+ * await stakingClient.allocate('0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);
368
+ * ```
193
369
  */
194
370
  async allocate(escrowAddress, amount) {
195
371
  if (!ethers_1.BigNumber.isBigNumber(amount)) {
@@ -213,11 +389,30 @@ class StakingClient {
213
389
  }
214
390
  }
215
391
  /**
216
- * **Drops the allocation from a specific escrow.*
392
+ * This function drops the allocation from a specific escrow.
393
+ *
394
+ * > The escrow must have allocation
395
+ * > The escrow must be cancelled or completed.
396
+ *
397
+ * @param {string} escrowAddress Address of the escrow contract to close allocation from.
398
+ * @returns Returns void if successful. Throws error if any.
399
+ *
400
+ *
401
+ * **Code example**
217
402
  *
218
- * @param {string} escrowAddress - Address of the escrow contract.
219
- * @returns {Promise<void>}
220
- * @throws {Error} - An error object if an error occurred, void otherwise
403
+ * ```ts
404
+ * import { Wallet, providers } from 'ethers';
405
+ * import { StakingClient } from '@human-protocol/sdk';
406
+ *
407
+ * const rpcUrl = 'YOUR_RPC_URL';
408
+ * const privateKey = 'YOUR_PRIVATE_KEY'
409
+ *
410
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
411
+ * const signer = new Wallet(privateKey, provider);
412
+ * const stakingClient = await StakingClient.build(signer);
413
+ *
414
+ * await stakingClient.closeAllocation('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
415
+ * ```
221
416
  */
222
417
  async closeAllocation(escrowAddress) {
223
418
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -235,11 +430,29 @@ class StakingClient {
235
430
  }
236
431
  }
237
432
  /**
238
- * **Pays out rewards to the slashers for the specified escrow address.*
433
+ * This function drops the allocation from a specific escrow.
434
+ *
435
+ * > The escrow must have rewards added
436
+ *
437
+ * @param {string} escrowAddress Escrow address from which rewards are distributed.
438
+ * @returns Returns void if successful. Throws error if any.
239
439
  *
240
- * @param {string} escrowAddress - Escrow address from which rewards are distributed.
241
- * @returns {Promise<void>}
242
- * @throws {Error} - An error object if an error occurred, void otherwise
440
+ *
441
+ * **Code example**
442
+ *
443
+ * ```ts
444
+ * import { Wallet, providers } from 'ethers';
445
+ * import { StakingClient } from '@human-protocol/sdk';
446
+ *
447
+ * const rpcUrl = 'YOUR_RPC_URL';
448
+ * const privateKey = 'YOUR_PRIVATE_KEY'
449
+ *
450
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
451
+ * const signer = new Wallet(privateKey, provider);
452
+ * const stakingClient = await StakingClient.build(signer);
453
+ *
454
+ * await stakingClient.distributeRewards('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
455
+ * ```
243
456
  */
244
457
  async distributeRewards(escrowAddress) {
245
458
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -258,11 +471,25 @@ class StakingClient {
258
471
  }
259
472
  }
260
473
  /**
261
- * **Returns the leader details for a given address**
474
+ * This function returns all the leader details of the protocol.
475
+ *
476
+ * @param {ILeadersFilter} filter Filter for the leaders.
477
+ * @returns {ILeader[]} Returns an array with all the leader details.
478
+ *
479
+ *
480
+ * **Code example**
481
+ *
482
+ * ```ts
483
+ * import { StakingClient } from '@human-protocol/sdk';
484
+ * import { providers } from 'ethers';
485
+ *
486
+ * const rpcUrl = 'YOUR_RPC_URL';
262
487
  *
263
- * @param {string} address - Leader address
264
- * @returns {Promise<ILeader>} - Return leader details
265
- * @throws {Error} - An error object if an error occurred, result otherwise
488
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
489
+ * const stakingClient = await StakingClient.build(provider);
490
+ *
491
+ * const leaders = await stakingClient.getLeaders();
492
+ * ```
266
493
  */
267
494
  async getLeader(address) {
268
495
  if (!ethers_1.ethers.utils.isAddress(address)) {
@@ -279,10 +506,25 @@ class StakingClient {
279
506
  }
280
507
  }
281
508
  /**
282
- * **Returns the leaders data **
509
+ * This function returns the leader data for the given address.
510
+ *
511
+ * @param {string} address Leader address.
512
+ * @returns {ILeader} Returns the leader details.
513
+ *
514
+ *
515
+ * **Code example**
516
+ *
517
+ * ```ts
518
+ * import { StakingClient } from '@human-protocol/sdk';
519
+ * import { providers } from 'ethers';
283
520
  *
284
- * @returns {Promise<ILeader[]>} - Return an array with leaders data
285
- * @throws {Error} - An error object if an error occurred, results otherwise
521
+ * const rpcUrl = 'YOUR_RPC_URL';
522
+ *
523
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
524
+ * const stakingClient = await StakingClient.build(provider);
525
+ *
526
+ * const leader = await stakingClient.getLeader('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
527
+ * ```
286
528
  */
287
529
  async getLeaders(filter = {}) {
288
530
  try {
@@ -296,11 +538,25 @@ class StakingClient {
296
538
  }
297
539
  }
298
540
  /**
299
- * **Returns information about the allocation of the specified escrow.*
541
+ * This function returns information about the allocation of the specified escrow.
542
+ *
543
+ * @param {string} escrowAddress Escrow address from which we want to get allocation information.
544
+ * @returns {IAllocation} Returns allocation info if exists.
545
+ *
546
+ *
547
+ * **Code example**
300
548
  *
301
- * @param {string} escrowAddress - The escrow address for the received allocation data
302
- * @returns {Promise<IAllocation>} - Returns allocation info if exists
303
- * @throws {Error} - An error object if an error occurred, result otherwise
549
+ * ```ts
550
+ * import { StakingClient } from '@human-protocol/sdk';
551
+ * import { providers } from 'ethers';
552
+ *
553
+ * const rpcUrl = 'YOUR_RPC_URL';
554
+ *
555
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
556
+ * const stakingClient = await StakingClient.build(provider);
557
+ *
558
+ * const allocationInfo = await stakingClient.getAllocation('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
559
+ * ```
304
560
  */
305
561
  async getAllocation(escrowAddress) {
306
562
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -318,11 +574,25 @@ class StakingClient {
318
574
  }
319
575
  }
320
576
  /**
321
- * **Returns information about the rewards for a given escrow address.*
577
+ * This function returns information about the rewards for a given slasher address.
578
+ *
579
+ * @param {string} slasherAddress Slasher address.
580
+ * @returns {IReward[]} Returns an array of Reward objects that contain the rewards earned by the user through slashing other users.
581
+ *
582
+ *
583
+ * **Code example**
584
+ *
585
+ * ```ts
586
+ * import { StakingClient } from '@human-protocol/sdk';
587
+ * import { providers } from 'ethers';
588
+ *
589
+ * const rpcUrl = 'YOUR_RPC_URL';
590
+ *
591
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
592
+ * const stakingClient = await StakingClient.build(provider);
322
593
  *
323
- * @param {string} slasherAddress - Address of the slasher
324
- * @returns {Promise<IReward[]>} - Returns rewards info if exists
325
- * @throws {Error} - An error object if an error occurred, results otherwise
594
+ * const rewards = await stakingClient.getRewards('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
595
+ * ```
326
596
  */
327
597
  async getRewards(slasherAddress) {
328
598
  if (!ethers_1.ethers.utils.isAddress(slasherAddress)) {