@@ -62,14 +62,19 @@ export const DEFAULT_COINBASE = "0xc014ba5ec014ba5ec014ba5ec014ba5ec014ba5e";
62
62
let _globalEdrContext : EdrContext | undefined ;
63
63
64
64
// Lazy initialize the global EDR context.
65
- export function getGlobalEdrContext ( ) : EdrContext {
66
- const { EdrContext } = requireNapiRsModule (
67
- "@nomicfoundation/edr"
68
- ) as typeof import ( "@nomicfoundation/edr" ) ;
65
+ export async function getGlobalEdrContext ( ) : Promise < EdrContext > {
66
+ const { EdrContext, GENERIC_CHAIN_TYPE , genericChainProviderFactory } =
67
+ requireNapiRsModule (
68
+ "@nomicfoundation/edr"
69
+ ) as typeof import ( "@nomicfoundation/edr" ) ;
69
70
70
71
if ( _globalEdrContext === undefined ) {
71
72
// Only one is allowed to exist
72
73
_globalEdrContext = new EdrContext ( ) ;
74
+ await _globalEdrContext . registerProviderFactory (
75
+ GENERIC_CHAIN_TYPE ,
76
+ genericChainProviderFactory ( )
77
+ ) ;
73
78
}
74
79
75
80
return _globalEdrContext ;
@@ -133,9 +138,10 @@ export class EdrProviderWrapper
133
138
loggerConfig : LoggerConfig ,
134
139
tracingConfig ?: TracingConfigWithBuffers
135
140
) : Promise < EdrProviderWrapper > {
136
- const { Provider } = requireNapiRsModule (
137
- "@nomicfoundation/edr"
138
- ) as typeof import ( "@nomicfoundation/edr" ) ;
141
+ const { GENERIC_CHAIN_TYPE , l1GenesisState, l1HardforkFromString } =
142
+ requireNapiRsModule (
143
+ "@nomicfoundation/edr"
144
+ ) as typeof import ( "@nomicfoundation/edr" ) ;
139
145
140
146
const coinbase = config . coinbase ?? DEFAULT_COINBASE ;
141
147
@@ -179,8 +185,16 @@ export class EdrProviderWrapper
179
185
180
186
const hardforkName = getHardforkName ( config . hardfork ) ;
181
187
182
- const provider = await Provider . withConfig (
183
- getGlobalEdrContext ( ) ,
188
+ const genesisState =
189
+ fork !== undefined
190
+ ? [ ] // TODO: Add support for overriding remote fork state when the local fork is different
191
+ : l1GenesisState (
192
+ l1HardforkFromString ( ethereumsjsHardforkToEdrSpecId ( hardforkName ) )
193
+ ) ;
194
+
195
+ const context = await getGlobalEdrContext ( ) ;
196
+ const provider = await context . createProvider (
197
+ GENERIC_CHAIN_TYPE ,
184
198
{
185
199
allowBlocksWithSameTimestamp :
186
200
config . allowBlocksWithSameTimestamp ?? false ,
@@ -209,13 +223,8 @@ export class EdrProviderWrapper
209
223
coinbase : Buffer . from ( coinbase . slice ( 2 ) , "hex" ) ,
210
224
enableRip7212 : config . enableRip7212 ,
211
225
fork,
226
+ genesisState,
212
227
hardfork : ethereumsjsHardforkToEdrSpecId ( hardforkName ) ,
213
- genesisAccounts : config . genesisAccounts . map ( ( account ) => {
214
- return {
215
- secretKey : account . privateKey ,
216
- balance : BigInt ( account . balance ) ,
217
- } ;
218
- } ) ,
219
228
initialDate,
220
229
initialBaseFeePerGas :
221
230
config . initialBaseFeePerGas !== undefined
@@ -230,6 +239,12 @@ export class EdrProviderWrapper
230
239
} ,
231
240
} ,
232
241
networkId : BigInt ( config . networkId ) ,
242
+ ownedAccounts : config . genesisAccounts . map ( ( account ) => {
243
+ return {
244
+ secretKey : account . privateKey ,
245
+ balance : BigInt ( account . balance ) ,
246
+ } ;
247
+ } ) ,
233
248
} ,
234
249
{
235
250
enable : loggerConfig . enabled ,
@@ -242,10 +257,12 @@ export class EdrProviderWrapper
242
257
}
243
258
} ,
244
259
} ,
245
- tracingConfig ?? { } ,
246
- ( event : SubscriptionEvent ) => {
247
- eventAdapter . emit ( "ethEvent" , event ) ;
248
- }
260
+ {
261
+ subscriptionCallback : ( event : SubscriptionEvent ) => {
262
+ eventAdapter . emit ( "ethEvent" , event ) ;
263
+ } ,
264
+ } ,
265
+ tracingConfig ?? { }
249
266
) ;
250
267
251
268
const minimalEthereumJsNode = {
@@ -398,18 +415,20 @@ export class EdrProviderWrapper
398
415
}
399
416
400
417
// temporarily added to make smock work with HH+EDR
401
- private _setCallOverrideCallback ( callback : CallOverrideCallback ) {
418
+ private async _setCallOverrideCallback (
419
+ callback : CallOverrideCallback
420
+ ) : Promise < void > {
402
421
this . _callOverrideCallback = callback ;
403
422
404
- this . _provider . setCallOverrideCallback (
423
+ await this . _provider . setCallOverrideCallback (
405
424
async ( address : Buffer , data : Buffer ) => {
406
425
return this . _callOverrideCallback ?.( address , data ) ;
407
426
}
408
427
) ;
409
428
}
410
429
411
- private _setVerboseTracing ( enabled : boolean ) {
412
- this . _provider . setVerboseTracing ( enabled ) ;
430
+ private async _setVerboseTracing ( enabled : boolean ) : Promise < void > {
431
+ await this . _provider . setVerboseTracing ( enabled ) ;
413
432
}
414
433
415
434
private _ethEventListener ( event : SubscriptionEvent ) {
0 commit comments