@@ -75,7 +75,23 @@ export class Wallet extends MiddlewareConsumer {
75
75
throw new Error ( 'supply nodeUrl in chain config or instantiate provider manually' ) ;
76
76
}
77
77
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
+
78
93
this . chainConfigs . set ( chainConfig . chainId , chainConfig ) ;
94
+
79
95
if (
80
96
this . chainConfigs . size === 1 &&
81
97
this . defaultChainId === DEFAULT_CHAIN &&
@@ -116,9 +132,11 @@ export class Wallet extends MiddlewareConsumer {
116
132
if ( ! this . chainConfigs . has ( chainId ) ) {
117
133
throw new Error ( `trying to use not configured chainId ${ chainId } ` ) ;
118
134
}
135
+
119
136
const chainConfig = this . chainConfigs . get ( chainId ) as ChainConfig ;
120
137
let client : AergoClient ;
121
-
138
+
139
+ // If the client does not exist, create a new one.
122
140
if ( ! this . clients . has ( chainId ) ) {
123
141
let provider = chainConfig . provider ;
124
142
if ( typeof provider === 'function' ) {
@@ -127,14 +145,33 @@ export class Wallet extends MiddlewareConsumer {
127
145
client = new AergoClient ( { } , provider ) ;
128
146
this . clients . set ( chainId , client ) ;
129
147
} else {
148
+ // If the client already exists, compare nodeUrl and update if necessary.
130
149
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
+ }
131
166
}
167
+
132
168
if ( this . defaultLimit ) {
133
169
client . setDefaultLimit ( this . defaultLimit ) ;
134
170
}
171
+
135
172
return client ;
136
173
}
137
-
174
+
138
175
/**
139
176
* Prepare a transaction from given account specified by simple TxBody.
140
177
* Completes missing information (chainIdHash, nonce) and signs tx using key of account.
0 commit comments