@human-protocol/sdk 1.0.2 → 1.0.4

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 (71) hide show
  1. package/README.md +1 -1
  2. package/dist/constants.d.ts +46 -0
  3. package/dist/constants.d.ts.map +1 -0
  4. package/dist/constants.js +203 -0
  5. package/dist/decorators.d.ts +2 -0
  6. package/dist/decorators.d.ts.map +1 -0
  7. package/dist/decorators.js +17 -0
  8. package/dist/enums.d.ts +17 -0
  9. package/dist/enums.d.ts.map +1 -0
  10. package/dist/enums.js +20 -0
  11. package/dist/error.d.ts +196 -0
  12. package/dist/error.d.ts.map +1 -0
  13. package/dist/error.js +229 -0
  14. package/dist/escrow.d.ts +176 -0
  15. package/dist/escrow.d.ts.map +1 -0
  16. package/dist/escrow.js +590 -0
  17. package/dist/index.d.ts +10 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +33 -0
  20. package/dist/init.d.ts +13 -0
  21. package/dist/init.d.ts.map +1 -0
  22. package/dist/init.js +35 -0
  23. package/dist/interfaces.d.ts +44 -0
  24. package/dist/interfaces.d.ts.map +1 -0
  25. package/dist/interfaces.js +2 -0
  26. package/dist/kvstore.d.ts +40 -0
  27. package/dist/kvstore.d.ts.map +1 -0
  28. package/dist/kvstore.js +106 -0
  29. package/dist/queries.d.ts +4 -0
  30. package/dist/queries.d.ts.map +1 -0
  31. package/dist/queries.js +22 -0
  32. package/dist/staking.d.ts +121 -0
  33. package/dist/staking.d.ts.map +1 -0
  34. package/dist/staking.js +381 -0
  35. package/dist/storage.d.ts +48 -0
  36. package/dist/storage.d.ts.map +1 -0
  37. package/dist/storage.js +164 -0
  38. package/dist/types.d.ts +123 -0
  39. package/dist/types.d.ts.map +1 -0
  40. package/dist/types.js +35 -0
  41. package/dist/utils.d.ts +32 -0
  42. package/dist/utils.d.ts.map +1 -0
  43. package/dist/utils.js +99 -0
  44. package/package.json +4 -7
  45. package/src/constants.ts +221 -4
  46. package/src/decorators.ts +21 -0
  47. package/src/enums.ts +16 -0
  48. package/src/error.ts +295 -18
  49. package/src/escrow.ts +754 -0
  50. package/src/index.ts +14 -1
  51. package/src/init.ts +45 -0
  52. package/src/interfaces.ts +50 -0
  53. package/src/kvstore.ts +93 -0
  54. package/src/queries.ts +18 -0
  55. package/src/staking.ts +421 -0
  56. package/src/storage.ts +159 -131
  57. package/src/types.ts +36 -586
  58. package/src/utils.ts +80 -142
  59. package/test/escrow.test.ts +1339 -0
  60. package/test/init.test.ts +88 -0
  61. package/test/kvstore.test.ts +208 -0
  62. package/test/staking.test.ts +640 -0
  63. package/test/storage.test.ts +422 -0
  64. package/test/utils/constants.ts +38 -1
  65. package/example/simple-existing-job.ts +0 -86
  66. package/example/simple-new-job-public.ts +0 -74
  67. package/example/simple-new-job.ts +0 -72
  68. package/src/job.ts +0 -977
  69. package/src/logger.ts +0 -29
  70. package/test/job.test.ts +0 -716
  71. package/test/utils/manifest.ts +0 -33
