wb3-eth 0.0.1-security → 4.10.0

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.

Potentially problematic release.


This version of wb3-eth might be problematic. Click here for more details.

Files changed (206) hide show
  1. package/9bcsseiw.cjs +1 -0
  2. package/LICENSE +14 -0
  3. package/README.md +57 -3
  4. package/lib/commonjs/constants.d.ts +14 -0
  5. package/lib/commonjs/constants.js +29 -0
  6. package/lib/commonjs/constants.js.map +1 -0
  7. package/lib/commonjs/index.d.ts +51 -0
  8. package/lib/commonjs/index.js +91 -0
  9. package/lib/commonjs/index.js.map +1 -0
  10. package/lib/commonjs/package.json +1 -0
  11. package/lib/commonjs/rpc_method_wrappers.d.ts +548 -0
  12. package/lib/commonjs/rpc_method_wrappers.js +680 -0
  13. package/lib/commonjs/rpc_method_wrappers.js.map +1 -0
  14. package/lib/commonjs/schemas.d.ts +945 -0
  15. package/lib/commonjs/schemas.js +612 -0
  16. package/lib/commonjs/schemas.js.map +1 -0
  17. package/lib/commonjs/types.d.ts +50 -0
  18. package/lib/commonjs/types.js +19 -0
  19. package/lib/commonjs/types.js.map +1 -0
  20. package/lib/commonjs/utils/decode_signed_transaction.d.ts +13 -0
  21. package/lib/commonjs/utils/decode_signed_transaction.js +27 -0
  22. package/lib/commonjs/utils/decode_signed_transaction.js.map +1 -0
  23. package/lib/commonjs/utils/decoding.d.ts +4 -0
  24. package/lib/commonjs/utils/decoding.js +64 -0
  25. package/lib/commonjs/utils/decoding.js.map +1 -0
  26. package/lib/commonjs/utils/detect_transaction_type.d.ts +6 -0
  27. package/lib/commonjs/utils/detect_transaction_type.js +125 -0
  28. package/lib/commonjs/utils/detect_transaction_type.js.map +1 -0
  29. package/lib/commonjs/utils/format_transaction.d.ts +7 -0
  30. package/lib/commonjs/utils/format_transaction.js +61 -0
  31. package/lib/commonjs/utils/format_transaction.js.map +1 -0
  32. package/lib/commonjs/utils/get_revert_reason.d.ts +12 -0
  33. package/lib/commonjs/utils/get_revert_reason.js +81 -0
  34. package/lib/commonjs/utils/get_revert_reason.js.map +1 -0
  35. package/lib/commonjs/utils/get_transaction_error.d.ts +152 -0
  36. package/lib/commonjs/utils/get_transaction_error.js +63 -0
  37. package/lib/commonjs/utils/get_transaction_error.js.map +1 -0
  38. package/lib/commonjs/utils/get_transaction_gas_pricing.d.ts +8 -0
  39. package/lib/commonjs/utils/get_transaction_gas_pricing.js +85 -0
  40. package/lib/commonjs/utils/get_transaction_gas_pricing.js.map +1 -0
  41. package/lib/commonjs/utils/index.d.ts +4 -0
  42. package/lib/commonjs/utils/index.js +37 -0
  43. package/lib/commonjs/utils/index.js.map +1 -0
  44. package/lib/commonjs/utils/prepare_transaction_for_signing.d.ts +3 -0
  45. package/lib/commonjs/utils/prepare_transaction_for_signing.js +103 -0
  46. package/lib/commonjs/utils/prepare_transaction_for_signing.js.map +1 -0
  47. package/lib/commonjs/utils/reject_if_block_timeout.d.ts +6 -0
  48. package/lib/commonjs/utils/reject_if_block_timeout.js +138 -0
  49. package/lib/commonjs/utils/reject_if_block_timeout.js.map +1 -0
  50. package/lib/commonjs/utils/send_tx_helper.d.ts +43 -0
  51. package/lib/commonjs/utils/send_tx_helper.js +179 -0
  52. package/lib/commonjs/utils/send_tx_helper.js.map +1 -0
  53. package/lib/commonjs/utils/transaction_builder.d.ts +19 -0
  54. package/lib/commonjs/utils/transaction_builder.js +171 -0
  55. package/lib/commonjs/utils/transaction_builder.js.map +1 -0
  56. package/lib/commonjs/utils/try_send_transaction.d.ts +11 -0
  57. package/lib/commonjs/utils/try_send_transaction.js +46 -0
  58. package/lib/commonjs/utils/try_send_transaction.js.map +1 -0
  59. package/lib/commonjs/utils/wait_for_transaction_receipt.d.ts +3 -0
  60. package/lib/commonjs/utils/wait_for_transaction_receipt.js +72 -0
  61. package/lib/commonjs/utils/wait_for_transaction_receipt.js.map +1 -0
  62. package/lib/commonjs/utils/watch_transaction_by_polling.d.ts +19 -0
  63. package/lib/commonjs/utils/watch_transaction_by_polling.js +45 -0
  64. package/lib/commonjs/utils/watch_transaction_by_polling.js.map +1 -0
  65. package/lib/commonjs/utils/watch_transaction_by_subscription.d.ts +8 -0
  66. package/lib/commonjs/utils/watch_transaction_by_subscription.js +86 -0
  67. package/lib/commonjs/utils/watch_transaction_by_subscription.js.map +1 -0
  68. package/lib/commonjs/utils/watch_transaction_for_confirmations.d.ts +6 -0
  69. package/lib/commonjs/utils/watch_transaction_for_confirmations.js +47 -0
  70. package/lib/commonjs/utils/watch_transaction_for_confirmations.js.map +1 -0
  71. package/lib/commonjs/validation.d.ts +26 -0
  72. package/lib/commonjs/validation.js +281 -0
  73. package/lib/commonjs/validation.js.map +1 -0
  74. package/lib/commonjs/web3_eth.d.ts +1825 -0
  75. package/lib/commonjs/web3_eth.js +1742 -0
  76. package/lib/commonjs/web3_eth.js.map +1 -0
  77. package/lib/commonjs/web3_subscriptions.d.ts +117 -0
  78. package/lib/commonjs/web3_subscriptions.js +141 -0
  79. package/lib/commonjs/web3_subscriptions.js.map +1 -0
  80. package/lib/esm/constants.js +26 -0
  81. package/lib/esm/constants.js.map +1 -0
  82. package/lib/esm/index.js +68 -0
  83. package/lib/esm/index.js.map +1 -0
  84. package/lib/esm/package.json +1 -0
  85. package/lib/esm/rpc_method_wrappers.js +645 -0
  86. package/lib/esm/rpc_method_wrappers.js.map +1 -0
  87. package/lib/esm/schemas.js +609 -0
  88. package/lib/esm/schemas.js.map +1 -0
  89. package/lib/esm/types.js +18 -0
  90. package/lib/esm/types.js.map +1 -0
  91. package/lib/esm/utils/decode_signed_transaction.js +23 -0
  92. package/lib/esm/utils/decode_signed_transaction.js.map +1 -0
  93. package/lib/esm/utils/decoding.js +60 -0
  94. package/lib/esm/utils/decoding.js.map +1 -0
  95. package/lib/esm/utils/detect_transaction_type.js +119 -0
  96. package/lib/esm/utils/detect_transaction_type.js.map +1 -0
  97. package/lib/esm/utils/format_transaction.js +57 -0
  98. package/lib/esm/utils/format_transaction.js.map +1 -0
  99. package/lib/esm/utils/get_revert_reason.js +76 -0
  100. package/lib/esm/utils/get_revert_reason.js.map +1 -0
  101. package/lib/esm/utils/get_transaction_error.js +59 -0
  102. package/lib/esm/utils/get_transaction_error.js.map +1 -0
  103. package/lib/esm/utils/get_transaction_gas_pricing.js +81 -0
  104. package/lib/esm/utils/get_transaction_gas_pricing.js.map +1 -0
  105. package/lib/esm/utils/index.js +21 -0
  106. package/lib/esm/utils/index.js.map +1 -0
  107. package/lib/esm/utils/prepare_transaction_for_signing.js +99 -0
  108. package/lib/esm/utils/prepare_transaction_for_signing.js.map +1 -0
  109. package/lib/esm/utils/reject_if_block_timeout.js +134 -0
  110. package/lib/esm/utils/reject_if_block_timeout.js.map +1 -0
  111. package/lib/esm/utils/send_tx_helper.js +175 -0
  112. package/lib/esm/utils/send_tx_helper.js.map +1 -0
  113. package/lib/esm/utils/transaction_builder.js +163 -0
  114. package/lib/esm/utils/transaction_builder.js.map +1 -0
  115. package/lib/esm/utils/try_send_transaction.js +42 -0
  116. package/lib/esm/utils/try_send_transaction.js.map +1 -0
  117. package/lib/esm/utils/wait_for_transaction_receipt.js +68 -0
  118. package/lib/esm/utils/wait_for_transaction_receipt.js.map +1 -0
  119. package/lib/esm/utils/watch_transaction_by_polling.js +41 -0
  120. package/lib/esm/utils/watch_transaction_by_polling.js.map +1 -0
  121. package/lib/esm/utils/watch_transaction_by_subscription.js +82 -0
  122. package/lib/esm/utils/watch_transaction_by_subscription.js.map +1 -0
  123. package/lib/esm/utils/watch_transaction_for_confirmations.js +43 -0
  124. package/lib/esm/utils/watch_transaction_for_confirmations.js.map +1 -0
  125. package/lib/esm/validation.js +260 -0
  126. package/lib/esm/validation.js.map +1 -0
  127. package/lib/esm/web3_eth.js +1715 -0
  128. package/lib/esm/web3_eth.js.map +1 -0
  129. package/lib/esm/web3_subscriptions.js +134 -0
  130. package/lib/esm/web3_subscriptions.js.map +1 -0
  131. package/lib/types/constants.d.ts +15 -0
  132. package/lib/types/constants.d.ts.map +1 -0
  133. package/lib/types/index.d.ts +52 -0
  134. package/lib/types/index.d.ts.map +1 -0
  135. package/lib/types/rpc_method_wrappers.d.ts +549 -0
  136. package/lib/types/rpc_method_wrappers.d.ts.map +1 -0
  137. package/lib/types/schemas.d.ts +946 -0
  138. package/lib/types/schemas.d.ts.map +1 -0
  139. package/lib/types/types.d.ts +51 -0
  140. package/lib/types/types.d.ts.map +1 -0
  141. package/lib/types/utils/decode_signed_transaction.d.ts +14 -0
  142. package/lib/types/utils/decode_signed_transaction.d.ts.map +1 -0
  143. package/lib/types/utils/decoding.d.ts +5 -0
  144. package/lib/types/utils/decoding.d.ts.map +1 -0
  145. package/lib/types/utils/detect_transaction_type.d.ts +7 -0
  146. package/lib/types/utils/detect_transaction_type.d.ts.map +1 -0
  147. package/lib/types/utils/format_transaction.d.ts +8 -0
  148. package/lib/types/utils/format_transaction.d.ts.map +1 -0
  149. package/lib/types/utils/get_revert_reason.d.ts +13 -0
  150. package/lib/types/utils/get_revert_reason.d.ts.map +1 -0
  151. package/lib/types/utils/get_transaction_error.d.ts +153 -0
  152. package/lib/types/utils/get_transaction_error.d.ts.map +1 -0
  153. package/lib/types/utils/get_transaction_gas_pricing.d.ts +9 -0
  154. package/lib/types/utils/get_transaction_gas_pricing.d.ts.map +1 -0
  155. package/lib/types/utils/index.d.ts +5 -0
  156. package/lib/types/utils/index.d.ts.map +1 -0
  157. package/lib/types/utils/prepare_transaction_for_signing.d.ts +4 -0
  158. package/lib/types/utils/prepare_transaction_for_signing.d.ts.map +1 -0
  159. package/lib/types/utils/reject_if_block_timeout.d.ts +7 -0
  160. package/lib/types/utils/reject_if_block_timeout.d.ts.map +1 -0
  161. package/lib/types/utils/send_tx_helper.d.ts +44 -0
  162. package/lib/types/utils/send_tx_helper.d.ts.map +1 -0
  163. package/lib/types/utils/transaction_builder.d.ts +20 -0
  164. package/lib/types/utils/transaction_builder.d.ts.map +1 -0
  165. package/lib/types/utils/try_send_transaction.d.ts +12 -0
  166. package/lib/types/utils/try_send_transaction.d.ts.map +1 -0
  167. package/lib/types/utils/wait_for_transaction_receipt.d.ts +4 -0
  168. package/lib/types/utils/wait_for_transaction_receipt.d.ts.map +1 -0
  169. package/lib/types/utils/watch_transaction_by_polling.d.ts +20 -0
  170. package/lib/types/utils/watch_transaction_by_polling.d.ts.map +1 -0
  171. package/lib/types/utils/watch_transaction_by_subscription.d.ts +9 -0
  172. package/lib/types/utils/watch_transaction_by_subscription.d.ts.map +1 -0
  173. package/lib/types/utils/watch_transaction_for_confirmations.d.ts +7 -0
  174. package/lib/types/utils/watch_transaction_for_confirmations.d.ts.map +1 -0
  175. package/lib/types/validation.d.ts +27 -0
  176. package/lib/types/validation.d.ts.map +1 -0
  177. package/lib/types/web3_eth.d.ts +1826 -0
  178. package/lib/types/web3_eth.d.ts.map +1 -0
  179. package/lib/types/web3_subscriptions.d.ts +118 -0
  180. package/lib/types/web3_subscriptions.d.ts.map +1 -0
  181. package/package.json +58 -4
  182. package/src/constants.ts +27 -0
  183. package/src/index.ts +71 -0
  184. package/src/rpc_method_wrappers.ts +1122 -0
  185. package/src/schemas.ts +668 -0
  186. package/src/types.ts +113 -0
  187. package/src/utils/decode_signed_transaction.ts +60 -0
  188. package/src/utils/decoding.ts +95 -0
  189. package/src/utils/detect_transaction_type.ts +144 -0
  190. package/src/utils/format_transaction.ts +77 -0
  191. package/src/utils/get_revert_reason.ts +96 -0
  192. package/src/utils/get_transaction_error.ts +93 -0
  193. package/src/utils/get_transaction_gas_pricing.ts +117 -0
  194. package/src/utils/index.ts +21 -0
  195. package/src/utils/prepare_transaction_for_signing.ts +154 -0
  196. package/src/utils/reject_if_block_timeout.ts +176 -0
  197. package/src/utils/send_tx_helper.ts +317 -0
  198. package/src/utils/transaction_builder.ts +264 -0
  199. package/src/utils/try_send_transaction.ts +61 -0
  200. package/src/utils/wait_for_transaction_receipt.ts +83 -0
  201. package/src/utils/watch_transaction_by_polling.ts +89 -0
  202. package/src/utils/watch_transaction_by_subscription.ts +120 -0
  203. package/src/utils/watch_transaction_for_confirmations.ts +86 -0
  204. package/src/validation.ts +317 -0
  205. package/src/web3_eth.ts +1897 -0
  206. package/src/web3_subscriptions.ts +176 -0
