essential-eth 0.5.0 → 0.5.4

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 (77) hide show
  1. package/lib/cjs/classes/Contract.d.ts +3 -2
  2. package/lib/cjs/classes/Contract.js +3 -2
  3. package/lib/cjs/classes/utils/clean-transaction-receipt.d.ts +5 -0
  4. package/lib/cjs/classes/utils/clean-transaction-receipt.js +55 -0
  5. package/lib/cjs/classes/utils/encode-decode-transaction.d.ts +1 -0
  6. package/lib/cjs/classes/utils/encode-decode-transaction.js +4 -4
  7. package/lib/cjs/classes/utils/fetchers.d.ts +1 -1
  8. package/lib/cjs/index.d.ts +7 -2
  9. package/lib/cjs/index.js +24 -3
  10. package/lib/cjs/logger/logger.d.ts +11 -0
  11. package/lib/cjs/logger/logger.js +36 -0
  12. package/lib/cjs/logger/package-version.d.ts +1 -0
  13. package/lib/cjs/logger/package-version.js +5 -0
  14. package/lib/cjs/providers/BaseProvider.d.ts +270 -0
  15. package/lib/cjs/providers/BaseProvider.js +361 -0
  16. package/lib/cjs/providers/FallthroughProvider.d.ts +25 -0
  17. package/lib/cjs/providers/FallthroughProvider.js +65 -0
  18. package/lib/cjs/providers/JsonRpcProvider.d.ts +4 -200
  19. package/lib/cjs/providers/JsonRpcProvider.js +11 -263
  20. package/lib/cjs/providers/test/rpc-urls.d.ts +1 -0
  21. package/lib/cjs/providers/test/rpc-urls.js +1 -0
  22. package/lib/cjs/providers/utils/chains-info.d.ts +8 -0
  23. package/lib/cjs/providers/utils/chains-info.js +24 -0
  24. package/lib/cjs/shared/tiny-big/tiny-big.d.ts +9 -2
  25. package/lib/cjs/shared/tiny-big/tiny-big.js +27 -9
  26. package/lib/cjs/types/Transaction.types.d.ts +53 -0
  27. package/lib/cjs/utils/bytes.d.ts +172 -0
  28. package/lib/cjs/utils/bytes.js +564 -0
  29. package/lib/cjs/utils/hash-message.d.ts +12 -0
  30. package/lib/cjs/utils/hash-message.js +26 -0
  31. package/lib/cjs/utils/keccak256.d.ts +2 -0
  32. package/lib/cjs/utils/keccak256.js +17 -0
  33. package/lib/cjs/utils/solidity-keccak256.d.ts +29 -0
  34. package/lib/cjs/utils/solidity-keccak256.js +118 -0
  35. package/lib/cjs/utils/to-utf8-bytes.d.ts +1 -0
  36. package/lib/cjs/utils/to-utf8-bytes.js +7 -0
  37. package/lib/esm/classes/Contract.js +1 -1
  38. package/lib/esm/classes/utils/clean-transaction-receipt.d.ts +2 -0
  39. package/lib/esm/classes/utils/clean-transaction-receipt.js +48 -0
  40. package/lib/esm/classes/utils/encode-decode-transaction.d.ts +1 -0
  41. package/lib/esm/classes/utils/encode-decode-transaction.js +2 -2
  42. package/lib/esm/classes/utils/fetchers.d.ts +1 -1
  43. package/lib/esm/index.d.ts +7 -2
  44. package/lib/esm/index.js +7 -2
  45. package/lib/esm/logger/logger.d.ts +11 -0
  46. package/lib/esm/logger/logger.js +33 -0
  47. package/lib/esm/logger/package-version.d.ts +1 -0
  48. package/lib/esm/logger/package-version.js +1 -0
  49. package/lib/esm/providers/BaseProvider.d.ts +18 -0
  50. package/lib/esm/providers/BaseProvider.js +101 -0
  51. package/lib/esm/providers/FallthroughProvider.d.ts +12 -0
  52. package/lib/esm/providers/FallthroughProvider.js +41 -0
  53. package/lib/esm/providers/JsonRpcProvider.d.ts +4 -12
  54. package/lib/esm/providers/JsonRpcProvider.js +8 -69
  55. package/lib/esm/providers/test/rpc-urls.d.ts +1 -0
  56. package/lib/esm/providers/test/rpc-urls.js +1 -0
  57. package/lib/esm/providers/utils/chains-info.d.ts +8 -0
  58. package/lib/esm/providers/utils/chains-info.js +24 -0
  59. package/lib/esm/shared/tiny-big/tiny-big.d.ts +2 -0
  60. package/lib/esm/shared/tiny-big/tiny-big.js +20 -7
  61. package/lib/esm/types/Transaction.types.d.ts +45 -0
  62. package/lib/esm/utils/bytes.d.ts +40 -0
  63. package/lib/esm/utils/bytes.js +245 -0
  64. package/lib/esm/utils/hash-message.d.ts +2 -0
  65. package/lib/esm/utils/hash-message.js +12 -0
  66. package/lib/esm/utils/keccak256.d.ts +2 -0
  67. package/lib/esm/utils/keccak256.js +13 -0
  68. package/lib/esm/utils/solidity-keccak256.d.ts +2 -0
  69. package/lib/esm/utils/solidity-keccak256.js +85 -0
  70. package/lib/esm/utils/to-utf8-bytes.d.ts +1 -0
  71. package/lib/esm/utils/to-utf8-bytes.js +3 -0
  72. package/package.json +19 -19
  73. package/readme.md +36 -5
  74. package/lib/cjs/utils/hex-zero-pad.d.ts +0 -32
  75. package/lib/cjs/utils/hex-zero-pad.js +0 -52
  76. package/lib/esm/utils/hex-zero-pad.d.ts +0 -1
  77. package/lib/esm/utils/hex-zero-pad.js +0 -17
