@@ -75,7 +75,23 @@ export class Wallet extends MiddlewareConsumer {
7575 throw new Error ( 'supply nodeUrl in chain config or instantiate provider manually' ) ;
7676 }
7777
78+ // Check if the existing chain exists.
79+ if ( this . chainConfigs . has ( chainConfig . chainId ) ) {
80+ const existingConfig = this . chainConfigs . get ( chainConfig . chainId ) ;
81+
82+ if ( existingConfig ) {
83+ if ( existingConfig . nodeUrl !== chainConfig . nodeUrl ) {
84+ this . chainConfigs . set ( chainConfig . chainId , {
85+ ...existingConfig ,
86+ nodeUrl : chainConfig . nodeUrl
87+ } ) ;
88+ }
89+ return ;
90+ }
91+ }
92+
7893 this . chainConfigs . set ( chainConfig . chainId , chainConfig ) ;
94+
7995 if (
8096 this . chainConfigs . size === 1 &&
8197 this . defaultChainId === DEFAULT_CHAIN &&
@@ -116,9 +132,11 @@ export class Wallet extends MiddlewareConsumer {
116132 if ( ! this . chainConfigs . has ( chainId ) ) {
117133 throw new Error ( `trying to use not configured chainId ${ chainId } ` ) ;
118134 }
135+
119136 const chainConfig = this . chainConfigs . get ( chainId ) as ChainConfig ;
120137 let client : AergoClient ;
121-
138+
139+ // If the client does not exist, create a new one.
122140 if ( ! this . clients . has ( chainId ) ) {
123141 let provider = chainConfig . provider ;
124142 if ( typeof provider === 'function' ) {
@@ -127,14 +145,33 @@ export class Wallet extends MiddlewareConsumer {
127145 client = new AergoClient ( { } , provider ) ;
128146 this . clients . set ( chainId , client ) ;
129147 } else {
148+ // If the client already exists, compare nodeUrl and update if necessary.
130149 client = this . clients . get ( chainId ) as AergoClient ;
150+ let existingNodeUrl ;
151+ if ( client && ( client as any ) . config && ( client as any ) . config . url ) {
152+ existingNodeUrl = ( client as any ) . config . url ;
153+ } else {
154+ existingNodeUrl = undefined ;
155+ }
156+
157+ if ( existingNodeUrl !== chainConfig . nodeUrl ) {
158+ // Create a new `AergoClient` and replace the existing one.
159+ let provider = chainConfig . provider ;
160+ if ( typeof provider === 'function' ) {
161+ provider = new provider ( { url : chainConfig . nodeUrl } ) ;
162+ }
163+ client = new AergoClient ( { } , provider ) ;
164+ this . clients . set ( chainId , client ) ;
165+ }
131166 }
167+
132168 if ( this . defaultLimit ) {
133169 client . setDefaultLimit ( this . defaultLimit ) ;
134170 }
171+
135172 return client ;
136173 }
137-
174+
138175 /**
139176 * Prepare a transaction from given account specified by simple TxBody.
140177 * Completes missing information (chainIdHash, nonce) and signs tx using key of account.
0 commit comments