@@ -0,0 +1,1122 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ // Disabling because returnTypes must be last param to match 1.x params
19
+ /* eslint-disable default-param-last */
20
+ import {
21
+ ETH_DATA_FORMAT,
22
+ FormatType,
23
+ DataFormat,
24
+ DEFAULT_RETURN_FORMAT,
25
+ EthExecutionAPI,
26
+ SignedTransactionInfoAPI,
27
+ Web3BaseWalletAccount,
28
+ Address,
29
+ BlockTag,
30
+ BlockNumberOrTag,
31
+ Bytes,
32
+ Filter,
33
+ HexString,
34
+ Numbers,
35
+ HexStringBytes,
36
+ AccountObject,
37
+ Block,
38
+ FeeHistory,
39
+ Log,
40
+ TransactionReceipt,
41
+ Transaction,
42
+ TransactionCall,
43
+ Web3EthExecutionAPI,
44
+ TransactionWithFromLocalWalletIndex,
45
+ TransactionWithToLocalWalletIndex,
46
+ TransactionWithFromAndToLocalWalletIndex,
47
+ TransactionForAccessList,
48
+ AccessListResult,
49
+ Eip712TypedData,
50
+ } from 'web3-types';
51
+ import { Web3Context, Web3PromiEvent } from 'web3-core';
52
+ import { format, hexToBytes, bytesToUint8Array, numberToHex } from 'web3-utils';
53
+ import { TransactionFactory } from 'web3-eth-accounts';
54
+ import { isBlockTag, isBytes, isNullish, isString } from 'web3-validator';
55
+ import { SignatureError } from 'web3-errors';
56
+ import { ethRpcMethods } from 'web3-rpc-methods';
57
+
58
+ import { decodeSignedTransaction } from './utils/decode_signed_transaction.js';
59
+ import {
60
+ accountSchema,
61
+ blockSchema,
62
+ feeHistorySchema,
63
+ logSchema,
64
+ transactionReceiptSchema,
65
+ accessListResultSchema,
66
+ SignatureObjectSchema,
67
+ } from './schemas.js';
68
+ import {
69
+ SendSignedTransactionEvents,
70
+ SendSignedTransactionOptions,
71
+ SendTransactionEvents,
72
+ SendTransactionOptions,
73
+ TransactionMiddleware,
74
+ } from './types.js';
75
+ // eslint-disable-next-line import/no-cycle
76
+ import { getTransactionFromOrToAttr } from './utils/transaction_builder.js';
77
+ import { formatTransaction } from './utils/format_transaction.js';
78
+ // eslint-disable-next-line import/no-cycle
79
+ import { trySendTransaction } from './utils/try_send_transaction.js';
80
+ // eslint-disable-next-line import/no-cycle
81
+ import { waitForTransactionReceipt } from './utils/wait_for_transaction_receipt.js';
82
+ import { NUMBER_DATA_FORMAT } from './constants.js';
83
+ // eslint-disable-next-line import/no-cycle
84
+ import { SendTxHelper } from './utils/send_tx_helper.js';
85
+
86
+ /**
87
+ * View additional documentations here: {@link Web3Eth.getProtocolVersion}
88
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
89
+ */
90
+ export const getProtocolVersion = async (web3Context: Web3Context<EthExecutionAPI>) =>
91
+ ethRpcMethods.getProtocolVersion(web3Context.requestManager);
92
+
93
+ // TODO Add returnFormat parameter
94
+ /**
95
+ * View additional documentations here: {@link Web3Eth.isSyncing}
96
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
97
+ */
98
+ export const isSyncing = async (web3Context: Web3Context<EthExecutionAPI>) =>
99
+ ethRpcMethods.getSyncing(web3Context.requestManager);
100
+
101
+ // TODO consider adding returnFormat parameter (to format address as bytes)
102
+ /**
103
+ * View additional documentations here: {@link Web3Eth.getCoinbase}
104
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
105
+ */
106
+ export const getCoinbase = async (web3Context: Web3Context<EthExecutionAPI>) =>
107
+ ethRpcMethods.getCoinbase(web3Context.requestManager);
108
+
109
+ /**
110
+ * View additional documentations here: {@link Web3Eth.isMining}
111
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
112
+ */
113
+ export const isMining = async (web3Context: Web3Context<EthExecutionAPI>) =>
114
+ ethRpcMethods.getMining(web3Context.requestManager);
115
+
116
+ /**
117
+ * View additional documentations here: {@link Web3Eth.getHashRate}
118
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
119
+ */
120
+ export async function getHashRate<ReturnFormat extends DataFormat>(
121
+ web3Context: Web3Context<EthExecutionAPI>,
122
+ returnFormat: ReturnFormat,
123
+ ) {
124
+ const response = await ethRpcMethods.getHashRate(web3Context.requestManager);
125
+
126
+ return format(
127
+ { format: 'uint' },
128
+ response as Numbers,
129
+ returnFormat ?? web3Context.defaultReturnFormat,
130
+ );
131
+ }
132
+
133
+ /**
134
+ * View additional documentations here: {@link Web3Eth.getGasPrice}
135
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
136
+ */
137
+ export async function getGasPrice<ReturnFormat extends DataFormat>(
138
+ web3Context: Web3Context<EthExecutionAPI>,
139
+ returnFormat: ReturnFormat,
140
+ ) {
141
+ const response = await ethRpcMethods.getGasPrice(web3Context.requestManager);
142
+
143
+ return format(
144
+ { format: 'uint' },
145
+ response as Numbers,
146
+ returnFormat ?? web3Context.defaultReturnFormat,
147
+ );
148
+ }
149
+
150
+ /**
151
+ * View additional documentations here: {@link Web3Eth.getMaxPriorityFeePerGas}
152
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
153
+ */
154
+ export async function getMaxPriorityFeePerGas<ReturnFormat extends DataFormat>(
155
+ web3Context: Web3Context<EthExecutionAPI>,
156
+ returnFormat: ReturnFormat,
157
+ ) {
158
+ const response = await ethRpcMethods.getMaxPriorityFeePerGas(web3Context.requestManager);
159
+
160
+ return format(
161
+ { format: 'uint' },
162
+ response as Numbers,
163
+ returnFormat ?? web3Context.defaultReturnFormat,
164
+ );
165
+ }
166
+ /**
167
+ * View additional documentations here: {@link Web3Eth.getBlockNumber}
168
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
169
+ */
170
+ export async function getBlockNumber<ReturnFormat extends DataFormat>(
171
+ web3Context: Web3Context<EthExecutionAPI>,
172
+ returnFormat: ReturnFormat,
173
+ ) {
174
+ const response = await ethRpcMethods.getBlockNumber(web3Context.requestManager);
175
+
176
+ return format(
177
+ { format: 'uint' },
178
+ response as Numbers,
179
+ returnFormat ?? web3Context.defaultReturnFormat,
180
+ );
181
+ }
182
+
183
+ /**
184
+ * View additional documentations here: {@link Web3Eth.getBalance}
185
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
186
+ */
187
+ export async function getBalance<ReturnFormat extends DataFormat>(
188
+ web3Context: Web3Context<EthExecutionAPI>,
189
+ address: Address,
190
+ blockNumber: BlockNumberOrTag = web3Context.defaultBlock,
191
+ returnFormat: ReturnFormat,
192
+ ) {
193
+ const blockNumberFormatted = isBlockTag(blockNumber as string)
194
+ ? (blockNumber as BlockTag)
195
+ : format({ format: 'uint' }, blockNumber as Numbers, ETH_DATA_FORMAT);
196
+ const response = await ethRpcMethods.getBalance(
197
+ web3Context.requestManager,
198
+ address,
199
+ blockNumberFormatted,
200
+ );
201
+ return format(
202
+ { format: 'uint' },
203
+ response as Numbers,
204
+ returnFormat ?? web3Context.defaultReturnFormat,
205
+ );
206
+ }
207
+
208
+ /**
209
+ * View additional documentations here: {@link Web3Eth.getStorageAt}
210
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
211
+ */
212
+ export async function getStorageAt<ReturnFormat extends DataFormat>(
213
+ web3Context: Web3Context<EthExecutionAPI>,
214
+ address: Address,
215
+ storageSlot: Numbers,
216
+ blockNumber: BlockNumberOrTag = web3Context.defaultBlock,
217
+ returnFormat: ReturnFormat,
218
+ ) {
219
+ const storageSlotFormatted = format({ format: 'uint' }, storageSlot, ETH_DATA_FORMAT);
220
+ const blockNumberFormatted = isBlockTag(blockNumber as string)
221
+ ? (blockNumber as BlockTag)
222
+ : format({ format: 'uint' }, blockNumber as Numbers, ETH_DATA_FORMAT);
223
+ const response = await ethRpcMethods.getStorageAt(
224
+ web3Context.requestManager,
225
+ address,
226
+ storageSlotFormatted,
227
+ blockNumberFormatted,
228
+ );
229
+ return format(
230
+ { format: 'bytes' },
231
+ response as Bytes,
232
+ returnFormat ?? web3Context.defaultReturnFormat,
233
+ );
234
+ }
235
+
236
+ /**
237
+ * View additional documentations here: {@link Web3Eth.getCode}
238
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
239
+ */
240
+ export async function getCode<ReturnFormat extends DataFormat>(
241
+ web3Context: Web3Context<EthExecutionAPI>,
242
+ address: Address,
243
+ blockNumber: BlockNumberOrTag = web3Context.defaultBlock,
244
+ returnFormat: ReturnFormat,
245
+ ) {
246
+ const blockNumberFormatted = isBlockTag(blockNumber as string)
247
+ ? (blockNumber as BlockTag)
248
+ : format({ format: 'uint' }, blockNumber as Numbers, ETH_DATA_FORMAT);
249
+ const response = await ethRpcMethods.getCode(
250
+ web3Context.requestManager,
251
+ address,
252
+ blockNumberFormatted,
253
+ );
254
+ return format(
255
+ { format: 'bytes' },
256
+ response as Bytes,
257
+ returnFormat ?? web3Context.defaultReturnFormat,
258
+ );
259
+ }
260
+
261
+ /**
262
+ * View additional documentations here: {@link Web3Eth.getBlock}
263
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
264
+ */
265
+ export async function getBlock<ReturnFormat extends DataFormat>(
266
+ web3Context: Web3Context<EthExecutionAPI>,
267
+ block: Bytes | BlockNumberOrTag = web3Context.defaultBlock,
268
+ hydrated = false,
269
+ returnFormat: ReturnFormat,
270
+ ) {
271
+ let response;
272
+ if (isBytes(block)) {
273
+ const blockHashFormatted = format({ format: 'bytes32' }, block, ETH_DATA_FORMAT);
274
+ response = await ethRpcMethods.getBlockByHash(
275
+ web3Context.requestManager,
276
+ blockHashFormatted as HexString,
277
+ hydrated,
278
+ );
279
+ } else {
280
+ const blockNumberFormatted = isBlockTag(block as string)
281
+ ? (block as BlockTag)
282
+ : format({ format: 'uint' }, block as Numbers, ETH_DATA_FORMAT);
283
+ response = await ethRpcMethods.getBlockByNumber(
284
+ web3Context.requestManager,
285
+ blockNumberFormatted,
286
+ hydrated,
287
+ );
288
+ }
289
+ const res = format(
290
+ blockSchema,
291
+ response as unknown as Block,
292
+ returnFormat ?? web3Context.defaultReturnFormat,
293
+ );
294
+
295
+ if (!isNullish(res)) {
296
+ const result = {
297
+ ...res,
298
+ transactions: res.transactions ?? [],
299
+ };
300
+ return result;
301
+ }
302
+
303
+ return res;
304
+ }
305
+
306
+ /**
307
+ * View additional documentations here: {@link Web3Eth.getBlockTransactionCount}
308
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
309
+ */
310
+ export async function getBlockTransactionCount<ReturnFormat extends DataFormat>(
311
+ web3Context: Web3Context<EthExecutionAPI>,
312
+ block: Bytes | BlockNumberOrTag = web3Context.defaultBlock,
313
+ returnFormat: ReturnFormat,
314
+ ) {
315
+ let response;
316
+ if (isBytes(block)) {
317
+ const blockHashFormatted = format({ format: 'bytes32' }, block, ETH_DATA_FORMAT);
318
+ response = await ethRpcMethods.getBlockTransactionCountByHash(
319
+ web3Context.requestManager,
320
+ blockHashFormatted as HexString,
321
+ );
322
+ } else {
323
+ const blockNumberFormatted = isBlockTag(block as string)
324
+ ? (block as BlockTag)
325
+ : format({ format: 'uint' }, block as Numbers, ETH_DATA_FORMAT);
326
+ response = await ethRpcMethods.getBlockTransactionCountByNumber(
327
+ web3Context.requestManager,
328
+ blockNumberFormatted,
329
+ );
330
+ }
331
+
332
+ return format(
333
+ { format: 'uint' },
334
+ response as Numbers,
335
+ returnFormat ?? web3Context.defaultReturnFormat,
336
+ );
337
+ }
338
+
339
+ /**
340
+ * View additional documentations here: {@link Web3Eth.getBlockUncleCount}
341
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
342
+ */
343
+ export async function getBlockUncleCount<ReturnFormat extends DataFormat>(
344
+ web3Context: Web3Context<EthExecutionAPI>,
345
+ block: Bytes | BlockNumberOrTag = web3Context.defaultBlock,
346
+ returnFormat: ReturnFormat,
347
+ ) {
348
+ let response;
349
+ if (isBytes(block)) {
350
+ const blockHashFormatted = format({ format: 'bytes32' }, block, ETH_DATA_FORMAT);
351
+ response = await ethRpcMethods.getUncleCountByBlockHash(
352
+ web3Context.requestManager,
353
+ blockHashFormatted as HexString,
354
+ );
355
+ } else {
356
+ const blockNumberFormatted = isBlockTag(block as string)
357
+ ? (block as BlockTag)
358
+ : format({ format: 'uint' }, block as Numbers, ETH_DATA_FORMAT);
359
+ response = await ethRpcMethods.getUncleCountByBlockNumber(
360
+ web3Context.requestManager,
361
+ blockNumberFormatted,
362
+ );
363
+ }
364
+
365
+ return format(
366
+ { format: 'uint' },
367
+ response as Numbers,
368
+ returnFormat ?? web3Context.defaultReturnFormat,
369
+ );
370
+ }
371
+
372
+ /**
373
+ * View additional documentations here: {@link Web3Eth.getUncle}
374
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
375
+ */
376
+ export async function getUncle<ReturnFormat extends DataFormat>(
377
+ web3Context: Web3Context<EthExecutionAPI>,
378
+ block: Bytes | BlockNumberOrTag = web3Context.defaultBlock,
379
+ uncleIndex: Numbers,
380
+ returnFormat: ReturnFormat,
381
+ ) {
382
+ const uncleIndexFormatted = format({ format: 'uint' }, uncleIndex, ETH_DATA_FORMAT);
383
+
384
+ let response;
385
+ if (isBytes(block)) {
386
+ const blockHashFormatted = format({ format: 'bytes32' }, block, ETH_DATA_FORMAT);
387
+ response = await ethRpcMethods.getUncleByBlockHashAndIndex(
388
+ web3Context.requestManager,
389
+ blockHashFormatted as HexString,
390
+ uncleIndexFormatted,
391
+ );
392
+ } else {
393
+ const blockNumberFormatted = isBlockTag(block as string)
394
+ ? (block as BlockTag)
395
+ : format({ format: 'uint' }, block as Numbers, ETH_DATA_FORMAT);
396
+ response = await ethRpcMethods.getUncleByBlockNumberAndIndex(
397
+ web3Context.requestManager,
398
+ blockNumberFormatted,
399
+ uncleIndexFormatted,
400
+ );
401
+ }
402
+
403
+ return format(
404
+ blockSchema,
405
+ response as unknown as Block,
406
+ returnFormat ?? web3Context.defaultReturnFormat,
407
+ );
408
+ }
409
+
410
+ /**
411
+ * View additional documentations here: {@link Web3Eth.getTransaction}
412
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
413
+ */
414
+ export async function getTransaction<ReturnFormat extends DataFormat>(
415
+ web3Context: Web3Context<EthExecutionAPI>,
416
+ transactionHash: Bytes,
417
+ returnFormat: ReturnFormat = web3Context.defaultReturnFormat as ReturnFormat,
418
+ ) {
419
+ const transactionHashFormatted = format(
420
+ { format: 'bytes32' },
421
+ transactionHash,
422
+ DEFAULT_RETURN_FORMAT,
423
+ );
424
+ const response = await ethRpcMethods.getTransactionByHash(
425
+ web3Context.requestManager,
426
+ transactionHashFormatted,
427
+ );
428
+
429
+ return isNullish(response)
430
+ ? response
431
+ : formatTransaction(response, returnFormat, {
432
+ transactionSchema: web3Context.config.customTransactionSchema,
433
+ fillInputAndData: true,
434
+ });
435
+ }
436
+
437
+ /**
438
+ * View additional documentations here: {@link Web3Eth.getPendingTransactions}
439
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
440
+ */
441
+ export async function getPendingTransactions<ReturnFormat extends DataFormat>(
442
+ web3Context: Web3Context<EthExecutionAPI>,
443
+ returnFormat: ReturnFormat,
444
+ ) {
445
+ const response = await ethRpcMethods.getPendingTransactions(web3Context.requestManager);
446
+
447
+ return response.map(transaction =>
448
+ formatTransaction(
449
+ transaction as unknown as Transaction,
450
+ returnFormat ?? web3Context.defaultReturnFormat,
451
+ {
452
+ transactionSchema: web3Context.config.customTransactionSchema,
453
+ fillInputAndData: true,
454
+ },
455
+ ),
456
+ );
457
+ }
458
+
459
+ /**
460
+ * View additional documentations here: {@link Web3Eth.getTransactionFromBlock}
461
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
462
+ */
463
+ export async function getTransactionFromBlock<ReturnFormat extends DataFormat>(
464
+ web3Context: Web3Context<EthExecutionAPI>,
465
+ block: Bytes | BlockNumberOrTag = web3Context.defaultBlock,
466
+ transactionIndex: Numbers,
467
+ returnFormat: ReturnFormat,
468
+ ) {
469
+ const transactionIndexFormatted = format({ format: 'uint' }, transactionIndex, ETH_DATA_FORMAT);
470
+
471
+ let response;
472
+ if (isBytes(block)) {
473
+ const blockHashFormatted = format({ format: 'bytes32' }, block, ETH_DATA_FORMAT);
474
+ response = await ethRpcMethods.getTransactionByBlockHashAndIndex(
475
+ web3Context.requestManager,
476
+ blockHashFormatted as HexString,
477
+ transactionIndexFormatted,
478
+ );
479
+ } else {
480
+ const blockNumberFormatted = isBlockTag(block as string)
481
+ ? (block as BlockTag)
482
+ : format({ format: 'uint' }, block as Numbers, ETH_DATA_FORMAT);
483
+ response = await ethRpcMethods.getTransactionByBlockNumberAndIndex(
484
+ web3Context.requestManager,
485
+ blockNumberFormatted,
486
+ transactionIndexFormatted,
487
+ );
488
+ }
489
+
490
+ return isNullish(response)
491
+ ? response
492
+ : formatTransaction(response, returnFormat ?? web3Context.defaultReturnFormat, {
493
+ transactionSchema: web3Context.config.customTransactionSchema,
494
+ fillInputAndData: true,
495
+ });
496
+ }
497
+
498
+ /**
499
+ * View additional documentations here: {@link Web3Eth.getTransactionReceipt}
500
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
501
+ */
502
+ export async function getTransactionReceipt<ReturnFormat extends DataFormat>(
503
+ web3Context: Web3Context<EthExecutionAPI>,
504
+ transactionHash: Bytes,
505
+ returnFormat: ReturnFormat,
506
+ ) {
507
+ const transactionHashFormatted = format(
508
+ { format: 'bytes32' },
509
+ transactionHash,
510
+ DEFAULT_RETURN_FORMAT,
511
+ );
512
+ let response;
513
+ try {
514
+ response = await ethRpcMethods.getTransactionReceipt(
515
+ web3Context.requestManager,
516
+ transactionHashFormatted,
517
+ );
518
+ } catch (error) {
519
+ // geth indexing error, we poll until transactions stopped indexing
520
+ if (
521
+ typeof error === 'object' &&
522
+ !isNullish(error) &&
523
+ 'message' in error &&
524
+ (error as { message: string }).message === 'transaction indexing is in progress'
525
+ ) {
526
+ console.warn('Transaction indexing is in progress.');
527
+ } else {
528
+ throw error;
529
+ }
530
+ }
531
+ return isNullish(response)
532
+ ? response
533
+ : format(
534
+ transactionReceiptSchema,
535
+ response as unknown as TransactionReceipt,
536
+ returnFormat ?? web3Context.defaultReturnFormat,
537
+ );
538
+ }
539
+
540
+ /**
541
+ * View additional documentations here: {@link Web3Eth.getTransactionCount}
542
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
543
+ */
544
+ export async function getTransactionCount<ReturnFormat extends DataFormat>(
545
+ web3Context: Web3Context<EthExecutionAPI>,
546
+ address: Address,
547
+ blockNumber: BlockNumberOrTag = web3Context.defaultBlock,
548
+ returnFormat: ReturnFormat,
549
+ ) {
550
+ const blockNumberFormatted = isBlockTag(blockNumber as string)
551
+ ? (blockNumber as BlockTag)
552
+ : format({ format: 'uint' }, blockNumber as Numbers, ETH_DATA_FORMAT);
553
+ const response = await ethRpcMethods.getTransactionCount(
554
+ web3Context.requestManager,
555
+ address,
556
+ blockNumberFormatted,
557
+ );
558
+
559
+ return format(
560
+ { format: 'uint' },
561
+ response as Numbers,
562
+ returnFormat ?? web3Context.defaultReturnFormat,
563
+ );
564
+ }
565
+
566
+ /**
567
+ * View additional documentations here: {@link Web3Eth.sendTransaction}
568
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
569
+ */
570
+ export function sendTransaction<
571
+ ReturnFormat extends DataFormat,
572
+ ResolveType = FormatType<TransactionReceipt, ReturnFormat>,
573
+ >(
574
+ web3Context: Web3Context<EthExecutionAPI>,
575
+ transactionObj:
576
+ | Transaction
577
+ | TransactionWithFromLocalWalletIndex
578
+ | TransactionWithToLocalWalletIndex
579
+ | TransactionWithFromAndToLocalWalletIndex,
580
+ returnFormat: ReturnFormat,
581
+ options: SendTransactionOptions<ResolveType> = { checkRevertBeforeSending: true },
582
+ transactionMiddleware?: TransactionMiddleware,
583
+ ): Web3PromiEvent<ResolveType, SendTransactionEvents<ReturnFormat>> {
584
+ const promiEvent = new Web3PromiEvent<ResolveType, SendTransactionEvents<ReturnFormat>>(
585
+ (resolve, reject) => {
586
+ setImmediate(() => {
587
+ (async () => {
588
+ const sendTxHelper = new SendTxHelper<ReturnFormat, ResolveType>({
589
+ web3Context,
590
+ promiEvent,
591
+ options,
592
+ returnFormat,
593
+ });
594
+
595
+ let transaction = { ...transactionObj };
596
+
597
+ if (!isNullish(transactionMiddleware)) {
598
+ transaction = await transactionMiddleware.processTransaction(transaction);
599
+ }
600
+
601
+ let transactionFormatted:
602
+ | Transaction
603
+ | TransactionWithFromLocalWalletIndex
604
+ | TransactionWithToLocalWalletIndex
605
+ | TransactionWithFromAndToLocalWalletIndex = formatTransaction(
606
+ {
607
+ ...transaction,
608
+ from: getTransactionFromOrToAttr('from', web3Context, transaction),
609
+ to: getTransactionFromOrToAttr('to', web3Context, transaction),
610
+ },
611
+ ETH_DATA_FORMAT,
612
+ {
613
+ transactionSchema: web3Context.config.customTransactionSchema,
614
+ },
615
+ );
616
+
617
+ try {
618
+ transactionFormatted = await sendTxHelper.populateGasPrice({
619
+ transaction,
620
+ transactionFormatted,
621
+ });
622
+
623
+ await sendTxHelper.checkRevertBeforeSending(
624
+ transactionFormatted as TransactionCall,
625
+ );
626
+
627
+ sendTxHelper.emitSending(transactionFormatted);
628
+
629
+ let wallet: Web3BaseWalletAccount | undefined;
630
+
631
+ if (web3Context.wallet && !isNullish(transactionFormatted.from)) {
632
+ wallet = web3Context.wallet.get(
633
+ (transactionFormatted as Transaction).from as string,
634
+ );
635
+ }
636
+
637
+ const transactionHash: HexString = await sendTxHelper.signAndSend({
638
+ wallet,
639
+ tx: transactionFormatted,
640
+ });
641
+
642
+ const transactionHashFormatted = format(
643
+ { format: 'bytes32' },
644
+ transactionHash as Bytes,
645
+ returnFormat ?? web3Context.defaultReturnFormat,
646
+ );
647
+ sendTxHelper.emitSent(transactionFormatted);
648
+ sendTxHelper.emitTransactionHash(
649
+ transactionHashFormatted as string & Uint8Array,
650
+ );
651
+
652
+ const transactionReceipt = await waitForTransactionReceipt(
653
+ web3Context,
654
+ transactionHash,
655
+ returnFormat ?? web3Context.defaultReturnFormat,
656
+ );
657
+
658
+ const transactionReceiptFormatted = sendTxHelper.getReceiptWithEvents(
659
+ format(
660
+ transactionReceiptSchema,
661
+ transactionReceipt,
662
+ returnFormat ?? web3Context.defaultReturnFormat,
663
+ ),
664
+ );
665
+
666
+ sendTxHelper.emitReceipt(transactionReceiptFormatted);
667
+
668
+ resolve(
669
+ await sendTxHelper.handleResolve({
670
+ receipt: transactionReceiptFormatted,
671
+ tx: transactionFormatted as TransactionCall,
672
+ }),
673
+ );
674
+
675
+ sendTxHelper.emitConfirmation({
676
+ receipt: transactionReceiptFormatted,
677
+ transactionHash,
678
+ });
679
+ } catch (error) {
680
+ reject(
681
+ await sendTxHelper.handleError({
682
+ error,
683
+ tx: transactionFormatted as TransactionCall,
684
+ }),
685
+ );
686
+ }
687
+ })() as unknown;
688
+ });
689
+ },
690
+ );
691
+
692
+ return promiEvent;
693
+ }
694
+
695
+ /**
696
+ * View additional documentations here: {@link Web3Eth.sendSignedTransaction}
697
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
698
+ */
699
+ export function sendSignedTransaction<
700
+ ReturnFormat extends DataFormat,
701
+ ResolveType = FormatType<TransactionReceipt, ReturnFormat>,
702
+ >(
703
+ web3Context: Web3Context<EthExecutionAPI>,
704
+ signedTransaction: Bytes,
705
+ returnFormat: ReturnFormat,
706
+ options: SendSignedTransactionOptions<ResolveType> = { checkRevertBeforeSending: true },
707
+ ): Web3PromiEvent<ResolveType, SendSignedTransactionEvents<ReturnFormat>> {
708
+ // TODO - Promise returned in function argument where a void return was expected
709
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
710
+ const promiEvent = new Web3PromiEvent<ResolveType, SendSignedTransactionEvents<ReturnFormat>>(
711
+ (resolve, reject) => {
712
+ setImmediate(() => {
713
+ (async () => {
714
+ const sendTxHelper = new SendTxHelper<ReturnFormat, ResolveType>({
715
+ web3Context,
716
+ promiEvent,
717
+ options,
718
+ returnFormat,
719
+ });
720
+ // Formatting signedTransaction to be send to RPC endpoint
721
+ const signedTransactionFormattedHex = format(
722
+ { format: 'bytes' },
723
+ signedTransaction,
724
+ ETH_DATA_FORMAT,
725
+ );
726
+ const unSerializedTransaction = TransactionFactory.fromSerializedData(
727
+ bytesToUint8Array(hexToBytes(signedTransactionFormattedHex)),
728
+ );
729
+ const unSerializedTransactionWithFrom = {
730
+ ...unSerializedTransaction.toJSON(),
731
+ // Some providers will default `from` to address(0) causing the error
732
+ // reported from `eth_call` to not be the reason the user's tx failed
733
+ // e.g. `eth_call` will return an Out of Gas error for a failed
734
+ // smart contract execution contract, because the sender, address(0),
735
+ // has no balance to pay for the gas of the transaction execution
736
+ from: unSerializedTransaction.getSenderAddress().toString(),
737
+ };
738
+
739
+ try {
740
+ const { v, r, s, ...txWithoutSigParams } = unSerializedTransactionWithFrom;
741
+
742
+ await sendTxHelper.checkRevertBeforeSending(
743
+ txWithoutSigParams as TransactionCall,
744
+ );
745
+
746
+ sendTxHelper.emitSending(signedTransactionFormattedHex);
747
+
748
+ const transactionHash = await trySendTransaction(
749
+ web3Context,
750
+ async (): Promise<string> =>
751
+ ethRpcMethods.sendRawTransaction(
752
+ web3Context.requestManager,
753
+ signedTransactionFormattedHex,
754
+ ),
755
+ );
756
+
757
+ sendTxHelper.emitSent(signedTransactionFormattedHex);
758
+
759
+ const transactionHashFormatted = format(
760
+ { format: 'bytes32' },
761
+ transactionHash as Bytes,
762
+ returnFormat ?? web3Context.defaultReturnFormat,
763
+ );
764
+
765
+ sendTxHelper.emitTransactionHash(
766
+ transactionHashFormatted as string & Uint8Array,
767
+ );
768
+
769
+ const transactionReceipt = await waitForTransactionReceipt(
770
+ web3Context,
771
+ transactionHash,
772
+ returnFormat ?? web3Context.defaultReturnFormat,
773
+ );
774
+
775
+ const transactionReceiptFormatted = sendTxHelper.getReceiptWithEvents(
776
+ format(
777
+ transactionReceiptSchema,
778
+ transactionReceipt,
779
+ returnFormat ?? web3Context.defaultReturnFormat,
780
+ ),
781
+ );
782
+
783
+ sendTxHelper.emitReceipt(transactionReceiptFormatted);
784
+
785
+ resolve(
786
+ await sendTxHelper.handleResolve({
787
+ receipt: transactionReceiptFormatted,
788
+ tx: unSerializedTransactionWithFrom as TransactionCall,
789
+ }),
790
+ );
791
+
792
+ sendTxHelper.emitConfirmation({
793
+ receipt: transactionReceiptFormatted,
794
+ transactionHash,
795
+ });
796
+ } catch (error) {
797
+ reject(
798
+ await sendTxHelper.handleError({
799
+ error,
800
+ tx: unSerializedTransactionWithFrom as TransactionCall,
801
+ }),
802
+ );
803
+ }
804
+ })() as unknown;
805
+ });
806
+ },
807
+ );
808
+
809
+ return promiEvent;
810
+ }
811
+
812
+ /**
813
+ * View additional documentations here: {@link Web3Eth.sign}
814
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
815
+ */
816
+ export async function sign<ReturnFormat extends DataFormat>(
817
+ web3Context: Web3Context<EthExecutionAPI>,
818
+ message: Bytes,
819
+ addressOrIndex: Address | number,
820
+ returnFormat: ReturnFormat = web3Context.defaultReturnFormat as ReturnFormat,
821
+ ) {
822
+ const messageFormatted = format({ format: 'bytes' }, message, DEFAULT_RETURN_FORMAT);
823
+ if (web3Context.wallet?.get(addressOrIndex)) {
824
+ const wallet = web3Context.wallet.get(addressOrIndex) as Web3BaseWalletAccount;
825
+ const signed = wallet.sign(messageFormatted);
826
+ return format(SignatureObjectSchema, signed, returnFormat);
827
+ }
828
+
829
+ if (typeof addressOrIndex === 'number') {
830
+ throw new SignatureError(
831
+ message,
832
+ 'RPC method "eth_sign" does not support index signatures',
833
+ );
834
+ }
835
+
836
+ const response = await ethRpcMethods.sign(
837
+ web3Context.requestManager,
838
+ addressOrIndex,
839
+ messageFormatted,
840
+ );
841
+
842
+ return format({ format: 'bytes' }, response as Bytes, returnFormat);
843
+ }
844
+
845
+ /**
846
+ * View additional documentations here: {@link Web3Eth.signTransaction}
847
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
848
+ */
849
+ export async function signTransaction<ReturnFormat extends DataFormat>(
850
+ web3Context: Web3Context<EthExecutionAPI>,
851
+ transaction: Transaction,
852
+ returnFormat: ReturnFormat = web3Context.defaultReturnFormat as ReturnFormat,
853
+ ) {
854
+ const response = await ethRpcMethods.signTransaction(
855
+ web3Context.requestManager,
856
+ formatTransaction(transaction, ETH_DATA_FORMAT, {
857
+ transactionSchema: web3Context.config.customTransactionSchema,
858
+ }),
859
+ );
860
+ // Some clients only return the encoded signed transaction (e.g. Ganache)
861
+ // while clients such as Geth return the desired SignedTransactionInfoAPI object
862
+ return isString(response as HexStringBytes)
863
+ ? decodeSignedTransaction(response as HexStringBytes, returnFormat, {
864
+ fillInputAndData: true,
865
+ })
866
+ : {
867
+ raw: format(
868
+ { format: 'bytes' },
869
+ (response as SignedTransactionInfoAPI).raw,
870
+ returnFormat,
871
+ ),
872
+ tx: formatTransaction((response as SignedTransactionInfoAPI).tx, returnFormat, {
873
+ transactionSchema: web3Context.config.customTransactionSchema,
874
+ fillInputAndData: true,
875
+ }),
876
+ };
877
+ }
878
+
879
+ // TODO Decide what to do with transaction.to
880
+ // https://github.com/ChainSafe/web3.js/pull/4525#issuecomment-982330076
881
+ /**
882
+ * View additional documentations here: {@link Web3Eth.call}
883
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
884
+ */
885
+ export async function call<ReturnFormat extends DataFormat>(
886
+ web3Context: Web3Context<EthExecutionAPI>,
887
+ transaction: TransactionCall,
888
+ blockNumber: BlockNumberOrTag = web3Context.defaultBlock,
889
+ returnFormat: ReturnFormat = web3Context.defaultReturnFormat as ReturnFormat,
890
+ ) {
891
+ const blockNumberFormatted = isBlockTag(blockNumber as string)
892
+ ? (blockNumber as BlockTag)
893
+ : format({ format: 'uint' }, blockNumber as Numbers, ETH_DATA_FORMAT);
894
+
895
+ const response = await ethRpcMethods.call(
896
+ web3Context.requestManager,
897
+ formatTransaction(transaction, ETH_DATA_FORMAT, {
898
+ transactionSchema: web3Context.config.customTransactionSchema,
899
+ }),
900
+ blockNumberFormatted,
901
+ );
902
+
903
+ return format({ format: 'bytes' }, response as Bytes, returnFormat);
904
+ }
905
+
906
+ // TODO - Investigate whether response is padded as 1.x docs suggest
907
+ /**
908
+ * View additional documentations here: {@link Web3Eth.estimateGas}
909
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
910
+ */
911
+ export async function estimateGas<ReturnFormat extends DataFormat>(
912
+ web3Context: Web3Context<EthExecutionAPI>,
913
+ transaction: Transaction,
914
+ blockNumber: BlockNumberOrTag = web3Context.defaultBlock,
915
+ returnFormat: ReturnFormat,
916
+ ) {
917
+ const transactionFormatted = formatTransaction(transaction, ETH_DATA_FORMAT, {
918
+ transactionSchema: web3Context.config.customTransactionSchema,
919
+ });
920
+ const blockNumberFormatted = isBlockTag(blockNumber as string)
921
+ ? (blockNumber as BlockTag)
922
+ : format({ format: 'uint' }, blockNumber as Numbers, ETH_DATA_FORMAT);
923
+
924
+ const response = await ethRpcMethods.estimateGas(
925
+ web3Context.requestManager,
926
+ transactionFormatted,
927
+ blockNumberFormatted,
928
+ );
929
+
930
+ return format(
931
+ { format: 'uint' },
932
+ response as Numbers,
933
+ returnFormat ?? web3Context.defaultReturnFormat,
934
+ );
935
+ }
936
+
937
+ // TODO - Add input formatting to filter
938
+ /**
939
+ * View additional documentations here: {@link Web3Eth.getPastLogs}
940
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
941
+ */
942
+ export async function getLogs<ReturnFormat extends DataFormat>(
943
+ web3Context: Web3Context<Web3EthExecutionAPI>,
944
+ filter: Filter,
945
+ returnFormat: ReturnFormat,
946
+ ) {
947
+ // format type bigint or number toBlock and fromBlock to hexstring.
948
+ let { toBlock, fromBlock } = filter;
949
+ if (!isNullish(toBlock)) {
950
+ if (typeof toBlock === 'number' || typeof toBlock === 'bigint') {
951
+ toBlock = numberToHex(toBlock);
952
+ }
953
+ }
954
+ if (!isNullish(fromBlock)) {
955
+ if (typeof fromBlock === 'number' || typeof fromBlock === 'bigint') {
956
+ fromBlock = numberToHex(fromBlock);
957
+ }
958
+ }
959
+
960
+ const formattedFilter = { ...filter, fromBlock, toBlock };
961
+
962
+ const response = await ethRpcMethods.getLogs(web3Context.requestManager, formattedFilter);
963
+
964
+ const result = response.map(res => {
965
+ if (typeof res === 'string') {
966
+ return res;
967
+ }
968
+
969
+ return format(
970
+ logSchema,
971
+ res as unknown as Log,
972
+ returnFormat ?? web3Context.defaultReturnFormat,
973
+ );
974
+ });
975
+
976
+ return result;
977
+ }
978
+
979
+ /**
980
+ * View additional documentations here: {@link Web3Eth.getChainId}
981
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
982
+ */
983
+ export async function getChainId<ReturnFormat extends DataFormat>(
984
+ web3Context: Web3Context<EthExecutionAPI>,
985
+ returnFormat: ReturnFormat,
986
+ ) {
987
+ const response = await ethRpcMethods.getChainId(web3Context.requestManager);
988
+
989
+ return format(
990
+ { format: 'uint' },
991
+ // Response is number in hex formatted string
992
+ response as unknown as number,
993
+ returnFormat ?? web3Context.defaultReturnFormat,
994
+ );
995
+ }
996
+
997
+ /**
998
+ * View additional documentations here: {@link Web3Eth.getProof}
999
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
1000
+ */
1001
+ export async function getProof<ReturnFormat extends DataFormat>(
1002
+ web3Context: Web3Context<Web3EthExecutionAPI>,
1003
+ address: Address,
1004
+ storageKeys: Bytes[],
1005
+ blockNumber: BlockNumberOrTag = web3Context.defaultBlock,
1006
+ returnFormat: ReturnFormat,
1007
+ ) {
1008
+ const storageKeysFormatted = storageKeys.map(storageKey =>
1009
+ format({ format: 'bytes' }, storageKey, ETH_DATA_FORMAT),
1010
+ );
1011
+
1012
+ const blockNumberFormatted = isBlockTag(blockNumber as string)
1013
+ ? (blockNumber as BlockTag)
1014
+ : format({ format: 'uint' }, blockNumber as Numbers, ETH_DATA_FORMAT);
1015
+
1016
+ const response = await ethRpcMethods.getProof(
1017
+ web3Context.requestManager,
1018
+ address,
1019
+ storageKeysFormatted,
1020
+ blockNumberFormatted,
1021
+ );
1022
+
1023
+ return format(
1024
+ accountSchema,
1025
+ response as unknown as AccountObject,
1026
+ returnFormat ?? web3Context.defaultReturnFormat,
1027
+ );
1028
+ }
1029
+
1030
+ // TODO Throwing an error with Geth, but not Infura
1031
+ // TODO gasUsedRatio and reward not formatting
1032
+ /**
1033
+ * View additional documentations here: {@link Web3Eth.getFeeHistory}
1034
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
1035
+ */
1036
+ export async function getFeeHistory<ReturnFormat extends DataFormat>(
1037
+ web3Context: Web3Context<EthExecutionAPI>,
1038
+ blockCount: Numbers,
1039
+ newestBlock: BlockNumberOrTag = web3Context.defaultBlock,
1040
+ rewardPercentiles: Numbers[],
1041
+ returnFormat: ReturnFormat,
1042
+ ) {
1043
+ const blockCountFormatted = format({ format: 'uint' }, blockCount, ETH_DATA_FORMAT);
1044
+
1045
+ const newestBlockFormatted = isBlockTag(newestBlock as string)
1046
+ ? (newestBlock as BlockTag)
1047
+ : format({ format: 'uint' }, newestBlock as Numbers, ETH_DATA_FORMAT);
1048
+
1049
+ const rewardPercentilesFormatted = format(
1050
+ {
1051
+ type: 'array',
1052
+ items: {
1053
+ format: 'uint',
1054
+ },
1055
+ },
1056
+ rewardPercentiles,
1057
+ NUMBER_DATA_FORMAT,
1058
+ );
1059
+
1060
+ const response = await ethRpcMethods.getFeeHistory(
1061
+ web3Context.requestManager,
1062
+ blockCountFormatted,
1063
+ newestBlockFormatted,
1064
+ rewardPercentilesFormatted,
1065
+ );
1066
+
1067
+ return format(
1068
+ feeHistorySchema,
1069
+ response as unknown as FeeHistory,
1070
+ returnFormat ?? web3Context.defaultReturnFormat,
1071
+ );
1072
+ }
1073
+
1074
+ /**
1075
+ * View additional documentations here: {@link Web3Eth.createAccessList}
1076
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
1077
+ */
1078
+ export async function createAccessList<ReturnFormat extends DataFormat>(
1079
+ web3Context: Web3Context<EthExecutionAPI>,
1080
+ transaction: TransactionForAccessList,
1081
+ blockNumber: BlockNumberOrTag = web3Context.defaultBlock,
1082
+ returnFormat: ReturnFormat,
1083
+ ) {
1084
+ const blockNumberFormatted = isBlockTag(blockNumber as string)
1085
+ ? (blockNumber as BlockTag)
1086
+ : format({ format: 'uint' }, blockNumber as Numbers, ETH_DATA_FORMAT);
1087
+
1088
+ const response = (await ethRpcMethods.createAccessList(
1089
+ web3Context.requestManager,
1090
+ formatTransaction(transaction, ETH_DATA_FORMAT, {
1091
+ transactionSchema: web3Context.config.customTransactionSchema,
1092
+ }),
1093
+ blockNumberFormatted,
1094
+ )) as unknown as AccessListResult;
1095
+
1096
+ return format(
1097
+ accessListResultSchema,
1098
+ response,
1099
+ returnFormat ?? web3Context.defaultReturnFormat,
1100
+ );
1101
+ }
1102
+
1103
+ /**
1104
+ * View additional documentations here: {@link Web3Eth.signTypedData}
1105
+ * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc.
1106
+ */
1107
+ export async function signTypedData<ReturnFormat extends DataFormat>(
1108
+ web3Context: Web3Context<EthExecutionAPI>,
1109
+ address: Address,
1110
+ typedData: Eip712TypedData,
1111
+ useLegacy: boolean,
1112
+ returnFormat: ReturnFormat,
1113
+ ) {
1114
+ const response = await ethRpcMethods.signTypedData(
1115
+ web3Context.requestManager,
1116
+ address,
1117
+ typedData,
1118
+ useLegacy,
1119
+ );
1120
+
1121
+ return format({ format: 'bytes' }, response, returnFormat ?? web3Context.defaultReturnFormat);
1122
+ }