@@ -0,0 +1,361 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.BaseProvider = void 0;
16
+ const clean_block_1 = require("../classes/utils/clean-block");
17
+ const clean_transaction_1 = require("../classes/utils/clean-transaction");
18
+ const clean_transaction_receipt_1 = require("../classes/utils/clean-transaction-receipt");
19
+ const fetchers_1 = require("../classes/utils/fetchers");
20
+ const hex_to_decimal_1 = require("../classes/utils/hex-to-decimal");
21
+ const tiny_big_1 = require("../shared/tiny-big/tiny-big");
22
+ const chains_info_1 = __importDefault(require("./utils/chains-info"));
23
+ class BaseProvider {
24
+ /**
25
+ * @param rpcUrl The URL to your Eth node. Consider POKT or Infura
26
+ */
27
+ constructor(rpcUrls) {
28
+ /**
29
+ * @ignore
30
+ */
31
+ this._rpcUrls = [];
32
+ /**
33
+ * @ignore
34
+ */
35
+ this._post = (body) => (0, fetchers_1.post)(this.selectRpcUrl(), body);
36
+ this._rpcUrls = rpcUrls;
37
+ }
38
+ /**
39
+ * Returns the network this provider is connected to
40
+ */
41
+ getNetwork() {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ const hexChainId = (yield this.post((0, fetchers_1.buildRPCPostBody)('eth_chainId', [])));
44
+ const chainId = (0, hex_to_decimal_1.hexToDecimal)(hexChainId);
45
+ const info = chains_info_1.default[chainId];
46
+ return {
47
+ chainId: Number(chainId),
48
+ name: info[0] || 'unknown',
49
+ ensAddress: info[1] || null, // only send ensAddress if it exists
50
+ };
51
+ });
52
+ }
53
+ /**
54
+ * * Similar to [`ethers.provider.getTransaction`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getTransaction), some information not included
55
+ *
56
+ * @returns information about one transaction
57
+ * @example
58
+ * ```js
59
+ * await provider.getTransaction('0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789');
60
+ * {
61
+ * accessList: [],
62
+ * blockHash: '0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d',
63
+ * blockNumber: 14578286,
64
+ * chainId: 1,
65
+ * from: '0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4',
66
+ * gas: Big {
67
+ * s: 1,
68
+ * e: 5,
69
+ * c: [ 1, 1, 2, 1, 6, 3 ],
70
+ * constructor: <ref *1> [Function: Big] {
71
+ * DP: 20,
72
+ * RM: 1,
73
+ * NE: -7,
74
+ * PE: 21,
75
+ * strict: false,
76
+ * roundDown: 0,
77
+ * roundHalfUp: 1,
78
+ * roundHalfEven: 2,
79
+ * roundUp: 3,
80
+ * Big: [Circular *1],
81
+ * default: [Circular *1]
82
+ * }
83
+ * },
84
+ * gasPrice: Big {
85
+ * s: 1,
86
+ * e: 10,
87
+ * c: [
88
+ * 4, 8, 5, 9, 2,
89
+ * 4, 2, 6, 8, 5,
90
+ * 8
91
+ * ],
92
+ * constructor: <ref *1> [Function: Big] {
93
+ * DP: 20,
94
+ * RM: 1,
95
+ * NE: -7,
96
+ * PE: 21,
97
+ * strict: false,
98
+ * roundDown: 0,
99
+ * roundHalfUp: 1,
100
+ * roundHalfEven: 2,
101
+ * roundUp: 3,
102
+ * Big: [Circular *1],
103
+ * default: [Circular *1]
104
+ * }
105
+ * },
106
+ * hash: '0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789',
107
+ * input: '0x83259f170000000000000000000000000000000000000000000000000000000000000080000000000000000000000000dfd9de5f6fa60bd70636c0900752e93a6144aed400000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009e99ad11a214fd016b19dc3648678c5944859ae292b21c24ca94f857836c4596f1950c82dd0c23dd621af4763edc2f66466e63c5df9de0c1107b1cd16bf460fe93e43fd308e3444bc79c3d88a4cb961dc8367ab6ad048867afc76d193bca99cf3a068864ed4a7df1dbf1d4c52238eced3e5e05644b4040fc2b3ccb8557b0e99fff6131305a0ea2b8061b90bd418db5bbdd2e92129f52d93f90531465e309c4caec5b85285822b6196398d36f16f511811b61bbda6461e80e29210cd303118bdcee8df6fa0505ffbe8642094fd2ba4dd458496fe3b459ac880bbf71877c713e969ccf5ed7efab8a84ebc07e3939901371ca427e1192e455a8f35a6a1d7ad09e1475dd1758b36fa631dab5d70e99316b23c4c43094188d360cd9c3457355904e07c00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000162074a7047f',
108
+ * maxFeePerGas: Big {
109
+ * s: 1,
110
+ * e: 10,
111
+ * c: [
112
+ * 6, 7, 6, 8, 1,
113
+ * 2, 6, 1, 6, 1,
114
+ * 8
115
+ * ],
116
+ * constructor: <ref *1> [Function: Big] {
117
+ * DP: 20,
118
+ * RM: 1,
119
+ * NE: -7,
120
+ * PE: 21,
121
+ * strict: false,
122
+ * roundDown: 0,
123
+ * roundHalfUp: 1,
124
+ * roundHalfEven: 2,
125
+ * roundUp: 3,
126
+ * Big: [Circular *1],
127
+ * default: [Circular *1]
128
+ * }
129
+ * },
130
+ * maxPriorityFeePerGas: Big {
131
+ * s: 1,
132
+ * e: 9,
133
+ * c: [ 1, 5 ],
134
+ * constructor: <ref *1> [Function: Big] {
135
+ * DP: 20,
136
+ * RM: 1,
137
+ * NE: -7,
138
+ * PE: 21,
139
+ * strict: false,
140
+ * roundDown: 0,
141
+ * roundHalfUp: 1,
142
+ * roundHalfEven: 2,
143
+ * roundUp: 3,
144
+ * Big: [Circular *1],
145
+ * default: [Circular *1]
146
+ * }
147
+ * },
148
+ * nonce: 129,
149
+ * r: '0x59a7c15b12c18cd68d6c440963d959bff3e73831ffc938e75ecad07f7ee43fbc',
150
+ * s: '0x1ebaf05f0d9273b16c2a7748b150a79d22533a8cd74552611cbe620fee3dcf1c',
151
+ * to: '0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B',
152
+ * transactionIndex: 29,
153
+ * type: 2,
154
+ * v: 0,
155
+ * value: Big {
156
+ * s: 1,
157
+ * e: 0,
158
+ * c: [ 0 ],
159
+ * constructor: <ref *1> [Function: Big] {
160
+ * DP: 20,
161
+ * RM: 1,
162
+ * NE: -7,
163
+ * PE: 21,
164
+ * strict: false,
165
+ * roundDown: 0,
166
+ * roundHalfUp: 1,
167
+ * roundHalfEven: 2,
168
+ * roundUp: 3,
169
+ * Big: [Circular *1],
170
+ * default: [Circular *1]
171
+ * }
172
+ * },
173
+ * confirmations: 1210
174
+ * }
175
+ * ```
176
+ */
177
+ getTransaction(transactionHash) {
178
+ return __awaiter(this, void 0, void 0, function* () {
179
+ const [rpcTransaction, blockNumber] = yield Promise.all([
180
+ this.post((0, fetchers_1.buildRPCPostBody)('eth_getTransactionByHash', [transactionHash])),
181
+ this.getBlock('latest'),
182
+ ]);
183
+ const cleanedTransaction = (0, clean_transaction_1.cleanTransaction)(rpcTransaction);
184
+ // https://ethereum.stackexchange.com/questions/2881/how-to-get-the-transaction-confirmations-using-the-json-rpc
185
+ cleanedTransaction.confirmations =
186
+ blockNumber.number - cleanedTransaction.blockNumber + 1;
187
+ return cleanedTransaction;
188
+ });
189
+ }
190
+ /**
191
+ * Gives information about a transaction that has already been mined. Includes additional information beyond what's provided by `getTransaction()`
192
+ *
193
+ * * Similar to [`ethers.provider.getTransactionReceipt`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getTransactionReceipt), some information not included
194
+ *
195
+ * @param transactionHash the hash of the transaction to get information about
196
+ *
197
+ */
198
+ getTransactionReceipt(transactionHash) {
199
+ return __awaiter(this, void 0, void 0, function* () {
200
+ const [rpcTransaction, blockNumber] = yield Promise.all([
201
+ this.post((0, fetchers_1.buildRPCPostBody)('eth_getTransactionReceipt', [transactionHash])),
202
+ this.getBlock('latest'),
203
+ ]);
204
+ const cleanedTransactionReceipt = (0, clean_transaction_receipt_1.cleanTransactionReceipt)(rpcTransaction);
205
+ cleanedTransactionReceipt.confirmations =
206
+ blockNumber.number - cleanedTransactionReceipt.blockNumber + 1;
207
+ return cleanedTransactionReceipt;
208
+ });
209
+ }
210
+ /**
211
+ * Returns the transaction count from genesis up to specified blockTag
212
+ *
213
+ * * Same as `ethers.provider.getTransactionCount`
214
+ * * Same as `web3.eth.getTransactionCount`
215
+ *
216
+ * @example
217
+ * ```js
218
+ * const address = '0x71660c4005ba85c37ccec55d0c4493e66fe775d3';
219
+ * await provider
220
+ * .getTransactionCount(address, 'latest')
221
+ * // 1060000
222
+ * ```
223
+ *
224
+ * @example
225
+ * ```js
226
+ * await provider
227
+ * .getTransactionCount(address)
228
+ * // 1053312
229
+ * ```
230
+ *
231
+ * @example
232
+ * ```js
233
+ * await provider
234
+ * .getTransactionCount(address, 14649390)
235
+ * // 1053312
236
+ * ```
237
+ */
238
+ getTransactionCount(address, blockTag = 'latest') {
239
+ return __awaiter(this, void 0, void 0, function* () {
240
+ if (typeof blockTag === 'number') {
241
+ blockTag = `0x${blockTag.toString(16)}`;
242
+ }
243
+ const transactionCount = (yield this.post((0, fetchers_1.buildRPCPostBody)('eth_getTransactionCount', [address, blockTag])));
244
+ return Number((0, hex_to_decimal_1.hexToDecimal)(transactionCount));
245
+ });
246
+ }
247
+ /**
248
+ * Gets information about a certain block.
249
+ * Same as `web3.eth.getBlock` and `ethers.providers.getBlock`
250
+ *
251
+ * @param timeFrame The number, hash, or text-based description ('latest', 'earliest', or 'pending') of the block to collect information on.
252
+ *
253
+ * @param returnTransactionObjects Whether to also return data about the transactions on the block.
254
+ *
255
+ * @returns A BlockResponse object with information about the specified block
256
+ *
257
+ * @example
258
+ * ```js
259
+ * await provider.getBlock(14645431);
260
+ * ```
261
+ *
262
+ * @example
263
+ * ```js
264
+ * await provider.getBlock('0x3e5cea9c2be7e0ab4b0aa04c24dafddc37571db2d2d345caf7f88b3366ece0cf');
265
+ * ```
266
+ *
267
+ * @example
268
+ * ```js
269
+ * await provider.getBlock('latest');
270
+ * {
271
+ * number: 4232826,
272
+ * hash: '0x93211a1cd17e154b183565ec685254a03f844a8e34824a46ce1bdd6753dcb669',
273
+ * parentHash: '0x1b32bfcba1bb2a57f56e166a3bb06875a1978992999dfc8828397b4c1526f472',
274
+ * sha3Uncles: '0x0fb399c67bb5a071ec8a22549223215ab76b7d4009941c9c37aa3c3936010463',
275
+ * logsBloom: '0x00000000000000000000101000000000020000000000000000000000000000000000400000010000000000000000000000000000010000000008800000000800000000200000000000000000000000000000000000000000000002000000000000000000000000000040000000000040000000000000000000000000000000000000000000000001000000000004000000000010000000000000000020000000000000000200100020000000000000000080000000000080001000000000000000000001040000000000000000008000000020010100000000200000100000000000000000000000002000000080000000020400000000002000200000000000',
276
+ * transactionsRoot: '0xc43b3f13e1fe810e34d3a26ffe465b72c7063a5c70a02de2c78e91e4d10bd9fb',
277
+ * stateRoot: '0x04d7bc816537ea7ef3a16e76c9879d29f34f99d4154273c2e98e012a31bad745',
278
+ * receiptsRoot: '0x89c6f781ceac0bd49c4d9aa9115df4a5d4dd0e0220ff7668012f15bc04222c6b',
279
+ * miner: '0x31fe561eb2c628cD32Ec52573D7c4b7E4C278Bfa',
280
+ * difficulty: '1300907486001755331049',
281
+ * totalDifficulty: '5989929395521171616186006183',
282
+ * extraData: '0xce018c495249532d62613031656132',
283
+ * size: 5416,
284
+ * gasLimit: 6800000,
285
+ * gasUsed: 202955,
286
+ * timestamp: 1649884910,
287
+ * transactions: [
288
+ * '0x6b34a59c7b9aead24fa6dad782f8a3ad84ed4a23ee09bcbf0bcf880840fbbe20',
289
+ * '0x9a3851ca24d5336c6a0d48aba2c4b4769d7a672c9b01729c5eb9924efd1b19a7',
290
+ * '0xc3ed3d198b62f2f3427ebfa3bbd0fcada4e3c0c189e4464e7eeceb403c75981e'
291
+ * ],
292
+ * uncles: [
293
+ * '0x0c567c054e98153f10d651fbbc018891c1dd9d62a9ffd998e87678803e95b6ed',
294
+ * '0xb7d69389dbfb057c6fcb4bc0582d46a2ba01170703f0dadf8cd1462b83e88753',
295
+ * '0xd5f74ccd0ad4c58b3161e8c2c507c264231e5f28925061b809c02e5e4bb6db28'
296
+ * ],
297
+ * minimumGasPrice: '0x387ee40',
298
+ * bitcoinMergedMiningHeader: '0x04000020e8567ed3d2480e15a1dd1b4335e4732ae343c037e4fd03000000000000000000ed10a8340d163d3e813bdd430f902f4e5a56828dc62313b2e23797c0be6b8516eb3e576297d8091735884f42',
299
+ * bitcoinMergedMiningCoinbaseTransaction: '0x0000000000000140e910128fda7bac502dc5e0573bbaf12de8e2524f70c22f7bd160dedcb19a2521002b6a2952534b424c4f434b3ae493303f597fa368c0ccc4f8aceabf1c315bb7c9a07605c073a89f260040967aace6a7d9',
300
+ * bitcoinMergedMiningMerkleProof: '0xdf63a3d7eb6fbcfb301311faa46e9a15b0408bb1a04e284daee86c273c1dfd65ede23f3170f806e9e0f4cef7ba6b56aa37470d9c23f96ec8e43d08b58645919c5e10bcb892897a731f8f9ce79c72dc0e390896bcd6c67bb38c0bdb72982b6cf05519968d76673572c3f3ef3a08b0ddb464863f1788f7cdbaad3fe44a8a8af576d430ac282fe28852c16df198ca96cc5f71a50695912efe1a836e8442be69e31b6d6f973da2818bce9a3a1c2d9be0671aee9a7776e398d6a03d1e178e20d84646004a3d03c0501334e629d9146aa6a01316dcbaa289df6e6c5e3090cadaddff22699cfc7ff09512fc0d65c5062f17c98561ce3c9510de210d9d654cf99f8d756ff37c9fa21e7122ee8cadb923341690845d572921425f2bd7e044558b7e07983ac4df28928028b0c13c3624dc7a965af8091b0cecc845bf7da5308c03b2c97d607f6706a599f802025894435f1d76ea4e67cc2fc4e1559f1206f559a24633de0f',
301
+ * hashForMergedMining: '0xe493303f597fa368c0ccc4f8aceabf1c315bb7c9a07605c073a89f260040967a',
302
+ * paidFees: '0xc0744dcb7a0',
303
+ * cumulativeDifficulty: '0x1190930db285269e582'
304
+ * }
305
+ *```
306
+ */
307
+ getBlock(timeFrame = 'latest', returnTransactionObjects = false) {
308
+ return __awaiter(this, void 0, void 0, function* () {
309
+ let rpcTimeFrame;
310
+ let type = 'Number';
311
+ if (typeof timeFrame === 'number') {
312
+ // exact block numbers require hex string format
313
+ rpcTimeFrame = `0x${timeFrame.toString(16)}`;
314
+ }
315
+ else if (timeFrame.startsWith('0x')) {
316
+ rpcTimeFrame = timeFrame;
317
+ // use endpoint that accepts string
318
+ type = 'Hash';
319
+ }
320
+ else {
321
+ // "latest", "earliest", "pending", or hex string require no manipulation
322
+ rpcTimeFrame = timeFrame;
323
+ }
324
+ const rpcBlock = (yield this.post((0, fetchers_1.buildRPCPostBody)(`eth_getBlockBy${type}`, [
325
+ rpcTimeFrame,
326
+ returnTransactionObjects,
327
+ ])));
328
+ return (0, clean_block_1.cleanBlock)(rpcBlock, returnTransactionObjects);
329
+ });
330
+ }
331
+ /**
332
+ * Returns the current gas price in wei as TinyBig
333
+ * Same as `ethers.provider.getGasPrice`
334
+ */
335
+ getGasPrice() {
336
+ return __awaiter(this, void 0, void 0, function* () {
337
+ const hexGasPrice = (yield this.post((0, fetchers_1.buildRPCPostBody)('eth_gasPrice', [])));
338
+ return (0, tiny_big_1.tinyBig)((0, hex_to_decimal_1.hexToDecimal)(hexGasPrice));
339
+ });
340
+ }
341
+ /**
342
+ * Returns the balance of the account in wei as TinyBig
343
+ * * Same as [`ethers.provider.getBalance`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getBalance)
344
+ * * Same as `web3.eth.getBalance`
345
+ *
346
+ * @example
347
+ * ```js
348
+ * await provider
349
+ * .getBalance('0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8')
350
+ * .then((balance) => console.log(balance.toString()));
351
+ * // "28798127851528138"
352
+ * ```
353
+ */
354
+ getBalance(address, blockTag = 'latest') {
355
+ return __awaiter(this, void 0, void 0, function* () {
356
+ const hexBalance = (yield this.post((0, fetchers_1.buildRPCPostBody)('eth_getBalance', [address, blockTag])));
357
+ return (0, tiny_big_1.tinyBig)((0, hex_to_decimal_1.hexToDecimal)(hexBalance));
358
+ });
359
+ }
360
+ }
361
+ exports.BaseProvider = BaseProvider;
@@ -0,0 +1,25 @@
1
+ import { BaseProvider } from './BaseProvider';
2
+ interface ConstructorOptions {
3
+ timeoutDuration?: number;
4
+ }
5
+ /**
6
+ * @beta
7
+ * A JSON RPC Provider which moves to the next URL when one fails.
8
+ */
9
+ export declare class FallthroughProvider extends BaseProvider {
10
+ /**
11
+ * @ignore
12
+ */
13
+ private rpcUrlCounter;
14
+ private readonly timeoutDuration;
15
+ /**
16
+ * @ignore
17
+ */
18
+ selectRpcUrl(): string;
19
+ constructor(rpcUrls: string[], options?: ConstructorOptions);
20
+ /**
21
+ * @ignore
22
+ */
23
+ post: (body: Record<string, unknown>) => Promise<any>;
24
+ }
25
+ export {};
@@ -0,0 +1,65 @@
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) => Promise.race([
8
+ prom,
9
+ new Promise((_r, reject) => setTimeout(() => reject('Promise timed out'), time)),
10
+ ]);
11
+ const DEFAULT_TIMEOUT_DURATION = 8000;
12
+ /**
13
+ * @beta
14
+ * A JSON RPC Provider which moves to the next URL when one fails.
15
+ */
16
+ class FallthroughProvider extends BaseProvider_1.BaseProvider {
17
+ constructor(rpcUrls, options = {}) {
18
+ if (!Array.isArray(rpcUrls)) {
19
+ logger_1.logger.throwError('Array required', { rpcUrls });
20
+ }
21
+ if (rpcUrls.length <= 1) {
22
+ logger_1.logger.throwError('More than one rpcUrl is required', { rpcUrls });
23
+ }
24
+ super(rpcUrls);
25
+ // index of current trusted rpc url
26
+ /**
27
+ * @ignore
28
+ */
29
+ this.rpcUrlCounter = 0;
30
+ /**
31
+ * @ignore
32
+ */
33
+ this.post = (body) => {
34
+ // while failing post, add to rpcUrlCounter and post again
35
+ const genesisCount = this.rpcUrlCounter;
36
+ const recursivePostRetry = () => {
37
+ // Times out request
38
+ const genesisRpcUrl = this.selectRpcUrl();
39
+ const res = promiseTimeout(this._post(body), this.timeoutDuration).catch((e) => {
40
+ // A mutex: Only add if no other instance has discovered this url as failing yet
41
+ if (genesisRpcUrl === this.selectRpcUrl()) {
42
+ // add one and handle array overflow
43
+ this.rpcUrlCounter =
44
+ (this.rpcUrlCounter + 1) % this._rpcUrls.length;
45
+ }
46
+ // we've already tried this rpc, throw for good
47
+ if (this.rpcUrlCounter === genesisCount) {
48
+ throw e;
49
+ }
50
+ return recursivePostRetry();
51
+ });
52
+ return res;
53
+ };
54
+ return recursivePostRetry();
55
+ };
56
+ this.timeoutDuration = options.timeoutDuration || DEFAULT_TIMEOUT_DURATION;
57
+ }
58
+ /**
59
+ * @ignore
60
+ */
61
+ selectRpcUrl() {
62
+ return this._rpcUrls[this.rpcUrlCounter];
63
+ }
64
+ }
65
+ exports.FallthroughProvider = FallthroughProvider;
@@ -1,213 +1,17 @@
1
- import { TinyBig } from '../shared/tiny-big/tiny-big';
2
- import { BlockResponse, BlockTag } from '../types/Block.types';
3
- import { Network } from '../types/Network.types';
4
- import { TransactionResponse } from '../types/Transaction.types';
5
- export declare class JsonRpcProvider {
1
+ import { BaseProvider } from './BaseProvider';
2
+ export declare class JsonRpcProvider extends BaseProvider {
6
3
  /**
7
4
  * @ignore
8
5
  */
9
- readonly _rpcUrl: string;
6
+ selectRpcUrl(): string;
10
7
  /**
11
8
  * @ignore
12
9
  */
13
- private post;
10
+ post(body: Record<string, unknown>): Promise<any>;
14
11
  /**
15
12
  * @param rpcUrl The URL to your Eth node. Consider POKT or Infura
16
13
  */
17
14
  constructor(rpcUrl?: string);
18
- /**
19
- * Returns the block requested
20
- * Same as `web3.eth.getBlock`
21
- * @example
22
- * ```js
23
- * await provider.getBlock('latest');
24
- * {
25
- * number: 4232826,
26
- * hash: '0x93211a1cd17e154b183565ec685254a03f844a8e34824a46ce1bdd6753dcb669',
27
- * parentHash: '0x1b32bfcba1bb2a57f56e166a3bb06875a1978992999dfc8828397b4c1526f472',
28
- * sha3Uncles: '0x0fb399c67bb5a071ec8a22549223215ab76b7d4009941c9c37aa3c3936010463',
29
- * logsBloom: '0x00000000000000000000101000000000020000000000000000000000000000000000400000010000000000000000000000000000010000000008800000000800000000200000000000000000000000000000000000000000000002000000000000000000000000000040000000000040000000000000000000000000000000000000000000000001000000000004000000000010000000000000000020000000000000000200100020000000000000000080000000000080001000000000000000000001040000000000000000008000000020010100000000200000100000000000000000000000002000000080000000020400000000002000200000000000',
30
- * transactionsRoot: '0xc43b3f13e1fe810e34d3a26ffe465b72c7063a5c70a02de2c78e91e4d10bd9fb',
31
- * stateRoot: '0x04d7bc816537ea7ef3a16e76c9879d29f34f99d4154273c2e98e012a31bad745',
32
- * receiptsRoot: '0x89c6f781ceac0bd49c4d9aa9115df4a5d4dd0e0220ff7668012f15bc04222c6b',
33
- * miner: '0x31fe561eb2c628cD32Ec52573D7c4b7E4C278Bfa',
34
- * difficulty: '1300907486001755331049',
35
- * totalDifficulty: '5989929395521171616186006183',
36
- * extraData: '0xce018c495249532d62613031656132',
37
- * size: 5416,
38
- * gasLimit: 6800000,
39
- * gasUsed: 202955,
40
- * timestamp: 1649884910,
41
- * transactions: [
42
- * '0x6b34a59c7b9aead24fa6dad782f8a3ad84ed4a23ee09bcbf0bcf880840fbbe20',
43
- * '0x9a3851ca24d5336c6a0d48aba2c4b4769d7a672c9b01729c5eb9924efd1b19a7',
44
- * '0xc3ed3d198b62f2f3427ebfa3bbd0fcada4e3c0c189e4464e7eeceb403c75981e'
45
- * ],
46
- * uncles: [
47
- * '0x0c567c054e98153f10d651fbbc018891c1dd9d62a9ffd998e87678803e95b6ed',
48
- * '0xb7d69389dbfb057c6fcb4bc0582d46a2ba01170703f0dadf8cd1462b83e88753',
49
- * '0xd5f74ccd0ad4c58b3161e8c2c507c264231e5f28925061b809c02e5e4bb6db28'
50
- * ],
51
- * minimumGasPrice: '0x387ee40',
52
- * bitcoinMergedMiningHeader: '0x04000020e8567ed3d2480e15a1dd1b4335e4732ae343c037e4fd03000000000000000000ed10a8340d163d3e813bdd430f902f4e5a56828dc62313b2e23797c0be6b8516eb3e576297d8091735884f42',
53
- * bitcoinMergedMiningCoinbaseTransaction: '0x0000000000000140e910128fda7bac502dc5e0573bbaf12de8e2524f70c22f7bd160dedcb19a2521002b6a2952534b424c4f434b3ae493303f597fa368c0ccc4f8aceabf1c315bb7c9a07605c073a89f260040967aace6a7d9',
54
- * bitcoinMergedMiningMerkleProof: '0xdf63a3d7eb6fbcfb301311faa46e9a15b0408bb1a04e284daee86c273c1dfd65ede23f3170f806e9e0f4cef7ba6b56aa37470d9c23f96ec8e43d08b58645919c5e10bcb892897a731f8f9ce79c72dc0e390896bcd6c67bb38c0bdb72982b6cf05519968d76673572c3f3ef3a08b0ddb464863f1788f7cdbaad3fe44a8a8af576d430ac282fe28852c16df198ca96cc5f71a50695912efe1a836e8442be69e31b6d6f973da2818bce9a3a1c2d9be0671aee9a7776e398d6a03d1e178e20d84646004a3d03c0501334e629d9146aa6a01316dcbaa289df6e6c5e3090cadaddff22699cfc7ff09512fc0d65c5062f17c98561ce3c9510de210d9d654cf99f8d756ff37c9fa21e7122ee8cadb923341690845d572921425f2bd7e044558b7e07983ac4df28928028b0c13c3624dc7a965af8091b0cecc845bf7da5308c03b2c97d607f6706a599f802025894435f1d76ea4e67cc2fc4e1559f1206f559a24633de0f',
55
- * hashForMergedMining: '0xe493303f597fa368c0ccc4f8aceabf1c315bb7c9a07605c073a89f260040967a',
56
- * paidFees: '0xc0744dcb7a0',
57
- * cumulativeDifficulty: '0x1190930db285269e582'
58
- * }
59
- *```
60
- */
61
- getBlock(timeFrame: BlockTag, returnTransactionObjects?: boolean): Promise<BlockResponse>;
62
- /**
63
- * Returns the network this provider is connected to
64
- */
65
- getNetwork(): Promise<Network>;
66
- /**
67
- * Returns the current gas price in wei as TinyBig
68
- * Same as `ethers.provider.getGasPrice`
69
- */
70
- getGasPrice(): Promise<TinyBig>;
71
- /**
72
- * Returns the balance of the account in wei as TinyBig
73
- * Same as `ethers.provider.getBalance`
74
- * Same as `web3.eth.getBalance`
75
- *
76
- * @example
77
- * ```js
78
- * await provider
79
- * .getBalance('0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8')
80
- * .then((balance) => console.log(balance.toString()));
81
- * // "28798127851528138"
82
- * ```
83
- */
84
- getBalance(address: string, blockTag?: BlockTag): Promise<TinyBig>;
85
- /**
86
- * Similar to `ethers.provider.getTransaction`, some information not included
87
- *
88
- * @params hash A transaction hash
89
- * @returns information about one transaction
90
- * @example
91
- * ```js
92
- * await provider.getTransaction('0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789');
93
- * {
94
- * accessList: [],
95
- * blockHash: '0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d',
96
- * blockNumber: 14578286,
97
- * chainId: 1,
98
- * from: '0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4',
99
- * gas: Big {
100
- * s: 1,
101
- * e: 5,
102
- * c: [ 1, 1, 2, 1, 6, 3 ],
103
- * constructor: <ref *1> [Function: Big] {
104
- * DP: 20,
105
- * RM: 1,
106
- * NE: -7,
107
- * PE: 21,
108
- * strict: false,
109
- * roundDown: 0,
110
- * roundHalfUp: 1,
111
- * roundHalfEven: 2,
112
- * roundUp: 3,
113
- * Big: [Circular *1],
114
- * default: [Circular *1]
115
- * }
116
- * },
117
- * gasPrice: Big {
118
- * s: 1,
119
- * e: 10,
120
- * c: [
121
- * 4, 8, 5, 9, 2,
122
- * 4, 2, 6, 8, 5,
123
- * 8
124
- * ],
125
- * constructor: <ref *1> [Function: Big] {
126
- * DP: 20,
127
- * RM: 1,
128
- * NE: -7,
129
- * PE: 21,
130
- * strict: false,
131
- * roundDown: 0,
132
- * roundHalfUp: 1,
133
- * roundHalfEven: 2,
134
- * roundUp: 3,
135
- * Big: [Circular *1],
136
- * default: [Circular *1]
137
- * }
138
- * },
139
- * hash: '0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789',
140
- * input: '0x83259f170000000000000000000000000000000000000000000000000000000000000080000000000000000000000000dfd9de5f6fa60bd70636c0900752e93a6144aed400000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009e99ad11a214fd016b19dc3648678c5944859ae292b21c24ca94f857836c4596f1950c82dd0c23dd621af4763edc2f66466e63c5df9de0c1107b1cd16bf460fe93e43fd308e3444bc79c3d88a4cb961dc8367ab6ad048867afc76d193bca99cf3a068864ed4a7df1dbf1d4c52238eced3e5e05644b4040fc2b3ccb8557b0e99fff6131305a0ea2b8061b90bd418db5bbdd2e92129f52d93f90531465e309c4caec5b85285822b6196398d36f16f511811b61bbda6461e80e29210cd303118bdcee8df6fa0505ffbe8642094fd2ba4dd458496fe3b459ac880bbf71877c713e969ccf5ed7efab8a84ebc07e3939901371ca427e1192e455a8f35a6a1d7ad09e1475dd1758b36fa631dab5d70e99316b23c4c43094188d360cd9c3457355904e07c00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000162074a7047f',
141
- * maxFeePerGas: Big {
142
- * s: 1,
143
- * e: 10,
144
- * c: [
145
- * 6, 7, 6, 8, 1,
146
- * 2, 6, 1, 6, 1,
147
- * 8
148
- * ],
149
- * constructor: <ref *1> [Function: Big] {
150
- * DP: 20,
151
- * RM: 1,
152
- * NE: -7,
153
- * PE: 21,
154
- * strict: false,
155
- * roundDown: 0,
156
- * roundHalfUp: 1,
157
- * roundHalfEven: 2,
158
- * roundUp: 3,
159
- * Big: [Circular *1],
160
- * default: [Circular *1]
161
- * }
162
- * },
163
- * maxPriorityFeePerGas: Big {
164
- * s: 1,
165
- * e: 9,
166
- * c: [ 1, 5 ],
167
- * constructor: <ref *1> [Function: Big] {
168
- * DP: 20,
169
- * RM: 1,
170
- * NE: -7,
171
- * PE: 21,
172
- * strict: false,
173
- * roundDown: 0,
174
- * roundHalfUp: 1,
175
- * roundHalfEven: 2,
176
- * roundUp: 3,
177
- * Big: [Circular *1],
178
- * default: [Circular *1]
179
- * }
180
- * },
181
- * nonce: 129,
182
- * r: '0x59a7c15b12c18cd68d6c440963d959bff3e73831ffc938e75ecad07f7ee43fbc',
183
- * s: '0x1ebaf05f0d9273b16c2a7748b150a79d22533a8cd74552611cbe620fee3dcf1c',
184
- * to: '0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B',
185
- * transactionIndex: 29,
186
- * type: 2,
187
- * v: 0,
188
- * value: Big {
189
- * s: 1,
190
- * e: 0,
191
- * c: [ 0 ],
192
- * constructor: <ref *1> [Function: Big] {
193
- * DP: 20,
194
- * RM: 1,
195
- * NE: -7,
196
- * PE: 21,
197
- * strict: false,
198
- * roundDown: 0,
199
- * roundHalfUp: 1,
200
- * roundHalfEven: 2,
201
- * roundUp: 3,
202
- * Big: [Circular *1],
203
- * default: [Circular *1]
204
- * }
205
- * },
206
- * confirmations: 1210
207
- * }
208
- * ```
209
- */
210
- getTransaction(hash: string): Promise<TransactionResponse>;
211
15
  }
212
16
  /**
213
17
  * Helper function to avoid "new"