@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/escrow.js CHANGED
@@ -22,12 +22,81 @@ const error_1 = require("./error");
22
22
  const types_1 = require("./types");
23
23
  const utils_1 = require("./utils");
24
24
  const graphql_1 = require("./graphql");
25
+ /**
26
+ * ## Introduction
27
+ *
28
+ * This client enables to perform actions on Escrow contracts and obtain 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 { EscrowClient } 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 escrowClient = await EscrowClient.build(signer);
70
+ * ```
71
+ *
72
+ * **Using Wagmi(frontend)**
73
+ *
74
+ * ```ts
75
+ * import { useSigner, useChainId } from 'wagmi';
76
+ * import { EscrowClient } from '@human-protocol/sdk';
77
+ *
78
+ * const { data: signer } = useSigner();
79
+ * const escrowClient = await EscrowClient.build(signer);
80
+ * ```
81
+ *
82
+ * ### Provider
83
+ *
84
+ * ```ts
85
+ * import { EscrowClient } 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 escrowClient = await EscrowClient.build(provider);
92
+ * ```
93
+ */
25
94
  class EscrowClient {
26
95
  /**
27
96
  * **EscrowClient constructor**
28
97
  *
29
- * @param {Signer | Provider} signerOrProvider - The Signer or Provider object to interact with the Ethereum network
30
- * @param {NetworkData} network - The network information required to connect to the Escrow contract
98
+ * @param {Signer | Provider} signerOrProvider The Signer or Provider object to interact with the Ethereum network
99
+ * @param {NetworkData} network The network information required to connect to the Escrow contract
31
100
  */
32
101
  constructor(signerOrProvider, network) {
33
102
  this.escrowFactoryContract = typechain_types_1.EscrowFactory__factory.connect(network.factoryAddress, signerOrProvider);
@@ -37,10 +106,10 @@ class EscrowClient {
37
106
  /**
38
107
  * Creates an instance of EscrowClient from a Signer or Provider.
39
108
  *
40
- * @param {Signer | Provider} signerOrProvider - The Signer or Provider object to interact with the Ethereum network
41
- * @returns {Promise<EscrowClient>} - An instance of EscrowClient
42
- * @throws {ErrorProviderDoesNotExist} - Thrown if the provider does not exist for the provided Signer
43
- * @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
109
+ * @param {Signer | Provider} signerOrProvider The Signer or Provider object to interact with the Ethereum network
110
+ * @returns {Promise<EscrowClient>} An instance of EscrowClient
111
+ * @throws {ErrorProviderDoesNotExist} Thrown if the provider does not exist for the provided Signer
112
+ * @throws {ErrorUnsupportedChainID} Thrown if the network's chainId is not supported
44
113
  */
45
114
  static async build(signerOrProvider) {
46
115
  let network;
@@ -61,12 +130,34 @@ class EscrowClient {
61
130
  return new EscrowClient(signerOrProvider, networkData);
62
131
  }
63
132
  /**
64
- * Creates an escrow contract that uses the token passed to pay oracle fees and reward workers.
133
+ * This function creates an escrow contract that uses the token passed to pay oracle fees and reward workers.
65
134
  *
66
- * @param {string} tokenAddress - Token address to use for pay outs.
67
- * @param {string[]} trustedHandlers - Array of addresses that can perform actions on the contract.
68
- * @returns {Promise<string>} - Return the address of the escrow created.
69
- * @throws {Error} - An error object if an error occurred.
135
+ * @param {string} tokenAddress Token address to use for pay outs.
136
+ * @param {string[]} trustedHandlers Array of addresses that can perform actions on the contract.
137
+ * @param {string} jobRequesterId Job Requester Id
138
+ * @returns {Promise<string>} Return the address of the escrow created.
139
+ *
140
+ *
141
+ * **Code example**
142
+ *
143
+ * > Need to have available stake.
144
+ *
145
+ * ```ts
146
+ * import { Wallet, providers } from 'ethers';
147
+ * import { EscrowClient } from '@human-protocol/sdk';
148
+ *
149
+ * const rpcUrl = 'YOUR_RPC_URL';
150
+ * const privateKey = 'YOUR_PRIVATE_KEY'
151
+ *
152
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
153
+ * const signer = new Wallet(privateKey, provider);
154
+ * const escrowClient = await EscrowClient.build(signer);
155
+ *
156
+ * const tokenAddress = '0x0376D26246Eb35FF4F9924cF13E6C05fd0bD7Fb4';
157
+ * const trustedHandlers = ['0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'];
158
+ * const jobRequesterId = "job-requester-id";
159
+ * const escrowAddress = await escrowClient.createEscrow(tokenAddress, trustedHandlers, jobRequesterId);
160
+ * ```
70
161
  */
71
162
  async createEscrow(tokenAddress, trustedHandlers, jobRequesterId) {
72
163
  if (!ethers_1.ethers.utils.isAddress(tokenAddress)) {
@@ -90,12 +181,41 @@ class EscrowClient {
90
181
  }
91
182
  }
92
183
  /**
93
- * Sets up the parameters of the escrow.
184
+ * This function sets up the parameters of the escrow.
185
+ *
186
+ * @param {string} escrowAddress Address of the escrow to set up.
187
+ * @param {IEscrowConfig} escrowConfig Escrow configuration parameters.
188
+ * @returns Returns void if successful. Throws error if any.
189
+ *
190
+ *
191
+ * **Code example**
94
192
  *
95
- * @param {string} escrowAddress - Address of the escrow to set up.
96
- * @param {IEscrowConfig} escrowConfig - Configuration object with escrow settings.
97
- * @returns {Promise<void>}
98
- * @throws {Error} - An error object if an error occurred.
193
+ * > Only Job Launcher or a trusted handler can call it.
194
+ *
195
+ * ```ts
196
+ * import { Wallet, providers } from 'ethers';
197
+ * import { EscrowClient } from '@human-protocol/sdk';
198
+ *
199
+ * const rpcUrl = 'YOUR_RPC_URL';
200
+ * const privateKey = 'YOUR_PRIVATE_KEY'
201
+ *
202
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
203
+ * const signer = new Wallet(privateKey, provider);
204
+ * const escrowClient = await EscrowClient.build(signer);
205
+ *
206
+ * const escrowAddress = '0x62dD51230A30401C455c8398d06F85e4EaB6309f';
207
+ * const escrowConfig = {
208
+ * recordingOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
209
+ * reputationOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
210
+ * exchangeOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
211
+ * recordingOracleFee: BigNumber.from('10'),
212
+ * reputationOracleFee: BigNumber.from('10'),
213
+ * exchangeOracleFee: BigNumber.from('10'),
214
+ * manifestUrl: 'htttp://localhost/manifest.json',
215
+ * manifestHash: 'b5dad76bf6772c0f07fd5e048f6e75a5f86ee079',
216
+ * };
217
+ * await escrowClient.setup(escrowAddress, escrowConfig);
218
+ * ```
99
219
  */
100
220
  async setup(escrowAddress, escrowConfig) {
101
221
  const { recordingOracle, reputationOracle, exchangeOracle, recordingOracleFee, reputationOracleFee, exchangeOracleFee, manifestUrl, manifestHash, } = escrowConfig;
@@ -141,14 +261,45 @@ class EscrowClient {
141
261
  }
142
262
  }
143
263
  /**
144
- * **Creates an escrow contract that uses the token passed to pay oracle fees and reward workers.*
145
- * **Sets up the parameters of the escrow.*
146
- *
147
- * @param {string} tokenAddress - Token address to use for pay outs.
148
- * @param {string[]} trustedHandlers - Array of addresses that can perform actions on the contract.
149
- * @param {IEscrowConfig} escrowConfig - Configuration object with escrow settings.
150
- * @returns {Promise<string>}
151
- * @throws {Error} - An error object if an error occurred.
264
+ * This function creates and sets up an escrow.
265
+ *
266
+ * @param {string} tokenAddress Token address to use for pay outs.
267
+ * @param {string[]} trustedHandlers Array of addresses that can perform actions on the contract.
268
+ * @param {string} jobRequesterId Job Requester Id
269
+ * @param {IEscrowConfig} escrowConfig Configuration object with escrow settings.
270
+ * @returns {Promise<string>} Returns the address of the escrow created.
271
+ *
272
+ *
273
+ * **Code example**
274
+ *
275
+ * ```ts
276
+ * import { ethers, Wallet, providers } from 'ethers';
277
+ * import { EscrowClient } from '@human-protocol/sdk';
278
+ *
279
+ * const rpcUrl = 'YOUR_RPC_URL';
280
+ * const privateKey = 'YOUR_PRIVATE_KEY'
281
+ *
282
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
283
+ * const signer = new Wallet(privateKey, provider);
284
+ * const escrowClient = await EscrowClient.build(signer);
285
+ *
286
+ * const tokenAddress = '0x0376D26246Eb35FF4F9924cF13E6C05fd0bD7Fb4';
287
+ * const trustedHandlers = ['0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'];
288
+ * const jobRequesterId = "job-requester-id";
289
+ *
290
+ * const escrowConfig = {
291
+ * recordingOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
292
+ * reputationOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
293
+ * exchangeOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
294
+ * recordingOracleFee: BigNumber.from('10'),
295
+ * reputationOracleFee: BigNumber.from('10'),
296
+ * exchangeOracleFee: BigNumber.from('10'),
297
+ * manifestUrl: 'htttp://localhost/manifest.json',
298
+ * manifestHash: 'b5dad76bf6772c0f07fd5e048f6e75a5f86ee079',
299
+ * };
300
+ *
301
+ * const escrowAddress = await escrowClient.createAndSetupEscrow(tokenAddress, trustedHandlers, jobRequesterId, escrowConfig);
302
+ * ```
152
303
  */
153
304
  async createAndSetupEscrow(tokenAddress, trustedHandlers, jobRequesterId, escrowConfig) {
154
305
  try {
@@ -161,12 +312,29 @@ class EscrowClient {
161
312
  }
162
313
  }
163
314
  /**
164
- * **Adds funds of the chosen token to the escrow.*
315
+ * This function adds funds of the chosen token to the escrow.
316
+ *
317
+ * @param {string} escrowAddress Address of the escrow to fund.
318
+ * @param {BigNumber} amount Amount to be added as funds.
319
+ * @returns Returns void if successful. Throws error if any.
320
+ *
321
+ *
322
+ * **Code example**
323
+ *
324
+ * ```ts
325
+ * import { ethers, Wallet, providers } from 'ethers';
326
+ * import { EscrowClient } from '@human-protocol/sdk';
327
+ *
328
+ * const rpcUrl = 'YOUR_RPC_URL';
329
+ * const privateKey = 'YOUR_PRIVATE_KEY'
330
+ *
331
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
332
+ * const signer = new Wallet(privateKey, provider);
333
+ * const escrowClient = await EscrowClient.build(signer);
165
334
  *
166
- * @param {string} escrowAddress - Address of the escrow to fund.
167
- * @param {BigNumber} amount - Amount to be added as funds.
168
- * @returns {Promise<void>}
169
- * @throws {Error} - An error object if an error occurred.
335
+ * const amount = ethers.utils.parseUnits(5, 'ether'); //convert from ETH to WEI
336
+ * await escrowClient.fund('0x62dD51230A30401C455c8398d06F85e4EaB6309f', amount);
337
+ * ```
170
338
  */
171
339
  async fund(escrowAddress, amount) {
172
340
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -190,14 +358,31 @@ class EscrowClient {
190
358
  }
191
359
  }
192
360
  /**
193
- * **Stores the results.*
194
- *
195
- * @param {string} escrowAddress - Address of the escrow.
196
- * @param {string} sender - Address of the sender.
197
- * @param {string} url - Results file url.
198
- * @param {string} hash - Results file hash.
199
- * @returns {Promise<void>}
200
- * @throws {Error} - An error object if an error occurred.
361
+ * This function stores the results url and hash.
362
+ *
363
+ * @param {string} escrowAddress Address of the escrow.
364
+ * @param {string} url Results file url.
365
+ * @param {string} hash Results file hash.
366
+ * @returns Returns void if successful. Throws error if any.
367
+ *
368
+ *
369
+ * **Code example**
370
+ *
371
+ * > Only Recording Oracle or a trusted handler can call it.
372
+ *
373
+ * ```ts
374
+ * import { ethers, Wallet, providers } from 'ethers';
375
+ * import { EscrowClient } from '@human-protocol/sdk';
376
+ *
377
+ * const rpcUrl = 'YOUR_RPC_URL';
378
+ * const privateKey = 'YOUR_PRIVATE_KEY'
379
+ *
380
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
381
+ * const signer = new Wallet(privateKey, provider);
382
+ * const escrowClient = await EscrowClient.build(signer);
383
+ *
384
+ * await storeResults.storeResults('0x62dD51230A30401C455c8398d06F85e4EaB6309f', 'http://localhost/results.json', 'b5dad76bf6772c0f07fd5e048f6e75a5f86ee079');
385
+ * ```
201
386
  */
202
387
  async storeResults(escrowAddress, url, hash) {
203
388
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -225,11 +410,29 @@ class EscrowClient {
225
410
  }
226
411
  }
227
412
  /**
228
- * **Sets the status of an escrow to completed.*
413
+ * This function sets the status of an escrow to completed.
414
+ *
415
+ * @param {string} escrowAddress Address of the escrow.
416
+ * @returns Returns void if successful. Throws error if any.
229
417
  *
230
- * @param {string} escrowAddress - Address of the escrow.
231
- * @returns {Promise<void>}
232
- * @throws {Error} - An error object if an error occurred.
418
+ *
419
+ * **Code example**
420
+ *
421
+ * > Only Recording Oracle or a trusted handler can call it.
422
+ *
423
+ * ```ts
424
+ * import { Wallet, providers } from 'ethers';
425
+ * import { EscrowClient } from '@human-protocol/sdk';
426
+ *
427
+ * const rpcUrl = 'YOUR_RPC_URL';
428
+ * const privateKey = 'YOUR_PRIVATE_KEY'
429
+ *
430
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
431
+ * const signer = new Wallet(privateKey, provider);
432
+ * const escrowClient = await EscrowClient.build(signer);
433
+ *
434
+ * await escrowClient.complete('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
435
+ * ```
233
436
  */
234
437
  async complete(escrowAddress) {
235
438
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -248,15 +451,38 @@ class EscrowClient {
248
451
  }
249
452
  }
250
453
  /**
251
- * Pays out the amounts specified to the workers and sets the URL of the final results file.
252
- *
253
- * @param {string} escrowAddress - Address of the escrow.
254
- * @param {string[]} recipients - Array of recipient addresses.
255
- * @param {BigNumber[]} amounts - Array of amounts the recipients will receive.
256
- * @param {string} finalResultsUrl - Final results file url.
257
- * @param {string} finalResultsHash - Final results file hash.
258
- * @returns {Promise<void>}
259
- * @throws {Error} - An error object if an error occurred.
454
+ * This function pays out the amounts specified to the workers and sets the URL of the final results file.
455
+ *
456
+ * @param {string} escrowAddress Escrow address to payout.
457
+ * @param {string[]} recipients Array of recipient addresses.
458
+ * @param {BigNumber[]} amounts Array of amounts the recipients will receive.
459
+ * @param {string} finalResultsUrl Final results file url.
460
+ * @param {string} finalResultsHash Final results file hash.
461
+ * @returns Returns void if successful. Throws error if any.
462
+ *
463
+ *
464
+ * **Code example**
465
+ *
466
+ * > Only Reputation Oracle or a trusted handler can call it.
467
+ *
468
+ * ```ts
469
+ * import { ethers, Wallet, providers } from 'ethers';
470
+ * import { EscrowClient } from '@human-protocol/sdk';
471
+ *
472
+ * const rpcUrl = 'YOUR_RPC_URL';
473
+ * const privateKey = 'YOUR_PRIVATE_KEY'
474
+ *
475
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
476
+ * const signer = new Wallet(privateKey, provider);
477
+ * const escrowClient = await EscrowClient.build(signer);
478
+ *
479
+ * const recipients = ['0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'];
480
+ * const amounts = [ethers.utils.parseUnits(5, 'ether'), ethers.utils.parseUnits(10, 'ether')];
481
+ * const resultsUrl = 'http://localhost/results.json';
482
+ * const resultsHash'b5dad76bf6772c0f07fd5e048f6e75a5f86ee079';
483
+ *
484
+ * await escrowClient.bulkPayOut('0x62dD51230A30401C455c8398d06F85e4EaB6309f', recipients, amounts, resultsUrl, resultsHash);
485
+ * ```
260
486
  */
261
487
  async bulkPayOut(escrowAddress, recipients, amounts, finalResultsUrl, finalResultsHash) {
262
488
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -306,11 +532,29 @@ class EscrowClient {
306
532
  }
307
533
  }
308
534
  /**
309
- * Cancels the specified escrow and sends the balance to the canceler.
535
+ * This function cancels the specified escrow and sends the balance to the canceler.
536
+ *
537
+ * @param {string} escrowAddress Address of the escrow to cancel.
538
+ * @returns {EscrowCancel} Returns the escrow cancellation data including transaction hash and refunded amount. Throws error if any.
539
+ *
540
+ *
541
+ * **Code example**
542
+ *
543
+ * > Only Job Launcher or a trusted handler can call it.
310
544
  *
311
- * @param {string} escrowAddress - Address of the escrow.
312
- * @returns {Promise<EscrowCancel>}
313
- * @throws {Error} - An error object if an error occurred.
545
+ * ```ts
546
+ * import { ethers, Wallet, providers } from 'ethers';
547
+ * import { EscrowClient } from '@human-protocol/sdk';
548
+ *
549
+ * const rpcUrl = 'YOUR_RPC_URL';
550
+ * const privateKey = 'YOUR_PRIVATE_KEY'
551
+ *
552
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
553
+ * const signer = new Wallet(privateKey, provider);
554
+ * const escrowClient = await EscrowClient.build(signer);
555
+ *
556
+ * await escrowClient.cancel('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
557
+ * ```
314
558
  */
315
559
  async cancel(escrowAddress) {
316
560
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -350,11 +594,29 @@ class EscrowClient {
350
594
  }
351
595
  }
352
596
  /**
353
- * Cancels the specified escrow, sends the balance to the canceler and selfdestructs the escrow contract.
597
+ * This function cancels the specified escrow, sends the balance to the canceler and selfdestructs the escrow contract.
598
+ *
599
+ * @param {string} escrowAddress Address of the escrow.
600
+ * @returns Returns void if successful. Throws error if any.
601
+ *
602
+ *
603
+ * **Code example**
604
+ *
605
+ * > Only Job Launcher or trusted handler can call it.
606
+ *
607
+ * ```ts
608
+ * import { Wallet, providers } from 'ethers';
609
+ * import { EscrowClient } from '@human-protocol/sdk';
610
+ *
611
+ * const rpcUrl = 'YOUR_RPC_URL';
612
+ * const privateKey = 'YOUR_PRIVATE_KEY'
613
+ *
614
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
615
+ * const signer = new Wallet(privateKey, provider);
616
+ * const escrowClient = await EscrowClient.build(signer);
354
617
  *
355
- * @param {string} escrowAddress - Address of the escrow.
356
- * @returns {Promise<void>}
357
- * @throws {Error} - An error object if an error occurred.
618
+ * await escrowClient.abort('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
619
+ * ```
358
620
  */
359
621
  async abort(escrowAddress) {
360
622
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -373,12 +635,31 @@ class EscrowClient {
373
635
  }
374
636
  }
375
637
  /**
376
- * Adds an array of addresses to the trusted handlers list.
638
+ * This function sets the status of an escrow to completed.
377
639
  *
378
- * @param {string} escrowAddress - Address of the escrow.
379
- * @param {string[]} trustedHandlers - List of trusted handler addresses.
380
- * @returns {Promise<void>}
381
- * @throws {Error} - An error object if an error occurred.
640
+ * @param {string} escrowAddress Address of the escrow.
641
+ * @param {string[]} trustedHandlers Array of addresses of trusted handlers to add.
642
+ * @returns Returns void if successful. Throws error if any.
643
+ *
644
+ *
645
+ * **Code example**
646
+ *
647
+ * > Only Job Launcher or trusted handler can call it.
648
+ *
649
+ * ```ts
650
+ * import { Wallet, providers } from 'ethers';
651
+ * import { EscrowClient } from '@human-protocol/sdk';
652
+ *
653
+ * const rpcUrl = 'YOUR_RPC_URL';
654
+ * const privateKey = 'YOUR_PRIVATE_KEY'
655
+ *
656
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
657
+ * const signer = new Wallet(privateKey, provider);
658
+ * const escrowClient = await EscrowClient.build(signer);
659
+ *
660
+ * const trustedHandlers = ['0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266']
661
+ * await escrowClient.addTrustedHandlers('0x62dD51230A30401C455c8398d06F85e4EaB6309f', trustedHandlers);
662
+ * ```
382
663
  */
383
664
  async addTrustedHandlers(escrowAddress, trustedHandlers) {
384
665
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -405,11 +686,24 @@ class EscrowClient {
405
686
  }
406
687
  }
407
688
  /**
408
- * Returns the balance for a specified escrow address.
689
+ * This function returns the balance for a specified escrow address.
690
+ *
691
+ * @param {string} escrowAddress Address of the escrow.
692
+ * @returns {BigNumber} Balance of the escrow in the token used to fund it.
693
+ *
694
+ * **Code example**
409
695
  *
410
- * @param {string} escrowAddress - Address of the escrow.
411
- * @returns {Promise<BigNumber>}
412
- * @throws {Error} - An error object if an error occurred.
696
+ * ```ts
697
+ * import { providers } from 'ethers';
698
+ * import { EscrowClient } from '@human-protocol/sdk';
699
+ *
700
+ * const rpcUrl = 'YOUR_RPC_URL';
701
+ *
702
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
703
+ * const escrowClient = await EscrowClient.build(signer);
704
+ *
705
+ * const balance = await escrowClient.getBalance('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
706
+ * ```
413
707
  */
414
708
  async getBalance(escrowAddress) {
415
709
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -427,11 +721,24 @@ class EscrowClient {
427
721
  }
428
722
  }
429
723
  /**
430
- * Returns the manifest file hash.
724
+ * This function returns the manifest file hash.
725
+ *
726
+ * @param {string} escrowAddress Address of the escrow.
727
+ * @returns {string} Hash of the manifest file content.
728
+ *
729
+ * **Code example**
431
730
  *
432
- * @param {string} escrowAddress - Address of the escrow.
433
- * @returns {Promise<void>}
434
- * @throws {Error} - An error object if an error occurred.
731
+ * ```ts
732
+ * import { providers } from 'ethers';
733
+ * import { EscrowClient } from '@human-protocol/sdk';
734
+ *
735
+ * const rpcUrl = 'YOUR_RPC_URL';
736
+ *
737
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
738
+ * const escrowClient = await EscrowClient.build(signer);
739
+ *
740
+ * const manifestHash = await escrowClient.getManifestHash('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
741
+ * ```
435
742
  */
436
743
  async getManifestHash(escrowAddress) {
437
744
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -449,11 +756,24 @@ class EscrowClient {
449
756
  }
450
757
  }
451
758
  /**
452
- * Returns the manifest file URL.
759
+ * This function returns the manifest file URL.
760
+ *
761
+ * @param {string} escrowAddress Address of the escrow.
762
+ * @returns {string} Url of the manifest.
763
+ *
764
+ * **Code example**
453
765
  *
454
- * @param {string} escrowAddress - Address of the escrow.
455
- * @returns {Promise<void>}
456
- * @throws {Error} - An error object if an error occurred.
766
+ * ```ts
767
+ * import { providers } from 'ethers';
768
+ * import { EscrowClient } from '@human-protocol/sdk';
769
+ *
770
+ * const rpcUrl = 'YOUR_RPC_URL';
771
+ *
772
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
773
+ * const escrowClient = await EscrowClient.build(signer);
774
+ *
775
+ * const manifestUrl = await escrowClient.getManifestUrl('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
776
+ * ```
457
777
  */
458
778
  async getManifestUrl(escrowAddress) {
459
779
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -471,11 +791,24 @@ class EscrowClient {
471
791
  }
472
792
  }
473
793
  /**
474
- * Returns the results file URL.
794
+ * This function returns the results file URL.
795
+ *
796
+ * @param {string} escrowAddress Address of the escrow.
797
+ * @returns {string} Results file url.
798
+ *
799
+ * **Code example**
475
800
  *
476
- * @param {string} escrowAddress - Address of the escrow.
477
- * @returns {Promise<void>}
478
- * @throws {Error} - An error object if an error occurred.
801
+ * ```ts
802
+ * import { providers } from 'ethers';
803
+ * import { EscrowClient } from '@human-protocol/sdk';
804
+ *
805
+ * const rpcUrl = 'YOUR_RPC_URL';
806
+ *
807
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
808
+ * const escrowClient = await EscrowClient.build(signer);
809
+ *
810
+ * const resultsUrl = await escrowClient.getResultsUrl('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
811
+ * ```
479
812
  */
480
813
  async getResultsUrl(escrowAddress) {
481
814
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -493,11 +826,24 @@ class EscrowClient {
493
826
  }
494
827
  }
495
828
  /**
496
- * Returns the intermediate results file URL.
829
+ * This function returns the intermediate results file URL.
830
+ *
831
+ * @param {string} escrowAddress Address of the escrow.
832
+ * @returns {string} Url of the file that store results from Recording Oracle.
497
833
  *
498
- * @param {string} escrowAddress - Address of the escrow.
499
- * @returns {Promise<void>}
500
- * @throws {Error} - An error object if an error occurred.
834
+ * **Code example**
835
+ *
836
+ * ```ts
837
+ * import { providers } from 'ethers';
838
+ * import { EscrowClient } from '@human-protocol/sdk';
839
+ *
840
+ * const rpcUrl = 'YOUR_RPC_URL';
841
+ *
842
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
843
+ * const escrowClient = await EscrowClient.build(signer);
844
+ *
845
+ * const intemediateResultsUrl = await escrowClient.getIntermediateResultsUrl('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
846
+ * ```
501
847
  */
502
848
  async getIntermediateResultsUrl(escrowAddress) {
503
849
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -515,11 +861,24 @@ class EscrowClient {
515
861
  }
516
862
  }
517
863
  /**
518
- * Returns the value for a specified key and address
864
+ * This function returns the token address used for funding the escrow.
865
+ *
866
+ * @param {string} escrowAddress Address of the escrow.
867
+ * @returns {string} Address of the token used to fund the escrow.
519
868
  *
520
- * @param {string} escrowAddress - Address of the escrow.
521
- * @returns {Promise<void>}
522
- * @throws {Error} - An error object if an error occurred.
869
+ * **Code example**
870
+ *
871
+ * ```ts
872
+ * import { providers } from 'ethers';
873
+ * import { EscrowClient } from '@human-protocol/sdk';
874
+ *
875
+ * const rpcUrl = 'YOUR_RPC_URL';
876
+ *
877
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
878
+ * const escrowClient = await EscrowClient.build(signer);
879
+ *
880
+ * const tokenAddress = await escrowClient.getTokenAddress('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
881
+ * ```
523
882
  */
524
883
  async getTokenAddress(escrowAddress) {
525
884
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -537,11 +896,24 @@ class EscrowClient {
537
896
  }
538
897
  }
539
898
  /**
540
- * Returns the current status of the escrow.
899
+ * This function returns the current status of the escrow.
900
+ *
901
+ * @param {string} escrowAddress Address of the escrow.
902
+ * @returns {EscrowStatus} Current status of the escrow.
541
903
  *
542
- * @param {string} escrowAddress - Address of the escrow.
543
- * @returns {Promise<void>}
544
- * @throws {Error} - An error object if an error occurred.
904
+ * **Code example**
905
+ *
906
+ * ```ts
907
+ * import { providers } from 'ethers';
908
+ * import { EscrowClient } from '@human-protocol/sdk';
909
+ *
910
+ * const rpcUrl = 'YOUR_RPC_URL';
911
+ *
912
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
913
+ * const escrowClient = await EscrowClient.build(signer);
914
+ *
915
+ * const status = await escrowClient.getStatus('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
916
+ * ```
545
917
  */
546
918
  async getStatus(escrowAddress) {
547
919
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -559,11 +931,24 @@ class EscrowClient {
559
931
  }
560
932
  }
561
933
  /**
562
- * Returns the recording oracle address of given escrow
934
+ * This function returns the recording oracle address for a given escrow.
935
+ *
936
+ * @param {string} escrowAddress Address of the escrow.
937
+ * @returns {string} Address of the Recording Oracle.
938
+ *
939
+ * **Code example**
940
+ *
941
+ * ```ts
942
+ * import { providers } from 'ethers';
943
+ * import { EscrowClient } from '@human-protocol/sdk';
944
+ *
945
+ * const rpcUrl = 'YOUR_RPC_URL';
946
+ *
947
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
948
+ * const escrowClient = await EscrowClient.build(signer);
563
949
  *
564
- * @param {string} escrowAddress - Address of the escrow.
565
- * @returns {Promise<string>} - Address of the recording oracle.
566
- * @throws {Error} - An error object if an error occurred.
950
+ * const oracleAddress = await escrowClient.getRecordingOracleAddress('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
951
+ * ```
567
952
  */
568
953
  async getRecordingOracleAddress(escrowAddress) {
569
954
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -581,11 +966,24 @@ class EscrowClient {
581
966
  }
582
967
  }
583
968
  /**
584
- * Returns the job launcher address of given escrow
969
+ * This function returns the job launcher address for a given escrow.
585
970
  *
586
- * @param {string} escrowAddress - Address of the escrow.
587
- * @returns {Promise<string>} - Address of the job launcher.
588
- * @throws {Error} - An error object if an error occurred.
971
+ * @param {string} escrowAddress Address of the escrow.
972
+ * @returns {string} Address of the Job Launcher.
973
+ *
974
+ * **Code example**
975
+ *
976
+ * ```ts
977
+ * import { providers } from 'ethers';
978
+ * import { EscrowClient } from '@human-protocol/sdk';
979
+ *
980
+ * const rpcUrl = 'YOUR_RPC_URL';
981
+ *
982
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
983
+ * const escrowClient = await EscrowClient.build(signer);
984
+ *
985
+ * const jobLauncherAddress = await escrowClient.getJobLauncherAddress('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
986
+ * ```
589
987
  */
590
988
  async getJobLauncherAddress(escrowAddress) {
591
989
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -603,11 +1001,24 @@ class EscrowClient {
603
1001
  }
604
1002
  }
605
1003
  /**
606
- * Returns the reputation oracle address of given escrow
1004
+ * This function returns the reputation oracle address for a given escrow.
1005
+ *
1006
+ * @param {string} escrowAddress Address of the escrow.
1007
+ * @returns {EscrowStatus} Address of the Reputation Oracle.
1008
+ *
1009
+ * **Code example**
1010
+ *
1011
+ * ```ts
1012
+ * import { providers } from 'ethers';
1013
+ * import { EscrowClient } from '@human-protocol/sdk';
1014
+ *
1015
+ * const rpcUrl = 'YOUR_RPC_URL';
1016
+ *
1017
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
1018
+ * const escrowClient = await EscrowClient.build(signer);
607
1019
  *
608
- * @param {string} escrowAddress - Address of the escrow.
609
- * @returns {Promise<string>} - Address of the reputation oracle.
610
- * @throws {Error} - An error object if an error occurred.
1020
+ * const oracleAddress = await escrowClient.getReputationOracleAddress('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
1021
+ * ```
611
1022
  */
612
1023
  async getReputationOracleAddress(escrowAddress) {
613
1024
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -625,11 +1036,24 @@ class EscrowClient {
625
1036
  }
626
1037
  }
627
1038
  /**
628
- * Returns the reputation oracle address of given escrow
1039
+ * This function returns the exchange oracle address for a given escrow.
629
1040
  *
630
- * @param {string} escrowAddress - Address of the escrow.
631
- * @returns {Promise<string>} - Address of the reputation oracle.
632
- * @throws {Error} - An error object if an error occurred.
1041
+ * @param {string} escrowAddress Address of the escrow.
1042
+ * @returns {EscrowStatus} Address of the Exchange Oracle.
1043
+ *
1044
+ * **Code example**
1045
+ *
1046
+ * ```ts
1047
+ * import { providers } from 'ethers';
1048
+ * import { EscrowClient } from '@human-protocol/sdk';
1049
+ *
1050
+ * const rpcUrl = 'YOUR_RPC_URL';
1051
+ *
1052
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
1053
+ * const escrowClient = await EscrowClient.build(signer);
1054
+ *
1055
+ * const oracleAddress = await escrowClient.getExchangeOracleAddress('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
1056
+ * ```
633
1057
  */
634
1058
  async getExchangeOracleAddress(escrowAddress) {
635
1059
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -647,11 +1071,24 @@ class EscrowClient {
647
1071
  }
648
1072
  }
649
1073
  /**
650
- * Returns the escrow factory address of given escrow
1074
+ * This function returns the escrow factory address for a given escrow.
1075
+ *
1076
+ * @param {string} escrowAddress Address of the escrow.
1077
+ * @returns {EscrowStatus} Address of the escrow factory.
1078
+ *
1079
+ * **Code example**
1080
+ *
1081
+ * ```ts
1082
+ * import { providers } from 'ethers';
1083
+ * import { EscrowClient } from '@human-protocol/sdk';
1084
+ *
1085
+ * const rpcUrl = 'YOUR_RPC_URL';
651
1086
  *
652
- * @param {string} escrowAddress - Address of the escrow.
653
- * @returns {Promise<string>} - Address of the escrow factory.
654
- * @throws {Error} - An error object if an error occurred.
1087
+ * const provider = new providers.JsonRpcProvider(rpcUrl);
1088
+ * const escrowClient = await EscrowClient.build(signer);
1089
+ *
1090
+ * const factoryAddress = await escrowClient.getFactoryAddress('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
1091
+ * ```
655
1092
  */
656
1093
  async getFactoryAddress(escrowAddress) {
657
1094
  if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
@@ -730,13 +1167,132 @@ __decorate([
730
1167
  __metadata("design:returntype", Promise)
731
1168
  ], EscrowClient.prototype, "addTrustedHandlers", null);
732
1169
  exports.EscrowClient = EscrowClient;
1170
+ /**
1171
+ * ## Introduction
1172
+ *
1173
+ * Utility class for escrow-related operations.
1174
+ *
1175
+ * ## Installation
1176
+ *
1177
+ * ### npm
1178
+ * ```bash
1179
+ * npm install @human-protocol/sdk
1180
+ * ```
1181
+ *
1182
+ * ### yarn
1183
+ * ```bash
1184
+ * yarn install @human-protocol/sdk
1185
+ * ```
1186
+ *
1187
+ * ## Code example
1188
+ *
1189
+ * ### Signer
1190
+ *
1191
+ * **Using private key(backend)**
1192
+ *
1193
+ * ```ts
1194
+ * import { ChainId, EscrowUtils } from '@human-protocol/sdk';
1195
+ *
1196
+ * const escrowAddresses = new EscrowUtils.getEscrows({
1197
+ * networks: [ChainId.POLYGON_MUMBAI]
1198
+ * });
1199
+ * ```
1200
+ */
733
1201
  class EscrowUtils {
734
1202
  /**
735
- * Returns the list of escrows for given filter
1203
+ * This function returns an array of escrows based on the specified filter parameters.
1204
+ *
1205
+ *
1206
+ * **Input parameters**
1207
+ *
1208
+ * ```ts
1209
+ * interface IEscrowsFilter {
1210
+ * networks: ChainId[];
1211
+ * launcher?: string;
1212
+ * reputationOracle?: string;
1213
+ * recordingOracle?: string;
1214
+ * exchangeOracle?: string;
1215
+ * jobRequesterId?: string;
1216
+ * status?: EscrowStatus;
1217
+ * from?: Date;
1218
+ * to?: Date;
1219
+ * }
1220
+ * ```
1221
+ *
1222
+ * ```ts
1223
+ * enum ChainId {
1224
+ * ALL = -1,
1225
+ * MAINNET = 1,
1226
+ * RINKEBY = 4,
1227
+ * GOERLI = 5,
1228
+ * BSC_MAINNET = 56,
1229
+ * BSC_TESTNET = 97,
1230
+ * POLYGON = 137,
1231
+ * POLYGON_MUMBAI = 80001,
1232
+ * MOONBEAM = 1284,
1233
+ * MOONBASE_ALPHA = 1287,
1234
+ * AVALANCHE = 43114,
1235
+ * AVALANCHE_TESTNET = 43113,
1236
+ * SKALE = 1273227453,
1237
+ * LOCALHOST = 1338,
1238
+ * }
1239
+ * ```
1240
+ *
1241
+ * ```ts
1242
+ * enum EscrowStatus {
1243
+ * Launched,
1244
+ * Pending,
1245
+ * Partial,
1246
+ * Paid,
1247
+ * Complete,
1248
+ * Cancelled,
1249
+ * }
1250
+ * ```
1251
+ *
1252
+ * ```ts
1253
+ * type EscrowData = {
1254
+ * id: string;
1255
+ * address: string;
1256
+ * amountPaid: string;
1257
+ * balance: string;
1258
+ * count: string;
1259
+ * jobRequesterId: string;
1260
+ * factoryAddress: string;
1261
+ * finalResultsUrl?: string;
1262
+ * intermediateResultsUrl?: string;
1263
+ * launcher: string;
1264
+ * manifestHash?: string;
1265
+ * manifestUrl?: string;
1266
+ * recordingOracle?: string;
1267
+ * recordingOracleFee?: string;
1268
+ * reputationOracle?: string;
1269
+ * reputationOracleFee?: string;
1270
+ * exchangeOracle?: string;
1271
+ * exchangeOracleFee?: string;
1272
+ * status: EscrowStatus;
1273
+ * token: string;
1274
+ * totalFundedAmount: string;
1275
+ * createdAt: string;
1276
+ * };
1277
+ * ```
1278
+ *
1279
+ *
1280
+ * @param {IEscrowsFilter} filter Filter parameters.
1281
+ * @returns {EscrowData[]} List of escrows that match the filter.
1282
+ *
1283
+ * **Code example**
736
1284
  *
737
- * @param {IEscrowsFilter} filter - Filter parameters.
738
- * @returns {Promise<EscrowData[]>}
739
- * @throws {Error} - An error object if an error occurred.
1285
+ * ```ts
1286
+ * import { ChainId, EscrowUtils, EscrowStatus } from '@human-protocol/sdk';
1287
+ *
1288
+ * const filters: IEscrowsFilter = {
1289
+ * status: EscrowStatus.Pending,
1290
+ * from: new Date(2023, 4, 8),
1291
+ * to: new Date(2023, 5, 8),
1292
+ * networks: [ChainId.POLYGON_MUMBAI]
1293
+ * };
1294
+ * const escrowDatas = await EscrowUtils.getEscrows(filters);
1295
+ * ```
740
1296
  */
741
1297
  static async getEscrows(filter) {
742
1298
  if (!filter?.networks?.length) {
@@ -783,12 +1339,70 @@ class EscrowUtils {
783
1339
  }
784
1340
  }
785
1341
  /**
786
- * Returns the escrow for a given address
1342
+ * This function returns the escrow data for a given address.
1343
+ *
1344
+ * > This uses Subgraph
1345
+ *
1346
+ * **Input parameters**
1347
+ *
1348
+ * ```ts
1349
+ * enum ChainId {
1350
+ * ALL = -1,
1351
+ * MAINNET = 1,
1352
+ * RINKEBY = 4,
1353
+ * GOERLI = 5,
1354
+ * BSC_MAINNET = 56,
1355
+ * BSC_TESTNET = 97,
1356
+ * POLYGON = 137,
1357
+ * POLYGON_MUMBAI = 80001,
1358
+ * MOONBEAM = 1284,
1359
+ * MOONBASE_ALPHA = 1287,
1360
+ * AVALANCHE = 43114,
1361
+ * AVALANCHE_TESTNET = 43113,
1362
+ * SKALE = 1273227453,
1363
+ * LOCALHOST = 1338,
1364
+ * }
1365
+ * ```
1366
+ *
1367
+ * ```ts
1368
+ * type EscrowData = {
1369
+ * id: string;
1370
+ * address: string;
1371
+ * amountPaid: string;
1372
+ * balance: string;
1373
+ * count: string;
1374
+ * jobRequesterId: string;
1375
+ * factoryAddress: string;
1376
+ * finalResultsUrl?: string;
1377
+ * intermediateResultsUrl?: string;
1378
+ * launcher: string;
1379
+ * manifestHash?: string;
1380
+ * manifestUrl?: string;
1381
+ * recordingOracle?: string;
1382
+ * recordingOracleFee?: string;
1383
+ * reputationOracle?: string;
1384
+ * reputationOracleFee?: string;
1385
+ * exchangeOracle?: string;
1386
+ * exchangeOracleFee?: string;
1387
+ * status: EscrowStatus;
1388
+ * token: string;
1389
+ * totalFundedAmount: string;
1390
+ * createdAt: string;
1391
+ * };
1392
+ * ```
1393
+ *
1394
+ *
1395
+ * @param {ChainId} chainId Network in which the escrow has been deployed
1396
+ * @param {string} escrowAddress Address of the escrow
1397
+ * @returns {EscrowData} Escrow data
1398
+ *
1399
+ * **Code example**
1400
+ *
1401
+ * ```ts
1402
+ * import { ChainId, EscrowUtils } from '@human-protocol/sdk';
787
1403
  *
788
- * @param {string} escrowAddress - Escrow address.
789
- * @param {ChainId} chainId - Chain id.
790
- * @returns {Promise<EscrowData>}
791
- * @throws {Error} - An error object if an error occurred.
1404
+ * const escrowData = new EscrowUtils.getEscrow(ChainId.POLYGON_MUMBAI, "0x1234567890123456789012345678901234567890");
1405
+ * ```
792
1406
  */
793
1407
  static async getEscrow(chainId, escrowAddress) {
794
1408
  const networkData = constants_1.NETWORKS[chainId];