@@ -16,7 +16,6 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
16
*/
17
17
18
18
import {
19
- Common ,
20
19
EthExecutionAPI ,
21
20
HexString ,
22
21
Web3NetAPI ,
@@ -33,6 +32,7 @@ import {
33
32
AccessListEIP2930Transaction ,
34
33
FeeMarketEIP1559Transaction ,
35
34
Transaction ,
35
+ Hardfork ,
36
36
} from 'web3-eth-accounts' ;
37
37
import { prepareTransactionForSigning } from '../../src/utils/prepare_transaction_for_signing' ;
38
38
import { validTransactions } from '../fixtures/prepare_transaction_for_signing' ;
@@ -70,7 +70,7 @@ describe('prepareTransactionForSigning', () => {
70
70
71
71
if ( isNullish ( tx . common ) ) {
72
72
if ( options . web3Context . defaultCommon ) {
73
- const common = options . web3Context . defaultCommon as unknown as Common ;
73
+ const common = options . web3Context . defaultCommon ;
74
74
const chainId = common . customChain . chainId as string ;
75
75
const networkId = common . customChain . networkId as string ;
76
76
const name = common . customChain . name as string ;
@@ -102,6 +102,153 @@ describe('prepareTransactionForSigning', () => {
102
102
expect ( ethereumjsTx . common . chainName ( ) ) . toBe ( 'test' ) ;
103
103
} ) ;
104
104
} ) ;
105
+
106
+ it ( 'should be able to read Hardfork from context.defaultHardfork' , async ( ) => {
107
+ const context = new Web3Context < EthExecutionAPI > ( {
108
+ provider : new HttpProvider ( 'http://127.0.0.1' ) ,
109
+ config : { defaultNetworkId : '0x9' } ,
110
+ } ) ;
111
+ context . defaultChain = 'mainnet' ;
112
+ context . defaultHardfork = Hardfork . Istanbul ;
113
+
114
+ async function transactionBuilder < ReturnType = TransactionType > ( options : {
115
+ transaction : TransactionType ;
116
+ web3Context : Web3Context < EthExecutionAPI & Web3NetAPI > ;
117
+ privateKey ?: HexString | Uint8Array ;
118
+ fillGasPrice ?: boolean ;
119
+ fillGasLimit ?: boolean ;
120
+ } ) : Promise < ReturnType > {
121
+ const tx = { ...options . transaction } ;
122
+ return tx as unknown as ReturnType ;
123
+ }
124
+
125
+ context . transactionBuilder = transactionBuilder ;
126
+
127
+ const ethereumjsTx = await prepareTransactionForSigning (
128
+ {
129
+ chainId : 1458 ,
130
+ nonce : 1 ,
131
+ gasPrice : BigInt ( 20000000000 ) ,
132
+ gas : BigInt ( 21000 ) ,
133
+ to : '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55' ,
134
+ from : '0x2c7536E3605D9C16a7a3D7b1898e529396a65c23' ,
135
+ value : '1000000000' ,
136
+ input : '' ,
137
+ networkId : 999 ,
138
+ } ,
139
+ context ,
140
+ ) ;
141
+ expect ( ethereumjsTx . common . hardfork ( ) ) . toBe ( Hardfork . Istanbul ) ;
142
+ expect ( ethereumjsTx . common . networkId ( ) . toString ( ) ) . toBe ( '999' ) ;
143
+ } ) ;
144
+
145
+ it ( 'should be able to read Hardfork from context.config.defaultHardfork and context.defaultCommon.hardfork' , async ( ) => {
146
+ const context = new Web3Context < EthExecutionAPI > ( {
147
+ provider : new HttpProvider ( 'http://127.0.0.1' ) ,
148
+ config : { defaultNetworkId : '0x9' } ,
149
+ } ) ;
150
+ context . defaultChain = 'mainnet' ;
151
+
152
+ // if the value here is different from the one in context.defaultCommon.hardfork
153
+ // Then an error will be thrown:
154
+ // "ConfigHardforkMismatchError: Web3Config hardfork doesnt match in defaultHardfork london and common.hardfork istanbul"
155
+ context . config . defaultHardfork = Hardfork . Istanbul ;
156
+ context . defaultCommon = {
157
+ customChain : {
158
+ name : 'test' ,
159
+ networkId : 111 ,
160
+ chainId : 1458 ,
161
+ } ,
162
+ hardfork : Hardfork . Istanbul ,
163
+ baseChain : 'mainnet' ,
164
+ } as any ;
165
+
166
+ async function transactionBuilder < ReturnType = TransactionType > ( options : {
167
+ transaction : TransactionType ;
168
+ web3Context : Web3Context < EthExecutionAPI & Web3NetAPI > ;
169
+ privateKey ?: HexString | Uint8Array ;
170
+ fillGasPrice ?: boolean ;
171
+ fillGasLimit ?: boolean ;
172
+ } ) : Promise < ReturnType > {
173
+ const tx = { ...options . transaction } ;
174
+ return tx as unknown as ReturnType ;
175
+ }
176
+
177
+ context . transactionBuilder = transactionBuilder ;
178
+
179
+ const ethereumjsTx = await prepareTransactionForSigning (
180
+ {
181
+ chainId : 1458 ,
182
+ nonce : 1 ,
183
+ gasPrice : BigInt ( 20000000000 ) ,
184
+ gas : BigInt ( 21000 ) ,
185
+ to : '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55' ,
186
+ from : '0x2c7536E3605D9C16a7a3D7b1898e529396a65c23' ,
187
+ value : '1000000000' ,
188
+ input : '' ,
189
+ } ,
190
+ context ,
191
+ ) ;
192
+ expect ( ethereumjsTx . common . hardfork ( ) ) . toBe ( Hardfork . Istanbul ) ;
193
+ expect ( ethereumjsTx . common . networkId ( ) . toString ( ) ) . toBe ( '111' ) ;
194
+ } ) ;
195
+
196
+ it ( 'should give priorities to tx.hardfork and tx.networkId over values from context' , async ( ) => {
197
+ const context = new Web3Context < EthExecutionAPI > ( {
198
+ provider : new HttpProvider ( 'http://127.0.0.1' ) ,
199
+ config : { defaultNetworkId : '0x9' } ,
200
+ } ) ;
201
+ context . defaultChain = 'mainnet' ;
202
+
203
+ // if the value here is different from the one in context.defaultCommon.hardfork
204
+ // Then an error will be thrown:
205
+ // "ConfigHardforkMismatchError: Web3Config hardfork doesnt match in defaultHardfork london and common.hardfork istanbul"
206
+ context . config . defaultHardfork = Hardfork . Istanbul ;
207
+ context . defaultCommon = {
208
+ customChain : {
209
+ name : 'test' ,
210
+ networkId : 111 ,
211
+ chainId : 1458 ,
212
+ } ,
213
+ hardfork : Hardfork . Istanbul ,
214
+ baseChain : 'mainnet' ,
215
+ } as any ;
216
+
217
+ async function transactionBuilder < ReturnType = TransactionType > ( options : {
218
+ transaction : TransactionType ;
219
+ web3Context : Web3Context < EthExecutionAPI & Web3NetAPI > ;
220
+ privateKey ?: HexString | Uint8Array ;
221
+ fillGasPrice ?: boolean ;
222
+ fillGasLimit ?: boolean ;
223
+ } ) : Promise < ReturnType > {
224
+ const tx = { ...options . transaction } ;
225
+ return tx as unknown as ReturnType ;
226
+ }
227
+
228
+ context . transactionBuilder = transactionBuilder ;
229
+
230
+ // context.transactionBuilder = defaultTransactionBuilder;
231
+
232
+ const ethereumjsTx = await prepareTransactionForSigning (
233
+ {
234
+ chainId : 1458 ,
235
+ nonce : 1 ,
236
+ gasPrice : BigInt ( 20000000000 ) ,
237
+ gas : BigInt ( 21000 ) ,
238
+ to : '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55' ,
239
+ from : '0x2c7536E3605D9C16a7a3D7b1898e529396a65c23' ,
240
+ value : '1000000000' ,
241
+ input : '' ,
242
+ networkId : 999 ,
243
+ hardfork : Hardfork . Chainstart ,
244
+ chain : 'mainnet' ,
245
+ } ,
246
+ context ,
247
+ ) ;
248
+ expect ( ethereumjsTx . common . hardfork ( ) ) . toBe ( Hardfork . Chainstart ) ;
249
+ expect ( ethereumjsTx . common . networkId ( ) . toString ( ) ) . toBe ( '999' ) ;
250
+ } ) ;
251
+
105
252
describe ( 'should return an web3-utils/tx instance with expected properties' , ( ) => {
106
253
it . each ( validTransactions ) (
107
254
'mockBlock: %s\nexpectedTransaction: %s\nexpectedPrivateKey: %s\nexpectedAddress: %s\nexpectedRlpEncodedTransaction: %s\nexpectedTransactionHash: %s\nexpectedMessageToSign: %s\nexpectedV: %s\nexpectedR: %s\nexpectedS: %s' ,
0 commit comments