@human-protocol/sdk 6.1.0 → 7.1.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/CHANGELOG.md +32 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +26 -14
- package/dist/encryption/encryption.d.ts +84 -0
- package/dist/encryption/encryption.d.ts.map +1 -0
- package/dist/{encryption.js → encryption/encryption.js} +3 -161
- package/dist/encryption/encryption_utils.d.ts +101 -0
- package/dist/encryption/encryption_utils.d.ts.map +1 -0
- package/dist/encryption/encryption_utils.js +191 -0
- package/dist/encryption/index.d.ts +4 -0
- package/dist/encryption/index.d.ts.map +1 -0
- package/dist/encryption/index.js +7 -0
- package/dist/encryption/types.d.ts +9 -0
- package/dist/encryption/types.d.ts.map +1 -0
- package/dist/encryption/types.js +9 -0
- package/dist/error.d.ts +6 -10
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +12 -14
- package/dist/{escrow.d.ts → escrow/escrow_client.d.ts} +4 -184
- package/dist/{escrow.d.ts.map → escrow/escrow_client.d.ts.map} +1 -1
- package/dist/{escrow.js → escrow/escrow_client.js} +35 -411
- package/dist/escrow/escrow_utils.d.ts +172 -0
- package/dist/escrow/escrow_utils.d.ts.map +1 -0
- package/dist/escrow/escrow_utils.js +394 -0
- package/dist/escrow/index.d.ts +3 -0
- package/dist/escrow/index.d.ts.map +1 -0
- package/dist/escrow/index.js +7 -0
- package/dist/graphql/queries/escrow.d.ts +1 -1
- package/dist/graphql/queries/escrow.d.ts.map +1 -1
- package/dist/graphql/queries/escrow.js +5 -1
- package/dist/graphql/queries/statistics.d.ts +1 -0
- package/dist/graphql/queries/statistics.d.ts.map +1 -1
- package/dist/graphql/queries/statistics.js +36 -2
- package/dist/graphql/queries/worker.d.ts.map +1 -1
- package/dist/graphql/queries/worker.js +0 -2
- package/dist/graphql/types.d.ts +5 -2
- package/dist/graphql/types.d.ts.map +1 -1
- package/dist/index.d.ts +10 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -22
- package/dist/interfaces.d.ts +4 -7
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/kvstore/index.d.ts +3 -0
- package/dist/kvstore/index.d.ts.map +1 -0
- package/dist/kvstore/index.js +7 -0
- package/dist/{kvstore.d.ts → kvstore/kvstore_client.d.ts} +3 -108
- package/dist/kvstore/kvstore_client.d.ts.map +1 -0
- package/dist/{kvstore.js → kvstore/kvstore_client.js} +22 -184
- package/dist/kvstore/kvstore_utils.d.ts +105 -0
- package/dist/kvstore/kvstore_utils.d.ts.map +1 -0
- package/dist/kvstore/kvstore_utils.js +184 -0
- package/dist/operator/index.d.ts +2 -0
- package/dist/operator/index.d.ts.map +1 -0
- package/dist/operator/index.js +5 -0
- package/dist/{operator.d.ts → operator/operator_utils.d.ts} +3 -3
- package/dist/operator/operator_utils.d.ts.map +1 -0
- package/dist/{operator.js → operator/operator_utils.js} +6 -6
- package/dist/staking/index.d.ts +3 -0
- package/dist/staking/index.d.ts.map +1 -0
- package/dist/staking/index.js +7 -0
- package/dist/{staking.d.ts → staking/staking_client.d.ts} +4 -65
- package/dist/staking/staking_client.d.ts.map +1 -0
- package/dist/{staking.js → staking/staking_client.js} +6 -136
- package/dist/staking/staking_utils.d.ts +63 -0
- package/dist/staking/staking_utils.d.ts.map +1 -0
- package/dist/staking/staking_utils.js +137 -0
- package/dist/statistics/index.d.ts +2 -0
- package/dist/statistics/index.d.ts.map +1 -0
- package/dist/statistics/index.js +5 -0
- package/dist/{statistics.d.ts → statistics/statistics_utils.d.ts} +4 -13
- package/dist/statistics/statistics_utils.d.ts.map +1 -0
- package/dist/{statistics.js → statistics/statistics_utils.js} +8 -22
- package/dist/transaction/index.d.ts +2 -0
- package/dist/transaction/index.d.ts.map +1 -0
- package/dist/transaction/index.js +5 -0
- package/dist/{transaction.d.ts → transaction/transaction_utils.d.ts} +3 -3
- package/dist/transaction/transaction_utils.d.ts.map +1 -0
- package/dist/{transaction.js → transaction/transaction_utils.js} +5 -5
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +15 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +71 -4
- package/dist/worker/index.d.ts +2 -0
- package/dist/worker/index.d.ts.map +1 -0
- package/dist/worker/index.js +5 -0
- package/dist/{worker.d.ts → worker/worker_utils.d.ts} +3 -4
- package/dist/worker/worker_utils.d.ts.map +1 -0
- package/dist/{worker.js → worker/worker_utils.js} +5 -7
- package/package.json +12 -8
- package/src/constants.ts +38 -14
- package/src/{encryption.ts → encryption/encryption.ts} +1 -193
- package/src/encryption/encryption_utils.ts +179 -0
- package/src/encryption/index.ts +3 -0
- package/src/encryption/types.ts +15 -0
- package/src/error.ts +11 -17
- package/src/{escrow.ts → escrow/escrow_client.ts} +36 -556
- package/src/escrow/escrow_utils.ts +518 -0
- package/src/escrow/index.ts +2 -0
- package/src/graphql/queries/escrow.ts +6 -1
- package/src/graphql/queries/statistics.ts +37 -1
- package/src/graphql/queries/worker.ts +0 -2
- package/src/graphql/types.ts +6 -2
- package/src/index.ts +11 -25
- package/src/interfaces.ts +4 -7
- package/src/kvstore/index.ts +2 -0
- package/src/{kvstore.ts → kvstore/kvstore_client.ts} +27 -241
- package/src/kvstore/kvstore_utils.ts +244 -0
- package/src/operator/index.ts +1 -0
- package/src/{operator.ts → operator/operator_utils.ts} +8 -8
- package/src/staking/index.ts +2 -0
- package/src/{staking.ts → staking/staking_client.ts} +8 -174
- package/src/staking/staking_utils.ts +170 -0
- package/src/statistics/index.ts +1 -0
- package/src/{statistics.ts → statistics/statistics_utils.ts} +14 -26
- package/src/transaction/index.ts +1 -0
- package/src/{transaction.ts → transaction/transaction_utils.ts} +7 -7
- package/src/types.ts +8 -0
- package/src/utils.ts +80 -3
- package/src/worker/index.ts +1 -0
- package/src/{worker.ts → worker/worker_utils.ts} +7 -9
- package/dist/encryption.d.ts +0 -189
- package/dist/encryption.d.ts.map +0 -1
- package/dist/kvstore.d.ts.map +0 -1
- package/dist/operator.d.ts.map +0 -1
- package/dist/staking.d.ts.map +0 -1
- package/dist/statistics.d.ts.map +0 -1
- package/dist/transaction.d.ts.map +0 -1
- package/dist/worker.d.ts.map +0 -1
|
@@ -9,18 +9,15 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.EscrowClient = void 0;
|
|
13
13
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
14
14
|
const typechain_types_1 = require("@human-protocol/core/typechain-types");
|
|
15
15
|
const ethers_1 = require("ethers");
|
|
16
|
-
const base_1 = require("
|
|
17
|
-
const constants_1 = require("
|
|
18
|
-
const decorators_1 = require("
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const graphql_1 = require("./graphql");
|
|
22
|
-
const types_1 = require("./types");
|
|
23
|
-
const utils_1 = require("./utils");
|
|
16
|
+
const base_1 = require("../base");
|
|
17
|
+
const constants_1 = require("../constants");
|
|
18
|
+
const decorators_1 = require("../decorators");
|
|
19
|
+
const error_1 = require("../error");
|
|
20
|
+
const utils_1 = require("../utils");
|
|
24
21
|
/**
|
|
25
22
|
* Client to perform actions on Escrow contracts and obtain information from the contracts.
|
|
26
23
|
*
|
|
@@ -151,7 +148,7 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
151
148
|
}
|
|
152
149
|
}
|
|
153
150
|
verifySetupParameters(escrowConfig) {
|
|
154
|
-
const { recordingOracle, reputationOracle, exchangeOracle,
|
|
151
|
+
const { recordingOracle, reputationOracle, exchangeOracle, manifest, manifestHash, } = escrowConfig;
|
|
155
152
|
if (!ethers_1.ethers.isAddress(recordingOracle)) {
|
|
156
153
|
throw error_1.ErrorInvalidRecordingOracleAddressProvided;
|
|
157
154
|
}
|
|
@@ -161,14 +158,6 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
161
158
|
if (!ethers_1.ethers.isAddress(exchangeOracle)) {
|
|
162
159
|
throw error_1.ErrorInvalidExchangeOracleAddressProvided;
|
|
163
160
|
}
|
|
164
|
-
if (recordingOracleFee <= 0 ||
|
|
165
|
-
reputationOracleFee <= 0 ||
|
|
166
|
-
exchangeOracleFee <= 0) {
|
|
167
|
-
throw error_1.ErrorAmountMustBeGreaterThanZero;
|
|
168
|
-
}
|
|
169
|
-
if (recordingOracleFee + reputationOracleFee + exchangeOracleFee > 100) {
|
|
170
|
-
throw error_1.ErrorTotalFeeMustBeLessThanHundred;
|
|
171
|
-
}
|
|
172
161
|
const isManifestValid = (0, utils_1.isValidUrl)(manifest) || (0, utils_1.isValidJson)(manifest);
|
|
173
162
|
if (!isManifestValid) {
|
|
174
163
|
throw error_1.ErrorInvalidManifest;
|
|
@@ -191,8 +180,6 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
191
180
|
* @throws ErrorInvalidRecordingOracleAddressProvided If the recording oracle address is invalid
|
|
192
181
|
* @throws ErrorInvalidReputationOracleAddressProvided If the reputation oracle address is invalid
|
|
193
182
|
* @throws ErrorInvalidExchangeOracleAddressProvided If the exchange oracle address is invalid
|
|
194
|
-
* @throws ErrorAmountMustBeGreaterThanZero If any oracle fee is less than or equal to zero
|
|
195
|
-
* @throws ErrorTotalFeeMustBeLessThanHundred If the total oracle fees exceed 100
|
|
196
183
|
* @throws ErrorInvalidManifest If the manifest is not a valid URL or JSON string
|
|
197
184
|
* @throws ErrorHashIsEmptyString If the manifest hash is empty
|
|
198
185
|
* @throws ErrorLaunchedEventIsNotEmitted If the LaunchedV2 event is not emitted
|
|
@@ -213,9 +200,6 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
213
200
|
* recordingOracle: '0xRecordingOracleAddress',
|
|
214
201
|
* reputationOracle: '0xReputationOracleAddress',
|
|
215
202
|
* exchangeOracle: '0xExchangeOracleAddress',
|
|
216
|
-
* recordingOracleFee: 5n,
|
|
217
|
-
* reputationOracleFee: 5n,
|
|
218
|
-
* exchangeOracleFee: 5n,
|
|
219
203
|
* manifest: 'https://example.com/manifest.json',
|
|
220
204
|
* manifestHash: 'manifestHash-123',
|
|
221
205
|
* };
|
|
@@ -234,9 +218,9 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
234
218
|
throw error_1.ErrorInvalidTokenAddress;
|
|
235
219
|
}
|
|
236
220
|
this.verifySetupParameters(escrowConfig);
|
|
237
|
-
const { recordingOracle, reputationOracle, exchangeOracle,
|
|
221
|
+
const { recordingOracle, reputationOracle, exchangeOracle, manifest, manifestHash, } = escrowConfig;
|
|
238
222
|
try {
|
|
239
|
-
const result = await this.sendTxAndWait((overrides) => this.escrowFactoryContract.createFundAndSetupEscrow(tokenAddress, amount, jobRequesterId, reputationOracle, recordingOracle, exchangeOracle,
|
|
223
|
+
const result = await this.sendTxAndWait((overrides) => this.escrowFactoryContract.createFundAndSetupEscrow(tokenAddress, amount, jobRequesterId, reputationOracle, recordingOracle, exchangeOracle, manifest, manifestHash, overrides), txOptions);
|
|
240
224
|
const event = result?.logs?.find(({ topics }) => topics.includes(ethers_1.ethers.id('LaunchedV2(address,address,string)')))?.args;
|
|
241
225
|
if (!event) {
|
|
242
226
|
throw error_1.ErrorLaunchedEventIsNotEmitted;
|
|
@@ -259,8 +243,6 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
259
243
|
* @throws ErrorInvalidRecordingOracleAddressProvided If the recording oracle address is invalid
|
|
260
244
|
* @throws ErrorInvalidReputationOracleAddressProvided If the reputation oracle address is invalid
|
|
261
245
|
* @throws ErrorInvalidExchangeOracleAddressProvided If the exchange oracle address is invalid
|
|
262
|
-
* @throws ErrorAmountMustBeGreaterThanZero If any oracle fee is less than or equal to zero
|
|
263
|
-
* @throws ErrorTotalFeeMustBeLessThanHundred If the total oracle fees exceed 100
|
|
264
246
|
* @throws ErrorInvalidManifest If the manifest is not a valid URL or JSON string
|
|
265
247
|
* @throws ErrorHashIsEmptyString If the manifest hash is empty
|
|
266
248
|
* @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
|
|
@@ -274,9 +256,6 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
274
256
|
* recordingOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
|
|
275
257
|
* reputationOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
|
|
276
258
|
* exchangeOracle: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
|
|
277
|
-
* recordingOracleFee: 10n,
|
|
278
|
-
* reputationOracleFee: 10n,
|
|
279
|
-
* exchangeOracleFee: 10n,
|
|
280
259
|
* manifest: 'http://localhost/manifest.json',
|
|
281
260
|
* manifestHash: 'b5dad76bf6772c0f07fd5e048f6e75a5f86ee079',
|
|
282
261
|
* };
|
|
@@ -284,7 +263,7 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
284
263
|
* ```
|
|
285
264
|
*/
|
|
286
265
|
async setup(escrowAddress, escrowConfig, txOptions = {}) {
|
|
287
|
-
const { recordingOracle, reputationOracle, exchangeOracle,
|
|
266
|
+
const { recordingOracle, reputationOracle, exchangeOracle, manifest, manifestHash, } = escrowConfig;
|
|
288
267
|
this.verifySetupParameters(escrowConfig);
|
|
289
268
|
if (!ethers_1.ethers.isAddress(escrowAddress)) {
|
|
290
269
|
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
@@ -294,7 +273,7 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
294
273
|
}
|
|
295
274
|
try {
|
|
296
275
|
const escrowContract = this.getEscrowContract(escrowAddress);
|
|
297
|
-
await this.sendTxAndWait((overrides) => escrowContract.setup(reputationOracle, recordingOracle, exchangeOracle,
|
|
276
|
+
await this.sendTxAndWait((overrides) => escrowContract.setup(reputationOracle, recordingOracle, exchangeOracle, manifest, manifestHash, overrides), txOptions);
|
|
298
277
|
return;
|
|
299
278
|
}
|
|
300
279
|
catch (e) {
|
|
@@ -794,7 +773,30 @@ class EscrowClient extends base_1.BaseEthersClient {
|
|
|
794
773
|
}
|
|
795
774
|
try {
|
|
796
775
|
const escrowContract = this.getEscrowContract(escrowAddress);
|
|
797
|
-
|
|
776
|
+
try {
|
|
777
|
+
return await escrowContract.manifest();
|
|
778
|
+
}
|
|
779
|
+
catch (manifestError) {
|
|
780
|
+
// Fallback for legacy escrows exposing `manifestUrl()` instead of `manifest()`.
|
|
781
|
+
try {
|
|
782
|
+
const provider = this.runner.provider;
|
|
783
|
+
if (!provider) {
|
|
784
|
+
throw error_1.ErrorProviderDoesNotExist;
|
|
785
|
+
}
|
|
786
|
+
const manifestInterface = new ethers_1.ethers.Interface([
|
|
787
|
+
'function manifestUrl() view returns (string)',
|
|
788
|
+
]);
|
|
789
|
+
const target = escrowContract.target;
|
|
790
|
+
const data = manifestInterface.encodeFunctionData('manifestUrl');
|
|
791
|
+
const result = await provider.call({ to: target, data });
|
|
792
|
+
return manifestInterface.decodeFunctionResult('manifestUrl', result)[0];
|
|
793
|
+
}
|
|
794
|
+
catch (fallbackError) {
|
|
795
|
+
throw new Error(`Failed to fetch manifest using both manifest() and manifestUrl(). ` +
|
|
796
|
+
`manifest() error: ${(0, utils_1.getErrorMessage)(manifestError)}. ` +
|
|
797
|
+
`manifestUrl() error: ${(0, utils_1.getErrorMessage)(fallbackError)}.`);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
798
800
|
}
|
|
799
801
|
catch (e) {
|
|
800
802
|
return (0, utils_1.throwError)(e);
|
|
@@ -1160,381 +1162,3 @@ __decorate([
|
|
|
1160
1162
|
__metadata("design:paramtypes", [String, Array, Array, String, String, String, Object, Object]),
|
|
1161
1163
|
__metadata("design:returntype", Promise)
|
|
1162
1164
|
], EscrowClient.prototype, "createBulkPayoutTransaction", null);
|
|
1163
|
-
/**
|
|
1164
|
-
* Utility helpers for escrow-related queries.
|
|
1165
|
-
*
|
|
1166
|
-
* @example
|
|
1167
|
-
* ```ts
|
|
1168
|
-
* import { ChainId, EscrowUtils } from '@human-protocol/sdk';
|
|
1169
|
-
*
|
|
1170
|
-
* const escrows = await EscrowUtils.getEscrows({
|
|
1171
|
-
* chainId: ChainId.POLYGON_AMOY
|
|
1172
|
-
* });
|
|
1173
|
-
* console.log('Escrows:', escrows);
|
|
1174
|
-
* ```
|
|
1175
|
-
*/
|
|
1176
|
-
class EscrowUtils {
|
|
1177
|
-
/**
|
|
1178
|
-
* This function returns an array of escrows based on the specified filter parameters.
|
|
1179
|
-
*
|
|
1180
|
-
* @param filter - Filter parameters.
|
|
1181
|
-
* @param options - Optional configuration for subgraph requests.
|
|
1182
|
-
* @returns List of escrows that match the filter.
|
|
1183
|
-
* @throws ErrorInvalidAddress If any filter address is invalid
|
|
1184
|
-
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
1185
|
-
*
|
|
1186
|
-
* @example
|
|
1187
|
-
* ```ts
|
|
1188
|
-
* import { ChainId, EscrowStatus } from '@human-protocol/sdk';
|
|
1189
|
-
*
|
|
1190
|
-
* const filters = {
|
|
1191
|
-
* status: EscrowStatus.Pending,
|
|
1192
|
-
* from: new Date(2023, 4, 8),
|
|
1193
|
-
* to: new Date(2023, 5, 8),
|
|
1194
|
-
* chainId: ChainId.POLYGON_AMOY
|
|
1195
|
-
* };
|
|
1196
|
-
* const escrows = await EscrowUtils.getEscrows(filters);
|
|
1197
|
-
* console.log('Found escrows:', escrows.length);
|
|
1198
|
-
* ```
|
|
1199
|
-
*/
|
|
1200
|
-
static async getEscrows(filter, options) {
|
|
1201
|
-
if (filter.launcher && !ethers_1.ethers.isAddress(filter.launcher)) {
|
|
1202
|
-
throw error_1.ErrorInvalidAddress;
|
|
1203
|
-
}
|
|
1204
|
-
if (filter.recordingOracle && !ethers_1.ethers.isAddress(filter.recordingOracle)) {
|
|
1205
|
-
throw error_1.ErrorInvalidAddress;
|
|
1206
|
-
}
|
|
1207
|
-
if (filter.reputationOracle && !ethers_1.ethers.isAddress(filter.reputationOracle)) {
|
|
1208
|
-
throw error_1.ErrorInvalidAddress;
|
|
1209
|
-
}
|
|
1210
|
-
if (filter.exchangeOracle && !ethers_1.ethers.isAddress(filter.exchangeOracle)) {
|
|
1211
|
-
throw error_1.ErrorInvalidAddress;
|
|
1212
|
-
}
|
|
1213
|
-
const first = filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
|
|
1214
|
-
const skip = filter.skip || 0;
|
|
1215
|
-
const orderDirection = filter.orderDirection || enums_1.OrderDirection.DESC;
|
|
1216
|
-
const networkData = constants_1.NETWORKS[filter.chainId];
|
|
1217
|
-
if (!networkData) {
|
|
1218
|
-
throw error_1.ErrorUnsupportedChainID;
|
|
1219
|
-
}
|
|
1220
|
-
let statuses;
|
|
1221
|
-
if (filter.status !== undefined) {
|
|
1222
|
-
statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
1223
|
-
statuses = statuses.map((status) => types_1.EscrowStatus[status]);
|
|
1224
|
-
}
|
|
1225
|
-
const { escrows } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_ESCROWS_QUERY)(filter), {
|
|
1226
|
-
...filter,
|
|
1227
|
-
launcher: filter.launcher?.toLowerCase(),
|
|
1228
|
-
reputationOracle: filter.reputationOracle?.toLowerCase(),
|
|
1229
|
-
recordingOracle: filter.recordingOracle?.toLowerCase(),
|
|
1230
|
-
exchangeOracle: filter.exchangeOracle?.toLowerCase(),
|
|
1231
|
-
status: statuses,
|
|
1232
|
-
from: filter.from ? (0, utils_1.getUnixTimestamp)(filter.from) : undefined,
|
|
1233
|
-
to: filter.to ? (0, utils_1.getUnixTimestamp)(filter.to) : undefined,
|
|
1234
|
-
orderDirection: orderDirection,
|
|
1235
|
-
first: first,
|
|
1236
|
-
skip: skip,
|
|
1237
|
-
}, options);
|
|
1238
|
-
return (escrows || []).map((e) => mapEscrow(e, networkData.chainId));
|
|
1239
|
-
}
|
|
1240
|
-
/**
|
|
1241
|
-
* This function returns the escrow data for a given address.
|
|
1242
|
-
*
|
|
1243
|
-
* > This uses Subgraph
|
|
1244
|
-
*
|
|
1245
|
-
* @param chainId - Network in which the escrow has been deployed
|
|
1246
|
-
* @param escrowAddress - Address of the escrow
|
|
1247
|
-
* @param options - Optional configuration for subgraph requests.
|
|
1248
|
-
* @returns Escrow data or null if not found.
|
|
1249
|
-
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
1250
|
-
* @throws ErrorInvalidAddress If the escrow address is invalid
|
|
1251
|
-
*
|
|
1252
|
-
* @example
|
|
1253
|
-
* ```ts
|
|
1254
|
-
* import { ChainId } from '@human-protocol/sdk';
|
|
1255
|
-
*
|
|
1256
|
-
* const escrow = await EscrowUtils.getEscrow(
|
|
1257
|
-
* ChainId.POLYGON_AMOY,
|
|
1258
|
-
* "0x1234567890123456789012345678901234567890"
|
|
1259
|
-
* );
|
|
1260
|
-
* if (escrow) {
|
|
1261
|
-
* console.log('Escrow status:', escrow.status);
|
|
1262
|
-
* }
|
|
1263
|
-
* ```
|
|
1264
|
-
*/
|
|
1265
|
-
static async getEscrow(chainId, escrowAddress, options) {
|
|
1266
|
-
const networkData = constants_1.NETWORKS[chainId];
|
|
1267
|
-
if (!networkData) {
|
|
1268
|
-
throw error_1.ErrorUnsupportedChainID;
|
|
1269
|
-
}
|
|
1270
|
-
if (escrowAddress && !ethers_1.ethers.isAddress(escrowAddress)) {
|
|
1271
|
-
throw error_1.ErrorInvalidAddress;
|
|
1272
|
-
}
|
|
1273
|
-
const { escrow } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_ESCROW_BY_ADDRESS_QUERY)(), { escrowAddress: escrowAddress.toLowerCase() }, options);
|
|
1274
|
-
if (!escrow)
|
|
1275
|
-
return null;
|
|
1276
|
-
return mapEscrow(escrow, networkData.chainId);
|
|
1277
|
-
}
|
|
1278
|
-
/**
|
|
1279
|
-
* This function returns the status events for a given set of networks within an optional date range.
|
|
1280
|
-
*
|
|
1281
|
-
* > This uses Subgraph
|
|
1282
|
-
*
|
|
1283
|
-
* @param filter - Filter parameters.
|
|
1284
|
-
* @param options - Optional configuration for subgraph requests.
|
|
1285
|
-
* @returns Array of status events with their corresponding statuses.
|
|
1286
|
-
* @throws ErrorInvalidAddress If the launcher address is invalid
|
|
1287
|
-
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
1288
|
-
*
|
|
1289
|
-
* @example
|
|
1290
|
-
* ```ts
|
|
1291
|
-
* import { ChainId, EscrowStatus } from '@human-protocol/sdk';
|
|
1292
|
-
*
|
|
1293
|
-
* const fromDate = new Date('2023-01-01');
|
|
1294
|
-
* const toDate = new Date('2023-12-31');
|
|
1295
|
-
* const statusEvents = await EscrowUtils.getStatusEvents({
|
|
1296
|
-
* chainId: ChainId.POLYGON,
|
|
1297
|
-
* statuses: [EscrowStatus.Pending, EscrowStatus.Complete],
|
|
1298
|
-
* from: fromDate,
|
|
1299
|
-
* to: toDate
|
|
1300
|
-
* });
|
|
1301
|
-
* console.log('Status events:', statusEvents.length);
|
|
1302
|
-
* ```
|
|
1303
|
-
*/
|
|
1304
|
-
static async getStatusEvents(filter, options) {
|
|
1305
|
-
const { chainId, statuses, from, to, launcher, first = 10, skip = 0, orderDirection = enums_1.OrderDirection.DESC, } = filter;
|
|
1306
|
-
if (launcher && !ethers_1.ethers.isAddress(launcher)) {
|
|
1307
|
-
throw error_1.ErrorInvalidAddress;
|
|
1308
|
-
}
|
|
1309
|
-
const networkData = constants_1.NETWORKS[chainId];
|
|
1310
|
-
if (!networkData) {
|
|
1311
|
-
throw error_1.ErrorUnsupportedChainID;
|
|
1312
|
-
}
|
|
1313
|
-
// If statuses are not provided, use all statuses except Launched
|
|
1314
|
-
const effectiveStatuses = statuses ?? [
|
|
1315
|
-
types_1.EscrowStatus.Launched,
|
|
1316
|
-
types_1.EscrowStatus.Pending,
|
|
1317
|
-
types_1.EscrowStatus.Partial,
|
|
1318
|
-
types_1.EscrowStatus.Paid,
|
|
1319
|
-
types_1.EscrowStatus.Complete,
|
|
1320
|
-
types_1.EscrowStatus.Cancelled,
|
|
1321
|
-
];
|
|
1322
|
-
const statusNames = effectiveStatuses.map((status) => types_1.EscrowStatus[status]);
|
|
1323
|
-
const data = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_STATUS_UPDATES_QUERY)(from, to, launcher), {
|
|
1324
|
-
status: statusNames,
|
|
1325
|
-
from: from ? (0, utils_1.getUnixTimestamp)(from) : undefined,
|
|
1326
|
-
to: to ? (0, utils_1.getUnixTimestamp)(to) : undefined,
|
|
1327
|
-
launcher: launcher || undefined,
|
|
1328
|
-
orderDirection,
|
|
1329
|
-
first: Math.min(first, 1000),
|
|
1330
|
-
skip,
|
|
1331
|
-
}, options);
|
|
1332
|
-
if (!data || !data['escrowStatusEvents']) {
|
|
1333
|
-
return [];
|
|
1334
|
-
}
|
|
1335
|
-
return data['escrowStatusEvents'].map((event) => ({
|
|
1336
|
-
timestamp: Number(event.timestamp) * 1000,
|
|
1337
|
-
escrowAddress: event.escrowAddress,
|
|
1338
|
-
status: types_1.EscrowStatus[event.status],
|
|
1339
|
-
chainId,
|
|
1340
|
-
}));
|
|
1341
|
-
}
|
|
1342
|
-
/**
|
|
1343
|
-
* This function returns the payouts for a given set of networks.
|
|
1344
|
-
*
|
|
1345
|
-
* > This uses Subgraph
|
|
1346
|
-
*
|
|
1347
|
-
* @param filter - Filter parameters.
|
|
1348
|
-
* @param options - Optional configuration for subgraph requests.
|
|
1349
|
-
* @returns List of payouts matching the filters.
|
|
1350
|
-
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
1351
|
-
* @throws ErrorInvalidAddress If any filter address is invalid
|
|
1352
|
-
*
|
|
1353
|
-
* @example
|
|
1354
|
-
* ```ts
|
|
1355
|
-
* import { ChainId } from '@human-protocol/sdk';
|
|
1356
|
-
*
|
|
1357
|
-
* const payouts = await EscrowUtils.getPayouts({
|
|
1358
|
-
* chainId: ChainId.POLYGON,
|
|
1359
|
-
* escrowAddress: '0x1234567890123456789012345678901234567890',
|
|
1360
|
-
* recipient: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef',
|
|
1361
|
-
* from: new Date('2023-01-01'),
|
|
1362
|
-
* to: new Date('2023-12-31')
|
|
1363
|
-
* });
|
|
1364
|
-
* console.log('Payouts:', payouts.length);
|
|
1365
|
-
* ```
|
|
1366
|
-
*/
|
|
1367
|
-
static async getPayouts(filter, options) {
|
|
1368
|
-
const networkData = constants_1.NETWORKS[filter.chainId];
|
|
1369
|
-
if (!networkData) {
|
|
1370
|
-
throw error_1.ErrorUnsupportedChainID;
|
|
1371
|
-
}
|
|
1372
|
-
if (filter.escrowAddress && !ethers_1.ethers.isAddress(filter.escrowAddress)) {
|
|
1373
|
-
throw error_1.ErrorInvalidAddress;
|
|
1374
|
-
}
|
|
1375
|
-
if (filter.recipient && !ethers_1.ethers.isAddress(filter.recipient)) {
|
|
1376
|
-
throw error_1.ErrorInvalidAddress;
|
|
1377
|
-
}
|
|
1378
|
-
const first = filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
|
|
1379
|
-
const skip = filter.skip || 0;
|
|
1380
|
-
const orderDirection = filter.orderDirection || enums_1.OrderDirection.DESC;
|
|
1381
|
-
const { payouts } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_PAYOUTS_QUERY)(filter), {
|
|
1382
|
-
escrowAddress: filter.escrowAddress?.toLowerCase(),
|
|
1383
|
-
recipient: filter.recipient?.toLowerCase(),
|
|
1384
|
-
from: filter.from ? (0, utils_1.getUnixTimestamp)(filter.from) : undefined,
|
|
1385
|
-
to: filter.to ? (0, utils_1.getUnixTimestamp)(filter.to) : undefined,
|
|
1386
|
-
first: Math.min(first, 1000),
|
|
1387
|
-
skip,
|
|
1388
|
-
orderDirection,
|
|
1389
|
-
}, options);
|
|
1390
|
-
if (!payouts) {
|
|
1391
|
-
return [];
|
|
1392
|
-
}
|
|
1393
|
-
return payouts.map((payout) => ({
|
|
1394
|
-
id: payout.id,
|
|
1395
|
-
escrowAddress: payout.escrowAddress,
|
|
1396
|
-
recipient: payout.recipient,
|
|
1397
|
-
amount: BigInt(payout.amount),
|
|
1398
|
-
createdAt: Number(payout.createdAt) * 1000,
|
|
1399
|
-
}));
|
|
1400
|
-
}
|
|
1401
|
-
/**
|
|
1402
|
-
* This function returns the cancellation refunds for a given set of networks.
|
|
1403
|
-
*
|
|
1404
|
-
* > This uses Subgraph
|
|
1405
|
-
*
|
|
1406
|
-
* @param filter - Filter parameters.
|
|
1407
|
-
* @param options - Optional configuration for subgraph requests.
|
|
1408
|
-
* @returns List of cancellation refunds matching the filters.
|
|
1409
|
-
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
1410
|
-
* @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
|
|
1411
|
-
* @throws ErrorInvalidAddress If the receiver address is invalid
|
|
1412
|
-
*
|
|
1413
|
-
* @example
|
|
1414
|
-
* ```ts
|
|
1415
|
-
* import { ChainId } from '@human-protocol/sdk';
|
|
1416
|
-
*
|
|
1417
|
-
* const cancellationRefunds = await EscrowUtils.getCancellationRefunds({
|
|
1418
|
-
* chainId: ChainId.POLYGON_AMOY,
|
|
1419
|
-
* escrowAddress: '0x1234567890123456789012345678901234567890',
|
|
1420
|
-
* });
|
|
1421
|
-
* console.log('Cancellation refunds:', cancellationRefunds.length);
|
|
1422
|
-
* ```
|
|
1423
|
-
*/
|
|
1424
|
-
static async getCancellationRefunds(filter, options) {
|
|
1425
|
-
const networkData = constants_1.NETWORKS[filter.chainId];
|
|
1426
|
-
if (!networkData)
|
|
1427
|
-
throw error_1.ErrorUnsupportedChainID;
|
|
1428
|
-
if (filter.escrowAddress && !ethers_1.ethers.isAddress(filter.escrowAddress)) {
|
|
1429
|
-
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
1430
|
-
}
|
|
1431
|
-
if (filter.receiver && !ethers_1.ethers.isAddress(filter.receiver)) {
|
|
1432
|
-
throw error_1.ErrorInvalidAddress;
|
|
1433
|
-
}
|
|
1434
|
-
const first = filter.first !== undefined ? Math.min(filter.first, 1000) : 10;
|
|
1435
|
-
const skip = filter.skip || 0;
|
|
1436
|
-
const orderDirection = filter.orderDirection || enums_1.OrderDirection.DESC;
|
|
1437
|
-
const { cancellationRefundEvents } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_CANCELLATION_REFUNDS_QUERY)(filter), {
|
|
1438
|
-
escrowAddress: filter.escrowAddress?.toLowerCase(),
|
|
1439
|
-
receiver: filter.receiver?.toLowerCase(),
|
|
1440
|
-
from: filter.from ? (0, utils_1.getUnixTimestamp)(filter.from) : undefined,
|
|
1441
|
-
to: filter.to ? (0, utils_1.getUnixTimestamp)(filter.to) : undefined,
|
|
1442
|
-
first,
|
|
1443
|
-
skip,
|
|
1444
|
-
orderDirection,
|
|
1445
|
-
}, options);
|
|
1446
|
-
if (!cancellationRefundEvents || cancellationRefundEvents.length === 0) {
|
|
1447
|
-
return [];
|
|
1448
|
-
}
|
|
1449
|
-
return cancellationRefundEvents.map((event) => ({
|
|
1450
|
-
id: event.id,
|
|
1451
|
-
escrowAddress: event.escrowAddress,
|
|
1452
|
-
receiver: event.receiver,
|
|
1453
|
-
amount: BigInt(event.amount),
|
|
1454
|
-
block: Number(event.block),
|
|
1455
|
-
timestamp: Number(event.timestamp) * 1000,
|
|
1456
|
-
txHash: event.txHash,
|
|
1457
|
-
}));
|
|
1458
|
-
}
|
|
1459
|
-
/**
|
|
1460
|
-
* This function returns the cancellation refund for a given escrow address.
|
|
1461
|
-
*
|
|
1462
|
-
* > This uses Subgraph
|
|
1463
|
-
*
|
|
1464
|
-
* @param chainId - Network in which the escrow has been deployed
|
|
1465
|
-
* @param escrowAddress - Address of the escrow
|
|
1466
|
-
* @param options - Optional configuration for subgraph requests.
|
|
1467
|
-
* @returns Cancellation refund data or null if not found.
|
|
1468
|
-
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
1469
|
-
* @throws ErrorInvalidEscrowAddressProvided If the escrow address is invalid
|
|
1470
|
-
*
|
|
1471
|
-
* @example
|
|
1472
|
-
* ```ts
|
|
1473
|
-
* import { ChainId } from '@human-protocol/sdk';
|
|
1474
|
-
*
|
|
1475
|
-
*
|
|
1476
|
-
* const cancellationRefund = await EscrowUtils.getCancellationRefund(
|
|
1477
|
-
* ChainId.POLYGON_AMOY,
|
|
1478
|
-
* "0x1234567890123456789012345678901234567890"
|
|
1479
|
-
* );
|
|
1480
|
-
* if (cancellationRefund) {
|
|
1481
|
-
* console.log('Refund amount:', cancellationRefund.amount);
|
|
1482
|
-
* }
|
|
1483
|
-
* ```
|
|
1484
|
-
*/
|
|
1485
|
-
static async getCancellationRefund(chainId, escrowAddress, options) {
|
|
1486
|
-
const networkData = constants_1.NETWORKS[chainId];
|
|
1487
|
-
if (!networkData)
|
|
1488
|
-
throw error_1.ErrorUnsupportedChainID;
|
|
1489
|
-
if (!ethers_1.ethers.isAddress(escrowAddress)) {
|
|
1490
|
-
throw error_1.ErrorInvalidEscrowAddressProvided;
|
|
1491
|
-
}
|
|
1492
|
-
const { cancellationRefundEvents } = await (0, utils_1.customGqlFetch)((0, utils_1.getSubgraphUrl)(networkData), (0, graphql_1.GET_CANCELLATION_REFUND_BY_ADDRESS_QUERY)(), { escrowAddress: escrowAddress.toLowerCase() }, options);
|
|
1493
|
-
if (!cancellationRefundEvents || cancellationRefundEvents.length === 0) {
|
|
1494
|
-
return null;
|
|
1495
|
-
}
|
|
1496
|
-
return {
|
|
1497
|
-
id: cancellationRefundEvents[0].id,
|
|
1498
|
-
escrowAddress: cancellationRefundEvents[0].escrowAddress,
|
|
1499
|
-
receiver: cancellationRefundEvents[0].receiver,
|
|
1500
|
-
amount: BigInt(cancellationRefundEvents[0].amount),
|
|
1501
|
-
block: Number(cancellationRefundEvents[0].block),
|
|
1502
|
-
timestamp: Number(cancellationRefundEvents[0].timestamp) * 1000,
|
|
1503
|
-
txHash: cancellationRefundEvents[0].txHash,
|
|
1504
|
-
};
|
|
1505
|
-
}
|
|
1506
|
-
}
|
|
1507
|
-
exports.EscrowUtils = EscrowUtils;
|
|
1508
|
-
function mapEscrow(e, chainId) {
|
|
1509
|
-
return {
|
|
1510
|
-
id: e.id,
|
|
1511
|
-
address: e.address,
|
|
1512
|
-
amountPaid: BigInt(e.amountPaid),
|
|
1513
|
-
balance: BigInt(e.balance),
|
|
1514
|
-
count: Number(e.count),
|
|
1515
|
-
factoryAddress: e.factoryAddress,
|
|
1516
|
-
finalResultsUrl: e.finalResultsUrl,
|
|
1517
|
-
finalResultsHash: e.finalResultsHash,
|
|
1518
|
-
intermediateResultsUrl: e.intermediateResultsUrl,
|
|
1519
|
-
intermediateResultsHash: e.intermediateResultsHash,
|
|
1520
|
-
launcher: e.launcher,
|
|
1521
|
-
jobRequesterId: e.jobRequesterId,
|
|
1522
|
-
manifestHash: e.manifestHash,
|
|
1523
|
-
manifest: e.manifest,
|
|
1524
|
-
recordingOracle: e.recordingOracle,
|
|
1525
|
-
reputationOracle: e.reputationOracle,
|
|
1526
|
-
exchangeOracle: e.exchangeOracle,
|
|
1527
|
-
recordingOracleFee: e.recordingOracleFee
|
|
1528
|
-
? Number(e.recordingOracleFee)
|
|
1529
|
-
: null,
|
|
1530
|
-
reputationOracleFee: e.reputationOracleFee
|
|
1531
|
-
? Number(e.reputationOracleFee)
|
|
1532
|
-
: null,
|
|
1533
|
-
exchangeOracleFee: e.exchangeOracleFee ? Number(e.exchangeOracleFee) : null,
|
|
1534
|
-
status: e.status,
|
|
1535
|
-
token: e.token,
|
|
1536
|
-
totalFundedAmount: BigInt(e.totalFundedAmount),
|
|
1537
|
-
createdAt: Number(e.createdAt) * 1000,
|
|
1538
|
-
chainId: Number(chainId),
|
|
1539
|
-
};
|
|
1540
|
-
}
|