@human-protocol/sdk 1.0.1 → 1.0.3
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/README.md +1 -1
- package/dist/constants.d.ts +46 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +203 -0
- package/dist/decorators.d.ts +2 -0
- package/dist/decorators.d.ts.map +1 -0
- package/dist/decorators.js +17 -0
- package/dist/enums.d.ts +17 -0
- package/dist/enums.d.ts.map +1 -0
- package/dist/enums.js +20 -0
- package/dist/error.d.ts +196 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +229 -0
- package/dist/escrow.d.ts +176 -0
- package/dist/escrow.d.ts.map +1 -0
- package/dist/escrow.js +590 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/init.d.ts +13 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +35 -0
- package/dist/interfaces.d.ts +44 -0
- package/dist/interfaces.d.ts.map +1 -0
- package/dist/interfaces.js +2 -0
- package/dist/kvstore.d.ts +40 -0
- package/dist/kvstore.d.ts.map +1 -0
- package/dist/kvstore.js +106 -0
- package/dist/queries.d.ts +4 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +22 -0
- package/dist/staking.d.ts +121 -0
- package/dist/staking.d.ts.map +1 -0
- package/dist/staking.js +381 -0
- package/dist/storage.d.ts +48 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +164 -0
- package/dist/types.d.ts +123 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +35 -0
- package/dist/utils.d.ts +32 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +99 -0
- package/package.json +4 -7
- package/src/constants.ts +221 -4
- package/src/decorators.ts +21 -0
- package/src/enums.ts +16 -0
- package/src/error.ts +298 -16
- package/src/escrow.ts +754 -0
- package/src/index.ts +14 -1
- package/src/init.ts +45 -0
- package/src/interfaces.ts +50 -0
- package/src/kvstore.ts +93 -0
- package/src/queries.ts +18 -0
- package/src/staking.ts +421 -0
- package/src/storage.ts +159 -130
- package/src/types.ts +37 -574
- package/src/utils.ts +80 -76
- package/test/escrow.test.ts +1339 -0
- package/test/init.test.ts +88 -0
- package/test/kvstore.test.ts +208 -0
- package/test/staking.test.ts +640 -0
- package/test/storage.test.ts +422 -0
- package/test/utils/constants.ts +40 -0
- package/example/simple-existing-job.ts +0 -86
- package/example/simple-new-job-public.ts +0 -74
- package/example/simple-new-job.ts +0 -72
- package/src/job.ts +0 -821
- package/src/logger.ts +0 -29
- package/test/job.test.ts +0 -753
- package/test/utils/manifest.ts +0 -33
package/src/utils.ts
CHANGED
|
@@ -1,98 +1,102 @@
|
|
|
1
|
-
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import { ethers } from 'ethers';
|
|
2
4
|
|
|
3
5
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
ContractExecutionError,
|
|
7
|
+
ErrorNoURLprovided,
|
|
8
|
+
EthereumError,
|
|
9
|
+
InvalidArgumentError,
|
|
10
|
+
NonceExpired,
|
|
11
|
+
NumericFault,
|
|
12
|
+
OutOfGasError,
|
|
13
|
+
ReplacementUnderpriced,
|
|
14
|
+
TransactionReplaced,
|
|
15
|
+
UnpredictableGasLimit,
|
|
16
|
+
} from './error';
|
|
11
17
|
|
|
12
18
|
/**
|
|
13
|
-
* **Get
|
|
19
|
+
* **Get specific error text.*
|
|
14
20
|
*
|
|
15
|
-
* @param {
|
|
16
|
-
* @
|
|
17
|
-
* @returns {Promise<HMToken>} Attached contract instance
|
|
21
|
+
* @param {any} error - An error message.
|
|
22
|
+
* @returns
|
|
18
23
|
*/
|
|
19
|
-
export const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return contract;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* **Deploy EscrowFactory contract**
|
|
32
|
-
*
|
|
33
|
-
* @param {string} hmTokenAddr HMToken address
|
|
34
|
-
* @param {ethers.Signer | undefined} signer Deployer signer
|
|
35
|
-
* @returns {Promise<EscrowFactory>} Deployed contract instance
|
|
36
|
-
*/
|
|
37
|
-
export const deployEscrowFactory = async (
|
|
38
|
-
hmTokenAddr: string,
|
|
39
|
-
signer?: ethers.Signer
|
|
40
|
-
): Promise<EscrowFactory> => {
|
|
41
|
-
const factory = new EscrowFactory__factory(signer);
|
|
42
|
-
|
|
43
|
-
const contract = await factory.deploy(hmTokenAddr);
|
|
44
|
-
|
|
45
|
-
return contract;
|
|
24
|
+
export const getRevertReason = (error: any): string => {
|
|
25
|
+
const prefix = "reverted with reason string '";
|
|
26
|
+
const suffix = "'";
|
|
27
|
+
const message = error.data.substring(
|
|
28
|
+
error.data.indexOf(prefix) + prefix.length
|
|
29
|
+
);
|
|
30
|
+
return message.substring(0, message.indexOf(suffix));
|
|
46
31
|
};
|
|
47
32
|
|
|
48
33
|
/**
|
|
49
|
-
* **
|
|
34
|
+
* **Handle and throw the error.*
|
|
50
35
|
*
|
|
51
|
-
* @param {
|
|
52
|
-
* @
|
|
53
|
-
* @returns {Promise<EscrowFactory>} Attached contract instance
|
|
36
|
+
* @param {any} e
|
|
37
|
+
* @returns
|
|
54
38
|
*/
|
|
55
|
-
export const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
39
|
+
export const throwError = (e: any) => {
|
|
40
|
+
if (e.code === ethers.utils.Logger.errors.INVALID_ARGUMENT) {
|
|
41
|
+
throw new InvalidArgumentError(e.message);
|
|
42
|
+
} else if (e.code === 'OUT_OF_GAS') {
|
|
43
|
+
throw new OutOfGasError(e.message);
|
|
44
|
+
} else if (e.code === ethers.utils.Logger.errors.CALL_EXCEPTION) {
|
|
45
|
+
const reason = getRevertReason(e.data);
|
|
46
|
+
throw new ContractExecutionError(reason);
|
|
47
|
+
} else if (e.code === ethers.utils.Logger.errors.UNPREDICTABLE_GAS_LIMIT) {
|
|
48
|
+
throw new UnpredictableGasLimit(e.message);
|
|
49
|
+
} else if (e.code === ethers.utils.Logger.errors.TRANSACTION_REPLACED) {
|
|
50
|
+
throw new TransactionReplaced(e.message);
|
|
51
|
+
} else if (e.code === ethers.utils.Logger.errors.REPLACEMENT_UNDERPRICED) {
|
|
52
|
+
throw new ReplacementUnderpriced(e.message);
|
|
53
|
+
} else if (e.code === ethers.utils.Logger.errors.NUMERIC_FAULT) {
|
|
54
|
+
throw new NumericFault(e.message);
|
|
55
|
+
} else if (e.code === ethers.utils.Logger.errors.NONCE_EXPIRED) {
|
|
56
|
+
throw new NonceExpired(e.message);
|
|
57
|
+
} else {
|
|
58
|
+
throw new EthereumError(e.message);
|
|
59
|
+
}
|
|
64
60
|
};
|
|
65
61
|
|
|
66
62
|
/**
|
|
67
|
-
* **
|
|
63
|
+
* **URL validation.*
|
|
68
64
|
*
|
|
69
|
-
* @param {string}
|
|
70
|
-
* @
|
|
71
|
-
* @returns {Promise<Escrow>} Attached contract instance
|
|
65
|
+
* @param {string} url
|
|
66
|
+
* @returns
|
|
72
67
|
*/
|
|
73
|
-
export const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
return contract;
|
|
68
|
+
export const isValidUrl = (url: string) => {
|
|
69
|
+
try {
|
|
70
|
+
new URL(url);
|
|
71
|
+
return true;
|
|
72
|
+
} catch (err) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
82
75
|
};
|
|
83
76
|
|
|
84
77
|
/**
|
|
85
|
-
* **
|
|
86
|
-
*
|
|
87
|
-
* Apply given decimals to the specified amount.
|
|
78
|
+
* **Fetching data with queries.*
|
|
88
79
|
*
|
|
89
|
-
* @param {string
|
|
90
|
-
* @param {
|
|
91
|
-
* @
|
|
80
|
+
* @param {string} url
|
|
81
|
+
* @param {string} query
|
|
82
|
+
* @param {any} variables
|
|
83
|
+
* @param {any} headers
|
|
84
|
+
* @returns
|
|
92
85
|
*/
|
|
93
|
-
export const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
86
|
+
export const gqlFetch = (
|
|
87
|
+
url: string,
|
|
88
|
+
query: string,
|
|
89
|
+
variables?: any,
|
|
90
|
+
headers?: any
|
|
91
|
+
) => {
|
|
92
|
+
if (url && url.length) {
|
|
93
|
+
return axios.post(url, JSON.stringify({ query, variables }), {
|
|
94
|
+
headers: {
|
|
95
|
+
'Content-Type': 'application/json',
|
|
96
|
+
...headers,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
return Promise.reject(ErrorNoURLprovided);
|
|
101
|
+
}
|
|
98
102
|
};
|