@@ -62,14 +62,19 @@ export const DEFAULT_COINBASE = "0xc014ba5ec014ba5ec014ba5ec014ba5ec014ba5e";
6262let _globalEdrContext : EdrContext | undefined ;
6363
6464// 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" ) ;
6970
7071 if ( _globalEdrContext === undefined ) {
7172 // Only one is allowed to exist
7273 _globalEdrContext = new EdrContext ( ) ;
74+ await _globalEdrContext . registerProviderFactory (
75+ GENERIC_CHAIN_TYPE ,
76+ genericChainProviderFactory ( )
77+ ) ;
7378 }
7479
7580 return _globalEdrContext ;
@@ -133,9 +138,10 @@ export class EdrProviderWrapper
133138 loggerConfig : LoggerConfig ,
134139 tracingConfig ?: TracingConfigWithBuffers
135140 ) : 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" ) ;
139145
140146 const coinbase = config . coinbase ?? DEFAULT_COINBASE ;
141147
@@ -179,8 +185,16 @@ export class EdrProviderWrapper
179185
180186 const hardforkName = getHardforkName ( config . hardfork ) ;
181187
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 ,
184198 {
185199 allowBlocksWithSameTimestamp :
186200 config . allowBlocksWithSameTimestamp ?? false ,
@@ -209,13 +223,8 @@ export class EdrProviderWrapper
209223 coinbase : Buffer . from ( coinbase . slice ( 2 ) , "hex" ) ,
210224 enableRip7212 : config . enableRip7212 ,
211225 fork,
226+ genesisState,
212227 hardfork : ethereumsjsHardforkToEdrSpecId ( hardforkName ) ,
213- genesisAccounts : config . genesisAccounts . map ( ( account ) => {
214- return {
215- secretKey : account . privateKey ,
216- balance : BigInt ( account . balance ) ,
217- } ;
218- } ) ,
219228 initialDate,
220229 initialBaseFeePerGas :
221230 config . initialBaseFeePerGas !== undefined
@@ -230,6 +239,12 @@ export class EdrProviderWrapper
230239 } ,
231240 } ,
232241 networkId : BigInt ( config . networkId ) ,
242+ ownedAccounts : config . genesisAccounts . map ( ( account ) => {
243+ return {
244+ secretKey : account . privateKey ,
245+ balance : BigInt ( account . balance ) ,
246+ } ;
247+ } ) ,
233248 } ,
234249 {
235250 enable : loggerConfig . enabled ,
@@ -242,10 +257,12 @@ export class EdrProviderWrapper
242257 }
243258 } ,
244259 } ,
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 ?? { }
249266 ) ;
250267
251268 const minimalEthereumJsNode = {
@@ -398,18 +415,20 @@ export class EdrProviderWrapper
398415 }
399416
400417 // temporarily added to make smock work with HH+EDR
401- private _setCallOverrideCallback ( callback : CallOverrideCallback ) {
418+ private async _setCallOverrideCallback (
419+ callback : CallOverrideCallback
420+ ) : Promise < void > {
402421 this . _callOverrideCallback = callback ;
403422
404- this . _provider . setCallOverrideCallback (
423+ await this . _provider . setCallOverrideCallback (
405424 async ( address : Buffer , data : Buffer ) => {
406425 return this . _callOverrideCallback ?.( address , data ) ;
407426 }
408427 ) ;
409428 }
410429
411- private _setVerboseTracing ( enabled : boolean ) {
412- this . _provider . setVerboseTracing ( enabled ) ;
430+ private async _setVerboseTracing ( enabled : boolean ) : Promise < void > {
431+ await this . _provider . setVerboseTracing ( enabled ) ;
413432 }
414433
415434 private _ethEventListener ( event : SubscriptionEvent ) {
0 commit comments