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

feat: add external wallet balance option #24

Closed
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
6 changes: 4 additions & 2 deletions bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import chalk from "chalk";
import { deployCommand } from "../src/commands/deploy.js";
import { verifyCommand } from "../src/commands/verify.js";
import { ReadContract } from "../src/commands/contract.js";
import { Address } from "viem";

interface CommandOptions {
testnet?: boolean;
address?: string;
address?: Address;
value?: string;
txid?: string;
abi?: string;
Expand Down Expand Up @@ -55,8 +56,9 @@ program
.command("balance")
.description("Check the balance of the saved wallet")
.option("-t, --testnet", "Check the balance on the testnet")
.option("-a, --address <address>", "Check the balance of a specific address")
.action(async (options: CommandOptions) => {
await balanceCommand(!!options.testnet);
await balanceCommand(!!options.testnet, options.address);
});

program
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"build": "tsc",
"wallet": "pnpm run build && node dist/bin/index.js wallet",
"balance": "pnpm run build && node dist/bin/index.js balance",
"balance-of": "pnpm run build && node dist/bin/index.js balance -t --address 0x02C8345B2DF9Ff6122b0bE7B79cB219d956bd701",
"transfer": "pnpm run build && node dist/bin/index.js transfer --testnet --address 0xa5f45f5bddefC810C48aCC1D5CdA5e5a4c6BC59E --value 0.001",
"tx-status": "pnpm run build && node dist/bin/index.js tx --testnet --txid 0x876a0a9b167889350c41930a4204e5d9acf5704a7f201447a337094189af961c4"
},
Expand Down
40 changes: 24 additions & 16 deletions src/commands/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,42 @@ import ViemProvider from "../utils/viemProvider.js";
import fs from "fs";
import path from "path";
import chalk from "chalk";

import { Address, isAddress } from "viem";
const walletFilePath = path.join(process.cwd(), "rootstock-wallet.json");

export async function balanceCommand(testnet: boolean) {
export async function balanceCommand(testnet: boolean, address?: Address) {
try {
if (!fs.existsSync(walletFilePath)) {
console.log(
chalk.red("🚫 No saved wallet found. Please create a wallet first.")
);
return;
}
let targetAddress: Address;

const walletData = JSON.parse(fs.readFileSync(walletFilePath, "utf8"));
const { address } = walletData;

if (!address) {
console.log(chalk.red("⚠️ No valid address found in the saved wallet."));
return;
if (address) {
if (!isAddress(address)) {
console.log(chalk.red("🚫 Invalid address provided"));
return;
}
targetAddress = address;
} else {
if (!fs.existsSync(walletFilePath)) {
console.log(chalk.red("🚫 No saved wallet found"));
return;
}
const { address: savedAddress } = JSON.parse(
fs.readFileSync(walletFilePath, "utf8")
);
if (!savedAddress) {
console.log(chalk.red("⚠️ Invalid wallet data"));
return;
}
targetAddress = savedAddress;
}

const provider = new ViemProvider(testnet);
const client = await provider.getPublicClient();

const balance = await client.getBalance({ address });
const balance = await client.getBalance({ address: targetAddress });

const rbtcBalance = Number(balance) / 10 ** 18;

console.log(chalk.white(`📄 Wallet Address:`), chalk.green(address));
console.log(chalk.white(`📄 Wallet Address:`), chalk.green(targetAddress));
console.log(
chalk.white(`🌐 Network:`),
chalk.green(testnet ? "Rootstock Testnet" : "Rootstock Mainnet")
Expand Down