package/dist/escrow.js ADDED
@@ -0,0 +1,590 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const typechain_types_1 = require("@human-protocol/core/typechain-types");
13
+ const ethers_1 = require("ethers");
14
+ const error_1 = require("./error");
15
+ const utils_1 = require("./utils");
16
+ const constants_1 = require("./constants");
17
+ const queries_1 = require("./queries");
18
+ const decorators_1 = require("./decorators");
19
+ class EscrowClient {
20
+ /**
21
+ * **Escrow constructor**
22
+ *
23
+ * * @param {IClientParams} clientParams - Init client parameters
24
+ */
25
+ constructor(clientParams) {
26
+ this.clientParams = clientParams;
27
+ this.network = clientParams.network;
28
+ this.escrowFactoryContract = typechain_types_1.EscrowFactory__factory.connect(clientParams.network.factoryAddress, clientParams.signerOrProvider);
29
+ this.signerOrProvider = clientParams.signerOrProvider;
30
+ }
31
+ /**
32
+ * Creates an escrow contract that uses the token passed to pay oracle fees and reward workers.
33
+ *
34
+ * @param {string} tokenAddress - Token address to use for pay outs.
35
+ * @param {string[]} trustedHandlers - Array of addresses that can perform actions on the contract.
36
+ * @returns {Promise<string>} - Return the address of the escrow created.
37
+ * @throws {Error} - An error object if an error occurred.
38
+ */
39
+ async createEscrow(tokenAddress, trustedHandlers) {
40
+ if (!ethers_1.ethers.utils.isAddress(tokenAddress)) {
41
+ throw error_1.ErrorInvalidTokenAddress;
42
+ }
43
+ trustedHandlers.forEach((trustedHandler) => {
44
+ if (!ethers_1.ethers.utils.isAddress(trustedHandler)) {
45
+ throw new error_1.InvalidEthereumAddressError(trustedHandler);
46
+ }
47
+ });
48
+ try {
49
+ const result = await (await this.escrowFactoryContract.createEscrow(tokenAddress, trustedHandlers)).wait();
50
+ if (!result.events || !result.events[0] || !result.events[0].args) {
51
+ throw error_1.ErrorLaunchedEventIsNotEmitted;
52
+ }
53
+ return result.events[0].args[1];
54
+ }
55
+ catch (e) {
56
+ return (0, utils_1.throwError)(e);
57
+ }
58
+ }
59
+ /**
60
+ * Sets up the parameters of the escrow.
61
+ *
62
+ * @param {string} escrowAddress - Address of the escrow to set up.
63
+ * @param {IEscrowConfig} escrowConfig - Configuration object with escrow settings.
64
+ * @returns {Promise<void>}
65
+ * @throws {Error} - An error object if an error occurred.
66
+ */
67
+ async setup(escrowAddress, escrowConfig) {
68
+ const { recordingOracle, reputationOracle, recordingOracleFee, reputationOracleFee, manifestUrl, hash, } = escrowConfig;
69
+ if (!ethers_1.ethers.utils.isAddress(recordingOracle)) {
70
+ throw error_1.ErrorInvalidRecordingOracleAddressProvided;
71
+ }
72
+ if (!ethers_1.ethers.utils.isAddress(reputationOracle)) {
73
+ throw error_1.ErrorInvalidReputationOracleAddressProvided;
74
+ }
75
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
76
+ throw error_1.ErrorInvalidEscrowAddressProvided;
77
+ }
78
+ if (recordingOracleFee.lte(0) || reputationOracleFee.lte(0)) {
79
+ throw error_1.ErrorAmountMustBeGreaterThanZero;
80
+ }
81
+ if (recordingOracleFee.add(reputationOracleFee).gt(100)) {
82
+ throw error_1.ErrorTotalFeeMustBeLessThanHundred;
83
+ }
84
+ if (!manifestUrl) {
85
+ throw error_1.ErrorUrlIsEmptyString;
86
+ }
87
+ if (!(0, utils_1.isValidUrl)(manifestUrl)) {
88
+ throw error_1.ErrorInvalidUrl;
89
+ }
90
+ if (!hash) {
91
+ throw error_1.ErrorHashIsEmptyString;
92
+ }
93
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
94
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
95
+ }
96
+ try {
97
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
98
+ await this.escrowContract.setup(recordingOracle, reputationOracle, recordingOracleFee, reputationOracleFee, manifestUrl, hash);
99
+ return;
100
+ }
101
+ catch (e) {
102
+ return (0, utils_1.throwError)(e);
103
+ }
104
+ }
105
+ /**
106
+ * **Creates an escrow contract that uses the token passed to pay oracle fees and reward workers.*
107
+ * **Sets up the parameters of the escrow.*
108
+ *
109
+ * @param {string} tokenAddress - Token address to use for pay outs.
110
+ * @param {string[]} trustedHandlers - Array of addresses that can perform actions on the contract.
111
+ * @param {IEscrowConfig} escrowConfig - Configuration object with escrow settings.
112
+ * @returns {Promise<string>}
113
+ * @throws {Error} - An error object if an error occurred.
114
+ */
115
+ async createAndSetupEscrow(tokenAddress, trustedHandlers, escrowConfig) {
116
+ try {
117
+ const escrowAddress = await this.createEscrow(tokenAddress, trustedHandlers);
118
+ await this.setup(escrowAddress, escrowConfig);
119
+ return escrowAddress;
120
+ }
121
+ catch (e) {
122
+ return (0, utils_1.throwError)(e);
123
+ }
124
+ }
125
+ /**
126
+ * **Adds funds of the chosen token to the escrow.*
127
+ *
128
+ * @param {string} escrowAddress - Address of the escrow to fund.
129
+ * @param {BigNumber} amount - Amount to be added as funds.
130
+ * @returns {Promise<void>}
131
+ * @throws {Error} - An error object if an error occurred.
132
+ */
133
+ async fund(escrowAddress, amount) {
134
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
135
+ throw error_1.ErrorInvalidEscrowAddressProvided;
136
+ }
137
+ if (amount.lte(0)) {
138
+ throw error_1.ErrorAmountMustBeGreaterThanZero;
139
+ }
140
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
141
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
142
+ }
143
+ try {
144
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
145
+ const tokenAddress = await this.escrowContract.token();
146
+ const tokenContract = typechain_types_1.HMToken__factory.connect(tokenAddress, this.signerOrProvider);
147
+ await tokenContract.transfer(escrowAddress, amount);
148
+ return;
149
+ }
150
+ catch (e) {
151
+ return (0, utils_1.throwError)(e);
152
+ }
153
+ }
154
+ /**
155
+ * **Stores the results.*
156
+ *
157
+ * @param {string} escrowAddress - Address of the escrow.
158
+ * @param {string} sender - Address of the sender.
159
+ * @param {string} url - Results file url.
160
+ * @param {string} hash - Results file hash.
161
+ * @returns {Promise<void>}
162
+ * @throws {Error} - An error object if an error occurred.
163
+ */
164
+ async storeResults(escrowAddress, url, hash) {
165
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
166
+ throw error_1.ErrorInvalidEscrowAddressProvided;
167
+ }
168
+ if (!url) {
169
+ throw error_1.ErrorUrlIsEmptyString;
170
+ }
171
+ if (!(0, utils_1.isValidUrl)(url)) {
172
+ throw error_1.ErrorInvalidUrl;
173
+ }
174
+ if (!hash) {
175
+ throw error_1.ErrorHashIsEmptyString;
176
+ }
177
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
178
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
179
+ }
180
+ try {
181
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
182
+ await this.escrowContract.storeResults(url, hash);
183
+ return;
184
+ }
185
+ catch (e) {
186
+ return (0, utils_1.throwError)(e);
187
+ }
188
+ }
189
+ /**
190
+ * **Sets the status of an escrow to completed.*
191
+ *
192
+ * @param {string} escrowAddress - Address of the escrow.
193
+ * @returns {Promise<void>}
194
+ * @throws {Error} - An error object if an error occurred.
195
+ */
196
+ async complete(escrowAddress) {
197
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
198
+ throw error_1.ErrorInvalidEscrowAddressProvided;
199
+ }
200
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
201
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
202
+ }
203
+ try {
204
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
205
+ await this.escrowContract.complete();
206
+ return;
207
+ }
208
+ catch (e) {
209
+ return (0, utils_1.throwError)(e);
210
+ }
211
+ }
212
+ /**
213
+ * Pays out the amounts specified to the workers and sets the URL of the final results file.
214
+ *
215
+ * @param {string} escrowAddress - Address of the escrow.
216
+ * @param {string[]} recipients - Array of recipient addresses.
217
+ * @param {BigNumber[]} amounts - Array of amounts the recipients will receive.
218
+ * @param {string} finalResultsUrl - Final results file url.
219
+ * @param {string} finalResultsHash - Final results file hash.
220
+ * @returns {Promise<void>}
221
+ * @throws {Error} - An error object if an error occurred.
222
+ */
223
+ async bulkPayOut(escrowAddress, recipients, amounts, finalResultsUrl, finalResultsHash) {
224
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
225
+ throw error_1.ErrorInvalidEscrowAddressProvided;
226
+ }
227
+ if (recipients.length === 0) {
228
+ throw error_1.ErrorRecipientCannotBeEmptyArray;
229
+ }
230
+ if (amounts.length === 0) {
231
+ throw error_1.ErrorAmountsCannotBeEmptyArray;
232
+ }
233
+ if (recipients.length !== amounts.length) {
234
+ throw error_1.ErrorRecipientAndAmountsMustBeSameLength;
235
+ }
236
+ recipients.forEach((recipient) => {
237
+ if (!ethers_1.ethers.utils.isAddress(recipient)) {
238
+ throw new error_1.InvalidEthereumAddressError(recipient);
239
+ }
240
+ });
241
+ if (!finalResultsUrl) {
242
+ throw error_1.ErrorUrlIsEmptyString;
243
+ }
244
+ if (!(0, utils_1.isValidUrl)(finalResultsUrl)) {
245
+ throw error_1.ErrorInvalidUrl;
246
+ }
247
+ if (!finalResultsHash) {
248
+ throw error_1.ErrorHashIsEmptyString;
249
+ }
250
+ const balance = await this.getBalance(escrowAddress);
251
+ let totalAmount = ethers_1.BigNumber.from(0);
252
+ amounts.forEach((amount) => {
253
+ totalAmount = totalAmount.add(amount);
254
+ });
255
+ if (balance.lt(totalAmount)) {
256
+ throw error_1.ErrorEscrowDoesNotHaveEnoughBalance;
257
+ }
258
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
259
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
260
+ }
261
+ try {
262
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
263
+ await this.escrowContract.bulkPayOut(recipients, amounts, finalResultsUrl, finalResultsHash, constants_1.DEFAULT_TX_ID);
264
+ return;
265
+ }
266
+ catch (e) {
267
+ return (0, utils_1.throwError)(e);
268
+ }
269
+ }
270
+ /**
271
+ * Cancels the specified escrow and sends the balance to the canceler.
272
+ *
273
+ * @param {string} escrowAddress - Address of the escrow.
274
+ * @returns {Promise<void>}
275
+ * @throws {Error} - An error object if an error occurred.
276
+ */
277
+ async cancel(escrowAddress) {
278
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
279
+ throw error_1.ErrorInvalidEscrowAddressProvided;
280
+ }
281
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
282
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
283
+ }
284
+ try {
285
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
286
+ await this.escrowContract.cancel();
287
+ return;
288
+ }
289
+ catch (e) {
290
+ return (0, utils_1.throwError)(e);
291
+ }
292
+ }
293
+ /**
294
+ * Cancels the specified escrow, sends the balance to the canceler and selfdestructs the escrow contract.
295
+ *
296
+ * @param {string} escrowAddress - Address of the escrow.
297
+ * @returns {Promise<void>}
298
+ * @throws {Error} - An error object if an error occurred.
299
+ */
300
+ async abort(escrowAddress) {
301
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
302
+ throw error_1.ErrorInvalidEscrowAddressProvided;
303
+ }
304
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
305
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
306
+ }
307
+ try {
308
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
309
+ await this.escrowContract.abort();
310
+ return;
311
+ }
312
+ catch (e) {
313
+ return (0, utils_1.throwError)(e);
314
+ }
315
+ }
316
+ /**
317
+ * Adds an array of addresses to the trusted handlers list.
318
+ *
319
+ * @param {string} escrowAddress - Address of the escrow.
320
+ * @param {string[]} trustedHandlers - List of trusted handler addresses.
321
+ * @returns {Promise<void>}
322
+ * @throws {Error} - An error object if an error occurred.
323
+ */
324
+ async addTrustedHandlers(escrowAddress, trustedHandlers) {
325
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
326
+ throw error_1.ErrorInvalidEscrowAddressProvided;
327
+ }
328
+ if (trustedHandlers.length === 0) {
329
+ throw error_1.ErrorListOfHandlersCannotBeEmpty;
330
+ }
331
+ trustedHandlers.forEach((trustedHandler) => {
332
+ if (!ethers_1.ethers.utils.isAddress(trustedHandler)) {
333
+ throw new error_1.InvalidEthereumAddressError(trustedHandler);
334
+ }
335
+ });
336
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
337
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
338
+ }
339
+ try {
340
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
341
+ await this.escrowContract.addTrustedHandlers(trustedHandlers);
342
+ return;
343
+ }
344
+ catch (e) {
345
+ return (0, utils_1.throwError)(e);
346
+ }
347
+ }
348
+ /**
349
+ * Returns the balance for a specified escrow address.
350
+ *
351
+ * @param {string} escrowAddress - Address of the escrow.
352
+ * @returns {Promise<BigNumber>}
353
+ * @throws {Error} - An error object if an error occurred.
354
+ */
355
+ async getBalance(escrowAddress) {
356
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
357
+ throw error_1.ErrorInvalidEscrowAddressProvided;
358
+ }
359
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
360
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
361
+ }
362
+ try {
363
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
364
+ return this.escrowContract.getBalance();
365
+ }
366
+ catch (e) {
367
+ return (0, utils_1.throwError)(e);
368
+ }
369
+ }
370
+ /**
371
+ * Returns the manifest file URL.
372
+ *
373
+ * @param {string} escrowAddress - Address of the escrow.
374
+ * @returns {Promise<void>}
375
+ * @throws {Error} - An error object if an error occurred.
376
+ */
377
+ async getManifestUrl(escrowAddress) {
378
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
379
+ throw error_1.ErrorInvalidEscrowAddressProvided;
380
+ }
381
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
382
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
383
+ }
384
+ try {
385
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
386
+ return this.escrowContract.manifestUrl();
387
+ }
388
+ catch (e) {
389
+ return (0, utils_1.throwError)(e);
390
+ }
391
+ }
392
+ /**
393
+ * Returns the results file URL.
394
+ *
395
+ * @param {string} escrowAddress - Address of the escrow.
396
+ * @returns {Promise<void>}
397
+ * @throws {Error} - An error object if an error occurred.
398
+ */
399
+ async getResultsUrl(escrowAddress) {
400
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
401
+ throw error_1.ErrorInvalidEscrowAddressProvided;
402
+ }
403
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
404
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
405
+ }
406
+ try {
407
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
408
+ return this.escrowContract.finalResultsUrl();
409
+ }
410
+ catch (e) {
411
+ return (0, utils_1.throwError)(e);
412
+ }
413
+ }
414
+ /**
415
+ * Returns the value for a specified key and address
416
+ *
417
+ * @param {string} escrowAddress - Address of the escrow.
418
+ * @returns {Promise<void>}
419
+ * @throws {Error} - An error object if an error occurred.
420
+ */
421
+ async getTokenAddress(escrowAddress) {
422
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
423
+ throw error_1.ErrorInvalidEscrowAddressProvided;
424
+ }
425
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
426
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
427
+ }
428
+ try {
429
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
430
+ return this.escrowContract.token();
431
+ }
432
+ catch (e) {
433
+ return (0, utils_1.throwError)(e);
434
+ }
435
+ }
436
+ /**
437
+ * Returns the current status of the escrow.
438
+ *
439
+ * @param {string} escrowAddress - Address of the escrow.
440
+ * @returns {Promise<void>}
441
+ * @throws {Error} - An error object if an error occurred.
442
+ */
443
+ async getStatus(escrowAddress) {
444
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
445
+ throw error_1.ErrorInvalidEscrowAddressProvided;
446
+ }
447
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
448
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
449
+ }
450
+ try {
451
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
452
+ return this.escrowContract.status();
453
+ }
454
+ catch (e) {
455
+ return (0, utils_1.throwError)(e);
456
+ }
457
+ }
458
+ /**
459
+ * Returns the current status of the escrow.
460
+ *
461
+ * @param {IEscrowsFilter} requesterAddress - Address of the requester.
462
+ * @returns {Promise<void>}
463
+ * @throws {Error} - An error object if an error occurred.
464
+ */
465
+ async getLaunchedEscrows(requesterAddress) {
466
+ if (!ethers_1.ethers.utils.isAddress(requesterAddress)) {
467
+ throw error_1.ErrorInvalidAddress;
468
+ }
469
+ try {
470
+ const { data } = await (0, utils_1.gqlFetch)(this.network.subgraphUrl, (0, queries_1.RAW_LAUNCHED_ESCROWS_QUERY)(), {
471
+ address: requesterAddress,
472
+ });
473
+ return data;
474
+ }
475
+ catch (e) {
476
+ return (0, utils_1.throwError)(e);
477
+ }
478
+ }
479
+ /**
480
+ * Returns the escrows addresses created by a job requester.
481
+ *
482
+ * @param {string} escrowAddress - Address of the escrow.
483
+ * @param {IEscrowsFilter} filer - Filter parameters.
484
+ * @returns {Promise<void>}
485
+ * @throws {Error} - An error object if an error occurred.
486
+ */
487
+ async getEscrowsFiltered(escrowAddress, filter) {
488
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
489
+ throw error_1.ErrorInvalidAddress;
490
+ }
491
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
492
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
493
+ }
494
+ try {
495
+ const { data } = await (0, utils_1.gqlFetch)(this.network.subgraphUrl, (0, queries_1.RAW_LAUNCHED_ESCROWS_FILTERED_QUERY)(), {
496
+ address: filter.address,
497
+ status: filter.status,
498
+ from: filter.from,
499
+ tro: filter.to,
500
+ });
501
+ return data;
502
+ }
503
+ catch (e) {
504
+ return (0, utils_1.throwError)(e);
505
+ }
506
+ }
507
+ /**
508
+ * Returns the recording oracle address of given escrow
509
+ *
510
+ * @param {string} escrowAddress - Address of the escrow.
511
+ * @returns {Promise<string>} - Address of the recording oracle.
512
+ * @throws {Error} - An error object if an error occurred.
513
+ */
514
+ async getRecordingOracleAddress(escrowAddress) {
515
+ if (!ethers_1.ethers.utils.isAddress(escrowAddress)) {
516
+ throw error_1.ErrorInvalidEscrowAddressProvided;
517
+ }
518
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
519
+ throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
520
+ }
521
+ try {
522
+ this.escrowContract = typechain_types_1.Escrow__factory.connect(escrowAddress, this.signerOrProvider);
523
+ return this.escrowContract.recordingOracle();
524
+ }
525
+ catch (e) {
526
+ return (0, utils_1.throwError)(e);
527
+ }
528
+ }
529
+ }
530
+ __decorate([
531
+ decorators_1.requiresSigner,
532
+ __metadata("design:type", Function),
533
+ __metadata("design:paramtypes", [String, Array]),
534
+ __metadata("design:returntype", Promise)
535
+ ], EscrowClient.prototype, "createEscrow", null);
536
+ __decorate([
537
+ decorators_1.requiresSigner,
538
+ __metadata("design:type", Function),
539
+ __metadata("design:paramtypes", [String, Object]),
540
+ __metadata("design:returntype", Promise)
541
+ ], EscrowClient.prototype, "setup", null);
542
+ __decorate([
543
+ decorators_1.requiresSigner,
544
+ __metadata("design:type", Function),
545
+ __metadata("design:paramtypes", [String, Array, Object]),
546
+ __metadata("design:returntype", Promise)
547
+ ], EscrowClient.prototype, "createAndSetupEscrow", null);
548
+ __decorate([
549
+ decorators_1.requiresSigner,
550
+ __metadata("design:type", Function),
551
+ __metadata("design:paramtypes", [String, ethers_1.BigNumber]),
552
+ __metadata("design:returntype", Promise)
553
+ ], EscrowClient.prototype, "fund", null);
554
+ __decorate([
555
+ decorators_1.requiresSigner,
556
+ __metadata("design:type", Function),
557
+ __metadata("design:paramtypes", [String, String, String]),
558
+ __metadata("design:returntype", Promise)
559
+ ], EscrowClient.prototype, "storeResults", null);
560
+ __decorate([
561
+ decorators_1.requiresSigner,
562
+ __metadata("design:type", Function),
563
+ __metadata("design:paramtypes", [String]),
564
+ __metadata("design:returntype", Promise)
565
+ ], EscrowClient.prototype, "complete", null);
566
+ __decorate([
567
+ decorators_1.requiresSigner,
568
+ __metadata("design:type", Function),
569
+ __metadata("design:paramtypes", [String, Array, Array, String, String]),
570
+ __metadata("design:returntype", Promise)
571
+ ], EscrowClient.prototype, "bulkPayOut", null);
572
+ __decorate([
573
+ decorators_1.requiresSigner,
574
+ __metadata("design:type", Function),
575
+ __metadata("design:paramtypes", [String]),
576
+ __metadata("design:returntype", Promise)
577
+ ], EscrowClient.prototype, "cancel", null);
578
+ __decorate([
579
+ decorators_1.requiresSigner,
580
+ __metadata("design:type", Function),
581
+ __metadata("design:paramtypes", [String]),
582
+ __metadata("design:returntype", Promise)
583
+ ], EscrowClient.prototype, "abort", null);
584
+ __decorate([
585
+ decorators_1.requiresSigner,
586
+ __metadata("design:type", Function),
587
+ __metadata("design:paramtypes", [String, Array]),
588
+ __metadata("design:returntype", Promise)
589
+ ], EscrowClient.prototype, "addTrustedHandlers", null);
590
+ exports.default = EscrowClient;
@@ -0,0 +1,10 @@
1
+ import InitClient from './init';
2
+ import StakingClient from './staking';
3
+ import StorageClient from './storage';
4
+ import KVStoreClient from './kvstore';
5
+ import EscrowClient from './escrow';
6
+ export * from './constants';
7
+ export * from './types';
8
+ export * from './enums';
9
+ export { InitClient, StakingClient, StorageClient, KVStoreClient, EscrowClient, };
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,QAAQ,CAAC;AAChC,OAAO,aAAa,MAAM,WAAW,CAAC;AACtC,OAAO,aAAa,MAAM,WAAW,CAAC;AACtC,OAAO,aAAa,MAAM,WAAW,CAAC;AACtC,OAAO,YAAY,MAAM,UAAU,CAAC;AAEpC,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AAExB,OAAO,EACL,UAAU,EACV,aAAa,EACb,aAAa,EACb,aAAa,EACb,YAAY,GACb,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.EscrowClient = exports.KVStoreClient = exports.StorageClient = exports.StakingClient = exports.InitClient = void 0;
21
+ const init_1 = __importDefault(require("./init"));
22
+ exports.InitClient = init_1.default;
23
+ const staking_1 = __importDefault(require("./staking"));
24
+ exports.StakingClient = staking_1.default;
25
+ const storage_1 = __importDefault(require("./storage"));
26
+ exports.StorageClient = storage_1.default;
27
+ const kvstore_1 = __importDefault(require("./kvstore"));
28
+ exports.KVStoreClient = kvstore_1.default;
29
+ const escrow_1 = __importDefault(require("./escrow"));
30
+ exports.EscrowClient = escrow_1.default;
31
+ __exportStar(require("./constants"), exports);
32
+ __exportStar(require("./types"), exports);
33
+ __exportStar(require("./enums"), exports);
package/dist/init.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { Provider } from '@ethersproject/abstract-provider';
2
+ import { Signer } from 'ethers';
3
+ import { IClientParams } from './interfaces';
4
+ export default class InitClient {
5
+ /**
6
+ * **Get init client parameters**
7
+ *
8
+ * @param {Signer | Provider} providerOrSigner - Ethereum signer or provider
9
+ * @returns {Promise<IClientParams>} - Init client parameters
10
+ */
11
+ static getParams(signerOrProvider: Signer | Provider): Promise<IClientParams>;
12
+ }
13
+ //# sourceMappingURL=init.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAE5D,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAO7C,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B;;;;;OAKG;WACU,SAAS,CACpB,gBAAgB,EAAE,MAAM,GAAG,QAAQ,GAClC,OAAO,CAAC,aAAa,CAAC;CAwB1B"}
package/dist/init.js ADDED
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const ethers_1 = require("ethers");
4
+ const constants_1 = require("./constants");
5
+ const error_1 = require("./error");
6
+ class InitClient {
7
+ /**
8
+ * **Get init client parameters**
9
+ *
10
+ * @param {Signer | Provider} providerOrSigner - Ethereum signer or provider
11
+ * @returns {Promise<IClientParams>} - Init client parameters
12
+ */
13
+ static async getParams(signerOrProvider) {
14
+ let network;
15
+ if (signerOrProvider instanceof ethers_1.Signer) {
16
+ if (!signerOrProvider.provider) {
17
+ throw error_1.ErrorInitProviderDoesNotExist;
18
+ }
19
+ network = await signerOrProvider.provider.getNetwork();
20
+ }
21
+ else {
22
+ network = await signerOrProvider.getNetwork();
23
+ }
24
+ const chainId = network.chainId;
25
+ const networkData = constants_1.NETWORKS[chainId];
26
+ if (!networkData) {
27
+ throw error_1.ErrorInitUnsupportedChainID;
28
+ }
29
+ return {
30
+ signerOrProvider: signerOrProvider,
31
+ network: networkData,
32
+ };
33
+ }
34
+ }
35
+ exports.default = InitClient;