@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/src/utils.ts CHANGED
@@ -1,165 +1,102 @@
1
- import { BigNumber, ethers } from 'ethers';
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import axios from 'axios';
3
+ import { ethers } from 'ethers';
2
4
 
3
5
  import {
4
- Escrow,
5
- Escrow__factory,
6
- EscrowFactory,
7
- EscrowFactory__factory,
8
- HMToken,
9
- HMToken__factory,
10
- Staking,
11
- Staking__factory,
12
- RewardPool,
13
- RewardPool__factory,
14
- } from '@human-protocol/core/typechain-types';
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 HMToken contract instance at given address**
19
+ * **Get specific error text.*
18
20
  *
19
- * @param {string} hmTokenAddr HMToken contract address
20
- * @param {ethers.Signer | undefined} signer Deployer signer
21
- * @returns {Promise<HMToken>} Attached contract instance
21
+ * @param {any} error - An error message.
22
+ * @returns
22
23
  */
23
- export const getHmToken = async (
24
- hmTokenAddr: string,
25
- signer?: ethers.Signer
26
- ): Promise<HMToken> => {
27
- const factory = new HMToken__factory(signer);
28
-
29
- const contract = await factory.attach(hmTokenAddr);
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
- * **Deploy EscrowFactory contract**
34
+ * **Handle and throw the error.*
36
35
  *
37
- * @param {string} stakingAddr Staking address
38
- * @param {ethers.Signer | undefined} signer Deployer signer
39
- * @returns {Promise<EscrowFactory>} Deployed contract instance
36
+ * @param {any} e
37
+ * @returns
40
38
  */
41
- export const deployEscrowFactory = async (
42
- stakingAddr: string,
43
- signer?: ethers.Signer
44
- ): Promise<EscrowFactory> => {
45
- const factory = new EscrowFactory__factory(signer);
46
-
47
- const contract = await factory.deploy();
48
- await contract.initialize(stakingAddr);
49
-
50
- return contract;
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
- * **Get EscrowFactory contract instance at given address**
63
+ * **URL validation.*
55
64
  *
56
- * @param {string} factoryAddr EscrowFactory contract address
57
- * @param {ethers.Signer | undefined} signer Deployer signer
58
- * @returns {Promise<EscrowFactory>} Attached contract instance
65
+ * @param {string} url
66
+ * @returns
59
67
  */
60
- export const getEscrowFactory = async (
61
- factoryAddr: string,
62
- signer?: ethers.Signer
63
- ): Promise<EscrowFactory> => {
64
- const factory = new EscrowFactory__factory(signer);
65
-
66
- const contract = await factory.attach(factoryAddr);
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
- * **Get Escrow contract instance at given address**
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 | number} amount - Amount to convert
157
- * @param {number} decimals - Decimal to convert
158
- * @returns {BigNumber} Converted amount
80
+ * @param {string} url
81
+ * @param {string} query
82
+ * @param {any} variables
83
+ * @param {any} headers
84
+ * @returns
159
85
  */
160
- export const toFullDigit = (
161
- amount: number | string,
162
- decimals = 18
163
- ): BigNumber => {
164
- return BigNumber.from(amount).mul(BigNumber.from(10).pow(decimals));
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();