From 812766c996fcea4d56af200d53b54f0240cd65a4 Mon Sep 17 00:00:00 2001 From: Javier Chatruc Date: Fri, 17 Nov 2023 16:03:07 +0300 Subject: [PATCH 1/3] Add balance command --- src/commands/wallet/balance.ts | 74 ++++++++++++++++++++++++++++++++++ src/commands/wallet/command.ts | 3 ++ src/commands/wallet/index.ts | 3 ++ src/common/options.ts | 7 ++++ src/index.ts | 2 + 5 files changed, 89 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/src/commands/wallet/balance.ts b/src/commands/wallet/balance.ts new file mode 100644 index 00000000..54628031 --- /dev/null +++ b/src/commands/wallet/balance.ts @@ -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); diff --git a/src/commands/wallet/command.ts b/src/commands/wallet/command.ts new file mode 100644 index 00000000..ce658740 --- /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 for L2 and L1"); 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..7a5e3100 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("--account, --account
", "Account in question"); 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)`); @@ -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; diff --git a/src/index.ts b/src/index.ts index 2a37bec9..69ce3c00 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,4 +7,6 @@ import "./commands/bridge/index.js"; import "./commands/create/index.js"; +import "./commands/wallet/index.js"; + Program.parse(); From aef66ef45749923c48763bc356c0c36a87e32b41 Mon Sep 17 00:00:00 2001 From: Javier Chatruc Date: Mon, 20 Nov 2023 16:42:45 +0300 Subject: [PATCH 2/3] Address comments --- src/commands/wallet/balance.ts | 108 ++++++++++++++++----------------- src/common/options.ts | 6 -- 2 files changed, 53 insertions(+), 61 deletions(-) diff --git a/src/commands/wallet/balance.ts b/src/commands/wallet/balance.ts index 54628031..d05c0700 100644 --- a/src/commands/wallet/balance.ts +++ b/src/commands/wallet/balance.ts @@ -1,74 +1,72 @@ import inquirer from "inquirer"; import Program from "./command.js"; -import { - accountOption, - chainOption, - zeekOption, -} from "../../common/options.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 { 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"; +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 - ); + 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, - }; + 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"); + 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${bigNumberToDecimal(balance)} ETH`); + 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); + 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); + .description("Get balance of an L2 or L1 account") + .addOption(chainOption) + .addOption(accountOption) + .addOption(zeekOption) + .action(handler); diff --git a/src/common/options.ts b/src/common/options.ts index 7a5e3100..b75a7923 100644 --- a/src/common/options.ts +++ b/src/common/options.ts @@ -27,12 +27,6 @@ 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; From 2e8892186e7e3538e4433467d4dae6970b90cf63 Mon Sep 17 00:00:00 2001 From: Javier Chatruc Date: Mon, 20 Nov 2023 17:05:39 +0300 Subject: [PATCH 3/3] Fix wallet command description --- src/commands/wallet/command.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/wallet/command.ts b/src/commands/wallet/command.ts index ce658740..e17a4b6a 100644 --- a/src/commands/wallet/command.ts +++ b/src/commands/wallet/command.ts @@ -1,3 +1,3 @@ import Program from "../../program.js"; -export default Program.command("wallet").description("Manage wallet related features for L2 and L1"); +export default Program.command("wallet").description("Manage zkSync wallet related features");