@human-protocol/sdk 1.1.2 → 1.1.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/package.json +5 -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 +781 -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 -143
- 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 -1067
- package/src/logger.ts +0 -29
- package/test/job.test.ts +0 -817
- package/test/utils/constants.ts +0 -30
- package/test/utils/manifest.ts +0 -33
package/src/utils.ts
CHANGED
|
@@ -1,165 +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
|
-
* @returns {Promise<EscrowFactory>} Deployed contract instance
|
|
36
|
+
* @param {any} e
|
|
37
|
+
* @returns
|
|
40
38
|
*/
|
|
41
|
-
export const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
+
}
|
|
51
60
|
};
|
|
52
61
|
|
|
53
62
|
/**
|
|
54
|
-
* **
|
|
63
|
+
* **URL validation.*
|
|
55
64
|
*
|
|
56
|
-
* @param {string}
|
|
57
|
-
* @
|
|
58
|
-
* @returns {Promise<EscrowFactory>} Attached contract instance
|
|
65
|
+
* @param {string} url
|
|
66
|
+
* @returns
|
|
59
67
|
*/
|
|
60
|
-
export const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return contract;
|
|
68
|
+
export const isValidUrl = (url: string) => {
|
|
69
|
+
try {
|
|
70
|
+
new URL(url);
|
|
71
|
+
return true;
|
|
72
|
+
} catch (err) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
69
75
|
};
|
|
70
76
|
|
|
71
77
|
/**
|
|
72
|
-
* **
|
|
73
|
-
*
|
|
74
|
-
* @param {string} escrowAddr Escrow contract address
|
|
75
|
-
* @param {ethers.Signer | undefined} signer Deployer signer
|
|
76
|
-
* @returns {Promise<Escrow>} Attached contract instance
|
|
77
|
-
*/
|
|
78
|
-
export const getEscrow = async (
|
|
79
|
-
escrowAddr: string,
|
|
80
|
-
signer?: ethers.Signer
|
|
81
|
-
): Promise<Escrow> => {
|
|
82
|
-
const factory = new Escrow__factory(signer);
|
|
83
|
-
|
|
84
|
-
const contract = await factory.attach(escrowAddr);
|
|
85
|
-
|
|
86
|
-
return contract;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* **Deploy Staking contract**
|
|
91
|
-
*
|
|
92
|
-
* @param {string} hmTokenAddr HMToken address
|
|
93
|
-
* @param {number} minimumStake Minimum amount to stake
|
|
94
|
-
* @param {number} lockPeriod Lock period after unstake
|
|
95
|
-
* @param {ethers.Signer | undefined} signer Deployer signer
|
|
96
|
-
* @returns {Promise<Staking>} Deployed contract instance
|
|
97
|
-
*/
|
|
98
|
-
export const deployStaking = async (
|
|
99
|
-
hmTokenAddr: string,
|
|
100
|
-
minimumStake: number,
|
|
101
|
-
lockPeriod: number,
|
|
102
|
-
signer?: ethers.Signer
|
|
103
|
-
): Promise<Staking> => {
|
|
104
|
-
const staking = new Staking__factory(signer);
|
|
105
|
-
const contract = await staking.deploy();
|
|
106
|
-
await contract.initialize(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();
|
|
146
|
-
await contract.initialize(hmTokenAddr, stakingAddr, fee);
|
|
147
|
-
|
|
148
|
-
return contract;
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* **Get specific amount representation in given decimals**
|
|
153
|
-
*
|
|
154
|
-
* Apply given decimals to the specified amount.
|
|
78
|
+
* **Fetching data with queries.*
|
|
155
79
|
*
|
|
156
|
-
* @param {string
|
|
157
|
-
* @param {
|
|
158
|
-
* @
|
|
80
|
+
* @param {string} url
|
|
81
|
+
* @param {string} query
|
|
82
|
+
* @param {any} variables
|
|
83
|
+
* @param {any} headers
|
|
84
|
+
* @returns
|
|
159
85
|
*/
|
|
160
|
-
export const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
+
}
|
|
165
102
|
};
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import { Job } from '../src';
|
|
3
|
-
import {
|
|
4
|
-
DEFAULT_GAS_PAYER_PRIVKEY,
|
|
5
|
-
DEFAULT_HMTOKEN_ADDR,
|
|
6
|
-
REPUTATION_ORACLE_PRIVKEY,
|
|
7
|
-
WORKER1_ADDR,
|
|
8
|
-
WORKER2_ADDR,
|
|
9
|
-
} from '../test/utils/constants';
|
|
10
|
-
import { manifest } from '../test/utils/manifest';
|
|
11
|
-
import * as dotenv from 'dotenv';
|
|
12
|
-
|
|
13
|
-
dotenv.config();
|
|
14
|
-
|
|
15
|
-
const main = async () => {
|
|
16
|
-
// Create job object
|
|
17
|
-
const newJob = new Job({
|
|
18
|
-
gasPayer: DEFAULT_GAS_PAYER_PRIVKEY,
|
|
19
|
-
reputationOracle: REPUTATION_ORACLE_PRIVKEY,
|
|
20
|
-
manifest: manifest,
|
|
21
|
-
hmTokenAddr: DEFAULT_HMTOKEN_ADDR,
|
|
22
|
-
logLevel: 'debug',
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
// Initialize new job object
|
|
26
|
-
await newJob.initialize();
|
|
27
|
-
|
|
28
|
-
// Launch the job
|
|
29
|
-
await newJob.launch();
|
|
30
|
-
|
|
31
|
-
// Access the existing job
|
|
32
|
-
const job = new Job({
|
|
33
|
-
gasPayer: DEFAULT_GAS_PAYER_PRIVKEY,
|
|
34
|
-
reputationOracle: REPUTATION_ORACLE_PRIVKEY,
|
|
35
|
-
manifest: manifest,
|
|
36
|
-
hmTokenAddr: DEFAULT_HMTOKEN_ADDR,
|
|
37
|
-
factoryAddr: newJob.contractData?.factoryAddr,
|
|
38
|
-
escrowAddr: newJob.contractData?.escrowAddr,
|
|
39
|
-
storageAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
40
|
-
storageSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
41
|
-
storageEndpoint: process.env.AWS_ENDPOINT,
|
|
42
|
-
storageBucket: process.env.AWS_BUCKET,
|
|
43
|
-
storagePublicBucket: process.env.AWS_PUBLIC_BUCKET,
|
|
44
|
-
logLevel: 'debug',
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// Initialize the job object
|
|
48
|
-
await job.initialize();
|
|
49
|
-
|
|
50
|
-
// Setup the job
|
|
51
|
-
await job.setup();
|
|
52
|
-
|
|
53
|
-
console.log(
|
|
54
|
-
`Status: ${await job.status()}, Balance: ${(
|
|
55
|
-
await job.balance()
|
|
56
|
-
)?.toString()}`
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
// Bulk payout workers
|
|
60
|
-
await job.bulkPayout(
|
|
61
|
-
[
|
|
62
|
-
{
|
|
63
|
-
address: WORKER1_ADDR,
|
|
64
|
-
amount: 70,
|
|
65
|
-
},
|
|
66
|
-
{
|
|
67
|
-
address: WORKER2_ADDR,
|
|
68
|
-
amount: 30,
|
|
69
|
-
},
|
|
70
|
-
],
|
|
71
|
-
{
|
|
72
|
-
result: 'result',
|
|
73
|
-
}
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
// Complete the job
|
|
77
|
-
await job.complete();
|
|
78
|
-
|
|
79
|
-
console.log(
|
|
80
|
-
`Status: ${await job.status()}, Balance: ${(
|
|
81
|
-
await job.balance()
|
|
82
|
-
)?.toString()}`
|
|
83
|
-
);
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
main();
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import { Job } from '../src';
|
|
3
|
-
import {
|
|
4
|
-
DEFAULT_GAS_PAYER_PRIVKEY,
|
|
5
|
-
DEFAULT_HMTOKEN_ADDR,
|
|
6
|
-
REPUTATION_ORACLE_PRIVKEY,
|
|
7
|
-
WORKER1_ADDR,
|
|
8
|
-
WORKER2_ADDR,
|
|
9
|
-
} from '../test/utils/constants';
|
|
10
|
-
import { manifest } from '../test/utils/manifest';
|
|
11
|
-
import * as dotenv from 'dotenv';
|
|
12
|
-
|
|
13
|
-
dotenv.config();
|
|
14
|
-
|
|
15
|
-
const main = async () => {
|
|
16
|
-
// Create job object
|
|
17
|
-
const job = new Job({
|
|
18
|
-
gasPayer: DEFAULT_GAS_PAYER_PRIVKEY,
|
|
19
|
-
reputationOracle: REPUTATION_ORACLE_PRIVKEY,
|
|
20
|
-
manifest: manifest,
|
|
21
|
-
hmTokenAddr: DEFAULT_HMTOKEN_ADDR,
|
|
22
|
-
storageAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
23
|
-
storageSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
24
|
-
storageEndpoint: process.env.AWS_ENDPOINT,
|
|
25
|
-
storageBucket: process.env.AWS_BUCKET,
|
|
26
|
-
storagePublicBucket: process.env.AWS_PUBLIC_BUCKET,
|
|
27
|
-
logLevel: 'debug',
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
// Initialize new job
|
|
31
|
-
await job.initialize();
|
|
32
|
-
|
|
33
|
-
// Launch the job
|
|
34
|
-
await job.launch();
|
|
35
|
-
|
|
36
|
-
// Setup the job
|
|
37
|
-
await job.setup();
|
|
38
|
-
|
|
39
|
-
console.log(
|
|
40
|
-
`Status: ${await job.status()}, Balance: ${(
|
|
41
|
-
await job.balance()
|
|
42
|
-
)?.toString()}`
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
// Bulk payout workers
|
|
46
|
-
await job.bulkPayout(
|
|
47
|
-
[
|
|
48
|
-
{
|
|
49
|
-
address: WORKER1_ADDR,
|
|
50
|
-
amount: 70,
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
address: WORKER2_ADDR,
|
|
54
|
-
amount: 30,
|
|
55
|
-
},
|
|
56
|
-
],
|
|
57
|
-
{
|
|
58
|
-
result: 'result',
|
|
59
|
-
},
|
|
60
|
-
false,
|
|
61
|
-
true
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
// Complete the job
|
|
65
|
-
await job.complete();
|
|
66
|
-
|
|
67
|
-
console.log(
|
|
68
|
-
`Status: ${await job.status()}, Balance: ${(
|
|
69
|
-
await job.balance()
|
|
70
|
-
)?.toString()}`
|
|
71
|
-
);
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
main();
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import { Job } from '../src';
|
|
3
|
-
import {
|
|
4
|
-
DEFAULT_GAS_PAYER_PRIVKEY,
|
|
5
|
-
DEFAULT_HMTOKEN_ADDR,
|
|
6
|
-
REPUTATION_ORACLE_PRIVKEY,
|
|
7
|
-
WORKER1_ADDR,
|
|
8
|
-
WORKER2_ADDR,
|
|
9
|
-
} from '../test/utils/constants';
|
|
10
|
-
import { manifest } from '../test/utils/manifest';
|
|
11
|
-
import * as dotenv from 'dotenv';
|
|
12
|
-
|
|
13
|
-
dotenv.config();
|
|
14
|
-
|
|
15
|
-
const main = async () => {
|
|
16
|
-
// Create job object
|
|
17
|
-
const job = new Job({
|
|
18
|
-
gasPayer: DEFAULT_GAS_PAYER_PRIVKEY,
|
|
19
|
-
reputationOracle: REPUTATION_ORACLE_PRIVKEY,
|
|
20
|
-
manifest: manifest,
|
|
21
|
-
hmTokenAddr: DEFAULT_HMTOKEN_ADDR,
|
|
22
|
-
storageAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
23
|
-
storageSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
24
|
-
storageEndpoint: process.env.AWS_ENDPOINT,
|
|
25
|
-
storageBucket: process.env.AWS_BUCKET,
|
|
26
|
-
storagePublicBucket: process.env.AWS_PUBLIC_BUCKET,
|
|
27
|
-
logLevel: 'debug',
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
// Initialize new job
|
|
31
|
-
await job.initialize();
|
|
32
|
-
|
|
33
|
-
// Launch the job
|
|
34
|
-
await job.launch();
|
|
35
|
-
|
|
36
|
-
// Setup the job
|
|
37
|
-
await job.setup();
|
|
38
|
-
|
|
39
|
-
console.log(
|
|
40
|
-
`Status: ${await job.status()}, Balance: ${(
|
|
41
|
-
await job.balance()
|
|
42
|
-
)?.toString()}`
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
// Bulk payout workers
|
|
46
|
-
await job.bulkPayout(
|
|
47
|
-
[
|
|
48
|
-
{
|
|
49
|
-
address: WORKER1_ADDR,
|
|
50
|
-
amount: 70,
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
address: WORKER2_ADDR,
|
|
54
|
-
amount: 30,
|
|
55
|
-
},
|
|
56
|
-
],
|
|
57
|
-
{
|
|
58
|
-
result: 'result',
|
|
59
|
-
}
|
|
60
|
-
);
|
|
61
|
-
|
|
62
|
-
// Complete the job
|
|
63
|
-
await job.complete();
|
|
64
|
-
|
|
65
|
-
console.log(
|
|
66
|
-
`Status: ${await job.status()}, Balance: ${(
|
|
67
|
-
await job.balance()
|
|
68
|
-
)?.toString()}`
|
|
69
|
-
);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
main();
|