@human-protocol/sdk 4.1.5 → 5.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.js +7 -7
- package/dist/error.d.ts +18 -6
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +20 -8
- package/dist/escrow.d.ts +244 -34
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +267 -166
- package/dist/graphql/queries/escrow.d.ts +3 -1
- package/dist/graphql/queries/escrow.d.ts.map +1 -1
- package/dist/graphql/queries/escrow.js +50 -2
- package/dist/graphql/queries/operator.d.ts.map +1 -1
- package/dist/graphql/queries/operator.js +11 -9
- package/dist/graphql/queries/staking.d.ts +4 -0
- package/dist/graphql/queries/staking.d.ts.map +1 -0
- package/dist/graphql/queries/staking.js +71 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/interfaces.d.ts +58 -18
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/operator.d.ts.map +1 -1
- package/dist/operator.js +53 -58
- package/dist/staking.d.ts +21 -1
- package/dist/staking.d.ts.map +1 -1
- package/dist/staking.js +84 -1
- package/dist/types.d.ts +39 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +4 -0
- package/dist/utils.d.ts +7 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +17 -1
- package/package.json +2 -2
- package/src/constants.ts +7 -7
- package/src/error.ts +27 -9
- package/src/escrow.ts +430 -129
- package/src/graphql/queries/escrow.ts +53 -2
- package/src/graphql/queries/operator.ts +11 -9
- package/src/graphql/queries/staking.ts +80 -0
- package/src/index.ts +2 -1
- package/src/interfaces.ts +65 -20
- package/src/operator.ts +62 -76
- package/src/staking.ts +106 -3
- package/src/types.ts +40 -15
- package/src/utils.ts +15 -0
package/dist/escrow.js
CHANGED
|
@@ -143,7 +143,6 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
143
143
|
* This function creates an escrow contract that uses the token passed to pay oracle fees and reward workers.
|
|
144
144
|
*
|
|
145
145
|
* @param {string} tokenAddress Token address to use for payouts.
|
|
146
|
-
* @param {string[]} trustedHandlers Array of addresses that can perform actions on the contract.
|
|
147
146
|
* @param {string} jobRequesterId Job Requester Id
|
|
148
147
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
149
148
|
* @returns {Promise<string>} Returns the address of the escrow created.
|
|
@@ -165,22 +164,16 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
165
164
|
* const escrowClient = await EscrowClient.build(signer);
|
|
166
165
|
*
|
|
167
166
|
* const tokenAddress = '0x0376D26246Eb35FF4F9924cF13E6C05fd0bD7Fb4';
|
|
168
|
-
* const trustedHandlers = ['0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'];
|
|
169
167
|
* const jobRequesterId = "job-requester-id";
|
|
170
|
-
* const escrowAddress = await escrowClient.createEscrow(tokenAddress,
|
|
168
|
+
* const escrowAddress = await escrowClient.createEscrow(tokenAddress, jobRequesterId);
|
|
171
169
|
* ```
|
|
172
170
|
*/
|
|
173
|
-
async createEscrow(tokenAddress,
|
|
171
|
+
async createEscrow(tokenAddress, jobRequesterId, txOptions = {}) {
|
|
174
172
|
if (!ethers_1.ethers.isAddress(tokenAddress)) {
|
|
175
173
|
throw error_1.ErrorInvalidTokenAddress;
|
|
176
174
|
}
|
|
177
|
-
trustedHandlers.forEach((trustedHandler) => {
|
|
178
|
-
if (!ethers_1.ethers.isAddress(trustedHandler)) {
|
|
179
|
-
throw new error_1.InvalidEthereumAddressError(trustedHandler);
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
175
|
try {
|
|
183
|
-
const result = await (await this.escrowFactoryContract.createEscrow(tokenAddress,
|
|
176
|
+
const result = await (await this.escrowFactoryContract.createEscrow(tokenAddress, jobRequesterId, txOptions)).wait();
|
|
184
177
|
const event = result?.logs?.find(({ topics }) => topics.includes(ethers_1.ethers.id('LaunchedV2(address,address,string)')))?.args;
|
|
185
178
|
if (!event) {
|
|
186
179
|
throw error_1.ErrorLaunchedEventIsNotEmitted;
|
|
@@ -202,7 +195,7 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
202
195
|
*
|
|
203
196
|
* **Code example**
|
|
204
197
|
*
|
|
205
|
-
* > Only Job Launcher or
|
|
198
|
+
* > Only Job Launcher or admin can call it.
|
|
206
199
|
*
|
|
207
200
|
* ```ts
|
|
208
201
|
* import { Wallet, providers } from 'ethers';
|
|
@@ -223,14 +216,14 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
223
216
|
* recordingOracleFee: BigInt('10'),
|
|
224
217
|
* reputationOracleFee: BigInt('10'),
|
|
225
218
|
* exchangeOracleFee: BigInt('10'),
|
|
226
|
-
*
|
|
219
|
+
* manifest: 'http://localhost/manifest.json',
|
|
227
220
|
* manifestHash: 'b5dad76bf6772c0f07fd5e048f6e75a5f86ee079',
|
|
228
221
|
* };
|
|
229
222
|
* await escrowClient.setup(escrowAddress, escrowConfig);
|
|
230
223
|
* ```
|
|
231
224
|
*/
|
|
232
225
|
async setup(escrowAddress, escrowConfig, txOptions = {}) {
|
|
233
|
-
const { recordingOracle, reputationOracle, exchangeOracle, recordingOracleFee, reputationOracleFee, exchangeOracleFee,
|
|
226
|
+
const { recordingOracle, reputationOracle, exchangeOracle, recordingOracleFee, reputationOracleFee, exchangeOracleFee, manifest, manifestHash, } = escrowConfig;
|
|
234
227
|
if (!ethers_1.ethers.isAddress(recordingOracle)) {
|
|
235
228
|
throw error_1.ErrorInvalidRecordingOracleAddressProvided;
|
|
236
229
|
}
|
|
@@ -251,11 +244,9 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
251
244
|
if (recordingOracleFee + reputationOracleFee + exchangeOracleFee > 100) {
|
|
252
245
|
throw error_1.ErrorTotalFeeMustBeLessThanHundred;
|
|
253
246
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
if (!(0, utils_1.isValidUrl)(manifestUrl)) {
|
|
258
|
-
throw error_1.ErrorInvalidUrl;
|
|
247
|
+
const isManifestValid = (0, utils_1.isValidUrl)(manifest) || (0, utils_1.isValidJson)(manifest);
|
|
248
|
+
if (!isManifestValid) {
|
|
249
|
+
throw error_1.ErrorInvalidManifest;
|
|
259
250
|
}
|
|
260
251
|
if (!manifestHash) {
|
|
261
252
|
throw error_1.ErrorHashIsEmptyString;
|
|
@@ -265,7 +256,7 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
265
256
|
}
|
|
266
257
|
try {
|
|
267
258
|
const escrowContract = this.getEscrowContract(escrowAddress);
|
|
268
|
-
await (await escrowContract.setup(reputationOracle, recordingOracle, exchangeOracle, reputationOracleFee, recordingOracleFee, exchangeOracleFee,
|
|
259
|
+
await (await escrowContract.setup(reputationOracle, recordingOracle, exchangeOracle, reputationOracleFee, recordingOracleFee, exchangeOracleFee, manifest, manifestHash, txOptions)).wait();
|
|
269
260
|
return;
|
|
270
261
|
}
|
|
271
262
|
catch (e) {
|
|
@@ -319,56 +310,40 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
319
310
|
return (0, utils_1.throwError)(e);
|
|
320
311
|
}
|
|
321
312
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
* **Code example**
|
|
333
|
-
*
|
|
334
|
-
* > Only Recording Oracle or a trusted handler can call it.
|
|
335
|
-
*
|
|
336
|
-
* ```ts
|
|
337
|
-
* import { ethers, Wallet, providers } from 'ethers';
|
|
338
|
-
* import { EscrowClient } from '@human-protocol/sdk';
|
|
339
|
-
*
|
|
340
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
341
|
-
* const privateKey = 'YOUR_PRIVATE_KEY';
|
|
342
|
-
*
|
|
343
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
344
|
-
* const signer = new Wallet(privateKey, provider);
|
|
345
|
-
* const escrowClient = await EscrowClient.build(signer);
|
|
346
|
-
*
|
|
347
|
-
* await escrowClient.storeResults('0x62dD51230A30401C455c8398d06F85e4EaB6309f', 'http://localhost/results.json', 'b5dad76bf6772c0f07fd5e048f6e75a5f86ee079');
|
|
348
|
-
* ```
|
|
349
|
-
*/
|
|
350
|
-
async storeResults(escrowAddress, url, hash, txOptions = {}) {
|
|
313
|
+
async storeResults(escrowAddress, url, hash, a, b) {
|
|
314
|
+
const escrowContract = this.getEscrowContract(escrowAddress);
|
|
315
|
+
const hasFundsToReserveParam = typeof a === 'bigint';
|
|
316
|
+
const fundsToReserve = hasFundsToReserveParam ? a : undefined;
|
|
317
|
+
const txOptions = (hasFundsToReserveParam ? b : a) || {};
|
|
318
|
+
// When fundsToReserve is provided and is 0, allow empty URL.
|
|
319
|
+
// In this situation not solutions might have been provided so the escrow can be straight cancelled.
|
|
320
|
+
const allowEmptyUrl = hasFundsToReserveParam && fundsToReserve === 0n;
|
|
351
321
|
if (!ethers_1.ethers.isAddress(escrowAddress)) {
|
|
352
322
|
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
353
323
|
}
|
|
354
|
-
if (!url) {
|
|
355
|
-
throw error_1.ErrorUrlIsEmptyString;
|
|
356
|
-
}
|
|
357
|
-
if (!(0, utils_1.isValidUrl)(url)) {
|
|
324
|
+
if (!allowEmptyUrl && !(0, utils_1.isValidUrl)(url)) {
|
|
358
325
|
throw error_1.ErrorInvalidUrl;
|
|
359
326
|
}
|
|
360
|
-
if (!hash) {
|
|
327
|
+
if (!hash && !allowEmptyUrl) {
|
|
361
328
|
throw error_1.ErrorHashIsEmptyString;
|
|
362
329
|
}
|
|
363
330
|
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
364
331
|
throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
|
|
365
332
|
}
|
|
366
333
|
try {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
334
|
+
if (fundsToReserve !== undefined) {
|
|
335
|
+
await (await escrowContract['storeResults(string,string,uint256)'](url, hash, fundsToReserve, txOptions)).wait();
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
await (await escrowContract['storeResults(string,string)'](url, hash, txOptions)).wait();
|
|
339
|
+
}
|
|
370
340
|
}
|
|
371
341
|
catch (e) {
|
|
342
|
+
if (!hasFundsToReserveParam && e.reason === 'DEPRECATED_SIGNATURE') {
|
|
343
|
+
throw error_1.ErrorStoreResultsVersion;
|
|
344
|
+
}
|
|
345
|
+
// eslint-disable-next-line no-console
|
|
346
|
+
console.warn(error_1.WarnVersionMismatch);
|
|
372
347
|
return (0, utils_1.throwError)(e);
|
|
373
348
|
}
|
|
374
349
|
}
|
|
@@ -382,7 +357,7 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
382
357
|
*
|
|
383
358
|
* **Code example**
|
|
384
359
|
*
|
|
385
|
-
* > Only Recording Oracle or
|
|
360
|
+
* > Only Recording Oracle or admin can call it.
|
|
386
361
|
*
|
|
387
362
|
* ```ts
|
|
388
363
|
* import { Wallet, providers } from 'ethers';
|
|
@@ -414,57 +389,24 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
414
389
|
return (0, utils_1.throwError)(e);
|
|
415
390
|
}
|
|
416
391
|
}
|
|
417
|
-
|
|
418
|
-
* This function pays out the amounts specified to the workers and sets the URL of the final results file.
|
|
419
|
-
*
|
|
420
|
-
* @param {string} escrowAddress Escrow address to payout.
|
|
421
|
-
* @param {string[]} recipients Array of recipient addresses.
|
|
422
|
-
* @param {bigint[]} amounts Array of amounts the recipients will receive.
|
|
423
|
-
* @param {string} finalResultsUrl Final results file URL.
|
|
424
|
-
* @param {string} finalResultsHash Final results file hash.
|
|
425
|
-
* @param {number} txId Transaction ID.
|
|
426
|
-
* @param {boolean} forceComplete Indicates if remaining balance should be transferred to the escrow creator (optional, defaults to false).
|
|
427
|
-
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
428
|
-
* @returns Returns void if successful. Throws error if any.
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
* **Code example**
|
|
432
|
-
*
|
|
433
|
-
* > Only Reputation Oracle or a trusted handler can call it.
|
|
434
|
-
*
|
|
435
|
-
* ```ts
|
|
436
|
-
* import { ethers, Wallet, providers } from 'ethers';
|
|
437
|
-
* import { EscrowClient } from '@human-protocol/sdk';
|
|
438
|
-
*
|
|
439
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
440
|
-
* const privateKey = 'YOUR_PRIVATE_KEY';
|
|
441
|
-
*
|
|
442
|
-
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
443
|
-
* const signer = new Wallet(privateKey, provider);
|
|
444
|
-
* const escrowClient = await EscrowClient.build(signer);
|
|
445
|
-
*
|
|
446
|
-
* const recipients = ['0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'];
|
|
447
|
-
* const amounts = [ethers.parseUnits(5, 'ether'), ethers.parseUnits(10, 'ether')];
|
|
448
|
-
* const resultsUrl = 'http://localhost/results.json';
|
|
449
|
-
* const resultsHash = 'b5dad76bf6772c0f07fd5e048f6e75a5f86ee079';
|
|
450
|
-
* const txId = 1;
|
|
451
|
-
*
|
|
452
|
-
* await escrowClient.bulkPayOut('0x62dD51230A30401C455c8398d06F85e4EaB6309f', recipients, amounts, resultsUrl, resultsHash, txId);
|
|
453
|
-
* ```
|
|
454
|
-
*/
|
|
455
|
-
async bulkPayOut(escrowAddress, recipients, amounts, finalResultsUrl, finalResultsHash, txId, forceComplete = false, txOptions = {}) {
|
|
392
|
+
async bulkPayOut(escrowAddress, recipients, amounts, finalResultsUrl, finalResultsHash, id, forceComplete, txOptions = {}) {
|
|
456
393
|
await this.ensureCorrectBulkPayoutInput(escrowAddress, recipients, amounts, finalResultsUrl, finalResultsHash);
|
|
394
|
+
const escrowContract = this.getEscrowContract(escrowAddress);
|
|
395
|
+
const idIsString = typeof id === 'string';
|
|
457
396
|
try {
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
await (await escrowContract['bulkPayOut(address[],uint256[],string,string,uint256,bool)'](recipients, amounts, finalResultsUrl, finalResultsHash, txId, forceComplete, txOptions)).wait();
|
|
397
|
+
if (idIsString) {
|
|
398
|
+
await (await escrowContract['bulkPayOut(address[],uint256[],string,string,string,bool)'](recipients, amounts, finalResultsUrl, finalResultsHash, id, forceComplete, txOptions)).wait();
|
|
461
399
|
}
|
|
462
400
|
else {
|
|
463
|
-
await (await escrowContract['bulkPayOut(address[],uint256[],string,string,uint256)'](recipients, amounts, finalResultsUrl, finalResultsHash,
|
|
401
|
+
await (await escrowContract['bulkPayOut(address[],uint256[],string,string,uint256,bool)'](recipients, amounts, finalResultsUrl, finalResultsHash, id, forceComplete, txOptions)).wait();
|
|
464
402
|
}
|
|
465
|
-
return;
|
|
466
403
|
}
|
|
467
404
|
catch (e) {
|
|
405
|
+
if (!idIsString && e.reason === 'DEPRECATED_SIGNATURE') {
|
|
406
|
+
throw error_1.ErrorBulkPayOutVersion;
|
|
407
|
+
}
|
|
408
|
+
// eslint-disable-next-line no-console
|
|
409
|
+
console.warn(error_1.WarnVersionMismatch);
|
|
468
410
|
return (0, utils_1.throwError)(e);
|
|
469
411
|
}
|
|
470
412
|
}
|
|
@@ -473,12 +415,11 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
473
415
|
*
|
|
474
416
|
* @param {string} escrowAddress Address of the escrow to cancel.
|
|
475
417
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
476
|
-
* @returns {EscrowCancel} Returns the escrow cancellation data including transaction hash and refunded amount. Throws error if any.
|
|
477
418
|
*
|
|
478
419
|
*
|
|
479
420
|
* **Code example**
|
|
480
421
|
*
|
|
481
|
-
* > Only Job Launcher or
|
|
422
|
+
* > Only Job Launcher or admin can call it.
|
|
482
423
|
*
|
|
483
424
|
* ```ts
|
|
484
425
|
* import { ethers, Wallet, providers } from 'ethers';
|
|
@@ -503,49 +444,22 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
503
444
|
}
|
|
504
445
|
try {
|
|
505
446
|
const escrowContract = this.getEscrowContract(escrowAddress);
|
|
506
|
-
|
|
507
|
-
let amountTransferred = undefined;
|
|
508
|
-
const tokenAddress = await escrowContract.token();
|
|
509
|
-
const tokenContract = typechain_types_1.HMToken__factory.connect(tokenAddress, this.runner);
|
|
510
|
-
if (transactionReceipt)
|
|
511
|
-
for (const log of transactionReceipt.logs) {
|
|
512
|
-
if (log.address === tokenAddress) {
|
|
513
|
-
const parsedLog = tokenContract.interface.parseLog({
|
|
514
|
-
topics: log.topics,
|
|
515
|
-
data: log.data,
|
|
516
|
-
});
|
|
517
|
-
const from = parsedLog?.args[0];
|
|
518
|
-
if (parsedLog?.name === 'Transfer' && from === escrowAddress) {
|
|
519
|
-
amountTransferred = parsedLog?.args[2];
|
|
520
|
-
break;
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
if (amountTransferred === undefined) {
|
|
525
|
-
throw error_1.ErrorTransferEventNotFoundInTransactionLogs;
|
|
526
|
-
}
|
|
527
|
-
const escrowCancelData = {
|
|
528
|
-
txHash: transactionReceipt?.hash || '',
|
|
529
|
-
amountRefunded: amountTransferred,
|
|
530
|
-
};
|
|
531
|
-
return escrowCancelData;
|
|
447
|
+
await (await escrowContract.cancel(txOptions)).wait();
|
|
532
448
|
}
|
|
533
449
|
catch (e) {
|
|
534
450
|
return (0, utils_1.throwError)(e);
|
|
535
451
|
}
|
|
536
452
|
}
|
|
537
453
|
/**
|
|
538
|
-
* This function
|
|
454
|
+
* This function requests the cancellation of the specified escrow (moves status to ToCancel or finalizes if expired).
|
|
539
455
|
*
|
|
540
|
-
* @param {string} escrowAddress Address of the escrow.
|
|
541
|
-
* @param {string[]} trustedHandlers Array of addresses of trusted handlers to add.
|
|
456
|
+
* @param {string} escrowAddress Address of the escrow to request cancellation.
|
|
542
457
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
543
458
|
* @returns Returns void if successful. Throws error if any.
|
|
544
459
|
*
|
|
545
|
-
*
|
|
546
460
|
* **Code example**
|
|
547
461
|
*
|
|
548
|
-
* > Only Job Launcher or
|
|
462
|
+
* > Only Job Launcher or admin can call it.
|
|
549
463
|
*
|
|
550
464
|
* ```ts
|
|
551
465
|
* import { Wallet, providers } from 'ethers';
|
|
@@ -558,29 +472,19 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
558
472
|
* const signer = new Wallet(privateKey, provider);
|
|
559
473
|
* const escrowClient = await EscrowClient.build(signer);
|
|
560
474
|
*
|
|
561
|
-
*
|
|
562
|
-
* await escrowClient.addTrustedHandlers('0x62dD51230A30401C455c8398d06F85e4EaB6309f', trustedHandlers);
|
|
475
|
+
* await escrowClient.requestCancellation('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
563
476
|
* ```
|
|
564
477
|
*/
|
|
565
|
-
async
|
|
478
|
+
async requestCancellation(escrowAddress, txOptions = {}) {
|
|
566
479
|
if (!ethers_1.ethers.isAddress(escrowAddress)) {
|
|
567
480
|
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
568
481
|
}
|
|
569
|
-
if (trustedHandlers.length === 0) {
|
|
570
|
-
throw error_1.ErrorListOfHandlersCannotBeEmpty;
|
|
571
|
-
}
|
|
572
|
-
trustedHandlers.forEach((trustedHandler) => {
|
|
573
|
-
if (!ethers_1.ethers.isAddress(trustedHandler)) {
|
|
574
|
-
throw new error_1.InvalidEthereumAddressError(trustedHandler);
|
|
575
|
-
}
|
|
576
|
-
});
|
|
577
482
|
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
578
483
|
throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
|
|
579
484
|
}
|
|
580
485
|
try {
|
|
581
486
|
const escrowContract = this.getEscrowContract(escrowAddress);
|
|
582
|
-
await (await escrowContract.
|
|
583
|
-
return;
|
|
487
|
+
await (await escrowContract.requestCancellation(txOptions)).wait();
|
|
584
488
|
}
|
|
585
489
|
catch (e) {
|
|
586
490
|
return (0, utils_1.throwError)(e);
|
|
@@ -597,7 +501,7 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
597
501
|
*
|
|
598
502
|
* **Code example**
|
|
599
503
|
*
|
|
600
|
-
* > Only Job Launcher or
|
|
504
|
+
* > Only Job Launcher or admin can call it.
|
|
601
505
|
*
|
|
602
506
|
* ```ts
|
|
603
507
|
* import { ethers, Wallet, providers } from 'ethers';
|
|
@@ -651,7 +555,7 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
651
555
|
const escrowWithdrawData = {
|
|
652
556
|
txHash: transactionReceipt?.hash || '',
|
|
653
557
|
tokenAddress,
|
|
654
|
-
|
|
558
|
+
withdrawnAmount: amountTransferred,
|
|
655
559
|
};
|
|
656
560
|
return escrowWithdrawData;
|
|
657
561
|
}
|
|
@@ -666,14 +570,14 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
666
570
|
* @param {bigint[]} amounts Array of amounts the recipients will receive.
|
|
667
571
|
* @param {string} finalResultsUrl Final results file URL.
|
|
668
572
|
* @param {string} finalResultsHash Final results file hash.
|
|
669
|
-
* @param {
|
|
573
|
+
* @param {string} payoutId Payout ID to identify the payout.
|
|
670
574
|
* @param {boolean} forceComplete Indicates if remaining balance should be transferred to the escrow creator (optional, defaults to false).
|
|
671
575
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
672
576
|
* @returns Returns object with raw transaction and signed transaction hash
|
|
673
577
|
*
|
|
674
578
|
* **Code example**
|
|
675
579
|
*
|
|
676
|
-
* > Only Reputation Oracle or
|
|
580
|
+
* > Only Reputation Oracle or admin can call it.
|
|
677
581
|
*
|
|
678
582
|
* ```ts
|
|
679
583
|
* import { ethers, Wallet, providers } from 'ethers';
|
|
@@ -690,7 +594,7 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
690
594
|
* const amounts = [ethers.parseUnits(5, 'ether'), ethers.parseUnits(10, 'ether')];
|
|
691
595
|
* const resultsUrl = 'http://localhost/results.json';
|
|
692
596
|
* const resultsHash = 'b5dad76bf6772c0f07fd5e048f6e75a5f86ee079';
|
|
693
|
-
* const
|
|
597
|
+
* const payoutId = '372f6916-fe34-4711-b6e3-274f682047de';
|
|
694
598
|
*
|
|
695
599
|
* const rawTransaction = await escrowClient.createBulkPayoutTransaction('0x62dD51230A30401C455c8398d06F85e4EaB6309f', recipients, amounts, resultsUrl, resultsHash, txId);
|
|
696
600
|
* console.log('Raw transaction:', rawTransaction);
|
|
@@ -699,12 +603,12 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
699
603
|
* console.log('Tx hash:', ethers.keccak256(signedTransaction));
|
|
700
604
|
* (await signer.sendTransaction(rawTransaction)).wait();
|
|
701
605
|
*/
|
|
702
|
-
async createBulkPayoutTransaction(escrowAddress, recipients, amounts, finalResultsUrl, finalResultsHash,
|
|
606
|
+
async createBulkPayoutTransaction(escrowAddress, recipients, amounts, finalResultsUrl, finalResultsHash, payoutId, forceComplete = false, txOptions = {}) {
|
|
703
607
|
await this.ensureCorrectBulkPayoutInput(escrowAddress, recipients, amounts, finalResultsUrl, finalResultsHash);
|
|
704
608
|
const signer = this.runner;
|
|
705
609
|
try {
|
|
706
610
|
const escrowContract = this.getEscrowContract(escrowAddress);
|
|
707
|
-
const populatedTransaction = await escrowContract['bulkPayOut(address[],uint256[],string,string,
|
|
611
|
+
const populatedTransaction = await escrowContract['bulkPayOut(address[],uint256[],string,string,string,bool)'].populateTransaction(recipients, amounts, finalResultsUrl, finalResultsHash, payoutId, forceComplete, txOptions);
|
|
708
612
|
/**
|
|
709
613
|
* Safety-belt: explicitly set the passed nonce
|
|
710
614
|
* because 'populateTransaction' return value
|
|
@@ -756,7 +660,7 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
756
660
|
}
|
|
757
661
|
});
|
|
758
662
|
if (!finalResultsUrl) {
|
|
759
|
-
throw error_1.
|
|
663
|
+
throw error_1.ErrorInvalidUrl;
|
|
760
664
|
}
|
|
761
665
|
if (!(0, utils_1.isValidUrl)(finalResultsUrl)) {
|
|
762
666
|
throw error_1.ErrorInvalidUrl;
|
|
@@ -817,6 +721,41 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
817
721
|
return (0, utils_1.throwError)(e);
|
|
818
722
|
}
|
|
819
723
|
}
|
|
724
|
+
/**
|
|
725
|
+
* This function returns the reserved funds for a specified escrow address.
|
|
726
|
+
*
|
|
727
|
+
* @param {string} escrowAddress Address of the escrow.
|
|
728
|
+
* @returns {Promise<bigint>} Reserved funds of the escrow in the token used to fund it.
|
|
729
|
+
*
|
|
730
|
+
* **Code example**
|
|
731
|
+
*
|
|
732
|
+
* ```ts
|
|
733
|
+
* import { providers } from 'ethers';
|
|
734
|
+
* import { EscrowClient } from '@human-protocol/sdk';
|
|
735
|
+
*
|
|
736
|
+
* const rpcUrl = 'YOUR_RPC_URL';
|
|
737
|
+
*
|
|
738
|
+
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
739
|
+
* const escrowClient = await EscrowClient.build(provider);
|
|
740
|
+
*
|
|
741
|
+
* const reservedFunds = await escrowClient.getReservedFunds('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
742
|
+
* ```
|
|
743
|
+
*/
|
|
744
|
+
async getReservedFunds(escrowAddress) {
|
|
745
|
+
if (!ethers_1.ethers.isAddress(escrowAddress)) {
|
|
746
|
+
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
747
|
+
}
|
|
748
|
+
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
749
|
+
throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
|
|
750
|
+
}
|
|
751
|
+
try {
|
|
752
|
+
const escrowContract = this.getEscrowContract(escrowAddress);
|
|
753
|
+
return await escrowContract.reservedFunds();
|
|
754
|
+
}
|
|
755
|
+
catch (e) {
|
|
756
|
+
return (0, utils_1.throwError)(e);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
820
759
|
/**
|
|
821
760
|
* This function returns the manifest file hash.
|
|
822
761
|
*
|
|
@@ -853,7 +792,7 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
853
792
|
}
|
|
854
793
|
}
|
|
855
794
|
/**
|
|
856
|
-
* This function returns the manifest
|
|
795
|
+
* This function returns the manifest. Could be a URL or a JSON string.
|
|
857
796
|
*
|
|
858
797
|
* @param {string} escrowAddress Address of the escrow.
|
|
859
798
|
* @returns {Promise<string>} Url of the manifest.
|
|
@@ -869,10 +808,10 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
869
808
|
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
870
809
|
* const escrowClient = await EscrowClient.build(provider);
|
|
871
810
|
*
|
|
872
|
-
* const
|
|
811
|
+
* const manifest = await escrowClient.getManifest('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
873
812
|
* ```
|
|
874
813
|
*/
|
|
875
|
-
async
|
|
814
|
+
async getManifest(escrowAddress) {
|
|
876
815
|
if (!ethers_1.ethers.isAddress(escrowAddress)) {
|
|
877
816
|
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
878
817
|
}
|
|
@@ -957,6 +896,41 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
957
896
|
return (0, utils_1.throwError)(e);
|
|
958
897
|
}
|
|
959
898
|
}
|
|
899
|
+
/**
|
|
900
|
+
* This function returns the intermediate results hash.
|
|
901
|
+
*
|
|
902
|
+
* @param {string} escrowAddress Address of the escrow.
|
|
903
|
+
* @returns {Promise<string>} Hash of the intermediate results file content.
|
|
904
|
+
*
|
|
905
|
+
* **Code example**
|
|
906
|
+
*
|
|
907
|
+
* ```ts
|
|
908
|
+
* import { providers } from 'ethers';
|
|
909
|
+
* import { EscrowClient } from '@human-protocol/sdk';
|
|
910
|
+
*
|
|
911
|
+
* const rpcUrl = 'YOUR_RPC_URL';
|
|
912
|
+
*
|
|
913
|
+
* const provider = new providers.JsonRpcProvider(rpcUrl);
|
|
914
|
+
* const escrowClient = await EscrowClient.build(provider);
|
|
915
|
+
*
|
|
916
|
+
* const intermediateResultsHash = await escrowClient.getIntermediateResultsHash('0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
917
|
+
* ```
|
|
918
|
+
*/
|
|
919
|
+
async getIntermediateResultsHash(escrowAddress) {
|
|
920
|
+
if (!ethers_1.ethers.isAddress(escrowAddress)) {
|
|
921
|
+
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
922
|
+
}
|
|
923
|
+
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
924
|
+
throw error_1.ErrorEscrowAddressIsNotProvidedByFactory;
|
|
925
|
+
}
|
|
926
|
+
try {
|
|
927
|
+
const escrowContract = this.getEscrowContract(escrowAddress);
|
|
928
|
+
return escrowContract.intermediateResultsHash();
|
|
929
|
+
}
|
|
930
|
+
catch (e) {
|
|
931
|
+
return (0, utils_1.throwError)(e);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
960
934
|
/**
|
|
961
935
|
* This function returns the token address used for funding the escrow.
|
|
962
936
|
*
|
|
@@ -1207,7 +1181,7 @@ exports.EscrowClient = EscrowClient;
|
|
|
1207
1181
|
__decorate([
|
|
1208
1182
|
decorators_1.requiresSigner,
|
|
1209
1183
|
__metadata("design:type", Function),
|
|
1210
|
-
__metadata("design:paramtypes", [String,
|
|
1184
|
+
__metadata("design:paramtypes", [String, String, Object]),
|
|
1211
1185
|
__metadata("design:returntype", Promise)
|
|
1212
1186
|
], EscrowClient.prototype, "createEscrow", null);
|
|
1213
1187
|
__decorate([
|
|
@@ -1225,7 +1199,7 @@ __decorate([
|
|
|
1225
1199
|
__decorate([
|
|
1226
1200
|
decorators_1.requiresSigner,
|
|
1227
1201
|
__metadata("design:type", Function),
|
|
1228
|
-
__metadata("design:paramtypes", [String, String, String, Object]),
|
|
1202
|
+
__metadata("design:paramtypes", [String, String, String, Object, Object]),
|
|
1229
1203
|
__metadata("design:returntype", Promise)
|
|
1230
1204
|
], EscrowClient.prototype, "storeResults", null);
|
|
1231
1205
|
__decorate([
|
|
@@ -1237,7 +1211,7 @@ __decorate([
|
|
|
1237
1211
|
__decorate([
|
|
1238
1212
|
decorators_1.requiresSigner,
|
|
1239
1213
|
__metadata("design:type", Function),
|
|
1240
|
-
__metadata("design:paramtypes", [String, Array, Array, String, String,
|
|
1214
|
+
__metadata("design:paramtypes", [String, Array, Array, String, String, Object, Boolean, Object]),
|
|
1241
1215
|
__metadata("design:returntype", Promise)
|
|
1242
1216
|
], EscrowClient.prototype, "bulkPayOut", null);
|
|
1243
1217
|
__decorate([
|
|
@@ -1249,9 +1223,9 @@ __decorate([
|
|
|
1249
1223
|
__decorate([
|
|
1250
1224
|
decorators_1.requiresSigner,
|
|
1251
1225
|
__metadata("design:type", Function),
|
|
1252
|
-
__metadata("design:paramtypes", [String,
|
|
1226
|
+
__metadata("design:paramtypes", [String, Object]),
|
|
1253
1227
|
__metadata("design:returntype", Promise)
|
|
1254
|
-
], EscrowClient.prototype, "
|
|
1228
|
+
], EscrowClient.prototype, "requestCancellation", null);
|
|
1255
1229
|
__decorate([
|
|
1256
1230
|
decorators_1.requiresSigner,
|
|
1257
1231
|
__metadata("design:type", Function),
|
|
@@ -1261,7 +1235,7 @@ __decorate([
|
|
|
1261
1235
|
__decorate([
|
|
1262
1236
|
decorators_1.requiresSigner,
|
|
1263
1237
|
__metadata("design:type", Function),
|
|
1264
|
-
__metadata("design:paramtypes", [String, Array, Array, String, String,
|
|
1238
|
+
__metadata("design:paramtypes", [String, Array, Array, String, String, String, Object, Object]),
|
|
1265
1239
|
__metadata("design:returntype", Promise)
|
|
1266
1240
|
], EscrowClient.prototype, "createBulkPayoutTransaction", null);
|
|
1267
1241
|
/**
|
|
@@ -1363,7 +1337,7 @@ class EscrowUtils {
|
|
|
1363
1337
|
* intermediateResultsUrl?: string;
|
|
1364
1338
|
* launcher: string;
|
|
1365
1339
|
* manifestHash?: string;
|
|
1366
|
-
*
|
|
1340
|
+
* manifest?: string;
|
|
1367
1341
|
* recordingOracle?: string;
|
|
1368
1342
|
* reputationOracle?: string;
|
|
1369
1343
|
* exchangeOracle?: string;
|
|
@@ -1469,7 +1443,7 @@ class EscrowUtils {
|
|
|
1469
1443
|
* intermediateResultsUrl?: string;
|
|
1470
1444
|
* launcher: string;
|
|
1471
1445
|
* manifestHash?: string;
|
|
1472
|
-
*
|
|
1446
|
+
* manifest?: string;
|
|
1473
1447
|
* recordingOracle?: string;
|
|
1474
1448
|
* reputationOracle?: string;
|
|
1475
1449
|
* exchangeOracle?: string;
|
|
@@ -1649,5 +1623,132 @@ class EscrowUtils {
|
|
|
1649
1623
|
});
|
|
1650
1624
|
return payouts || [];
|
|
1651
1625
|
}
|
|
1626
|
+
/**
|
|
1627
|
+
* This function returns the cancellation refunds for a given set of networks.
|
|
1628
|
+
*
|
|
1629
|
+
* > This uses Subgraph
|
|
1630
|
+
*
|
|
1631
|
+
* **Input parameters**
|
|
1632
|
+
*
|
|
1633
|
+
* ```ts
|
|
1634
|
+
* enum ChainId {
|
|
1635
|
+
* ALL = -1,
|
|
1636
|
+
* MAINNET = 1,
|
|
1637
|
+
* SEPOLIA = 11155111,
|
|
1638
|
+
* BSC_MAINNET = 56,
|
|
1639
|
+
* BSC_TESTNET = 97,
|
|
1640
|
+
* POLYGON = 137,
|
|
1641
|
+
* POLYGON_AMOY = 80002,
|
|
1642
|
+
* LOCALHOST = 1338,
|
|
1643
|
+
* }
|
|
1644
|
+
* ```
|
|
1645
|
+
*
|
|
1646
|
+
* ```ts
|
|
1647
|
+
* type CancellationRefund = {
|
|
1648
|
+
* id: string;
|
|
1649
|
+
* escrowAddress: string;
|
|
1650
|
+
* receiver: string;
|
|
1651
|
+
* amount: bigint;
|
|
1652
|
+
* block: number;
|
|
1653
|
+
* timestamp: number;
|
|
1654
|
+
* txHash: string;
|
|
1655
|
+
* };
|
|
1656
|
+
* ```
|
|
1657
|
+
*
|
|
1658
|
+
*
|
|
1659
|
+
* @param {Object} filter Filter parameters.
|
|
1660
|
+
* @returns {Promise<CancellationRefund[]>} List of cancellation refunds matching the filters.
|
|
1661
|
+
*
|
|
1662
|
+
* **Code example**
|
|
1663
|
+
*
|
|
1664
|
+
* ```ts
|
|
1665
|
+
* import { ChainId, EscrowUtils } from '@human-protocol/sdk';
|
|
1666
|
+
*
|
|
1667
|
+
* const cancellationRefunds = await EscrowUtils.getCancellationRefunds({
|
|
1668
|
+
* chainId: ChainId.POLYGON_AMOY,
|
|
1669
|
+
* escrowAddress: '0x1234567890123456789012345678901234567890',
|
|
1670
|
+
* });
|
|
1671
|
+
* console.log(cancellationRefunds);
|
|
1672
|
+
* ```
|
|
1673
|
+
*/
|
|
1674
|
+
static async getCancellationRefunds(filter) {
|
|
1675
|
+
const networkData = constants_1.NETWORKS[filter.chainId];
|
|
1676
|
+
if (!networkData)
|
|
1677
|
+
throw error_1.ErrorUnsupportedChainID;
|
|
1678
|
+
if (filter.escrowAddress && !ethers_1.ethers.isAddress(filter.escrowAddress)) {
|
|
1679
|
+
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
1680
|
+
}
|
|
1681
|
+
if (filter.receiver && !ethers_1.ethers.isAddress(filter.receiver)) {
|
|
1682
|
+
throw error_1.ErrorInvalidAddress;
|
|
1683
|
+
}
|
|
1684
|
+
const first = filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
|
|
1685
|
+
const skip = filter.skip || 0;
|
|
1686
|
+
const orderDirection = filter.orderDirection || enums_1.OrderDirection.DESC;
|
|
1687
|
+
const { cancellationRefundEvents } = await (0, graphql_request_1.default)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_CANCELLATION_REFUNDS_QUERY)(filter), {
|
|
1688
|
+
escrowAddress: filter.escrowAddress?.toLowerCase(),
|
|
1689
|
+
receiver: filter.receiver?.toLowerCase(),
|
|
1690
|
+
from: filter.from ? (0, utils_1.getUnixTimestamp)(filter.from) : undefined,
|
|
1691
|
+
to: filter.to ? (0, utils_1.getUnixTimestamp)(filter.to) : undefined,
|
|
1692
|
+
first,
|
|
1693
|
+
skip,
|
|
1694
|
+
orderDirection,
|
|
1695
|
+
});
|
|
1696
|
+
return cancellationRefundEvents || [];
|
|
1697
|
+
}
|
|
1698
|
+
/**
|
|
1699
|
+
* This function returns the cancellation refund for a given escrow address.
|
|
1700
|
+
*
|
|
1701
|
+
* > This uses Subgraph
|
|
1702
|
+
*
|
|
1703
|
+
* **Input parameters**
|
|
1704
|
+
*
|
|
1705
|
+
* ```ts
|
|
1706
|
+
* enum ChainId {
|
|
1707
|
+
* ALL = -1,
|
|
1708
|
+
* MAINNET = 1,
|
|
1709
|
+
* SEPOLIA = 11155111,
|
|
1710
|
+
* BSC_MAINNET = 56,
|
|
1711
|
+
* BSC_TESTNET = 97,
|
|
1712
|
+
* POLYGON = 137,
|
|
1713
|
+
* POLYGON_AMOY = 80002,
|
|
1714
|
+
* LOCALHOST = 1338,
|
|
1715
|
+
* }
|
|
1716
|
+
* ```
|
|
1717
|
+
*
|
|
1718
|
+
* ```ts
|
|
1719
|
+
* type CancellationRefund = {
|
|
1720
|
+
* id: string;
|
|
1721
|
+
* escrowAddress: string;
|
|
1722
|
+
* receiver: string;
|
|
1723
|
+
* amount: bigint;
|
|
1724
|
+
* block: number;
|
|
1725
|
+
* timestamp: number;
|
|
1726
|
+
* txHash: string;
|
|
1727
|
+
* };
|
|
1728
|
+
* ```
|
|
1729
|
+
*
|
|
1730
|
+
*
|
|
1731
|
+
* @param {ChainId} chainId Network in which the escrow has been deployed
|
|
1732
|
+
* @param {string} escrowAddress Address of the escrow
|
|
1733
|
+
* @returns {Promise<CancellationRefund>} Cancellation refund data
|
|
1734
|
+
*
|
|
1735
|
+
* **Code example**
|
|
1736
|
+
*
|
|
1737
|
+
* ```ts
|
|
1738
|
+
* import { ChainId, EscrowUtils } from '@human-protocol/sdk';
|
|
1739
|
+
*
|
|
1740
|
+
* const cancellationRefund = await EscrowUtils.getCancellationRefund(ChainId.POLYGON_AMOY, "0x1234567890123456789012345678901234567890");
|
|
1741
|
+
* ```
|
|
1742
|
+
*/
|
|
1743
|
+
static async getCancellationRefund(chainId, escrowAddress) {
|
|
1744
|
+
const networkData = constants_1.NETWORKS[chainId];
|
|
1745
|
+
if (!networkData)
|
|
1746
|
+
throw error_1.ErrorUnsupportedChainID;
|
|
1747
|
+
if (!ethers_1.ethers.isAddress(escrowAddress)) {
|
|
1748
|
+
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
1749
|
+
}
|
|
1750
|
+
const { cancellationRefundEvents } = await (0, graphql_request_1.default)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_CANCELLATION_REFUND_BY_ADDRESS_QUERY)(), { escrowAddress: escrowAddress.toLowerCase() });
|
|
1751
|
+
return cancellationRefundEvents?.[0] || null;
|
|
1752
|
+
}
|
|
1652
1753
|
}
|
|
1653
1754
|
exports.EscrowUtils = EscrowUtils;
|