@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.
Files changed (71) hide show
  1. package/README.md +1 -1
  2. package/dist/constants.d.ts +46 -0
  3. package/dist/constants.d.ts.map +1 -0
  4. package/dist/constants.js +203 -0
  5. package/dist/decorators.d.ts +2 -0
  6. package/dist/decorators.d.ts.map +1 -0
  7. package/dist/decorators.js +17 -0
  8. package/dist/enums.d.ts +17 -0
  9. package/dist/enums.d.ts.map +1 -0
  10. package/dist/enums.js +20 -0
  11. package/dist/error.d.ts +196 -0
  12. package/dist/error.d.ts.map +1 -0
  13. package/dist/error.js +229 -0
  14. package/dist/escrow.d.ts +176 -0
  15. package/dist/escrow.d.ts.map +1 -0
  16. package/dist/escrow.js +590 -0
  17. package/dist/index.d.ts +10 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +33 -0
  20. package/dist/init.d.ts +13 -0
  21. package/dist/init.d.ts.map +1 -0
  22. package/dist/init.js +35 -0
  23. package/dist/interfaces.d.ts +44 -0
  24. package/dist/interfaces.d.ts.map +1 -0
  25. package/dist/interfaces.js +2 -0
  26. package/dist/kvstore.d.ts +40 -0
  27. package/dist/kvstore.d.ts.map +1 -0
  28. package/dist/kvstore.js +106 -0
  29. package/dist/queries.d.ts +4 -0
  30. package/dist/queries.d.ts.map +1 -0
  31. package/dist/queries.js +22 -0
  32. package/dist/staking.d.ts +121 -0
  33. package/dist/staking.d.ts.map +1 -0
  34. package/dist/staking.js +381 -0
  35. package/dist/storage.d.ts +48 -0
  36. package/dist/storage.d.ts.map +1 -0
  37. package/dist/storage.js +164 -0
  38. package/dist/types.d.ts +123 -0
  39. package/dist/types.d.ts.map +1 -0
  40. package/dist/types.js +35 -0
  41. package/dist/utils.d.ts +32 -0
  42. package/dist/utils.d.ts.map +1 -0
  43. package/dist/utils.js +99 -0
  44. package/package.json +4 -7
  45. package/src/constants.ts +221 -4
  46. package/src/decorators.ts +21 -0
  47. package/src/enums.ts +16 -0
  48. package/src/error.ts +295 -18
  49. package/src/escrow.ts +754 -0
  50. package/src/index.ts +14 -1
  51. package/src/init.ts +45 -0
  52. package/src/interfaces.ts +50 -0
  53. package/src/kvstore.ts +93 -0
  54. package/src/queries.ts +18 -0
  55. package/src/staking.ts +421 -0
  56. package/src/storage.ts +159 -131
  57. package/src/types.ts +36 -586
  58. package/src/utils.ts +80 -142
  59. package/test/escrow.test.ts +1339 -0
  60. package/test/init.test.ts +88 -0
  61. package/test/kvstore.test.ts +208 -0
  62. package/test/staking.test.ts +640 -0
  63. package/test/storage.test.ts +422 -0
  64. package/test/utils/constants.ts +38 -1
  65. package/example/simple-existing-job.ts +0 -86
  66. package/example/simple-new-job-public.ts +0 -74
  67. package/example/simple-new-job.ts +0 -72
  68. package/src/job.ts +0 -977
  69. package/src/logger.ts +0 -29
  70. package/test/job.test.ts +0 -716
  71. package/test/utils/manifest.ts +0 -33
package/src/utils.ts CHANGED
@@ -1,164 +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} hmTokenAddr HMToken address
38
- * @param {string} stakingAddr Staking address
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 deployEscrowFactory = async (
43
- hmTokenAddr: string,
44
- stakingAddr: string,
45
- signer?: ethers.Signer
46
- ): Promise<EscrowFactory> => {
47
- const factory = new EscrowFactory__factory(signer);
48
-
49
- const contract = await factory.deploy(hmTokenAddr, stakingAddr);
50
-
51
- 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
+ }
52
60
  };
53
61
 
54
62
  /**
55
- * **Get EscrowFactory contract instance at given address**
63
+ * **URL validation.*
56
64
  *
57
- * @param {string} factoryAddr EscrowFactory contract address
58
- * @param {ethers.Signer | undefined} signer Deployer signer
59
- * @returns {Promise<EscrowFactory>} Attached contract instance
65
+ * @param {string} url
66
+ * @returns
60
67
  */
61
- export const getEscrowFactory = async (
62
- factoryAddr: string,
63
- signer?: ethers.Signer
64
- ): Promise<EscrowFactory> => {
65
- const factory = new EscrowFactory__factory(signer);
66
-
67
- const contract = await factory.attach(factoryAddr);
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
- * **Get Escrow contract instance at given address**
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 | number} amount - Amount to convert
156
- * @param {number} decimals - Decimal to convert
157
- * @returns {BigNumber} Converted amount
80
+ * @param {string} url
81
+ * @param {string} query
82
+ * @param {any} variables
83
+ * @param {any} headers
84
+ * @returns
158
85
  */
159
- export const toFullDigit = (
160
- amount: number | string,
161
- decimals = 18
162
- ): BigNumber => {
163
- 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
+ }
164
102
  };