Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add balance command #88

Merged
merged 5 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ In addition to default modules, you can install custom modules from NPM.

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

### Wallet commands
- `npx zksync-cli wallet balance`: displays ETH balance of the specified address

### Bridge commands
- `npx zksync-cli bridge deposit`: deposits funds from Ethereum (L1) to zkSync (L2)
- `npx zksync-cli bridge withdraw`: withdraws funds from zkSync (L2) to Ethereum (L1)
Expand Down
73 changes: 73 additions & 0 deletions src/commands/wallet/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import inquirer from "inquirer";

import Program from "./command.js";
import { accountOption, chainOption, zeekOption } from "../../common/options.js";
import { l2Chains } from "../../data/chains.js";
import { bigNumberToDecimal } from "../../utils/formatters.js";
import { getL2Provider, optionNameToParam } from "../../utils/helpers.js";
import Logger from "../../utils/logger.js";
import { isAddress } from "../../utils/validators.js";
import zeek from "../../utils/zeek.js";

import type { DefaultOptions } from "../../common/options.js";

type BalanceOptions = DefaultOptions & {
chain?: string;
l2RpcUrl?: string;
address?: string;
};

export const handler = async (options: BalanceOptions) => {
try {
const answers: BalanceOptions = await inquirer.prompt(
[
{
message: chainOption.description,
name: optionNameToParam(chainOption.long!),
type: "list",
choices: l2Chains.filter((e) => e.l1Chain).map((e) => ({ name: e.name, value: e.network })),
required: true,
when(answers: BalanceOptions) {
if (answers.l2RpcUrl) {
return false;
}
return true;
},
},
{
message: accountOption.description,
name: optionNameToParam(accountOption.long!),
type: "input",
required: true,
validate: (input: string) => isAddress(input),
},
],
options
);

options = {
...options,
...answers,
};

const selectedChain = l2Chains.find((e) => e.network === options.chain);
const l2Provider = getL2Provider(options.l2RpcUrl ?? selectedChain!.rpcUrl);
const balance = await l2Provider.getBalance(options.address!);

Logger.info(`\n${selectedChain?.name} Balance: ${bigNumberToDecimal(balance)} ETH`);

if (options.zeek) {
zeek();
}
} catch (error) {
Logger.error("There was an error while fetching balance for the specified address:");
Logger.error(error);
}
};

Program.command("balance")
.description("Get balance of an L2 account")
.addOption(chainOption)
.addOption(accountOption)
.addOption(zeekOption)
.action(handler);
3 changes: 3 additions & 0 deletions src/commands/wallet/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Program from "../../program.js";

export default Program.command("wallet").description("Manage wallet related features");
3 changes: 3 additions & 0 deletions src/commands/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "./balance.js";

import "./command.js"; // registers all the commands above
1 change: 1 addition & 0 deletions src/common/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const chainOption = new Option("--c, --chain <chain>", "Chain to use").ch
);
export const l1RpcUrlOption = new Option("--l1-rpc, --l1-rpc-url <URL>", "Override L1 RPC URL");
export const l2RpcUrlOption = new Option("--l2-rpc, --l2-rpc-url <URL>", "Override L2 RPC URL");
export const accountOption = new Option("--address, --address <ADDRESS>", "Account address");
export const privateKeyOption = new Option("--pk, --private-key <URL>", "Private key of the sender");
export const amountOptionCreate = (action: string) =>
new Option("--a, --amount <amount>", `Amount of ETH to ${action} (eg. 0.1)`);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Program from "./program.js";

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

import "./commands/wallet/index.js";
import "./commands/bridge/index.js";

import "./commands/create/index.js";
Expand Down