@human-protocol/sdk 1.0.1 → 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 +298 -16
  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 -130
  57. package/src/types.ts +37 -574
  58. package/src/utils.ts +80 -76
  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 +40 -0
  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 -821
  69. package/src/logger.ts +0 -29
  70. package/test/job.test.ts +0 -753
  71. package/test/utils/manifest.ts +0 -33
package/src/utils.ts CHANGED
@@ -1,98 +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
- } 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';
11
17
 
12
18
  /**
13
- * **Get HMToken contract instance at given address**
19
+ * **Get specific error text.*
14
20
  *
15
- * @param {string} hmTokenAddr HMToken contract address
16
- * @param {ethers.Signer | undefined} signer Deployer signer
17
- * @returns {Promise<HMToken>} Attached contract instance
21
+ * @param {any} error - An error message.
22
+ * @returns
18
23
  */
19
- export const getHmToken = async (
20
- hmTokenAddr: string,
21
- signer?: ethers.Signer
22
- ): Promise<HMToken> => {
23
- const factory = new HMToken__factory(signer);
24
-
25
- const contract = await factory.attach(hmTokenAddr);
26
-
27
- return contract;
28
- };
29
-
30
- /**
31
- * **Deploy EscrowFactory contract**
32
- *
33
- * @param {string} hmTokenAddr HMToken address
34
- * @param {ethers.Signer | undefined} signer Deployer signer
35
- * @returns {Promise<EscrowFactory>} Deployed contract instance
36
- */
37
- export const deployEscrowFactory = async (
38
- hmTokenAddr: string,
39
- signer?: ethers.Signer
40
- ): Promise<EscrowFactory> => {
41
- const factory = new EscrowFactory__factory(signer);
42
-
43
- const contract = await factory.deploy(hmTokenAddr);
44
-
45
- 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));
46
31
  };
47
32
 
48
33
  /**
49
- * **Get EscrowFactory contract instance at given address**
34
+ * **Handle and throw the error.*
50
35
  *
51
- * @param {string} factoryAddr EscrowFactory contract address
52
- * @param {ethers.Signer | undefined} signer Deployer signer
53
- * @returns {Promise<EscrowFactory>} Attached contract instance
36
+ * @param {any} e
37
+ * @returns
54
38
  */
55
- export const getEscrowFactory = async (
56
- factoryAddr: string,
57
- signer?: ethers.Signer
58
- ): Promise<EscrowFactory> => {
59
- const factory = new EscrowFactory__factory(signer);
60
-
61
- const contract = await factory.attach(factoryAddr);
62
-
63
- 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
+ }
64
60
  };
65
61
 
66
62
  /**
67
- * **Get Escrow contract instance at given address**
63
+ * **URL validation.*
68
64
  *
69
- * @param {string} escrowAddr Escrow contract address
70
- * @param {ethers.Signer | undefined} signer Deployer signer
71
- * @returns {Promise<Escrow>} Attached contract instance
65
+ * @param {string} url
66
+ * @returns
72
67
  */
73
- export const getEscrow = async (
74
- escrowAddr: string,
75
- signer?: ethers.Signer
76
- ): Promise<Escrow> => {
77
- const factory = new Escrow__factory(signer);
78
-
79
- const contract = await factory.attach(escrowAddr);
80
-
81
- return contract;
68
+ export const isValidUrl = (url: string) => {
69
+ try {
70
+ new URL(url);
71
+ return true;
72
+ } catch (err) {
73
+ return false;
74
+ }
82
75
  };
83
76
 
84
77
  /**
85
- * **Get specific amount representation in given decimals**
86
- *
87
- * Apply given decimals to the specified amount.
78
+ * **Fetching data with queries.*
88
79
  *
89
- * @param {string | number} amount - Amount to convert
90
- * @param {number} decimals - Decimal to convert
91
- * @returns {BigNumber} Converted amount
80
+ * @param {string} url
81
+ * @param {string} query
82
+ * @param {any} variables
83
+ * @param {any} headers
84
+ * @returns
92
85
  */
93
- export const toFullDigit = (
94
- amount: number | string,
95
- decimals = 18
96
- ): BigNumber => {
97
- 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
+ }
98
102
  };