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 option to query an account's balance on a given erc20 contract #93

Closed
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
33 changes: 29 additions & 4 deletions src/commands/wallet/balance.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import inquirer from "inquirer";

import Program from "./command.js";
import { accountOption, chainOption, l2RpcUrlOption, zeekOption } from "../../common/options.js";
import { accountOption, chainOption, l2RpcUrlOption, erc20AddressOption, 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 { utils } from 'zksync-web3';

import type { DefaultOptions } from "../../common/options.js";

type BalanceOptions = DefaultOptions & {
chain?: string;
l2RpcUrl?: string;
address?: string;
erc20Address?: string;
};

export const handler = async (options: BalanceOptions) => {
Expand Down Expand Up @@ -52,9 +54,31 @@ export const handler = async (options: BalanceOptions) => {

const selectedChain = l2Chains.find((e) => e.network === options.chain);
const l2Provider = getL2Provider(options.l2RpcUrl ?? selectedChain!.rpcUrl);
const balance = await l2Provider.getBalance(options.address!);
if (options.erc20Address) {
const tokenNameEncodedData = utils.IERC20.encodeFunctionData("name()");
const balanceOfEncodedData = utils.IERC20.encodeFunctionData("balanceOf(address)", [options.address!]);

const tokenNameTransactionReq = {
to: options.erc20Address!,
data: tokenNameEncodedData
};

const balanceOfTransactionReq = {
to: options.erc20Address!,
data: balanceOfEncodedData
};

const tokenNameResponse = await l2Provider.call(tokenNameTransactionReq);
const balanceResponse = await l2Provider.call(balanceOfTransactionReq);

const tokenName = utils.IERC20.decodeFunctionResult("name()", tokenNameResponse);
const balance = utils.IERC20.decodeFunctionResult("balanceOf(address)", balanceResponse);
Logger.info(`\n${selectedChain?.name} Balance: ${balance} ${tokenName}`);
} else {
const balance = await l2Provider.getBalance(options.address ?? "Unknown account");
Logger.info(`\n${selectedChain?.name} Balance: ${bigNumberToDecimal(balance)} ETH`);
}

Logger.info(`\n${selectedChain?.name} Balance: ${bigNumberToDecimal(balance)} ETH`);

if (options.zeek) {
zeek();
Expand All @@ -70,5 +94,6 @@ Program.command("balance")
.addOption(chainOption)
.addOption(l2RpcUrlOption)
.addOption(accountOption)
.addOption(erc20AddressOption)
.addOption(zeekOption)
.action(handler);
.action(handler);
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 erc20AddressOption = new Option("--erc20-address, --erc20-address <ADDRESS>", "ERC20 token address to query");
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) =>
Expand Down