Skip to content

Commit c447667

Browse files
feat: add balance command (#88)
Co-authored-by: Javier Chatruc <[email protected]>
1 parent 5f6f192 commit c447667

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ In addition to default modules, you can install custom modules from NPM.
4242

4343
Run `npx zksync-cli dev` to see the full list of commands.
4444

45+
### Wallet commands
46+
- `npx zksync-cli wallet balance`: displays ETH balance of the specified address
47+
4548
### Bridge commands
4649
- `npx zksync-cli bridge deposit`: deposits funds from Ethereum (L1) to zkSync (L2)
4750
- `npx zksync-cli bridge withdraw`: withdraws funds from zkSync (L2) to Ethereum (L1)

src/commands/wallet/balance.ts

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import inquirer from "inquirer";
2+
3+
import Program from "./command.js";
4+
import { accountOption, chainOption, zeekOption } from "../../common/options.js";
5+
import { l2Chains } from "../../data/chains.js";
6+
import { bigNumberToDecimal } from "../../utils/formatters.js";
7+
import { getL2Provider, optionNameToParam } from "../../utils/helpers.js";
8+
import Logger from "../../utils/logger.js";
9+
import { isAddress } from "../../utils/validators.js";
10+
import zeek from "../../utils/zeek.js";
11+
12+
import type { DefaultOptions } from "../../common/options.js";
13+
14+
type BalanceOptions = DefaultOptions & {
15+
chain?: string;
16+
l2RpcUrl?: string;
17+
address?: string;
18+
};
19+
20+
export const handler = async (options: BalanceOptions) => {
21+
try {
22+
const answers: BalanceOptions = await inquirer.prompt(
23+
[
24+
{
25+
message: chainOption.description,
26+
name: optionNameToParam(chainOption.long!),
27+
type: "list",
28+
choices: l2Chains.filter((e) => e.l1Chain).map((e) => ({ name: e.name, value: e.network })),
29+
required: true,
30+
when(answers: BalanceOptions) {
31+
if (answers.l2RpcUrl) {
32+
return false;
33+
}
34+
return true;
35+
},
36+
},
37+
{
38+
message: accountOption.description,
39+
name: optionNameToParam(accountOption.long!),
40+
type: "input",
41+
required: true,
42+
validate: (input: string) => isAddress(input),
43+
},
44+
],
45+
options
46+
);
47+
48+
options = {
49+
...options,
50+
...answers,
51+
};
52+
53+
const selectedChain = l2Chains.find((e) => e.network === options.chain);
54+
const l2Provider = getL2Provider(options.l2RpcUrl ?? selectedChain!.rpcUrl);
55+
const balance = await l2Provider.getBalance(options.address!);
56+
57+
Logger.info(`\n${selectedChain?.name} Balance: ${bigNumberToDecimal(balance)} ETH`);
58+
59+
if (options.zeek) {
60+
zeek();
61+
}
62+
} catch (error) {
63+
Logger.error("There was an error while fetching balance for the specified address:");
64+
Logger.error(error);
65+
}
66+
};
67+
68+
Program.command("balance")
69+
.description("Get balance of an L2 account")
70+
.addOption(chainOption)
71+
.addOption(accountOption)
72+
.addOption(zeekOption)
73+
.action(handler);

src/commands/wallet/command.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Program from "../../program.js";
2+
3+
export default Program.command("wallet").description("Manage wallet related features");

src/commands/wallet/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "./balance.js";
2+
3+
import "./command.js"; // registers all the commands above

src/common/options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const chainOption = new Option("--c, --chain <chain>", "Chain to use").ch
77
);
88
export const l1RpcUrlOption = new Option("--l1-rpc, --l1-rpc-url <URL>", "Override L1 RPC URL");
99
export const l2RpcUrlOption = new Option("--l2-rpc, --l2-rpc-url <URL>", "Override L2 RPC URL");
10+
export const accountOption = new Option("--address, --address <ADDRESS>", "Account address");
1011
export const privateKeyOption = new Option("--pk, --private-key <URL>", "Private key of the sender");
1112
export const amountOptionCreate = (action: string) =>
1213
new Option("--a, --amount <amount>", `Amount of ETH to ${action} (eg. 0.1)`);

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Program from "./program.js";
33

44
import "./commands/dev/index.js";
55

6+
import "./commands/wallet/index.js";
67
import "./commands/bridge/index.js";
78

89
import "./commands/create/index.js";

0 commit comments

Comments
 (0)