essential-eth 0.9.2-beta.1 → 0.9.2-beta.2

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 (205) hide show
  1. package/dist/cjs/classes/Contract.js +139 -0
  2. package/dist/cjs/classes/test/Contract/crv-abi.js +488 -0
  3. package/dist/cjs/classes/test/Contract/ens-abi.js +453 -0
  4. package/dist/cjs/classes/test/Contract/fei-abi.js +526 -0
  5. package/dist/cjs/classes/test/Contract/foo-abi.js +42 -0
  6. package/dist/cjs/classes/test/Contract/uniswap-abi.js +121 -0
  7. package/dist/cjs/classes/utils/clean-block.js +160 -0
  8. package/dist/cjs/classes/utils/clean-log.js +39 -0
  9. package/dist/cjs/classes/utils/clean-transaction-receipt.js +53 -0
  10. package/dist/cjs/classes/utils/clean-transaction.js +54 -0
  11. package/dist/cjs/classes/utils/encode-decode-transaction.js +175 -0
  12. package/dist/cjs/classes/utils/fetchers.js +85 -0
  13. package/dist/cjs/classes/utils/hex-to-decimal.js +24 -0
  14. package/dist/cjs/classes/utils/prepare-transaction.js +47 -0
  15. package/dist/cjs/index.js +56 -0
  16. package/dist/cjs/index.umd.js +2 -0
  17. package/dist/cjs/index.umd.js.map +1 -0
  18. package/dist/cjs/logger/logger.js +37 -0
  19. package/dist/cjs/logger/package-version.d.ts +1 -0
  20. package/dist/cjs/logger/package-version.js +5 -0
  21. package/dist/cjs/providers/AlchemyProvider.js +11 -0
  22. package/dist/cjs/providers/BaseProvider.js +514 -0
  23. package/dist/cjs/providers/FallthroughProvider.js +74 -0
  24. package/dist/cjs/providers/JsonRpcProvider.js +46 -0
  25. package/dist/cjs/providers/test/json-rpc-provider/get-logs/mocks.js +152 -0
  26. package/dist/cjs/providers/test/mock-of.js +12 -0
  27. package/dist/cjs/providers/test/rpc-urls.js +25 -0
  28. package/dist/cjs/providers/utils/chains-info.js +71 -0
  29. package/dist/cjs/shared/tiny-big/helpers.js +97 -0
  30. package/dist/cjs/shared/tiny-big/tiny-big.js +101 -0
  31. package/dist/cjs/shared/validate-type.js +9 -0
  32. package/dist/cjs/types/Block.types.js +2 -0
  33. package/dist/cjs/types/Contract.types.js +2 -0
  34. package/dist/cjs/types/FeeData.types.js +2 -0
  35. package/dist/cjs/types/Filter.types.js +2 -0
  36. package/dist/cjs/types/Network.types.js +2 -0
  37. package/dist/cjs/types/Transaction.types.js +2 -0
  38. package/dist/cjs/utils/bytes.js +711 -0
  39. package/dist/cjs/utils/compute-address.js +34 -0
  40. package/dist/cjs/utils/compute-public-key.js +26 -0
  41. package/dist/cjs/utils/ether-to-gwei.js +35 -0
  42. package/dist/cjs/utils/ether-to-wei.js +35 -0
  43. package/dist/cjs/utils/gwei-to-ether.js +35 -0
  44. package/dist/cjs/utils/hash-message.js +30 -0
  45. package/dist/cjs/utils/is-address.js +39 -0
  46. package/dist/cjs/utils/keccak256.js +31 -0
  47. package/dist/cjs/utils/solidity-keccak256.js +138 -0
  48. package/dist/cjs/utils/split-signature.js +163 -0
  49. package/dist/cjs/utils/to-checksum-address.js +46 -0
  50. package/dist/cjs/utils/to-utf8-bytes.js +21 -0
  51. package/dist/cjs/utils/wei-to-ether.js +45 -0
  52. package/dist/esm/classes/Contract.d.ts +11 -0
  53. package/dist/esm/classes/Contract.js +58 -0
  54. package/dist/esm/classes/test/Contract/crv-abi.d.ts +2 -0
  55. package/dist/esm/classes/test/Contract/crv-abi.js +470 -0
  56. package/dist/esm/classes/test/Contract/ens-abi.d.ts +2 -0
  57. package/dist/esm/classes/test/Contract/ens-abi.js +435 -0
  58. package/dist/esm/classes/test/Contract/fei-abi.d.ts +2 -0
  59. package/dist/esm/classes/test/Contract/fei-abi.js +521 -0
  60. package/dist/esm/classes/test/Contract/foo-abi.d.ts +2 -0
  61. package/dist/esm/classes/test/Contract/foo-abi.js +33 -0
  62. package/dist/esm/classes/test/Contract/uniswap-abi.d.ts +2 -0
  63. package/dist/esm/classes/test/Contract/uniswap-abi.js +116 -0
  64. package/dist/esm/classes/utils/clean-block.d.ts +2 -0
  65. package/dist/esm/classes/utils/clean-block.js +35 -0
  66. package/dist/esm/classes/utils/clean-log.d.ts +2 -0
  67. package/dist/esm/classes/utils/clean-log.js +28 -0
  68. package/dist/esm/classes/utils/clean-transaction-receipt.d.ts +2 -0
  69. package/dist/esm/classes/utils/clean-transaction-receipt.js +37 -0
  70. package/dist/esm/classes/utils/clean-transaction.d.ts +2 -0
  71. package/dist/esm/classes/utils/clean-transaction.js +36 -0
  72. package/dist/esm/classes/utils/encode-decode-transaction.d.ts +4 -0
  73. package/dist/esm/classes/utils/encode-decode-transaction.js +132 -0
  74. package/dist/esm/classes/utils/fetchers.d.ts +15 -0
  75. package/dist/esm/classes/utils/fetchers.js +37 -0
  76. package/dist/esm/classes/utils/hex-to-decimal.d.ts +1 -0
  77. package/dist/esm/classes/utils/hex-to-decimal.js +3 -0
  78. package/dist/esm/classes/utils/prepare-transaction.d.ts +2 -0
  79. package/dist/esm/classes/utils/prepare-transaction.js +36 -0
  80. package/dist/esm/index.d.ts +25 -0
  81. package/dist/esm/index.js +20 -0
  82. package/dist/esm/logger/logger.d.ts +11 -0
  83. package/dist/esm/logger/logger.js +34 -0
  84. package/dist/esm/logger/package-version.d.ts +1 -0
  85. package/dist/esm/logger/package-version.js +1 -0
  86. package/dist/esm/providers/AlchemyProvider.d.ts +4 -0
  87. package/dist/esm/providers/AlchemyProvider.js +7 -0
  88. package/dist/esm/providers/BaseProvider.d.ts +26 -0
  89. package/dist/esm/providers/BaseProvider.js +137 -0
  90. package/dist/esm/providers/FallthroughProvider.d.ts +11 -0
  91. package/dist/esm/providers/FallthroughProvider.js +50 -0
  92. package/dist/esm/providers/JsonRpcProvider.d.ts +7 -0
  93. package/dist/esm/providers/JsonRpcProvider.js +15 -0
  94. package/dist/esm/providers/test/json-rpc-provider/get-logs/mocks.d.ts +14 -0
  95. package/dist/esm/providers/test/json-rpc-provider/get-logs/mocks.js +142 -0
  96. package/dist/esm/providers/test/mock-of.d.ts +2 -0
  97. package/dist/esm/providers/test/mock-of.js +1 -0
  98. package/dist/esm/providers/test/rpc-urls.d.ts +12 -0
  99. package/dist/esm/providers/test/rpc-urls.js +19 -0
  100. package/dist/esm/providers/utils/chains-info.d.ts +24 -0
  101. package/dist/esm/providers/utils/chains-info.js +67 -0
  102. package/dist/esm/shared/tiny-big/helpers.d.ts +1 -0
  103. package/dist/esm/shared/tiny-big/helpers.js +58 -0
  104. package/dist/esm/shared/tiny-big/tiny-big.d.ts +10 -0
  105. package/dist/esm/shared/tiny-big/tiny-big.js +45 -0
  106. package/dist/esm/shared/validate-type.d.ts +3 -0
  107. package/dist/esm/shared/validate-type.js +5 -0
  108. package/dist/esm/types/Block.types.d.ts +40 -0
  109. package/dist/esm/types/Block.types.js +1 -0
  110. package/dist/esm/types/Contract.types.d.ts +24 -0
  111. package/dist/esm/types/Contract.types.js +1 -0
  112. package/dist/esm/types/FeeData.types.d.ts +7 -0
  113. package/dist/esm/types/FeeData.types.js +1 -0
  114. package/dist/esm/types/Filter.types.d.ts +12 -0
  115. package/dist/esm/types/Filter.types.js +1 -0
  116. package/dist/esm/types/Network.types.d.ts +5 -0
  117. package/dist/esm/types/Network.types.js +1 -0
  118. package/dist/esm/types/Transaction.types.d.ts +113 -0
  119. package/dist/esm/types/Transaction.types.js +1 -0
  120. package/dist/esm/utils/bytes.d.ts +40 -0
  121. package/dist/esm/utils/bytes.js +245 -0
  122. package/dist/esm/utils/compute-address.d.ts +1 -0
  123. package/dist/esm/utils/compute-address.js +12 -0
  124. package/dist/esm/utils/compute-public-key.d.ts +2 -0
  125. package/dist/esm/utils/compute-public-key.js +6 -0
  126. package/dist/esm/utils/ether-to-gwei.d.ts +3 -0
  127. package/dist/esm/utils/ether-to-gwei.js +7 -0
  128. package/dist/esm/utils/ether-to-wei.d.ts +3 -0
  129. package/dist/esm/utils/ether-to-wei.js +7 -0
  130. package/dist/esm/utils/gwei-to-ether.d.ts +3 -0
  131. package/dist/esm/utils/gwei-to-ether.js +7 -0
  132. package/dist/esm/utils/hash-message.d.ts +2 -0
  133. package/dist/esm/utils/hash-message.js +14 -0
  134. package/dist/esm/utils/is-address.d.ts +1 -0
  135. package/dist/esm/utils/is-address.js +12 -0
  136. package/dist/esm/utils/keccak256.d.ts +2 -0
  137. package/dist/esm/utils/keccak256.js +13 -0
  138. package/dist/esm/utils/solidity-keccak256.d.ts +2 -0
  139. package/dist/esm/utils/solidity-keccak256.js +85 -0
  140. package/dist/esm/utils/split-signature.d.ts +2 -0
  141. package/dist/esm/utils/split-signature.js +126 -0
  142. package/dist/esm/utils/to-checksum-address.d.ts +1 -0
  143. package/dist/esm/utils/to-checksum-address.js +25 -0
  144. package/dist/esm/utils/to-utf8-bytes.d.ts +1 -0
  145. package/dist/esm/utils/to-utf8-bytes.js +3 -0
  146. package/dist/esm/utils/wei-to-ether.d.ts +3 -0
  147. package/dist/esm/utils/wei-to-ether.js +16 -0
  148. package/package.json +14 -12
  149. package/readme.md +1 -1
  150. package/dist/essential-eth.esm.mjs +0 -2
  151. package/dist/essential-eth.esm.mjs.map +0 -1
  152. package/dist/essential-eth.js +0 -2
  153. package/dist/essential-eth.js.map +0 -1
  154. package/dist/essential-eth.modern.mjs +0 -2
  155. package/dist/essential-eth.modern.mjs.map +0 -1
  156. package/dist/essential-eth.umd.js +0 -2
  157. package/dist/essential-eth.umd.js.map +0 -1
  158. package/dist/logger/package-version.d.ts +0 -1
  159. /package/dist/{classes → cjs/classes}/Contract.d.ts +0 -0
  160. /package/dist/{classes → cjs/classes}/test/Contract/crv-abi.d.ts +0 -0
  161. /package/dist/{classes → cjs/classes}/test/Contract/ens-abi.d.ts +0 -0
  162. /package/dist/{classes → cjs/classes}/test/Contract/fei-abi.d.ts +0 -0
  163. /package/dist/{classes → cjs/classes}/test/Contract/foo-abi.d.ts +0 -0
  164. /package/dist/{classes → cjs/classes}/test/Contract/uniswap-abi.d.ts +0 -0
  165. /package/dist/{classes → cjs/classes}/utils/clean-block.d.ts +0 -0
  166. /package/dist/{classes → cjs/classes}/utils/clean-log.d.ts +0 -0
  167. /package/dist/{classes → cjs/classes}/utils/clean-transaction-receipt.d.ts +0 -0
  168. /package/dist/{classes → cjs/classes}/utils/clean-transaction.d.ts +0 -0
  169. /package/dist/{classes → cjs/classes}/utils/encode-decode-transaction.d.ts +0 -0
  170. /package/dist/{classes → cjs/classes}/utils/fetchers.d.ts +0 -0
  171. /package/dist/{classes → cjs/classes}/utils/hex-to-decimal.d.ts +0 -0
  172. /package/dist/{classes → cjs/classes}/utils/prepare-transaction.d.ts +0 -0
  173. /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  174. /package/dist/{logger → cjs/logger}/logger.d.ts +0 -0
  175. /package/dist/{providers → cjs/providers}/AlchemyProvider.d.ts +0 -0
  176. /package/dist/{providers → cjs/providers}/BaseProvider.d.ts +0 -0
  177. /package/dist/{providers → cjs/providers}/FallthroughProvider.d.ts +0 -0
  178. /package/dist/{providers → cjs/providers}/JsonRpcProvider.d.ts +0 -0
  179. /package/dist/{providers → cjs/providers}/test/json-rpc-provider/get-logs/mocks.d.ts +0 -0
  180. /package/dist/{providers → cjs/providers}/test/mock-of.d.ts +0 -0
  181. /package/dist/{providers → cjs/providers}/test/rpc-urls.d.ts +0 -0
  182. /package/dist/{providers → cjs/providers}/utils/chains-info.d.ts +0 -0
  183. /package/dist/{shared → cjs/shared}/tiny-big/helpers.d.ts +0 -0
  184. /package/dist/{shared → cjs/shared}/tiny-big/tiny-big.d.ts +0 -0
  185. /package/dist/{shared → cjs/shared}/validate-type.d.ts +0 -0
  186. /package/dist/{types → cjs/types}/Block.types.d.ts +0 -0
  187. /package/dist/{types → cjs/types}/Contract.types.d.ts +0 -0
  188. /package/dist/{types → cjs/types}/FeeData.types.d.ts +0 -0
  189. /package/dist/{types → cjs/types}/Filter.types.d.ts +0 -0
  190. /package/dist/{types → cjs/types}/Network.types.d.ts +0 -0
  191. /package/dist/{types → cjs/types}/Transaction.types.d.ts +0 -0
  192. /package/dist/{utils → cjs/utils}/bytes.d.ts +0 -0
  193. /package/dist/{utils → cjs/utils}/compute-address.d.ts +0 -0
  194. /package/dist/{utils → cjs/utils}/compute-public-key.d.ts +0 -0
  195. /package/dist/{utils → cjs/utils}/ether-to-gwei.d.ts +0 -0
  196. /package/dist/{utils → cjs/utils}/ether-to-wei.d.ts +0 -0
  197. /package/dist/{utils → cjs/utils}/gwei-to-ether.d.ts +0 -0
  198. /package/dist/{utils → cjs/utils}/hash-message.d.ts +0 -0
  199. /package/dist/{utils → cjs/utils}/is-address.d.ts +0 -0
  200. /package/dist/{utils → cjs/utils}/keccak256.d.ts +0 -0
  201. /package/dist/{utils → cjs/utils}/solidity-keccak256.d.ts +0 -0
  202. /package/dist/{utils → cjs/utils}/split-signature.d.ts +0 -0
  203. /package/dist/{utils → cjs/utils}/to-checksum-address.d.ts +0 -0
  204. /package/dist/{utils → cjs/utils}/to-utf8-bytes.d.ts +0 -0
  205. /package/dist/{utils → cjs/utils}/wei-to-ether.d.ts +0 -0
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.logger = void 0;
4
+ const package_version_1 = require("./package-version");
5
+ class Logger {
6
+ packageVersion;
7
+ constructor() {
8
+ this.packageVersion = package_version_1.version;
9
+ }
10
+ throwError(message, args) {
11
+ const argsLength = Object.keys(args).length;
12
+ throw new Error(`${message} (${Object.entries(args).map(([key, value], index) => `${key}=${value}${index < argsLength - 1 && ', '}`)}, version=essential-eth@${this.packageVersion})`);
13
+ }
14
+ throwArgumentError(message, arg, value) {
15
+ throw new Error(`${message} (argument="${arg}" value=${value}, version=essential-eth@${this.packageVersion})`);
16
+ }
17
+ checkSafeUint53(value, message = 'value not safe') {
18
+ if (typeof value !== 'number') {
19
+ return;
20
+ }
21
+ if (value < 0 || value >= 0x1fffffffffffff) {
22
+ this.throwError(message, {
23
+ operation: 'checkSafeInteger',
24
+ fault: 'out-of-safe-range',
25
+ value: value,
26
+ });
27
+ }
28
+ if (value % 1) {
29
+ this.throwError(message, {
30
+ operation: 'checkSafeInteger',
31
+ fault: 'non-integer',
32
+ value: value,
33
+ });
34
+ }
35
+ }
36
+ }
37
+ exports.logger = new Logger();
@@ -0,0 +1 @@
1
+ export declare const version = "0.9.2-beta.2";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.version = void 0;
4
+ // Generated by genversion.
5
+ exports.version = '0.9.2-beta.2';
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AlchemyProvider = void 0;
4
+ const JsonRpcProvider_1 = require("./JsonRpcProvider");
5
+ class AlchemyProvider extends JsonRpcProvider_1.JsonRpcProvider {
6
+ constructor(apiKey, network = 'mainnet') {
7
+ const alchemyUrl = `https://eth-${network}.alchemyapi.io/v2/${apiKey}`;
8
+ super(alchemyUrl);
9
+ }
10
+ }
11
+ exports.AlchemyProvider = AlchemyProvider;
@@ -0,0 +1,514 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BaseProvider = void 0;
7
+ const clean_block_1 = require("../classes/utils/clean-block");
8
+ const clean_log_1 = require("../classes/utils/clean-log");
9
+ const clean_transaction_1 = require("../classes/utils/clean-transaction");
10
+ const clean_transaction_receipt_1 = require("../classes/utils/clean-transaction-receipt");
11
+ const fetchers_1 = require("../classes/utils/fetchers");
12
+ const hex_to_decimal_1 = require("../classes/utils/hex-to-decimal");
13
+ const prepare_transaction_1 = require("../classes/utils/prepare-transaction");
14
+ const logger_1 = require("../logger/logger");
15
+ const tiny_big_1 = require("../shared/tiny-big/tiny-big");
16
+ const chains_info_1 = __importDefault(require("./utils/chains-info"));
17
+ /**
18
+ * Converts a block tag into the right format when needed.
19
+ *
20
+ * * No equivalent in ethers.js
21
+ * * No equivalent in web3.js
22
+ *
23
+ * @internal
24
+ * @param blockTag the block tag to convert/return as a hex string
25
+ * @returns the specified block tag formatted as a hex string
26
+ * @example
27
+ * ```javascript
28
+ * prepBlockTag(14848183);
29
+ * // '0xe290b7'
30
+ * ```
31
+ * @example
32
+ * ```javascript
33
+ * prepBlockTag('0xe290b7');
34
+ * // '0xe290b7'
35
+ * ```
36
+ */
37
+ function prepBlockTag(blockTag) {
38
+ return typeof blockTag === 'number'
39
+ ? (0, tiny_big_1.tinyBig)(blockTag).toHexString()
40
+ : blockTag;
41
+ }
42
+ class BaseProvider {
43
+ /**
44
+ * @ignore
45
+ */
46
+ _rpcUrls = [];
47
+ /**
48
+ * @ignore
49
+ */
50
+ _post = (body) => (0, fetchers_1.post)(this.selectRpcUrl(), body);
51
+ /**
52
+ * @param rpcUrls The URL(s) to your Eth node(s). Consider POKT or Infura
53
+ * @example
54
+ * `https://free-eth-node.com/api/eth`
55
+ * @example
56
+ * `https://mainnet.infura.io/v3/YOUR-PROJECT-ID`
57
+ */
58
+ constructor(rpcUrls) {
59
+ this._rpcUrls = rpcUrls;
60
+ }
61
+ /**
62
+ * Gets information (name, chainId, and ensAddress when applicable) about the network the provider is connected to.
63
+ *
64
+ * * [Identical](/docs/api#isd) to [`ethers.provider.getNetwork`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getNetwork) in ethers.js
65
+ * * [Similar](/docs/api#isd) to [`web3.eth.getChainId`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getchainid) in web3.js, returns more than just the `chainId`
66
+ *
67
+ * @returns information about the network this provider is currently connected to
68
+ * @example
69
+ * ```javascript
70
+ * jsonRpcProvider('https://free-eth-node.com/api/eth').getNetwork();
71
+ * // { chainId: 1, name: 'eth', ensAddress: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e' }
72
+ * ```
73
+ * @example
74
+ * ```javascript
75
+ * jsonRpcProvider('https://free-eth-node.com/api/MATIC').getNetwork();
76
+ * // { chainId: 137, name: 'MATIC', ensAddress: null }
77
+ * ```
78
+ */
79
+ async getNetwork() {
80
+ const hexChainId = (await this.post((0, fetchers_1.buildRPCPostBody)('eth_chainId', [])));
81
+ const chainId = (0, hex_to_decimal_1.hexToDecimal)(hexChainId);
82
+ const info = chains_info_1.default[chainId];
83
+ return {
84
+ chainId: Number(chainId),
85
+ name: info[0] || 'unknown',
86
+ ensAddress: info[1] || null, // only send ensAddress if it exists
87
+ };
88
+ }
89
+ /**
90
+ * Gets the number of the most recently mined block on the network the provider is connected to.
91
+ *
92
+ * * [Identical](/docs/api#isd) to [`ethers.provider.getBlockNumber`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getBlockNumber) in ethers.js
93
+ * * [Identical](/docs/api#isd) to [`web3.eth.getBlockNumber`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getblocknumber) in web3.js
94
+ *
95
+ * @returns the number of the most recently mined block
96
+ * @example
97
+ * ```javascript
98
+ * await provider.getBlockNumber();
99
+ * // 1053312
100
+ * ```
101
+ */
102
+ async getBlockNumber() {
103
+ const currentBlockNumber = (await this.post((0, fetchers_1.buildRPCPostBody)('eth_blockNumber', [])));
104
+ return Number((0, hex_to_decimal_1.hexToDecimal)(currentBlockNumber));
105
+ }
106
+ /**
107
+ * Gets information about a specified transaction, even if it hasn't been mined yet.
108
+ *
109
+ * * [Similar](/docs/api#isd) to [`ethers.provider.getTransaction`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getTransaction) in ethers.js, does not have `wait` method that waits until the transaction has been mined
110
+ * * [Similar](/docs/api#isd) to [`web3.eth.getTransaction`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#gettransaction) in web3.js, some information returned using different types
111
+ *
112
+ * @param transactionHash the hash of the transaction to get information about
113
+ * @returns information about the specified transaction
114
+ * @example
115
+ * ```javascript
116
+ * await provider.getTransaction('0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789');
117
+ * // {
118
+ * // accessList: [],
119
+ * // blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
120
+ * // blockNumber: 14578286,
121
+ * // chainId: 1,
122
+ * // from: "0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4",
123
+ * // gas: { TinyBig: 112163 },
124
+ * // gasPrice: { TinyBig: 48592426858 },
125
+ * // hash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
126
+ * // input: "0x83259f17000000000000000000000000000000000000000000...",
127
+ * // maxFeePerGas: { TinyBig: 67681261618 },
128
+ * // maxPriorityFeePerGas: { TinyBig: 1500000000 },
129
+ * // nonce: { TinyBig: 129 },
130
+ * // r: "0x59a7c15b12c18cd68d6c440963d959bff3e73831ffc938e75ecad07f7ee43fbc",
131
+ * // s: "0x1ebaf05f0d9273b16c2a7748b150a79d22533a8cd74552611cbe620fee3dcf1c",
132
+ * // to: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
133
+ * // transactionIndex: 29,
134
+ * // type: 2,
135
+ * // v: 0,
136
+ * // value: { TinyBig: 0 },
137
+ * // confirmations: 298140,
138
+ * // }
139
+ * ```
140
+ */
141
+ async getTransaction(transactionHash) {
142
+ const [rpcTransaction, blockNumber] = await Promise.all([
143
+ this.post((0, fetchers_1.buildRPCPostBody)('eth_getTransactionByHash', [transactionHash])),
144
+ this.getBlock('latest'),
145
+ ]);
146
+ const cleanedTransaction = (0, clean_transaction_1.cleanTransaction)(rpcTransaction);
147
+ // https://ethereum.stackexchange.com/questions/2881/how-to-get-the-transaction-confirmations-using-the-json-rpc
148
+ cleanedTransaction.confirmations =
149
+ blockNumber.number - cleanedTransaction.blockNumber + 1;
150
+ return cleanedTransaction;
151
+ }
152
+ /**
153
+ * Gives information about a transaction that has already been mined. Includes additional information beyond what's provided by [`getTransaction`](/docs/api/modules#gettransaction).
154
+ *
155
+ * * [Identical](/docs/api#isd) to [`ethers.provider.getTransactionReceipt`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getTransactionReceipt) in ethers.js
156
+ * * [Similar](/docs/api#isd) to [`web3.eth.getTransactionReceipt`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#gettransactionreceipt) in web3.js, some information returned using different types
157
+ *
158
+ * @param transactionHash the hash of the transaction to get information about
159
+ * @returns information about the specified transaction that has already been mined
160
+ * @example
161
+ * ```javascript
162
+ * await provider.getTransactionReceipt('0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789');
163
+ * // {
164
+ * // blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
165
+ * // blockNumber: 14578286,
166
+ * // contractAddress: null,
167
+ * // cumulativeGasUsed: { TinyBig: 3067973 },
168
+ * // effectiveGasPrice: { TinyBig: 48592426858 },
169
+ * // from: "0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4",
170
+ * // gasUsed: { TinyBig: 112163 },
171
+ * // logs: [
172
+ * // {
173
+ * // address: "0x0eDF9bc41Bbc1354c70e2107F80C42caE7FBBcA8",
174
+ * // blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
175
+ * // blockNumber: 14578286,
176
+ * // data: "0x0000000000000000000000000000000000000000000003a12ec797b5484968c1",
177
+ * // logIndex: 42,
178
+ * // topics: [
179
+ * // "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
180
+ * // "0x00000000000000000000000039b72d136ba3e4cef35f48cd09587ffab754dd8b",
181
+ * // "0x000000000000000000000000dfd9de5f6fa60bd70636c0900752e93a6144aed4",
182
+ * // ],
183
+ * // transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
184
+ * // transactionIndex: 29,
185
+ * // },
186
+ * // {
187
+ * // address: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
188
+ * // blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
189
+ * // blockNumber: 14578286,
190
+ * // data: "0x0000000000000000000000000000000000000000000003a12ec797b5484968c1",
191
+ * // logIndex: 43,
192
+ * // topics: [
193
+ * // "0x34fcbac0073d7c3d388e51312faf357774904998eeb8fca628b9e6f65ee1cbf7",
194
+ * // "0x000000000000000000000000dfd9de5f6fa60bd70636c0900752e93a6144aed4",
195
+ * // "0x0000000000000000000000000000000000000000000000000000000000000003",
196
+ * // ],
197
+ * // transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
198
+ * // transactionIndex: 29,
199
+ * // },
200
+ * // ],
201
+ * // logsBloom: "0x00000000000000000000000000000...",
202
+ * // status: 1,
203
+ * // to: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
204
+ * // transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
205
+ * // transactionIndex: 29,
206
+ * // type: 2,
207
+ * // byzantium: true,
208
+ * // confirmations: 298171,
209
+ * // }
210
+ * ```
211
+ */
212
+ async getTransactionReceipt(transactionHash) {
213
+ const [rpcTransaction, blockNumber] = await Promise.all([
214
+ this.post((0, fetchers_1.buildRPCPostBody)('eth_getTransactionReceipt', [transactionHash])),
215
+ this.getBlock('latest'),
216
+ ]);
217
+ const cleanedTransactionReceipt = (0, clean_transaction_receipt_1.cleanTransactionReceipt)(rpcTransaction);
218
+ cleanedTransactionReceipt.confirmations =
219
+ blockNumber.number - cleanedTransactionReceipt.blockNumber + 1;
220
+ return cleanedTransactionReceipt;
221
+ }
222
+ /**
223
+ * Returns the number of sent transactions by an address, from genesis (or as far back as a provider looks) up to specified block.
224
+ *
225
+ * * [Identical](/docs/api#isd) to [`ethers.provider.getTransactionCount`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getTransactionCount) in ethers.js
226
+ * * [Identical](/docs/api#isd) to [`web3.eth.getTransactionCount`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#gettransactioncount) in web3.js
227
+ *
228
+ * @param address the address to count number of sent transactions
229
+ * @param blockTag the block to count transactions up to, inclusive
230
+ * @returns the number of transactions sent by the specified address
231
+ * @example
232
+ * ```javascript
233
+ * await provider.getTransactionCount('0x71660c4005ba85c37ccec55d0c4493e66fe775d3');
234
+ * // 1060000
235
+ * ```
236
+ * @example
237
+ * ```javascript
238
+ * await provider.getTransactionCount('0x71660c4005ba85c37ccec55d0c4493e66fe775d3', 'latest');
239
+ * // 1060000
240
+ * ```
241
+ * @example
242
+ * ```javascript
243
+ * await provider.getTransactionCount('0x71660c4005ba85c37ccec55d0c4493e66fe775d3', 14649390);
244
+ * // 1053312
245
+ * ```
246
+ */
247
+ async getTransactionCount(address, blockTag = 'latest') {
248
+ blockTag = prepBlockTag(blockTag);
249
+ const transactionCount = (await this.post((0, fetchers_1.buildRPCPostBody)('eth_getTransactionCount', [address, blockTag])));
250
+ return Number((0, hex_to_decimal_1.hexToDecimal)(transactionCount));
251
+ }
252
+ /**
253
+ * Gets information about a certain block, optionally with full transaction objects.
254
+ *
255
+ * * [Similar](/docs/api#isd) to [`ethers.provider.getBlock`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getLogs) in ethers.js, includes some additional information. Can also return block with full transaction objects, similar to [`ethers.providers.getBlockWithTransactions`]
256
+ * * [Identical](/docs/api#isd) to [`web3.eth.getBlock`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getpastlogs) in web3.js
257
+ *
258
+ * @param timeFrame The number, hash, or text-based description ('latest', 'earliest', or 'pending') of the block to collect information on.
259
+ * @param returnTransactionObjects Whether to also return data about the transactions on the block.
260
+ * @returns A BlockResponse object with information about the specified block
261
+ * @example
262
+ * ```javascript
263
+ * await provider.getBlock(14879862);
264
+ * // {
265
+ * // baseFeePerGas: { TinyBig: 39095728776 },
266
+ * // difficulty: { TinyBig: 14321294455359973 },
267
+ * // extraData: "0x486976656f6e2073672d6865617679",
268
+ * // gasLimit: { TinyBig: 29970620 },
269
+ * // gasUsed: { TinyBig: 20951384 },
270
+ * // hash: "0x563b458ec3c4f87393b53f70bdddc0058497109b784d8cacd9247ddf267049ab",
271
+ * // logsBloom:
272
+ * // "0x9f38794fe80b521794df6efad8b0d2e9582f9ec3959a3f9384bda0fa371cfa5fac5af9d515c6bdf1ec325f5b5f7ebdd6a3a9fae17b38a86d4dc4b0971afc68d8086640550f4c156e6f923f4a1bb94fb0bed6cdcc474c5c64bfeff7a4a906f72b9a7b94004ee58efc53d63ac66961acd3a431b2d896cc9fd75f6072960bced45f770587caf130f57504decfcb63c6ca8fbc5bdbd749edd5a99a7375d2b81872289adb775fb3c928259f4be39c6d3f4d5b6217822979bb88c1f1fb62429b1b6d41cf4e3f77f9e1db3f5723108f1e5b1255dd734ad8cdb11e7ea22487c788e67c83777b6f395e504ca59c64f52245ee6de3804cf809e5caa4f0ea6a9aa9eb6ed801",
273
+ * // miner: "0x1aD91ee08f21bE3dE0BA2ba6918E714dA6B45836",
274
+ * // mixHash: "0x73cc9419bfb89c9d41c3a8c34ce56b5ebe468bdcf870258d2e77262275d580ec",
275
+ * // nonce: "0x976f3f5d596ffb08",
276
+ * // number: 14879862,
277
+ * // parentHash: "0x95986ae14a71face8d9a6a379edd875b2e8bc73e4de0d9d460e7752bddb0f579",
278
+ * // receiptsRoot: "0x8e6ba2fd9bee602b653dae6e3132f16538c2c5df24f1df8c000392053f73defa",
279
+ * // sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
280
+ * // size: { TinyBig: 134483 },
281
+ * // stateRoot: "0xbf2bb67bd1c741f3d00904b8451d7c2cf4e3a2726f5a5884792ede2074747b85",
282
+ * // timestamp: { TinyBig: 1654016186 },
283
+ * // totalDifficulty: { TinyBig: 50478104614257705213748 },
284
+ * // transactions: [
285
+ * // "0xb3326a9149809603a2c28545e50e4f7d16e194bf5ee9764e0544603854c4a8d2",
286
+ * // "0x8b42095f8d335404a4896b2817b8e5e3d86a5a87cb434a8eec295d5280a7f48e",
287
+ * // "0x882f78fcb73f0f7ad0700bb0424a8b4beb366aaa93b88a3562c49a8d0ce4dcff",
288
+ * // ...
289
+ * // ],
290
+ * // transactionsRoot: "0x5934902f3dcc263ec34f24318179bf6301f53f4834685792066026f3a4849d72",
291
+ * // uncles: [],
292
+ * // }
293
+ * ```
294
+ */
295
+ async getBlock(timeFrame = 'latest', returnTransactionObjects = false) {
296
+ let type = 'Number';
297
+ if (typeof timeFrame === 'string' && timeFrame.length === 66) {
298
+ // use endpoint that accepts string
299
+ type = 'Hash';
300
+ }
301
+ else {
302
+ timeFrame = prepBlockTag(timeFrame);
303
+ }
304
+ const rpcBlock = (await this.post((0, fetchers_1.buildRPCPostBody)(`eth_getBlockBy${type}`, [
305
+ timeFrame,
306
+ returnTransactionObjects,
307
+ ])));
308
+ return (0, clean_block_1.cleanBlock)(rpcBlock, returnTransactionObjects);
309
+ }
310
+ /**
311
+ * Gives an estimate of the current gas price in wei.
312
+ *
313
+ * * [Similar](/docs/api#isd) to [`ethers.provider.getGasPrice`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getGasPrice) in ethers.js, does not have a parameter specifying what unit you'd like to return. See also [`weiToEther`](/docs/api/modules#weitoether) and [`etherToGwei`](/docs/api/modules#ethertogwei)
314
+ * * [Identical](/docs/api#isd) to [`web3.eth.getGasPrice`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getgasprice) in web3.js, returns a number (TinyBig) instead of a string
315
+ *
316
+ * @returns an estimate of the current gas price in wei
317
+ * @example
318
+ * ```javascript
319
+ * await provider.getGasPrice();
320
+ * // 52493941856
321
+ * ```
322
+ */
323
+ async getGasPrice() {
324
+ const hexGasPrice = (await this.post((0, fetchers_1.buildRPCPostBody)('eth_gasPrice', [])));
325
+ return (0, tiny_big_1.tinyBig)((0, hex_to_decimal_1.hexToDecimal)(hexGasPrice));
326
+ }
327
+ /**
328
+ * Returns the balance of the account in wei.
329
+ *
330
+ * * [Identical](/docs/api#isd) to [`ethers.provider.getBalance`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getBalance) in ethers.js
331
+ * * [Identical](/docs/api#isd) to [`web3.eth.getBalance`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getbalance) in web3.js, returns a number (TinyBig) instead of a string
332
+ *
333
+ * @param address the address to check the balance of
334
+ * @param blockTag the block to check the specified address' balance on
335
+ * @returns the balance of the network's native token for the specified address on the specified block
336
+ * @example
337
+ * ```javascript
338
+ * await provider.getBalance('0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8');
339
+ * // 28798127851528138
340
+ * ```
341
+ */
342
+ async getBalance(address, blockTag = 'latest') {
343
+ blockTag = prepBlockTag(blockTag);
344
+ const hexBalance = (await this.post((0, fetchers_1.buildRPCPostBody)('eth_getBalance', [address, blockTag])));
345
+ return (0, tiny_big_1.tinyBig)((0, hex_to_decimal_1.hexToDecimal)(hexBalance));
346
+ }
347
+ /**
348
+ * Gets the code of a contract on a specified block.
349
+ *
350
+ * * [Identical](/docs/api#isd) to [`ethers.provider.getCode`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getCode) in ethers.js
351
+ * * [Identical](/docs/api#isd) to [`web3.eth.getCode`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getcode) in web3.js
352
+ *
353
+ * @param address the contract address to get the contract code from
354
+ * @param blockTag the block height to search for the contract code from. Contract code can change, so this allows for checking a specific block
355
+ * @returns the contract creation code for the specified address at the specified block height
356
+ * @example
357
+ * ```javascript
358
+ * await jsonRpcProvider().getCode('0xaC6095720221C79C6E7C638d260A2eFBC5D8d880', 'latest');
359
+ * // '0x608060405234801561001057600080fd5b506004361061...'
360
+ * ```
361
+ */
362
+ async getCode(address, blockTag = 'latest') {
363
+ blockTag = prepBlockTag(blockTag);
364
+ const contractCode = (await this.post((0, fetchers_1.buildRPCPostBody)('eth_getCode', [address, blockTag])));
365
+ return contractCode;
366
+ }
367
+ /**
368
+ * Returns an estimate of the amount of gas that would be required to submit transaction to the network.
369
+ * An estimate may not be accurate since there could be another transaction on the network that was not accounted for.
370
+ *
371
+ * * [Identical](/docs/api#isd) to [`ethers.provider.estimateGas`](https://docs.ethers.io/v5/api/providers/provider/#Provider-estimateGas) in ethers.js
372
+ * * [Identical](/docs/api#isd) to [`web3.eth.estimateGas`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#estimateGas) in web3.js
373
+ *
374
+ * @param transaction the transaction to check the estimated gas cost for
375
+ * @returns the estimated amount of gas charged for submitting the specified transaction to the blockchain
376
+ * @example
377
+ * ```javascript
378
+ * await provider.estimateGas({
379
+ * // Wrapped ETH address
380
+ * to: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
381
+ * data: "0xd0e30db0",
382
+ * value: etherToWei('1.0').toHexString(),
383
+ * });
384
+ * // { TinyBig: "27938" }
385
+ * ```
386
+ */
387
+ async estimateGas(transaction) {
388
+ const rpcTransaction = (0, prepare_transaction_1.prepareTransaction)(transaction);
389
+ const gasUsed = (await this.post((0, fetchers_1.buildRPCPostBody)('eth_estimateGas', [rpcTransaction])));
390
+ return (0, tiny_big_1.tinyBig)((0, hex_to_decimal_1.hexToDecimal)(gasUsed));
391
+ }
392
+ /**
393
+ * Returns the current recommended FeeData to use in a transaction.
394
+ * For an EIP-1559 transaction, the maxFeePerGas and maxPriorityFeePerGas should be used.
395
+ * For legacy transactions and networks which do not support EIP-1559, the gasPrice should be used.Returns an estimate of the amount of gas that would be required to submit transaction to the network.
396
+ *
397
+ * * [Identical](/docs/api#isd) to [`ethers.provider.getFeeData`](https://docs.ethers.org/v5/api/providers/provider/#Provider-getFeeData) in ethers.js
398
+ *
399
+ * @returns an object with gas estimates for the network currently
400
+ * @example
401
+ * ```javascript
402
+ * await provider.getFeeData();
403
+ * // {
404
+ * // gasPrice: { TinyBig: "14184772639" },
405
+ * // lastBaseFeePerGas: { TinyBig: "14038523098" },
406
+ * // maxFeePerGas: { TinyBig: "29577046196" },
407
+ * // maxPriorityFeePerGas: { TinyBig: "1500000000" }
408
+ * // }
409
+ * ```
410
+ */
411
+ async getFeeData() {
412
+ const [block, gasPrice] = await Promise.all([
413
+ this.getBlock('latest'),
414
+ this.getGasPrice(),
415
+ ]);
416
+ let lastBaseFeePerGas = null, maxFeePerGas = null, maxPriorityFeePerGas = null;
417
+ if (block && block.baseFeePerGas) {
418
+ // We may want to compute this more accurately in the future,
419
+ // using the formula "check if the base fee is correct".
420
+ // See: https://eips.ethereum.org/EIPS/eip-1559
421
+ lastBaseFeePerGas = block.baseFeePerGas;
422
+ maxPriorityFeePerGas = (0, tiny_big_1.tinyBig)('1500000000');
423
+ maxFeePerGas = (0, tiny_big_1.tinyBig)(block.baseFeePerGas.mul(2).add(maxPriorityFeePerGas));
424
+ }
425
+ return { lastBaseFeePerGas, maxFeePerGas, maxPriorityFeePerGas, gasPrice };
426
+ }
427
+ /**
428
+ * Returns transaction receipt event logs that match a specified filter.
429
+ * May return `[]` if parameters are too broad, even if logs exist.
430
+ *
431
+ * * [Identical](/docs/api#isd) to [`ethers.provider.getLogs`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getLogs) in ethers.js
432
+ * * [Identical](/docs/api#isd) to [`web3.eth.getPastLogs`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getpastlogs) in web3.js
433
+ *
434
+ * @param filter parameters to filter the logs by
435
+ * @returns an array of logs matching the specified filter
436
+ * @example
437
+ * ```javascript
438
+ * provider.getLogs({
439
+ * address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
440
+ * topics: [
441
+ * "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
442
+ * "0x00000000000000000000000021b8065d10f73ee2e260e5b47d3344d3ced7596e",
443
+ * ],
444
+ * fromBlock: 14825027,
445
+ * toBlock: 14825039,
446
+ * });
447
+ *
448
+ * [
449
+ * {
450
+ * address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
451
+ * blockHash: '0x8e0dfac2f704851960f866c8708b3bef2f66c0fee0329cf25ff0261b264ca6bc',
452
+ * blockNumber: 14825029,
453
+ * data: '0x000000000000000000000000000000000000000000000000005f862ee352a38a',
454
+ * logIndex: 384,
455
+ * removed: false,
456
+ * topics: [
457
+ * '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
458
+ * '0x00000000000000000000000021b8065d10f73ee2e260e5b47d3344d3ced7596e',
459
+ * '0x00000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45'
460
+ * ],
461
+ * transactionHash: '0xbd49031be16f8fd1775f4e0fe79b408ffd8ae9c65b2827ee47e3238e3f51f4c0',
462
+ * transactionIndex: 226
463
+ * }
464
+ * ]
465
+ * ```
466
+ */
467
+ async getLogs(filter) {
468
+ const filterByRange = filter;
469
+ if (filterByRange.fromBlock)
470
+ filterByRange.fromBlock = prepBlockTag(filterByRange.fromBlock);
471
+ if (filterByRange.toBlock)
472
+ filterByRange.toBlock = prepBlockTag(filterByRange.toBlock);
473
+ const rpcLogs = (await this.post((0, fetchers_1.buildRPCPostBody)('eth_getLogs', [filter])));
474
+ const logs = rpcLogs.map((log) => (0, clean_log_1.cleanLog)(log, false));
475
+ return logs;
476
+ }
477
+ /**
478
+ * Returns the result of adding a transaction to the blockchain without actually adding that transaction to the blockchain.
479
+ * Does not require any ether as gas.
480
+ *
481
+ * * [Identical](/docs/api#isd) to [`ethers.provider.call`](https://docs.ethers.io/v5/api/providers/provider/#Provider-call) in ethers.js
482
+ * * [Identical](/docs/api#isd) to [`web3.eth.call`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#call) in web3.js
483
+ *
484
+ * @param transaction the transaction object to, in theory, execute. Doesn't actually get added to the blockchain.
485
+ * @param blockTag the block to execute this transaction on
486
+ * @returns the result of executing the transaction on the specified block
487
+ * @example
488
+ * ```javascript
489
+ * await provider.call({ to: "0x6b175474e89094c44da98b954eedeac495271d0f", data: "0x70a082310000000000000000000000006E0d01A76C3Cf4288372a29124A26D4353EE51BE" });
490
+ * // '0x0000000000000000000000000000000000000000000000000858898f93629000'
491
+ * ```
492
+ */
493
+ async call(transaction, blockTag = 'latest') {
494
+ if (transaction.gasPrice &&
495
+ (transaction.maxPriorityFeePerGas || transaction.maxFeePerGas)) {
496
+ logger_1.logger.throwError('Cannot specify both "gasPrice" and ("maxPriorityFeePerGas" or "maxFeePerGas")', {
497
+ gasPrice: transaction.gasPrice,
498
+ maxFeePerGas: transaction.maxFeePerGas,
499
+ maxPriorityFeePerGas: transaction.maxPriorityFeePerGas,
500
+ });
501
+ }
502
+ if (transaction.maxFeePerGas && transaction.maxPriorityFeePerGas) {
503
+ logger_1.logger.throwError('Cannot specify both "maxFeePerGas" and "maxPriorityFeePerGas"', {
504
+ maxFeePerGas: transaction.maxFeePerGas,
505
+ maxPriorityFeePerGas: transaction.maxPriorityFeePerGas,
506
+ });
507
+ }
508
+ blockTag = prepBlockTag(blockTag);
509
+ const rpcTransaction = (0, prepare_transaction_1.prepareTransaction)(transaction);
510
+ const transactionRes = (await this.post((0, fetchers_1.buildRPCPostBody)('eth_call', [rpcTransaction, blockTag])));
511
+ return transactionRes;
512
+ }
513
+ }
514
+ exports.BaseProvider = BaseProvider;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FallthroughProvider = void 0;
4
+ const logger_1 = require("../logger/logger");
5
+ const BaseProvider_1 = require("./BaseProvider");
6
+ // https://advancedweb.hu/how-to-add-timeout-to-a-promise-in-javascript/
7
+ const promiseTimeout = (prom, time) => new Promise((resolve, reject) => {
8
+ const timeout = setTimeout(() => reject(new Error('Promise timed out')), time);
9
+ prom
10
+ .then((result) => {
11
+ clearTimeout(timeout);
12
+ resolve(result);
13
+ })
14
+ .catch((error) => {
15
+ clearTimeout(timeout);
16
+ reject(error);
17
+ });
18
+ });
19
+ const DEFAULT_TIMEOUT_DURATION = 8000;
20
+ /**
21
+ * @beta
22
+ * A JSON RPC Provider which moves to the next URL when one fails.
23
+ */
24
+ class FallthroughProvider extends BaseProvider_1.BaseProvider {
25
+ // index of current trusted rpc url
26
+ /**
27
+ * @ignore
28
+ */
29
+ rpcUrlCounter = 0;
30
+ timeoutDuration;
31
+ /**
32
+ * @ignore
33
+ */
34
+ selectRpcUrl() {
35
+ return this._rpcUrls[this.rpcUrlCounter];
36
+ }
37
+ constructor(rpcUrls, options = {}) {
38
+ if (!Array.isArray(rpcUrls)) {
39
+ logger_1.logger.throwError('Array required', { rpcUrls });
40
+ }
41
+ if (rpcUrls.length <= 1) {
42
+ logger_1.logger.throwError('More than one rpcUrl is required', { rpcUrls });
43
+ }
44
+ super(rpcUrls);
45
+ this.timeoutDuration = options.timeoutDuration || DEFAULT_TIMEOUT_DURATION;
46
+ }
47
+ /**
48
+ * @ignore
49
+ */
50
+ post = (body) => {
51
+ // while failing post, add to rpcUrlCounter and post again
52
+ const genesisCount = this.rpcUrlCounter;
53
+ const recursivePostRetry = () => {
54
+ // Times out request
55
+ const genesisRpcUrl = this.selectRpcUrl();
56
+ const res = promiseTimeout(this._post(body), this.timeoutDuration).catch((e) => {
57
+ // A mutex: Only add if no other instance has discovered this url as failing yet
58
+ if (genesisRpcUrl === this.selectRpcUrl()) {
59
+ // add one and handle array overflow
60
+ this.rpcUrlCounter =
61
+ (this.rpcUrlCounter + 1) % this._rpcUrls.length;
62
+ }
63
+ // we've already tried this rpc, throw for good
64
+ if (this.rpcUrlCounter === genesisCount) {
65
+ throw e;
66
+ }
67
+ return recursivePostRetry();
68
+ });
69
+ return res;
70
+ };
71
+ return recursivePostRetry();
72
+ };
73
+ }
74
+ exports.FallthroughProvider = FallthroughProvider;