Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit fd5f05d

Browse files
perf: optimize account init logging (#4318)
Co-authored-by: jeffsmale90 <[email protected]>
1 parent ac5deea commit fd5f05d

File tree

1 file changed

+56
-53
lines changed

1 file changed

+56
-53
lines changed

src/packages/cli/src/initialize/ethereum.ts

+56-53
Original file line numberDiff line numberDiff line change
@@ -16,82 +16,85 @@ export default function (provider: EthereumProvider, cliSettings: CliSettings) {
1616
const liveOptions = provider.getOptions();
1717
const accounts = provider.getInitialAccounts();
1818

19-
const addresses = Object.keys(accounts);
20-
const logs = [];
21-
logs.push("");
22-
logs.push("Available Accounts");
23-
logs.push("==================");
19+
const addresses = Object.entries(accounts);
20+
let log = "\n";
21+
const appendLog = line => (log += line + "\n");
22+
23+
appendLog("Available Accounts");
24+
appendLog("==================");
2425
if (addresses.length > 0) {
25-
addresses.forEach(function (address, index) {
26-
const balance = accounts[address].balance;
26+
let index = 0;
27+
for (const [address, account] of addresses) {
28+
const balance = account.balance;
2729
const strBalance = balance / WEI;
2830
const about = balance % WEI === 0n ? "" : "~";
29-
let line = `(${index}) ${toChecksumAddress(
31+
let line = `(${index++}) ${toChecksumAddress(
3032
address
3133
)} (${about}${strBalance} ETH)`;
3234

33-
if (!accounts[address].unlocked) {
35+
if (!account.unlocked) {
3436
line += " 🔒";
3537
}
3638

37-
logs.push(line);
38-
});
39+
appendLog(line);
40+
}
3941

40-
logs.push("");
41-
logs.push("Private Keys");
42-
logs.push("==================");
42+
appendLog("");
43+
appendLog("Private Keys");
44+
appendLog("==================");
4345

44-
addresses.forEach(function (address, index) {
45-
logs.push(`(${index}) ${accounts[address].secretKey}`);
46-
});
46+
index = 0;
47+
for (const [address, account] of addresses) {
48+
appendLog(`(${index++}) ${account.secretKey}`);
49+
}
4750

4851
if (liveOptions.wallet.accountKeysPath != null) {
49-
logs.push("");
50-
logs.push(
52+
appendLog("");
53+
appendLog(
5154
`Accounts and keys saved to ${liveOptions.wallet.accountKeysPath}`
5255
);
5356
}
5457
} else {
55-
logs.push("(no accounts unlocked)");
58+
appendLog("(no accounts unlocked)");
5659
}
5760

5861
if (liveOptions.wallet.accounts == null) {
59-
logs.push("");
60-
logs.push("HD Wallet");
61-
logs.push("==================");
62-
logs.push(`Mnemonic: ${color(liveOptions.wallet.mnemonic)}`);
63-
logs.push(
62+
appendLog("");
63+
appendLog("HD Wallet");
64+
appendLog("==================");
65+
appendLog(`Mnemonic: ${color(liveOptions.wallet.mnemonic)}`);
66+
appendLog(
6467
`Base HD Path: ${color(
6568
liveOptions.wallet.hdPath.join("/") + "/{account_index}"
6669
)}`
6770
);
6871
}
6972

7073
if (liveOptions.miner.defaultGasPrice) {
71-
logs.push("");
72-
logs.push("Default Gas Price");
73-
logs.push("==================");
74-
logs.push(color(liveOptions.miner.defaultGasPrice.toBigInt().toString()));
74+
appendLog("");
75+
appendLog("Default Gas Price");
76+
appendLog("==================");
77+
appendLog(color(liveOptions.miner.defaultGasPrice.toBigInt().toString()));
7578
}
7679

7780
if (liveOptions.miner.blockGasLimit) {
78-
logs.push("");
79-
logs.push("BlockGas Limit");
80-
logs.push("==================");
81-
logs.push(color(liveOptions.miner.blockGasLimit.toBigInt().toString()));
81+
appendLog("");
82+
appendLog("BlockGas Limit");
83+
appendLog("==================");
84+
appendLog(color(liveOptions.miner.blockGasLimit.toBigInt().toString()));
8285
}
8386

8487
if (liveOptions.miner.callGasLimit) {
85-
logs.push("");
86-
logs.push("Call Gas Limit");
87-
logs.push("==================");
88-
logs.push(color(liveOptions.miner.callGasLimit.toBigInt().toString()));
88+
appendLog("");
89+
appendLog("Call Gas Limit");
90+
appendLog("==================");
91+
appendLog(color(liveOptions.miner.callGasLimit.toBigInt().toString()));
8992
}
9093

9194
if (liveOptions.fork.network || liveOptions.fork.url) {
92-
logs.push("");
93-
logs.push("Forked Chain");
94-
logs.push("==================");
95+
appendLog("");
96+
appendLog("Forked Chain");
97+
appendLog("==================");
9598
let location: string;
9699
if (liveOptions.fork.network) {
97100
location = `Ethereum ${capitalizeFirstLetter(
@@ -101,31 +104,31 @@ export default function (provider: EthereumProvider, cliSettings: CliSettings) {
101104
location = (liveOptions.fork.url as any).toString();
102105
}
103106

104-
logs.push(`Location: ${color(location)}`);
105-
logs.push(
107+
appendLog(`Location: ${color(location)}`);
108+
appendLog(
106109
`Block: ${color(liveOptions.fork.blockNumber.toString())}`
107110
);
108-
logs.push(
111+
appendLog(
109112
`Network ID: ${color(liveOptions.chain.networkId.toString())}`
110113
);
111-
logs.push(`Time: ${color(liveOptions.chain.time.toString())}`);
114+
appendLog(`Time: ${color(liveOptions.chain.time.toString())}`);
112115

113116
if (liveOptions.fork.requestsPerSecond !== 0) {
114-
logs.push(
117+
appendLog(
115118
`Requests/Second: ${color(
116119
liveOptions.fork.requestsPerSecond.toString()
117120
)}`
118121
);
119122
}
120123
}
121124

122-
logs.push("");
123-
logs.push("Chain");
124-
logs.push("==================");
125-
logs.push(`Hardfork: ${color(liveOptions.chain.hardfork)}`);
126-
logs.push(`Id: ${color(liveOptions.chain.chainId.toString())}`);
125+
appendLog("");
126+
appendLog("Chain");
127+
appendLog("==================");
128+
appendLog(`Hardfork: ${color(liveOptions.chain.hardfork)}`);
129+
appendLog(`Id: ${color(liveOptions.chain.chainId.toString())}`);
127130

128-
logs.push("");
129-
logs.push("RPC Listening on " + cliSettings.host + ":" + cliSettings.port);
130-
console.log(logs.join("\n"));
131+
appendLog("");
132+
appendLog("RPC Listening on " + cliSettings.host + ":" + cliSettings.port);
133+
console.log(log);
131134
}

0 commit comments

Comments
 (0)