From c447667ee89124c8a5720c161ec72e472e7ee347 Mon Sep 17 00:00:00 2001 From: Jack Hamer <47187316+JackHamer09@users.noreply.github.com> Date: Mon, 27 Nov 2023 14:40:17 +0200 Subject: [PATCH] feat: add balance command (#88) Co-authored-by: Javier Chatruc --- README.md | 3 ++ src/commands/wallet/balance.ts | 73 ++++++++++++++++++++++++++++++++++ src/commands/wallet/command.ts | 3 ++ src/commands/wallet/index.ts | 3 ++ src/common/options.ts | 1 + src/index.ts | 1 + 6 files changed, 84 insertions(+) create mode 100644 src/commands/wallet/balance.ts create mode 100644 src/commands/wallet/command.ts create mode 100644 src/commands/wallet/index.ts diff --git a/README.md b/README.md index 3d60f498..39f449d2 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/commands/wallet/balance.ts b/src/commands/wallet/balance.ts new file mode 100644 index 00000000..efdc7984 --- /dev/null +++ b/src/commands/wallet/balance.ts @@ -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); diff --git a/src/commands/wallet/command.ts b/src/commands/wallet/command.ts new file mode 100644 index 00000000..19461f51 --- /dev/null +++ b/src/commands/wallet/command.ts @@ -0,0 +1,3 @@ +import Program from "../../program.js"; + +export default Program.command("wallet").description("Manage wallet related features"); diff --git a/src/commands/wallet/index.ts b/src/commands/wallet/index.ts new file mode 100644 index 00000000..87170b20 --- /dev/null +++ b/src/commands/wallet/index.ts @@ -0,0 +1,3 @@ +import "./balance.js"; + +import "./command.js"; // registers all the commands above diff --git a/src/common/options.ts b/src/common/options.ts index 87fe64be..986108c9 100644 --- a/src/common/options.ts +++ b/src/common/options.ts @@ -7,6 +7,7 @@ export const chainOption = new Option("--c, --chain ", "Chain to use").ch ); export const l1RpcUrlOption = new Option("--l1-rpc, --l1-rpc-url ", "Override L1 RPC URL"); export const l2RpcUrlOption = new Option("--l2-rpc, --l2-rpc-url ", "Override L2 RPC URL"); +export const accountOption = new Option("--address, --address
", "Account address"); export const privateKeyOption = new Option("--pk, --private-key ", "Private key of the sender"); export const amountOptionCreate = (action: string) => new Option("--a, --amount ", `Amount of ETH to ${action} (eg. 0.1)`); diff --git a/src/index.ts b/src/index.ts index 2a37bec9..5611fbfd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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";