Skip to content

Commit

Permalink
feat: add balance command (#88)
Browse files Browse the repository at this point in the history
Co-authored-by: Javier Chatruc <[email protected]>
  • Loading branch information
JackHamer09 and jrchatruc authored Nov 27, 2023
1 parent 5f6f192 commit c447667
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
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

0 comments on commit c447667

Please sign in to comment.