Skip to content

Commit

Permalink
Add balance command
Browse files Browse the repository at this point in the history
  • Loading branch information
jrchatruc committed Nov 17, 2023
1 parent b036d3c commit 812766c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/commands/wallet/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 { BalanceOptions } from "../../common/options.js";

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 toChain = l2Chains.find((e) => e.network === options.chain);
const Provider = getL2Provider(options.l2RpcUrl ?? toChain!.rpcUrl);
const balance = await Provider.getBalance(options.account ?? "Unknown account");

Logger.info(`\n${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 wallet related features for L2 and L1");
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
7 changes: 7 additions & 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 All @@ -26,6 +27,12 @@ export type DefaultTransactionOptions = DefaultOptions & {
l2RpcUrl?: string;
privateKey: string;
};
export type BalanceOptions = DefaultOptions & {
chain?: string;
l1RpcUrl?: string;
l2RpcUrl?: string;
account?: string;
};
export type DefaultTransferOptions = DefaultTransactionOptions & {
amount: string;
recipient: string;
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();

0 comments on commit 812766c

Please sign in to comment.