@human-protocol/sdk 5.0.0-beta.2 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.js +6 -6
- package/dist/escrow.d.ts +114 -44
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +236 -78
- package/dist/graphql/types.d.ts +101 -74
- package/dist/graphql/types.d.ts.map +1 -1
- package/dist/interfaces.d.ts +119 -77
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/operator.d.ts.map +1 -1
- package/dist/operator.js +13 -7
- package/dist/staking.d.ts.map +1 -1
- package/dist/staking.js +13 -2
- package/dist/statistics.d.ts +32 -33
- package/dist/statistics.d.ts.map +1 -1
- package/dist/statistics.js +38 -39
- package/dist/transaction.d.ts +26 -2
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +54 -4
- package/dist/types.d.ts +0 -75
- package/dist/types.d.ts.map +1 -1
- package/dist/worker.d.ts +1 -1
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +14 -4
- package/package.json +2 -2
- package/src/constants.ts +6 -6
- package/src/escrow.ts +307 -108
- package/src/graphql/types.ts +108 -87
- package/src/interfaces.ts +132 -78
- package/src/operator.ts +17 -13
- package/src/staking.ts +17 -4
- package/src/statistics.ts +58 -62
- package/src/transaction.ts +66 -8
- package/src/types.ts +0 -79
- package/src/worker.ts +18 -6
package/src/escrow.ts
CHANGED
|
@@ -43,6 +43,7 @@ import {
|
|
|
43
43
|
WarnVersionMismatch,
|
|
44
44
|
} from './error';
|
|
45
45
|
import {
|
|
46
|
+
CancellationRefundData,
|
|
46
47
|
EscrowData,
|
|
47
48
|
GET_CANCELLATION_REFUNDS_QUERY,
|
|
48
49
|
GET_CANCELLATION_REFUND_BY_ADDRESS_QUERY,
|
|
@@ -50,6 +51,7 @@ import {
|
|
|
50
51
|
GET_ESCROW_BY_ADDRESS_QUERY,
|
|
51
52
|
GET_PAYOUTS_QUERY,
|
|
52
53
|
GET_STATUS_UPDATES_QUERY,
|
|
54
|
+
PayoutData,
|
|
53
55
|
StatusEvent,
|
|
54
56
|
} from './graphql';
|
|
55
57
|
import {
|
|
@@ -58,15 +60,12 @@ import {
|
|
|
58
60
|
IEscrowsFilter,
|
|
59
61
|
IPayoutFilter,
|
|
60
62
|
IStatusEventFilter,
|
|
63
|
+
IStatusEvent,
|
|
64
|
+
ICancellationRefund,
|
|
65
|
+
IPayout,
|
|
66
|
+
IEscrowWithdraw,
|
|
61
67
|
} from './interfaces';
|
|
62
|
-
import {
|
|
63
|
-
EscrowStatus,
|
|
64
|
-
EscrowWithdraw,
|
|
65
|
-
NetworkData,
|
|
66
|
-
TransactionLikeWithNonce,
|
|
67
|
-
Payout,
|
|
68
|
-
CancellationRefund,
|
|
69
|
-
} from './types';
|
|
68
|
+
import { EscrowStatus, NetworkData, TransactionLikeWithNonce } from './types';
|
|
70
69
|
import {
|
|
71
70
|
getSubgraphUrl,
|
|
72
71
|
getUnixTimestamp,
|
|
@@ -204,8 +203,8 @@ export class EscrowClient extends BaseEthersClient {
|
|
|
204
203
|
/**
|
|
205
204
|
* This function creates an escrow contract that uses the token passed to pay oracle fees and reward workers.
|
|
206
205
|
*
|
|
207
|
-
* @param {string} tokenAddress
|
|
208
|
-
* @param {string} jobRequesterId
|
|
206
|
+
* @param {string} tokenAddress - The address of the token to use for escrow funding.
|
|
207
|
+
* @param {string} jobRequesterId - Identifier for the job requester.
|
|
209
208
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
210
209
|
* @returns {Promise<string>} Returns the address of the escrow created.
|
|
211
210
|
*
|
|
@@ -264,6 +263,167 @@ export class EscrowClient extends BaseEthersClient {
|
|
|
264
263
|
return throwError(e);
|
|
265
264
|
}
|
|
266
265
|
}
|
|
266
|
+
private verifySetupParameters(escrowConfig: IEscrowConfig) {
|
|
267
|
+
const {
|
|
268
|
+
recordingOracle,
|
|
269
|
+
reputationOracle,
|
|
270
|
+
exchangeOracle,
|
|
271
|
+
recordingOracleFee,
|
|
272
|
+
reputationOracleFee,
|
|
273
|
+
exchangeOracleFee,
|
|
274
|
+
manifest,
|
|
275
|
+
manifestHash,
|
|
276
|
+
} = escrowConfig;
|
|
277
|
+
|
|
278
|
+
if (!ethers.isAddress(recordingOracle)) {
|
|
279
|
+
throw ErrorInvalidRecordingOracleAddressProvided;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (!ethers.isAddress(reputationOracle)) {
|
|
283
|
+
throw ErrorInvalidReputationOracleAddressProvided;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (!ethers.isAddress(exchangeOracle)) {
|
|
287
|
+
throw ErrorInvalidExchangeOracleAddressProvided;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (
|
|
291
|
+
recordingOracleFee <= 0 ||
|
|
292
|
+
reputationOracleFee <= 0 ||
|
|
293
|
+
exchangeOracleFee <= 0
|
|
294
|
+
) {
|
|
295
|
+
throw ErrorAmountMustBeGreaterThanZero;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (recordingOracleFee + reputationOracleFee + exchangeOracleFee > 100) {
|
|
299
|
+
throw ErrorTotalFeeMustBeLessThanHundred;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const isManifestValid = isValidUrl(manifest) || isValidJson(manifest);
|
|
303
|
+
if (!isManifestValid) {
|
|
304
|
+
throw ErrorInvalidManifest;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (!manifestHash) {
|
|
308
|
+
throw ErrorHashIsEmptyString;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Creates, funds, and sets up a new escrow contract in a single transaction.
|
|
314
|
+
*
|
|
315
|
+
* @param {string} tokenAddress - The ERC-20 token address used to fund the escrow.
|
|
316
|
+
* @param {bigint} amount - The token amount to fund the escrow with.
|
|
317
|
+
* @param {string} jobRequesterId - An off-chain identifier for the job requester.
|
|
318
|
+
* @param {IEscrowConfig} escrowConfig - Configuration parameters for escrow setup:
|
|
319
|
+
* - `recordingOracle`: Address of the recording oracle.
|
|
320
|
+
* - `reputationOracle`: Address of the reputation oracle.
|
|
321
|
+
* - `exchangeOracle`: Address of the exchange oracle.
|
|
322
|
+
* - `recordingOracleFee`: Fee (in basis points or percentage * 100) for the recording oracle.
|
|
323
|
+
* - `reputationOracleFee`: Fee for the reputation oracle.
|
|
324
|
+
* - `exchangeOracleFee`: Fee for the exchange oracle.
|
|
325
|
+
* - `manifest`: URL to the manifest file.
|
|
326
|
+
* - `manifestHash`: Hash of the manifest content.
|
|
327
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
328
|
+
*
|
|
329
|
+
* @returns {Promise<string>} Returns the address of the escrow created.
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* import { Wallet, ethers } from 'ethers';
|
|
333
|
+
* import { EscrowClient, IERC20__factory } from '@human-protocol/sdk';
|
|
334
|
+
*
|
|
335
|
+
* const rpcUrl = 'YOUR_RPC_URL';
|
|
336
|
+
* const privateKey = 'YOUR_PRIVATE_KEY';
|
|
337
|
+
* const provider = new ethers.JsonRpcProvider(rpcUrl);
|
|
338
|
+
* const signer = new Wallet(privateKey, provider);
|
|
339
|
+
*
|
|
340
|
+
* const escrowClient = await EscrowClient.build(signer);
|
|
341
|
+
*
|
|
342
|
+
* const tokenAddress = '0xTokenAddress';
|
|
343
|
+
* const amount = ethers.parseUnits('1000', 18);
|
|
344
|
+
* const jobRequesterId = 'requester-123';
|
|
345
|
+
*
|
|
346
|
+
* const token = IERC20__factory.connect(tokenAddress, signer);
|
|
347
|
+
* await token.approve(escrowClient.escrowFactoryContract.target, amount);
|
|
348
|
+
*
|
|
349
|
+
* const escrowConfig = {
|
|
350
|
+
* recordingOracle: '0xRecordingOracle',
|
|
351
|
+
* reputationOracle: '0xReputationOracle',
|
|
352
|
+
* exchangeOracle: '0xExchangeOracle',
|
|
353
|
+
* recordingOracleFee: 5n,
|
|
354
|
+
* reputationOracleFee: 5n,
|
|
355
|
+
* exchangeOracleFee: 5n,
|
|
356
|
+
* manifest: 'https://example.com/manifest.json',
|
|
357
|
+
* manifestHash: 'manifestHash-123',
|
|
358
|
+
* } satisfies IEscrowConfig;
|
|
359
|
+
*
|
|
360
|
+
* const escrowAddress = await escrowClient.createFundAndSetupEscrow(
|
|
361
|
+
* tokenAddress,
|
|
362
|
+
* amount,
|
|
363
|
+
* jobRequesterId,
|
|
364
|
+
* escrowConfig
|
|
365
|
+
* );
|
|
366
|
+
*
|
|
367
|
+
* console.log('Escrow created at:', escrowAddress);
|
|
368
|
+
*/
|
|
369
|
+
@requiresSigner
|
|
370
|
+
public async createFundAndSetupEscrow(
|
|
371
|
+
tokenAddress: string,
|
|
372
|
+
amount: bigint,
|
|
373
|
+
jobRequesterId: string,
|
|
374
|
+
escrowConfig: IEscrowConfig,
|
|
375
|
+
txOptions: Overrides = {}
|
|
376
|
+
): Promise<string> {
|
|
377
|
+
if (!ethers.isAddress(tokenAddress)) {
|
|
378
|
+
throw ErrorInvalidTokenAddress;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
this.verifySetupParameters(escrowConfig);
|
|
382
|
+
|
|
383
|
+
const {
|
|
384
|
+
recordingOracle,
|
|
385
|
+
reputationOracle,
|
|
386
|
+
exchangeOracle,
|
|
387
|
+
recordingOracleFee,
|
|
388
|
+
reputationOracleFee,
|
|
389
|
+
exchangeOracleFee,
|
|
390
|
+
manifest,
|
|
391
|
+
manifestHash,
|
|
392
|
+
} = escrowConfig;
|
|
393
|
+
|
|
394
|
+
try {
|
|
395
|
+
const result = await (
|
|
396
|
+
await this.escrowFactoryContract.createFundAndSetupEscrow(
|
|
397
|
+
tokenAddress,
|
|
398
|
+
amount,
|
|
399
|
+
jobRequesterId,
|
|
400
|
+
reputationOracle,
|
|
401
|
+
recordingOracle,
|
|
402
|
+
exchangeOracle,
|
|
403
|
+
reputationOracleFee,
|
|
404
|
+
recordingOracleFee,
|
|
405
|
+
exchangeOracleFee,
|
|
406
|
+
manifest,
|
|
407
|
+
manifestHash,
|
|
408
|
+
this.applyTxDefaults(txOptions)
|
|
409
|
+
)
|
|
410
|
+
).wait();
|
|
411
|
+
|
|
412
|
+
const event = (
|
|
413
|
+
result?.logs?.find(({ topics }) =>
|
|
414
|
+
topics.includes(ethers.id('LaunchedV2(address,address,string)'))
|
|
415
|
+
) as EventLog
|
|
416
|
+
)?.args;
|
|
417
|
+
|
|
418
|
+
if (!event) {
|
|
419
|
+
throw ErrorLaunchedEventIsNotEmitted;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return event.escrow;
|
|
423
|
+
} catch (e: any) {
|
|
424
|
+
return throwError(e);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
267
427
|
|
|
268
428
|
/**
|
|
269
429
|
* This function sets up the parameters of the escrow.
|
|
@@ -320,43 +480,12 @@ export class EscrowClient extends BaseEthersClient {
|
|
|
320
480
|
manifestHash,
|
|
321
481
|
} = escrowConfig;
|
|
322
482
|
|
|
323
|
-
|
|
324
|
-
throw ErrorInvalidRecordingOracleAddressProvided;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
if (!ethers.isAddress(reputationOracle)) {
|
|
328
|
-
throw ErrorInvalidReputationOracleAddressProvided;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
if (!ethers.isAddress(exchangeOracle)) {
|
|
332
|
-
throw ErrorInvalidExchangeOracleAddressProvided;
|
|
333
|
-
}
|
|
483
|
+
this.verifySetupParameters(escrowConfig);
|
|
334
484
|
|
|
335
485
|
if (!ethers.isAddress(escrowAddress)) {
|
|
336
486
|
throw ErrorInvalidEscrowAddressProvided;
|
|
337
487
|
}
|
|
338
488
|
|
|
339
|
-
if (
|
|
340
|
-
recordingOracleFee <= 0 ||
|
|
341
|
-
reputationOracleFee <= 0 ||
|
|
342
|
-
exchangeOracleFee <= 0
|
|
343
|
-
) {
|
|
344
|
-
throw ErrorAmountMustBeGreaterThanZero;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
if (recordingOracleFee + reputationOracleFee + exchangeOracleFee > 100) {
|
|
348
|
-
throw ErrorTotalFeeMustBeLessThanHundred;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
const isManifestValid = isValidUrl(manifest) || isValidJson(manifest);
|
|
352
|
-
if (!isManifestValid) {
|
|
353
|
-
throw ErrorInvalidManifest;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
if (!manifestHash) {
|
|
357
|
-
throw ErrorHashIsEmptyString;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
489
|
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
361
490
|
throw ErrorEscrowAddressIsNotProvidedByFactory;
|
|
362
491
|
}
|
|
@@ -535,7 +664,7 @@ export class EscrowClient extends BaseEthersClient {
|
|
|
535
664
|
const escrowContract = this.getEscrowContract(escrowAddress);
|
|
536
665
|
|
|
537
666
|
const hasFundsToReserveParam = typeof a === 'bigint';
|
|
538
|
-
const fundsToReserve = hasFundsToReserveParam ? (a as bigint) :
|
|
667
|
+
const fundsToReserve = hasFundsToReserveParam ? (a as bigint) : null;
|
|
539
668
|
const txOptions = (hasFundsToReserveParam ? b : a) || {};
|
|
540
669
|
// When fundsToReserve is provided and is 0, allow empty URL.
|
|
541
670
|
// In this situation not solutions might have been provided so the escrow can be straight cancelled.
|
|
@@ -557,7 +686,7 @@ export class EscrowClient extends BaseEthersClient {
|
|
|
557
686
|
}
|
|
558
687
|
|
|
559
688
|
try {
|
|
560
|
-
if (fundsToReserve !==
|
|
689
|
+
if (fundsToReserve !== null) {
|
|
561
690
|
await (
|
|
562
691
|
await escrowContract['storeResults(string,string,uint256)'](
|
|
563
692
|
url,
|
|
@@ -901,7 +1030,7 @@ export class EscrowClient extends BaseEthersClient {
|
|
|
901
1030
|
* @param {string} escrowAddress Address of the escrow to withdraw.
|
|
902
1031
|
* @param {string} tokenAddress Address of the token to withdraw.
|
|
903
1032
|
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
904
|
-
* @returns {
|
|
1033
|
+
* @returns {IEscrowWithdraw} Returns the escrow withdrawal data including transaction hash and withdrawal amount. Throws error if any.
|
|
905
1034
|
*
|
|
906
1035
|
*
|
|
907
1036
|
* **Code example**
|
|
@@ -930,7 +1059,7 @@ export class EscrowClient extends BaseEthersClient {
|
|
|
930
1059
|
escrowAddress: string,
|
|
931
1060
|
tokenAddress: string,
|
|
932
1061
|
txOptions: Overrides = {}
|
|
933
|
-
): Promise<
|
|
1062
|
+
): Promise<IEscrowWithdraw> {
|
|
934
1063
|
if (!ethers.isAddress(escrowAddress)) {
|
|
935
1064
|
throw ErrorInvalidEscrowAddressProvided;
|
|
936
1065
|
}
|
|
@@ -969,7 +1098,7 @@ export class EscrowClient extends BaseEthersClient {
|
|
|
969
1098
|
|
|
970
1099
|
const from = parsedLog?.args[0];
|
|
971
1100
|
if (parsedLog?.name === 'Transfer' && from === escrowAddress) {
|
|
972
|
-
amountTransferred = parsedLog?.args[2];
|
|
1101
|
+
amountTransferred = BigInt(parsedLog?.args[2]);
|
|
973
1102
|
break;
|
|
974
1103
|
}
|
|
975
1104
|
}
|
|
@@ -979,13 +1108,11 @@ export class EscrowClient extends BaseEthersClient {
|
|
|
979
1108
|
throw ErrorTransferEventNotFoundInTransactionLogs;
|
|
980
1109
|
}
|
|
981
1110
|
|
|
982
|
-
|
|
1111
|
+
return {
|
|
983
1112
|
txHash: transactionReceipt?.hash || '',
|
|
984
1113
|
tokenAddress,
|
|
985
1114
|
withdrawnAmount: amountTransferred,
|
|
986
1115
|
};
|
|
987
|
-
|
|
988
|
-
return escrowWithdrawData;
|
|
989
1116
|
} catch (e) {
|
|
990
1117
|
return throwError(e);
|
|
991
1118
|
}
|
|
@@ -1785,23 +1912,29 @@ export class EscrowUtils {
|
|
|
1785
1912
|
* interface IEscrow {
|
|
1786
1913
|
* id: string;
|
|
1787
1914
|
* address: string;
|
|
1788
|
-
* amountPaid:
|
|
1789
|
-
* balance:
|
|
1790
|
-
* count:
|
|
1791
|
-
* jobRequesterId: string;
|
|
1915
|
+
* amountPaid: bigint;
|
|
1916
|
+
* balance: bigint;
|
|
1917
|
+
* count: bigint;
|
|
1792
1918
|
* factoryAddress: string;
|
|
1793
|
-
* finalResultsUrl
|
|
1794
|
-
*
|
|
1919
|
+
* finalResultsUrl: string | null;
|
|
1920
|
+
* finalResultsHash: string | null;
|
|
1921
|
+
* intermediateResultsUrl: string | null;
|
|
1922
|
+
* intermediateResultsHash: string | null;
|
|
1795
1923
|
* launcher: string;
|
|
1796
|
-
*
|
|
1797
|
-
*
|
|
1798
|
-
*
|
|
1799
|
-
*
|
|
1800
|
-
*
|
|
1801
|
-
*
|
|
1924
|
+
* jobRequesterId: string | null;
|
|
1925
|
+
* manifestHash: string | null;
|
|
1926
|
+
* manifest: string | null;
|
|
1927
|
+
* recordingOracle: string | null;
|
|
1928
|
+
* reputationOracle: string | null;
|
|
1929
|
+
* exchangeOracle: string | null;
|
|
1930
|
+
* recordingOracleFee: number | null;
|
|
1931
|
+
* reputationOracleFee: number | null;
|
|
1932
|
+
* exchangeOracleFee: number | null;
|
|
1933
|
+
* status: string;
|
|
1802
1934
|
* token: string;
|
|
1803
|
-
* totalFundedAmount:
|
|
1804
|
-
* createdAt:
|
|
1935
|
+
* totalFundedAmount: bigint;
|
|
1936
|
+
* createdAt: number;
|
|
1937
|
+
* chainId: number;
|
|
1805
1938
|
* };
|
|
1806
1939
|
* ```
|
|
1807
1940
|
*
|
|
@@ -1873,13 +2006,7 @@ export class EscrowUtils {
|
|
|
1873
2006
|
skip: skip,
|
|
1874
2007
|
}
|
|
1875
2008
|
);
|
|
1876
|
-
escrows.map((
|
|
1877
|
-
|
|
1878
|
-
if (!escrows) {
|
|
1879
|
-
return [];
|
|
1880
|
-
}
|
|
1881
|
-
|
|
1882
|
-
return escrows;
|
|
2009
|
+
return (escrows || []).map((e) => mapEscrow(e, networkData.chainId));
|
|
1883
2010
|
}
|
|
1884
2011
|
|
|
1885
2012
|
/**
|
|
@@ -1906,23 +2033,29 @@ export class EscrowUtils {
|
|
|
1906
2033
|
* interface IEscrow {
|
|
1907
2034
|
* id: string;
|
|
1908
2035
|
* address: string;
|
|
1909
|
-
* amountPaid:
|
|
1910
|
-
* balance:
|
|
1911
|
-
* count:
|
|
1912
|
-
* jobRequesterId: string;
|
|
2036
|
+
* amountPaid: bigint;
|
|
2037
|
+
* balance: bigint;
|
|
2038
|
+
* count: bigint;
|
|
1913
2039
|
* factoryAddress: string;
|
|
1914
|
-
* finalResultsUrl
|
|
1915
|
-
*
|
|
2040
|
+
* finalResultsUrl: string | null;
|
|
2041
|
+
* finalResultsHash: string | null;
|
|
2042
|
+
* intermediateResultsUrl: string | null;
|
|
2043
|
+
* intermediateResultsHash: string | null;
|
|
1916
2044
|
* launcher: string;
|
|
1917
|
-
*
|
|
1918
|
-
*
|
|
1919
|
-
*
|
|
1920
|
-
*
|
|
1921
|
-
*
|
|
1922
|
-
*
|
|
2045
|
+
* jobRequesterId: string | null;
|
|
2046
|
+
* manifestHash: string | null;
|
|
2047
|
+
* manifest: string | null;
|
|
2048
|
+
* recordingOracle: string | null;
|
|
2049
|
+
* reputationOracle: string | null;
|
|
2050
|
+
* exchangeOracle: string | null;
|
|
2051
|
+
* recordingOracleFee: number | null;
|
|
2052
|
+
* reputationOracleFee: number | null;
|
|
2053
|
+
* exchangeOracleFee: number | null;
|
|
2054
|
+
* status: string;
|
|
1923
2055
|
* token: string;
|
|
1924
|
-
* totalFundedAmount:
|
|
1925
|
-
* createdAt:
|
|
2056
|
+
* totalFundedAmount: bigint;
|
|
2057
|
+
* createdAt: number;
|
|
2058
|
+
* chainId: number;
|
|
1926
2059
|
* };
|
|
1927
2060
|
* ```
|
|
1928
2061
|
*
|
|
@@ -1953,13 +2086,14 @@ export class EscrowUtils {
|
|
|
1953
2086
|
throw ErrorInvalidAddress;
|
|
1954
2087
|
}
|
|
1955
2088
|
|
|
1956
|
-
const { escrow } = await gqlFetch<{ escrow: EscrowData }>(
|
|
2089
|
+
const { escrow } = await gqlFetch<{ escrow: EscrowData | null }>(
|
|
1957
2090
|
getSubgraphUrl(networkData),
|
|
1958
2091
|
GET_ESCROW_BY_ADDRESS_QUERY(),
|
|
1959
2092
|
{ escrowAddress: escrowAddress.toLowerCase() }
|
|
1960
2093
|
);
|
|
2094
|
+
if (!escrow) return null;
|
|
1961
2095
|
|
|
1962
|
-
return escrow
|
|
2096
|
+
return mapEscrow(escrow, networkData.chainId);
|
|
1963
2097
|
}
|
|
1964
2098
|
|
|
1965
2099
|
/**
|
|
@@ -2020,7 +2154,7 @@ export class EscrowUtils {
|
|
|
2020
2154
|
*/
|
|
2021
2155
|
public static async getStatusEvents(
|
|
2022
2156
|
filter: IStatusEventFilter
|
|
2023
|
-
): Promise<
|
|
2157
|
+
): Promise<IStatusEvent[]> {
|
|
2024
2158
|
const {
|
|
2025
2159
|
chainId,
|
|
2026
2160
|
statuses,
|
|
@@ -2073,14 +2207,12 @@ export class EscrowUtils {
|
|
|
2073
2207
|
return [];
|
|
2074
2208
|
}
|
|
2075
2209
|
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2210
|
+
return data['escrowStatusEvents'].map((event) => ({
|
|
2211
|
+
timestamp: Number(event.timestamp) * 1000,
|
|
2212
|
+
escrowAddress: event.escrowAddress,
|
|
2213
|
+
status: EscrowStatus[event.status as keyof typeof EscrowStatus],
|
|
2080
2214
|
chainId,
|
|
2081
2215
|
}));
|
|
2082
|
-
|
|
2083
|
-
return eventsWithChainId;
|
|
2084
2216
|
}
|
|
2085
2217
|
|
|
2086
2218
|
/**
|
|
@@ -2092,7 +2224,7 @@ export class EscrowUtils {
|
|
|
2092
2224
|
* Fetch payouts from the subgraph.
|
|
2093
2225
|
*
|
|
2094
2226
|
* @param {IPayoutFilter} filter Filter parameters.
|
|
2095
|
-
* @returns {Promise<
|
|
2227
|
+
* @returns {Promise<IPayout[]>} List of payouts matching the filters.
|
|
2096
2228
|
*
|
|
2097
2229
|
* **Code example**
|
|
2098
2230
|
*
|
|
@@ -2109,7 +2241,7 @@ export class EscrowUtils {
|
|
|
2109
2241
|
* console.log(payouts);
|
|
2110
2242
|
* ```
|
|
2111
2243
|
*/
|
|
2112
|
-
public static async getPayouts(filter: IPayoutFilter): Promise<
|
|
2244
|
+
public static async getPayouts(filter: IPayoutFilter): Promise<IPayout[]> {
|
|
2113
2245
|
const networkData = NETWORKS[filter.chainId];
|
|
2114
2246
|
if (!networkData) {
|
|
2115
2247
|
throw ErrorUnsupportedChainID;
|
|
@@ -2126,7 +2258,7 @@ export class EscrowUtils {
|
|
|
2126
2258
|
const skip = filter.skip || 0;
|
|
2127
2259
|
const orderDirection = filter.orderDirection || OrderDirection.DESC;
|
|
2128
2260
|
|
|
2129
|
-
const { payouts } = await gqlFetch<{ payouts:
|
|
2261
|
+
const { payouts } = await gqlFetch<{ payouts: PayoutData[] }>(
|
|
2130
2262
|
getSubgraphUrl(networkData),
|
|
2131
2263
|
GET_PAYOUTS_QUERY(filter),
|
|
2132
2264
|
{
|
|
@@ -2139,8 +2271,17 @@ export class EscrowUtils {
|
|
|
2139
2271
|
orderDirection,
|
|
2140
2272
|
}
|
|
2141
2273
|
);
|
|
2274
|
+
if (!payouts) {
|
|
2275
|
+
return [];
|
|
2276
|
+
}
|
|
2142
2277
|
|
|
2143
|
-
return payouts
|
|
2278
|
+
return payouts.map((payout) => ({
|
|
2279
|
+
id: payout.id,
|
|
2280
|
+
escrowAddress: payout.escrowAddress,
|
|
2281
|
+
recipient: payout.recipient,
|
|
2282
|
+
amount: BigInt(payout.amount),
|
|
2283
|
+
createdAt: Number(payout.createdAt) * 1000,
|
|
2284
|
+
}));
|
|
2144
2285
|
}
|
|
2145
2286
|
|
|
2146
2287
|
/**
|
|
@@ -2164,7 +2305,7 @@ export class EscrowUtils {
|
|
|
2164
2305
|
* ```
|
|
2165
2306
|
*
|
|
2166
2307
|
* ```ts
|
|
2167
|
-
*
|
|
2308
|
+
* interface ICancellationRefund {
|
|
2168
2309
|
* id: string;
|
|
2169
2310
|
* escrowAddress: string;
|
|
2170
2311
|
* receiver: string;
|
|
@@ -2177,7 +2318,7 @@ export class EscrowUtils {
|
|
|
2177
2318
|
*
|
|
2178
2319
|
*
|
|
2179
2320
|
* @param {Object} filter Filter parameters.
|
|
2180
|
-
* @returns {Promise<
|
|
2321
|
+
* @returns {Promise<ICancellationRefund[]>} List of cancellation refunds matching the filters.
|
|
2181
2322
|
*
|
|
2182
2323
|
* **Code example**
|
|
2183
2324
|
*
|
|
@@ -2200,7 +2341,7 @@ export class EscrowUtils {
|
|
|
2200
2341
|
first?: number;
|
|
2201
2342
|
skip?: number;
|
|
2202
2343
|
orderDirection?: OrderDirection;
|
|
2203
|
-
}): Promise<
|
|
2344
|
+
}): Promise<ICancellationRefund[]> {
|
|
2204
2345
|
const networkData = NETWORKS[filter.chainId];
|
|
2205
2346
|
if (!networkData) throw ErrorUnsupportedChainID;
|
|
2206
2347
|
if (filter.escrowAddress && !ethers.isAddress(filter.escrowAddress)) {
|
|
@@ -2216,7 +2357,7 @@ export class EscrowUtils {
|
|
|
2216
2357
|
const orderDirection = filter.orderDirection || OrderDirection.DESC;
|
|
2217
2358
|
|
|
2218
2359
|
const { cancellationRefundEvents } = await gqlFetch<{
|
|
2219
|
-
cancellationRefundEvents:
|
|
2360
|
+
cancellationRefundEvents: CancellationRefundData[];
|
|
2220
2361
|
}>(getSubgraphUrl(networkData), GET_CANCELLATION_REFUNDS_QUERY(filter), {
|
|
2221
2362
|
escrowAddress: filter.escrowAddress?.toLowerCase(),
|
|
2222
2363
|
receiver: filter.receiver?.toLowerCase(),
|
|
@@ -2227,7 +2368,19 @@ export class EscrowUtils {
|
|
|
2227
2368
|
orderDirection,
|
|
2228
2369
|
});
|
|
2229
2370
|
|
|
2230
|
-
|
|
2371
|
+
if (!cancellationRefundEvents || cancellationRefundEvents.length === 0) {
|
|
2372
|
+
return [];
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
return cancellationRefundEvents.map((event) => ({
|
|
2376
|
+
id: event.id,
|
|
2377
|
+
escrowAddress: event.escrowAddress,
|
|
2378
|
+
receiver: event.receiver,
|
|
2379
|
+
amount: BigInt(event.amount),
|
|
2380
|
+
block: Number(event.block),
|
|
2381
|
+
timestamp: Number(event.timestamp) * 1000,
|
|
2382
|
+
txHash: event.txHash,
|
|
2383
|
+
}));
|
|
2231
2384
|
}
|
|
2232
2385
|
|
|
2233
2386
|
/**
|
|
@@ -2251,7 +2404,7 @@ export class EscrowUtils {
|
|
|
2251
2404
|
* ```
|
|
2252
2405
|
*
|
|
2253
2406
|
* ```ts
|
|
2254
|
-
*
|
|
2407
|
+
* interface ICancellationRefund {
|
|
2255
2408
|
* id: string;
|
|
2256
2409
|
* escrowAddress: string;
|
|
2257
2410
|
* receiver: string;
|
|
@@ -2265,7 +2418,7 @@ export class EscrowUtils {
|
|
|
2265
2418
|
*
|
|
2266
2419
|
* @param {ChainId} chainId Network in which the escrow has been deployed
|
|
2267
2420
|
* @param {string} escrowAddress Address of the escrow
|
|
2268
|
-
* @returns {Promise<
|
|
2421
|
+
* @returns {Promise<ICancellationRefund>} Cancellation refund data
|
|
2269
2422
|
*
|
|
2270
2423
|
* **Code example**
|
|
2271
2424
|
*
|
|
@@ -2278,7 +2431,7 @@ export class EscrowUtils {
|
|
|
2278
2431
|
public static async getCancellationRefund(
|
|
2279
2432
|
chainId: ChainId,
|
|
2280
2433
|
escrowAddress: string
|
|
2281
|
-
): Promise<
|
|
2434
|
+
): Promise<ICancellationRefund | null> {
|
|
2282
2435
|
const networkData = NETWORKS[chainId];
|
|
2283
2436
|
if (!networkData) throw ErrorUnsupportedChainID;
|
|
2284
2437
|
|
|
@@ -2287,13 +2440,59 @@ export class EscrowUtils {
|
|
|
2287
2440
|
}
|
|
2288
2441
|
|
|
2289
2442
|
const { cancellationRefundEvents } = await gqlFetch<{
|
|
2290
|
-
cancellationRefundEvents:
|
|
2443
|
+
cancellationRefundEvents: CancellationRefundData[];
|
|
2291
2444
|
}>(
|
|
2292
2445
|
getSubgraphUrl(networkData),
|
|
2293
2446
|
GET_CANCELLATION_REFUND_BY_ADDRESS_QUERY(),
|
|
2294
2447
|
{ escrowAddress: escrowAddress.toLowerCase() }
|
|
2295
2448
|
);
|
|
2296
2449
|
|
|
2297
|
-
|
|
2450
|
+
if (!cancellationRefundEvents || cancellationRefundEvents.length === 0) {
|
|
2451
|
+
return null;
|
|
2452
|
+
}
|
|
2453
|
+
|
|
2454
|
+
return {
|
|
2455
|
+
id: cancellationRefundEvents[0].id,
|
|
2456
|
+
escrowAddress: cancellationRefundEvents[0].escrowAddress,
|
|
2457
|
+
receiver: cancellationRefundEvents[0].receiver,
|
|
2458
|
+
amount: BigInt(cancellationRefundEvents[0].amount),
|
|
2459
|
+
block: Number(cancellationRefundEvents[0].block),
|
|
2460
|
+
timestamp: Number(cancellationRefundEvents[0].timestamp) * 1000,
|
|
2461
|
+
txHash: cancellationRefundEvents[0].txHash,
|
|
2462
|
+
};
|
|
2298
2463
|
}
|
|
2299
2464
|
}
|
|
2465
|
+
|
|
2466
|
+
function mapEscrow(e: EscrowData, chainId: ChainId | number): IEscrow {
|
|
2467
|
+
return {
|
|
2468
|
+
id: e.id,
|
|
2469
|
+
address: e.address,
|
|
2470
|
+
amountPaid: BigInt(e.amountPaid),
|
|
2471
|
+
balance: BigInt(e.balance),
|
|
2472
|
+
count: Number(e.count),
|
|
2473
|
+
factoryAddress: e.factoryAddress,
|
|
2474
|
+
finalResultsUrl: e.finalResultsUrl,
|
|
2475
|
+
finalResultsHash: e.finalResultsHash,
|
|
2476
|
+
intermediateResultsUrl: e.intermediateResultsUrl,
|
|
2477
|
+
intermediateResultsHash: e.intermediateResultsHash,
|
|
2478
|
+
launcher: e.launcher,
|
|
2479
|
+
jobRequesterId: e.jobRequesterId,
|
|
2480
|
+
manifestHash: e.manifestHash,
|
|
2481
|
+
manifest: e.manifest,
|
|
2482
|
+
recordingOracle: e.recordingOracle,
|
|
2483
|
+
reputationOracle: e.reputationOracle,
|
|
2484
|
+
exchangeOracle: e.exchangeOracle,
|
|
2485
|
+
recordingOracleFee: e.recordingOracleFee
|
|
2486
|
+
? Number(e.recordingOracleFee)
|
|
2487
|
+
: null,
|
|
2488
|
+
reputationOracleFee: e.reputationOracleFee
|
|
2489
|
+
? Number(e.reputationOracleFee)
|
|
2490
|
+
: null,
|
|
2491
|
+
exchangeOracleFee: e.exchangeOracleFee ? Number(e.exchangeOracleFee) : null,
|
|
2492
|
+
status: e.status,
|
|
2493
|
+
token: e.token,
|
|
2494
|
+
totalFundedAmount: BigInt(e.totalFundedAmount),
|
|
2495
|
+
createdAt: Number(e.createdAt) * 1000,
|
|
2496
|
+
chainId: Number(chainId),
|
|
2497
|
+
};
|
|
2498
|
+
}
|