@human-protocol/sdk 0.0.10
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 +9 -0
- package/example/simple-existing-job.ts +86 -0
- package/example/simple-new-job-public.ts +74 -0
- package/example/simple-new-job.ts +72 -0
- package/package.json +52 -0
- package/src/constants.ts +9 -0
- package/src/error.ts +43 -0
- package/src/index.ts +4 -0
- package/src/job.ts +1064 -0
- package/src/logger.ts +29 -0
- package/src/storage.ts +135 -0
- package/src/types.ts +676 -0
- package/src/utils.ts +164 -0
- package/test/job.test.ts +817 -0
- package/test/utils/constants.ts +30 -0
- package/test/utils/manifest.ts +33 -0
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
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();
|
|
@@ -0,0 +1,74 @@
|
|
|
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();
|
|
@@ -0,0 +1,72 @@
|
|
|
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();
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@human-protocol/sdk",
|
|
3
|
+
"description": "Human Protocol SDK",
|
|
4
|
+
"version": "0.0.10",
|
|
5
|
+
"files": [
|
|
6
|
+
"src",
|
|
7
|
+
"dist",
|
|
8
|
+
"example",
|
|
9
|
+
"test"
|
|
10
|
+
],
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"types": "dist/index.d.ts",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"clean": "rm -rf ./dist",
|
|
15
|
+
"build": "npm run clean && tsc",
|
|
16
|
+
"prepublish": "npm run build",
|
|
17
|
+
"test": "concurrently -k -s first -g --hide 0 \"yarn workspace @human-protocol/core local\" \"sleep 5 && jest --runInBand\"",
|
|
18
|
+
"lint": "eslint .",
|
|
19
|
+
"lint:fix": "eslint . --fix",
|
|
20
|
+
"format": "prettier --write '**/*.{ts,json}'"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"url": "https://github.com/humanprotocol/human-protocol.git",
|
|
24
|
+
"directory": "packages/sdk/typescript/human-protocol-sdk"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"human-protocol",
|
|
28
|
+
"sdk",
|
|
29
|
+
"node",
|
|
30
|
+
"typescript",
|
|
31
|
+
"ethereum"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"lint-staged": {
|
|
35
|
+
"*.ts": [
|
|
36
|
+
"prettier --write",
|
|
37
|
+
"eslint --fix"
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@human-protocol/core": "^1.0.12",
|
|
42
|
+
"aws-sdk": "^2.1255.0",
|
|
43
|
+
"crypto": "^1.0.1",
|
|
44
|
+
"dotenv": "^16.0.3",
|
|
45
|
+
"ethers": "^5.7.2",
|
|
46
|
+
"secp256k1": "^4.0.3",
|
|
47
|
+
"winston": "^3.8.2"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@human-protocol/core": "^1.0.12"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/constants.ts
ADDED
package/src/error.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @constant {Error} - The job is not initialized yet.
|
|
3
|
+
*/
|
|
4
|
+
export const ErrorJobNotInitialized = new Error('Job is not initialized');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @constant {Error} - The job is not launched yet.
|
|
8
|
+
*/
|
|
9
|
+
export const ErrorJobNotLaunched = new Error('Job is not launched');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @constant {Error} - The job is already launched.
|
|
13
|
+
*/
|
|
14
|
+
export const ErrorJobAlreadyLaunched = new Error('Job is already launched');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @constant {Error} - The reputation oracle is missing.
|
|
18
|
+
*/
|
|
19
|
+
export const ErrorReputationOracleMissing = new Error(
|
|
20
|
+
'Reputation oracle is missing'
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @constant {Error} - The manifest is missing.
|
|
25
|
+
*/
|
|
26
|
+
export const ErrorManifestMissing = new Error('Manifest is missing');
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @constant {Error} - The HMToken is missing.
|
|
30
|
+
*/
|
|
31
|
+
export const ErrorHMTokenMissing = new Error('HMToken is missing');
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @constant {Error} - The Storage access data is missing.
|
|
35
|
+
*/
|
|
36
|
+
export const ErrorStorageAccessDataMissing = new Error(
|
|
37
|
+
'Storage access data is missing'
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @constant {Error} - The Staking contract is missing.
|
|
42
|
+
*/
|
|
43
|
+
export const ErrorStakingMissing = new Error('Staking contract is missing');
|
package/src/index.ts
ADDED