@@ -190,7 +190,7 @@ export type DebugEventJsonRpcApiProvider = {
190190 */
191191export type JsonRpcApiProviderOptions = {
192192 polling ?: boolean ;
193- staticNetwork ?: null | Network ;
193+ staticNetwork ?: null | boolean | Network ;
194194 batchStallTime ?: number ;
195195 batchMaxSize ?: number ;
196196 batchMaxCount ?: number ;
@@ -463,6 +463,7 @@ export abstract class JsonRpcApiProvider extends AbstractProvider {
463463 } ;
464464
465465 #network: null | Network ;
466+ #pendingDetectNetwork: null | Promise < Network > ;
466467
467468 #scheduleDrain( ) : void {
468469 if ( this . #drainTimer) { return ; }
@@ -554,6 +555,7 @@ export abstract class JsonRpcApiProvider extends AbstractProvider {
554555 this . #drainTimer = null ;
555556
556557 this . #network = null ;
558+ this . #pendingDetectNetwork = null ;
557559
558560 {
559561 let resolve : null | ( ( value : void ) => void ) = null ;
@@ -563,9 +565,15 @@ export abstract class JsonRpcApiProvider extends AbstractProvider {
563565 this . #notReady = { promise, resolve } ;
564566 }
565567
566- // Make sure any static network is compatbile with the provided netwrok
567568 const staticNetwork = this . _getOption ( "staticNetwork" ) ;
568- if ( staticNetwork ) {
569+ if ( typeof ( staticNetwork ) === "boolean" ) {
570+ assertArgument ( ! staticNetwork || network !== "any" , "staticNetwork cannot be used on special network 'any'" , "options" , options ) ;
571+ if ( staticNetwork && network != null ) {
572+ this . #network = Network . from ( network ) ;
573+ }
574+
575+ } else if ( staticNetwork ) {
576+ // Make sure any static network is compatbile with the provided netwrok
569577 assertArgument ( network == null || staticNetwork . matches ( network ) ,
570578 "staticNetwork MUST match network object" , "options" , options ) ;
571579 this . #network = staticNetwork ;
@@ -641,36 +649,56 @@ export abstract class JsonRpcApiProvider extends AbstractProvider {
641649 */
642650 async _detectNetwork ( ) : Promise < Network > {
643651 const network = this . _getOption ( "staticNetwork" ) ;
644- if ( network ) { return network ; }
652+ if ( network ) {
653+ if ( network === true ) {
654+ if ( this . #network) { return this . #network; }
655+ } else {
656+ return network ;
657+ }
658+ }
659+
660+ if ( this . #pendingDetectNetwork) {
661+ return await this . #pendingDetectNetwork;
662+ }
645663
646664 // If we are ready, use ``send``, which enabled requests to be batched
647665 if ( this . ready ) {
648- return Network . from ( getBigInt ( await this . send ( "eth_chainId" , [ ] ) ) ) ;
666+ this . #pendingDetectNetwork = ( async ( ) => {
667+ const result = Network . from ( getBigInt ( await this . send ( "eth_chainId" , [ ] ) ) ) ;
668+ this . #pendingDetectNetwork = null ;
669+ return result ;
670+ } ) ( ) ;
671+ return await this . #pendingDetectNetwork;
649672 }
650673
651674 // We are not ready yet; use the primitive _send
675+ this . #pendingDetectNetwork = ( async ( ) => {
676+ const payload : JsonRpcPayload = {
677+ id : this . #nextId++ , method : "eth_chainId" , params : [ ] , jsonrpc : "2.0"
678+ } ;
652679
653- const payload : JsonRpcPayload = {
654- id : this . #nextId++ , method : "eth_chainId" , params : [ ] , jsonrpc : "2.0"
655- } ;
680+ this . emit ( "debug" , { action : "sendRpcPayload" , payload } ) ;
656681
657- this . emit ( "debug" , { action : "sendRpcPayload" , payload } ) ;
682+ let result : JsonRpcResult | JsonRpcError ;
683+ try {
684+ result = ( await this . _send ( payload ) ) [ 0 ] ;
685+ this . #pendingDetectNetwork = null ;
686+ } catch ( error ) {
687+ this . #pendingDetectNetwork = null ;
688+ this . emit ( "debug" , { action : "receiveRpcError" , error } ) ;
689+ throw error ;
690+ }
658691
659- let result : JsonRpcResult | JsonRpcError ;
660- try {
661- result = ( await this . _send ( payload ) ) [ 0 ] ;
662- } catch ( error ) {
663- this . emit ( "debug" , { action : "receiveRpcError" , error } ) ;
664- throw error ;
665- }
692+ this . emit ( "debug" , { action : "receiveRpcResult" , result } ) ;
666693
667- this . emit ( "debug" , { action : "receiveRpcResult" , result } ) ;
694+ if ( "result" in result ) {
695+ return Network . from ( getBigInt ( result . result ) ) ;
696+ }
668697
669- if ( "result" in result ) {
670- return Network . from ( getBigInt ( result . result ) ) ;
671- }
698+ throw this . getRpcError ( payload , result ) ;
699+ } ) ( ) ;
672700
673- throw this . getRpcError ( payload , result ) ;
701+ return await this . #pendingDetectNetwork ;
674702 }
675703
676704 /**
0 commit comments