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

Add balance command #9

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
72 changes: 72 additions & 0 deletions src/commands/wallet/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import inquirer from "inquirer";

import Program from "./command.js";
import { DefaultOptions, 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";

type BalanceOptions = DefaultOptions & {
chain?: string;
l1RpcUrl?: string;
l2RpcUrl?: string;
account?: 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.l1RpcUrl && 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 provider = getL2Provider(options.l2RpcUrl ?? selectedChain!.rpcUrl);
const balance = await provider.getBalance(options.account ?? "Unknown account");

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 account:");
Logger.error(error);
}
};

Program.command("balance")
.description("Get balance of an L2 or L1 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 zkSync 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("--account, --account <ADDRESS>", "Account in question");
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
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ import "./commands/bridge/index.js";

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

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

Program.parse();