@human-protocol/sdk 1.0.2 → 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 +295 -18
- 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 -131
- package/src/types.ts +36 -586
- package/src/utils.ts +80 -142
- 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 +38 -1
- 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 -977
- package/src/logger.ts +0 -29
- package/test/job.test.ts +0 -716
- package/test/utils/manifest.ts +0 -33
package/src/utils.ts
CHANGED
|
@@ -1,164 +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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
} from '
|
|
6
|
+
ContractExecutionError,
|
|
7
|
+
ErrorNoURLprovided,
|
|
8
|
+
EthereumError,
|
|
9
|
+
InvalidArgumentError,
|
|
10
|
+
NonceExpired,
|
|
11
|
+
NumericFault,
|
|
12
|
+
OutOfGasError,
|
|
13
|
+
ReplacementUnderpriced,
|
|
14
|
+
TransactionReplaced,
|
|
15
|
+
UnpredictableGasLimit,
|
|
16
|
+
} from './error';
|
|
15
17
|
|
|
16
18
|
/**
|
|
17
|
-
* **Get
|
|
19
|
+
* **Get specific error text.*
|
|
18
20
|
*
|
|
19
|
-
* @param {
|
|
20
|
-
* @
|
|
21
|
-
* @returns {Promise<HMToken>} Attached contract instance
|
|
21
|
+
* @param {any} error - An error message.
|
|
22
|
+
* @returns
|
|
22
23
|
*/
|
|
23
|
-
export const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
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));
|
|
32
31
|
};
|
|
33
32
|
|
|
34
33
|
/**
|
|
35
|
-
* **
|
|
34
|
+
* **Handle and throw the error.*
|
|
36
35
|
*
|
|
37
|
-
* @param {
|
|
38
|
-
* @
|
|
39
|
-
* @param {ethers.Signer | undefined} signer Deployer signer
|
|
40
|
-
* @returns {Promise<EscrowFactory>} Deployed contract instance
|
|
36
|
+
* @param {any} e
|
|
37
|
+
* @returns
|
|
41
38
|
*/
|
|
42
|
-
export const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
+
}
|
|
52
60
|
};
|
|
53
61
|
|
|
54
62
|
/**
|
|
55
|
-
* **
|
|
63
|
+
* **URL validation.*
|
|
56
64
|
*
|
|
57
|
-
* @param {string}
|
|
58
|
-
* @
|
|
59
|
-
* @returns {Promise<EscrowFactory>} Attached contract instance
|
|
65
|
+
* @param {string} url
|
|
66
|
+
* @returns
|
|
60
67
|
*/
|
|
61
|
-
export const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return contract;
|
|
68
|
+
export const isValidUrl = (url: string) => {
|
|
69
|
+
try {
|
|
70
|
+
new URL(url);
|
|
71
|
+
return true;
|
|
72
|
+
} catch (err) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
70
75
|
};
|
|
71
76
|
|
|
72
77
|
/**
|
|
73
|
-
* **
|
|
74
|
-
*
|
|
75
|
-
* @param {string} escrowAddr Escrow contract address
|
|
76
|
-
* @param {ethers.Signer | undefined} signer Deployer signer
|
|
77
|
-
* @returns {Promise<Escrow>} Attached contract instance
|
|
78
|
-
*/
|
|
79
|
-
export const getEscrow = async (
|
|
80
|
-
escrowAddr: string,
|
|
81
|
-
signer?: ethers.Signer
|
|
82
|
-
): Promise<Escrow> => {
|
|
83
|
-
const factory = new Escrow__factory(signer);
|
|
84
|
-
|
|
85
|
-
const contract = await factory.attach(escrowAddr);
|
|
86
|
-
|
|
87
|
-
return contract;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* **Deploy Staking contract**
|
|
92
|
-
*
|
|
93
|
-
* @param {string} hmTokenAddr HMToken address
|
|
94
|
-
* @param {number} minimumStake Minimum amount to stake
|
|
95
|
-
* @param {number} lockPeriod Lock period after unstake
|
|
96
|
-
* @param {ethers.Signer | undefined} signer Deployer signer
|
|
97
|
-
* @returns {Promise<Staking>} Deployed contract instance
|
|
98
|
-
*/
|
|
99
|
-
export const deployStaking = async (
|
|
100
|
-
hmTokenAddr: string,
|
|
101
|
-
minimumStake: number,
|
|
102
|
-
lockPeriod: number,
|
|
103
|
-
signer?: ethers.Signer
|
|
104
|
-
): Promise<Staking> => {
|
|
105
|
-
const staking = new Staking__factory(signer);
|
|
106
|
-
const contract = await staking.deploy(hmTokenAddr, minimumStake, lockPeriod);
|
|
107
|
-
|
|
108
|
-
return contract;
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* **Get Staking contract instance at given address**
|
|
113
|
-
*
|
|
114
|
-
* @param {string} stakingAddr Staking contract address
|
|
115
|
-
* @param {ethers.Signer | undefined} signer Deployer signer
|
|
116
|
-
* @returns {Promise<Staking>} Attached contract instance
|
|
117
|
-
*/
|
|
118
|
-
export const getStaking = async (
|
|
119
|
-
stakingAddr: string,
|
|
120
|
-
signer?: ethers.Signer
|
|
121
|
-
): Promise<Staking> => {
|
|
122
|
-
const factory = new Staking__factory(signer);
|
|
123
|
-
|
|
124
|
-
const contract = await factory.attach(stakingAddr);
|
|
125
|
-
|
|
126
|
-
return contract;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* **Deploy RewardPool contract**
|
|
131
|
-
*
|
|
132
|
-
* @param {string} hmTokenAddr HMToken address
|
|
133
|
-
* @param {string} stakingAddr Staking address
|
|
134
|
-
* @param {number} fee Reward fee of the protocol
|
|
135
|
-
* @param {ethers.Signer | undefined} signer Deployer signer
|
|
136
|
-
* @returns {Promise<Staking>} Deployed contract instance
|
|
137
|
-
*/
|
|
138
|
-
export const deployRewardPool = async (
|
|
139
|
-
hmTokenAddr: string,
|
|
140
|
-
stakingAddr: string,
|
|
141
|
-
fee: number,
|
|
142
|
-
signer?: ethers.Signer
|
|
143
|
-
): Promise<RewardPool> => {
|
|
144
|
-
const rewardPool = new RewardPool__factory(signer);
|
|
145
|
-
const contract = await rewardPool.deploy(hmTokenAddr, stakingAddr, fee);
|
|
146
|
-
|
|
147
|
-
return contract;
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* **Get specific amount representation in given decimals**
|
|
152
|
-
*
|
|
153
|
-
* Apply given decimals to the specified amount.
|
|
78
|
+
* **Fetching data with queries.*
|
|
154
79
|
*
|
|
155
|
-
* @param {string
|
|
156
|
-
* @param {
|
|
157
|
-
* @
|
|
80
|
+
* @param {string} url
|
|
81
|
+
* @param {string} query
|
|
82
|
+
* @param {any} variables
|
|
83
|
+
* @param {any} headers
|
|
84
|
+
* @returns
|
|
158
85
|
*/
|
|
159
|
-
export const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
+
}
|
|
164
102
|
};
|