Skip to content

Commit af7fcbf

Browse files
fix: mirror network factory method issues (#3510)
Signed-off-by: venilinvasilev <[email protected]>
1 parent e93981e commit af7fcbf

File tree

4 files changed

+306
-48
lines changed

4 files changed

+306
-48
lines changed

src/client/WebClient.js

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,45 @@ export default class WebClient extends Client {
2727
*/
2828
constructor(props) {
2929
super(props);
30+
3031
if (props != null) {
3132
if (typeof props.network === "string") {
32-
switch (props.network) {
33+
this._setNetworkFromName(props.network);
34+
} else if (props.network != null) {
35+
Client._validateNetworkConsistency(props.network);
36+
37+
const { shard, realm } = Client._extractShardRealm(
38+
props.network,
39+
);
40+
41+
// Shard and realm are inferred from the network, so we need to set them here
42+
// to ensure that the client is properly configured.
43+
this._shard = shard;
44+
this._realm = realm;
45+
46+
this.setNetwork(props.network);
47+
}
48+
49+
if (typeof props.mirrorNetwork === "string") {
50+
switch (props.mirrorNetwork) {
3351
case "mainnet":
34-
this.setNetwork(WebNetwork.MAINNET);
3552
this.setMirrorNetwork(WebMirrorNetwork.MAINNET);
36-
this.setLedgerId(LedgerId.MAINNET);
3753
break;
3854

3955
case "testnet":
40-
this.setNetwork(WebNetwork.TESTNET);
41-
this.setLedgerId(LedgerId.TESTNET);
4256
this.setMirrorNetwork(WebMirrorNetwork.TESTNET);
4357
break;
4458

4559
case "previewnet":
46-
this.setNetwork(WebNetwork.PREVIEWNET);
47-
this.setLedgerId(LedgerId.PREVIEWNET);
4860
this.setMirrorNetwork(WebMirrorNetwork.PREVIEWNET);
4961
break;
5062

51-
case "local-node":
52-
this.setNetwork(WebNetwork.LOCAL_NODE);
53-
this.setLedgerId(LedgerId.LOCAL_NODE);
54-
this.setMirrorNetwork(WebMirrorNetwork.LOCAL_NODE);
55-
break;
56-
5763
default:
58-
throw new Error(
59-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
60-
`unknown network: ${props.network}`,
61-
);
64+
this.setMirrorNetwork([props.mirrorNetwork]);
65+
break;
6266
}
63-
} else if (props.network != null) {
64-
Client._validateNetworkConsistency(props.network);
65-
66-
const { shard, realm } = Client._extractShardRealm(
67-
props.network,
68-
);
69-
70-
// Shard and realm are inferred from the network, so we need to set them here
71-
// to ensure that the client is properly configured.
72-
this._shard = shard;
73-
this._realm = realm;
74-
75-
this.setNetwork(props.network);
67+
} else if (props.mirrorNetwork != null) {
68+
this.setMirrorNetwork(props.mirrorNetwork);
7669
}
7770
}
7871
}
@@ -295,6 +288,46 @@ export default class WebClient extends Client {
295288
return this;
296289
}
297290

291+
/**
292+
* @private
293+
* @param {string} name
294+
* @returns {this}
295+
*/
296+
_setNetworkFromName(name) {
297+
switch (name) {
298+
case "mainnet":
299+
this.setNetwork(WebNetwork.MAINNET);
300+
this.setMirrorNetwork(WebMirrorNetwork.MAINNET);
301+
this.setLedgerId(LedgerId.MAINNET);
302+
break;
303+
304+
case "testnet":
305+
this.setNetwork(WebNetwork.TESTNET);
306+
this.setMirrorNetwork(WebMirrorNetwork.TESTNET);
307+
this.setLedgerId(LedgerId.TESTNET);
308+
break;
309+
310+
case "previewnet":
311+
this.setNetwork(WebNetwork.PREVIEWNET);
312+
this.setMirrorNetwork(WebMirrorNetwork.PREVIEWNET);
313+
this.setLedgerId(LedgerId.PREVIEWNET);
314+
break;
315+
316+
case "local-node":
317+
this.setNetwork(WebNetwork.LOCAL_NODE);
318+
this.setMirrorNetwork(WebMirrorNetwork.LOCAL_NODE);
319+
this.setLedgerId(LedgerId.LOCAL_NODE);
320+
break;
321+
322+
default:
323+
throw new Error(
324+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
325+
`unknown network: ${name}`,
326+
);
327+
}
328+
return this;
329+
}
330+
298331
/**
299332
* @override
300333
* @returns {Promise<this>}

src/network/AddressBookQuery.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,6 @@ export default class AddressBookQuery extends Query {
163163
* @returns {Promise<NodeAddressBook>}
164164
*/
165165
execute(client, requestTimeout) {
166-
// Extra validation when initializing the client with only a mirror network
167-
if (client._network._network.size === 0 && !client._timer) {
168-
throw new Error(
169-
"The client's network update period is required. Please set it using the setNetworkUpdatePeriod method.",
170-
);
171-
}
172-
173166
return new Promise((resolve, reject) => {
174167
this._makeServerStreamRequest(
175168
client,

src/network/AddressBookQueryWeb.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,6 @@ export default class AddressBookQueryWeb extends Query {
201201
* @returns {Promise<NodeAddressBook>}
202202
*/
203203
execute(client, requestTimeout) {
204-
// Extra validation when initializing the client with only a mirror network
205-
if (client._network._network.size === 0 && !client._timer) {
206-
throw new Error(
207-
"The client's network update period is required. Please set it using the setNetworkUpdatePeriod method.",
208-
);
209-
}
210-
211204
return new Promise((resolve, reject) => {
212205
void this._makeFetchRequest(
213206
client,
@@ -298,7 +291,7 @@ export default class AddressBookQueryWeb extends Query {
298291
certHash: node.node_cert_hash,
299292
publicKey: node.public_key,
300293
description: node.description,
301-
stake: node.stake.toString(),
294+
stake: node.stake?.toString(),
302295
}),
303296
);
304297

0 commit comments

Comments
 (0)