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,117 @@
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
+ import { Web3Context } from 'web3-core';
19
+ import {
20
+ EthExecutionAPI,
21
+ Numbers,
22
+ Transaction,
23
+ DataFormat,
24
+ FormatType,
25
+ ETH_DATA_FORMAT,
26
+ } from 'web3-types';
27
+ import { isNullish } from 'web3-validator';
28
+ import { Eip1559NotSupportedError, UnsupportedTransactionTypeError } from 'web3-errors';
29
+ import { format } from 'web3-utils';
30
+ // eslint-disable-next-line import/no-cycle
31
+ import { getBlock, getGasPrice } from '../rpc_method_wrappers.js';
32
+ import { InternalTransaction } from '../types.js';
33
+ // eslint-disable-next-line import/no-cycle
34
+ import { getTransactionType } from './transaction_builder.js';
35
+
36
+ async function getEip1559GasPricing<ReturnFormat extends DataFormat>(
37
+ transaction: FormatType<Transaction, typeof ETH_DATA_FORMAT>,
38
+ web3Context: Web3Context<EthExecutionAPI>,
39
+ returnFormat: ReturnFormat,
40
+ ): Promise<FormatType<{ maxPriorityFeePerGas?: Numbers; maxFeePerGas?: Numbers }, ReturnFormat>> {
41
+ const block = await getBlock(web3Context, web3Context.defaultBlock, false, ETH_DATA_FORMAT);
42
+ if (isNullish(block.baseFeePerGas)) throw new Eip1559NotSupportedError();
43
+
44
+ let gasPrice: Numbers | undefined;
45
+ if (isNullish(transaction.gasPrice) && BigInt(block.baseFeePerGas) === BigInt(0)) {
46
+ gasPrice = await getGasPrice(web3Context, returnFormat);
47
+ }
48
+ if (!isNullish(transaction.gasPrice) || !isNullish(gasPrice)) {
49
+ const convertedTransactionGasPrice = format(
50
+ { format: 'uint' },
51
+ transaction.gasPrice ?? gasPrice,
52
+ returnFormat,
53
+ );
54
+
55
+ return {
56
+ maxPriorityFeePerGas: convertedTransactionGasPrice,
57
+ maxFeePerGas: convertedTransactionGasPrice,
58
+ };
59
+ }
60
+ return {
61
+ maxPriorityFeePerGas: format(
62
+ { format: 'uint' },
63
+ transaction.maxPriorityFeePerGas ?? web3Context.defaultMaxPriorityFeePerGas,
64
+ returnFormat,
65
+ ),
66
+ maxFeePerGas: format(
67
+ { format: 'uint' },
68
+ (transaction.maxFeePerGas ??
69
+ BigInt(block.baseFeePerGas) * BigInt(2) +
70
+ BigInt(
71
+ transaction.maxPriorityFeePerGas ?? web3Context.defaultMaxPriorityFeePerGas,
72
+ )) as Numbers,
73
+ returnFormat,
74
+ ),
75
+ };
76
+ }
77
+
78
+ export async function getTransactionGasPricing<ReturnFormat extends DataFormat>(
79
+ transaction: InternalTransaction,
80
+ web3Context: Web3Context<EthExecutionAPI>,
81
+ returnFormat: ReturnFormat,
82
+ ): Promise<
83
+ | FormatType<
84
+ { gasPrice?: Numbers; maxPriorityFeePerGas?: Numbers; maxFeePerGas?: Numbers },
85
+ ReturnFormat
86
+ >
87
+ | undefined
88
+ > {
89
+ const transactionType = getTransactionType(transaction, web3Context);
90
+ if (!isNullish(transactionType)) {
91
+ if (transactionType.startsWith('-'))
92
+ throw new UnsupportedTransactionTypeError(transactionType);
93
+
94
+ // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2718.md#transactions
95
+ if (Number(transactionType) < 0 || Number(transactionType) > 127)
96
+ throw new UnsupportedTransactionTypeError(transactionType);
97
+
98
+ if (
99
+ isNullish(transaction.gasPrice) &&
100
+ (transactionType === '0x0' || transactionType === '0x1')
101
+ )
102
+ return {
103
+ gasPrice: await getGasPrice(web3Context, returnFormat),
104
+ maxPriorityFeePerGas: undefined,
105
+ maxFeePerGas: undefined,
106
+ };
107
+
108
+ if (transactionType === '0x2') {
109
+ return {
110
+ gasPrice: undefined,
111
+ ...(await getEip1559GasPricing(transaction, web3Context, returnFormat)),
112
+ };
113
+ }
114
+ }
115
+
116
+ return undefined;
117
+ }
@@ -0,0 +1,21 @@
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
+ export * from './detect_transaction_type.js';
19
+ export * from './format_transaction.js';
20
+ export * from './prepare_transaction_for_signing.js';
21
+ export * from './transaction_builder.js';
@@ -0,0 +1,154 @@
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
+ import {
19
+ EthExecutionAPI,
20
+ HexString,
21
+ PopulatedUnsignedEip1559Transaction,
22
+ PopulatedUnsignedEip2930Transaction,
23
+ PopulatedUnsignedTransaction,
24
+ Transaction,
25
+ ValidChains,
26
+ FormatType,
27
+ ETH_DATA_FORMAT,
28
+ } from 'web3-types';
29
+ import { Web3Context } from 'web3-core';
30
+ import { toNumber } from 'web3-utils';
31
+ import { TransactionFactory, TxOptions, Common } from 'web3-eth-accounts';
32
+ import { isNullish } from 'web3-validator';
33
+ import { validateTransactionForSigning } from '../validation.js';
34
+ import { formatTransaction } from './format_transaction.js';
35
+ import { transactionBuilder } from './transaction_builder.js';
36
+
37
+ const getEthereumjsTxDataFromTransaction = (
38
+ transaction: FormatType<PopulatedUnsignedTransaction, typeof ETH_DATA_FORMAT>,
39
+ ) => ({
40
+ ...transaction,
41
+ nonce: transaction.nonce,
42
+ gasPrice: transaction.gasPrice,
43
+ gasLimit: transaction.gasLimit ?? transaction.gas,
44
+ to: transaction.to,
45
+ value: transaction.value,
46
+ data: transaction.data ?? transaction.input,
47
+ type: transaction.type,
48
+ chainId: transaction.chainId,
49
+ accessList: (
50
+ transaction as FormatType<PopulatedUnsignedEip2930Transaction, typeof ETH_DATA_FORMAT>
51
+ ).accessList,
52
+ maxPriorityFeePerGas: (
53
+ transaction as FormatType<PopulatedUnsignedEip1559Transaction, typeof ETH_DATA_FORMAT>
54
+ ).maxPriorityFeePerGas,
55
+ maxFeePerGas: (
56
+ transaction as FormatType<PopulatedUnsignedEip1559Transaction, typeof ETH_DATA_FORMAT>
57
+ ).maxFeePerGas,
58
+ });
59
+
60
+ const getEthereumjsTransactionOptions = (
61
+ transaction: FormatType<PopulatedUnsignedTransaction, typeof ETH_DATA_FORMAT>,
62
+ web3Context: Web3Context<EthExecutionAPI>,
63
+ ) => {
64
+ const hasTransactionSigningOptions =
65
+ (!isNullish(transaction.chain) && !isNullish(transaction.hardfork)) ||
66
+ !isNullish(transaction.common);
67
+
68
+ let common;
69
+ if (!hasTransactionSigningOptions) {
70
+ // if defaultcommon is specified, use that.
71
+ if (web3Context.defaultCommon) {
72
+ common = { ...web3Context.defaultCommon };
73
+
74
+ if (isNullish(common.hardfork))
75
+ common.hardfork = transaction.hardfork ?? web3Context.defaultHardfork;
76
+ if (isNullish(common.baseChain))
77
+ common.baseChain = web3Context.defaultChain as ValidChains;
78
+ } else {
79
+ common = Common.custom(
80
+ {
81
+ name: 'custom-network',
82
+ chainId: toNumber(transaction.chainId) as number,
83
+ networkId: !isNullish(transaction.networkId)
84
+ ? (toNumber(transaction.networkId) as number)
85
+ : undefined,
86
+ defaultHardfork: transaction.hardfork ?? web3Context.defaultHardfork,
87
+ },
88
+ {
89
+ baseChain: web3Context.defaultChain,
90
+ },
91
+ );
92
+ }
93
+ } else {
94
+ const name =
95
+ transaction?.common?.customChain?.name ?? transaction.chain ?? 'custom-network';
96
+ const chainId = toNumber(
97
+ transaction?.common?.customChain?.chainId ?? transaction?.chainId,
98
+ ) as number;
99
+ const networkId = toNumber(
100
+ transaction?.common?.customChain?.networkId ?? transaction?.networkId,
101
+ ) as number;
102
+ const defaultHardfork =
103
+ transaction?.common?.hardfork ?? transaction?.hardfork ?? web3Context.defaultHardfork;
104
+ const baseChain =
105
+ transaction.common?.baseChain ?? transaction.chain ?? web3Context.defaultChain;
106
+
107
+ if (chainId && networkId && name) {
108
+ common = Common.custom(
109
+ {
110
+ name,
111
+ chainId,
112
+ networkId,
113
+ defaultHardfork,
114
+ },
115
+ {
116
+ baseChain,
117
+ },
118
+ );
119
+ }
120
+ }
121
+ return { common } as TxOptions;
122
+ };
123
+
124
+ export const prepareTransactionForSigning = async (
125
+ transaction: Transaction,
126
+ web3Context: Web3Context<EthExecutionAPI>,
127
+ privateKey?: HexString | Uint8Array,
128
+ fillGasPrice = false,
129
+ fillGasLimit = true,
130
+ ) => {
131
+ const populatedTransaction = (await transactionBuilder({
132
+ transaction,
133
+ web3Context,
134
+ privateKey,
135
+ fillGasPrice,
136
+ fillGasLimit,
137
+ })) as unknown as PopulatedUnsignedTransaction;
138
+ const formattedTransaction = formatTransaction(populatedTransaction, ETH_DATA_FORMAT, {
139
+ transactionSchema: web3Context.config.customTransactionSchema,
140
+ }) as unknown as FormatType<PopulatedUnsignedTransaction, typeof ETH_DATA_FORMAT>;
141
+
142
+ validateTransactionForSigning(
143
+ formattedTransaction as unknown as FormatType<Transaction, typeof ETH_DATA_FORMAT>,
144
+ undefined,
145
+ {
146
+ transactionSchema: web3Context.config.customTransactionSchema,
147
+ },
148
+ );
149
+
150
+ return TransactionFactory.fromTxData(
151
+ getEthereumjsTxDataFromTransaction(formattedTransaction),
152
+ getEthereumjsTransactionOptions(formattedTransaction, web3Context),
153
+ );
154
+ };
@@ -0,0 +1,176 @@
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
+ import { EthExecutionAPI, Bytes, Web3BaseProvider, BlockHeaderOutput } from 'web3-types';
18
+ import { Web3Context } from 'web3-core';
19
+ import { rejectIfConditionAtInterval } from 'web3-utils';
20
+
21
+ import { TransactionBlockTimeoutError } from 'web3-errors';
22
+ import { NUMBER_DATA_FORMAT } from '../constants.js';
23
+ // eslint-disable-next-line import/no-cycle
24
+ import { getBlockNumber } from '../rpc_method_wrappers.js';
25
+ import { NewHeadsSubscription } from '../web3_subscriptions.js';
26
+
27
+ export interface ResourceCleaner {
28
+ clean: () => void;
29
+ }
30
+
31
+ function resolveByPolling(
32
+ web3Context: Web3Context<EthExecutionAPI>,
33
+ starterBlockNumber: number,
34
+ transactionHash?: Bytes,
35
+ ): [Promise<never>, ResourceCleaner] {
36
+ const pollingInterval = web3Context.transactionPollingInterval;
37
+ const [intervalId, promiseToError] = rejectIfConditionAtInterval(async () => {
38
+ let lastBlockNumber;
39
+ try {
40
+ lastBlockNumber = await getBlockNumber(web3Context, NUMBER_DATA_FORMAT);
41
+ } catch (error) {
42
+ console.warn('An error happen while trying to get the block number', error);
43
+ return undefined;
44
+ }
45
+ const numberOfBlocks = lastBlockNumber - starterBlockNumber;
46
+ if (numberOfBlocks >= web3Context.transactionBlockTimeout) {
47
+ return new TransactionBlockTimeoutError({
48
+ starterBlockNumber,
49
+ numberOfBlocks,
50
+ transactionHash,
51
+ });
52
+ }
53
+ return undefined;
54
+ }, pollingInterval);
55
+
56
+ const clean = () => {
57
+ clearInterval(intervalId);
58
+ };
59
+
60
+ return [promiseToError, { clean }];
61
+ }
62
+
63
+ async function resolveBySubscription(
64
+ web3Context: Web3Context<EthExecutionAPI>,
65
+ starterBlockNumber: number,
66
+ transactionHash?: Bytes,
67
+ ): Promise<[Promise<never>, ResourceCleaner]> {
68
+ // The following variable will stay true except if the data arrived,
69
+ // or if watching started after an error had occurred.
70
+ let needToWatchLater = true;
71
+
72
+ let subscription: NewHeadsSubscription;
73
+ let resourceCleaner: ResourceCleaner;
74
+ // internal helper function
75
+ function revertToPolling(
76
+ reject: (value: Error | PromiseLike<Error>) => void,
77
+ previousError?: Error,
78
+ ) {
79
+ if (previousError) {
80
+ console.warn('error happened at subscription. So revert to polling...', previousError);
81
+ }
82
+ resourceCleaner.clean();
83
+
84
+ needToWatchLater = false;
85
+ const [promiseToError, newResourceCleaner] = resolveByPolling(
86
+ web3Context,
87
+ starterBlockNumber,
88
+ transactionHash,
89
+ );
90
+ resourceCleaner.clean = newResourceCleaner.clean;
91
+ promiseToError.catch(error => reject(error as Error));
92
+ }
93
+ try {
94
+ subscription = (await web3Context.subscriptionManager?.subscribe(
95
+ 'newHeads',
96
+ )) as unknown as NewHeadsSubscription;
97
+ resourceCleaner = {
98
+ clean: () => {
99
+ // Remove the subscription, if it was not removed somewhere
100
+ // else by calling, for example, subscriptionManager.clear()
101
+ if (subscription.id) {
102
+ web3Context.subscriptionManager
103
+ ?.removeSubscription(subscription)
104
+ .then(() => {
105
+ // Subscription ended successfully
106
+ })
107
+ .catch(() => {
108
+ // An error happened while ending subscription. But no need to take any action.
109
+ });
110
+ }
111
+ },
112
+ };
113
+ } catch (error) {
114
+ return resolveByPolling(web3Context, starterBlockNumber, transactionHash);
115
+ }
116
+ const promiseToError: Promise<never> = new Promise((_, reject) => {
117
+ try {
118
+ subscription.on('data', (lastBlockHeader: BlockHeaderOutput) => {
119
+ needToWatchLater = false;
120
+ if (!lastBlockHeader?.number) {
121
+ return;
122
+ }
123
+ const numberOfBlocks = Number(
124
+ BigInt(lastBlockHeader.number) - BigInt(starterBlockNumber),
125
+ );
126
+
127
+ if (numberOfBlocks >= web3Context.transactionBlockTimeout) {
128
+ // Transaction Block Timeout is known to be reached by subscribing to new heads
129
+ reject(
130
+ new TransactionBlockTimeoutError({
131
+ starterBlockNumber,
132
+ numberOfBlocks,
133
+ transactionHash,
134
+ }),
135
+ );
136
+ }
137
+ });
138
+ subscription.on('error', error => {
139
+ revertToPolling(reject, error);
140
+ });
141
+ } catch (error) {
142
+ revertToPolling(reject, error as Error);
143
+ }
144
+
145
+ // Fallback to polling if tx receipt didn't arrived in "blockHeaderTimeout" [10 seconds]
146
+ setTimeout(() => {
147
+ if (needToWatchLater) {
148
+ revertToPolling(reject);
149
+ }
150
+ }, web3Context.blockHeaderTimeout * 1000);
151
+ });
152
+
153
+ return [promiseToError, resourceCleaner];
154
+ }
155
+
156
+ /* TODO: After merge, there will be constant block mining time (exactly 12 second each block, except slot missed that currently happens in <1% of slots. ) so we can optimize following function
157
+ for POS NWs, we can skip checking getBlockNumber(); after interval and calculate only based on time that certain num of blocked are mined after that for internal double check, can do one getBlockNumber() call and timeout.
158
+ */
159
+ export async function rejectIfBlockTimeout(
160
+ web3Context: Web3Context<EthExecutionAPI>,
161
+ transactionHash?: Bytes,
162
+ ): Promise<[Promise<never>, ResourceCleaner]> {
163
+ const { provider } = web3Context.requestManager;
164
+ let callingRes: [Promise<never>, ResourceCleaner];
165
+ const starterBlockNumber = await getBlockNumber(web3Context, NUMBER_DATA_FORMAT);
166
+ // TODO: once https://github.com/web3/web3.js/issues/5521 is implemented, remove checking for `enableExperimentalFeatures.useSubscriptionWhenCheckingBlockTimeout`
167
+ if (
168
+ (provider as Web3BaseProvider).supportsSubscriptions?.() &&
169
+ web3Context.enableExperimentalFeatures.useSubscriptionWhenCheckingBlockTimeout
170
+ ) {
171
+ callingRes = await resolveBySubscription(web3Context, starterBlockNumber, transactionHash);
172
+ } else {
173
+ callingRes = resolveByPolling(web3Context, starterBlockNumber, transactionHash);
174
+ }
175
+ return callingRes;
176
+ }