UNPKG

intuition-cli

Version:
48 lines (47 loc) 1.77 kB
import { Args, Command } from '@oclif/core'; import { createPublicClient, formatEther, http } from 'viem'; import { getDefaultAccount } from '../../config.js'; import { supportedNetworks } from '../../networks.js'; export default class AccountBalance extends Command { static args = { address: Args.string({ description: 'Address to check balance for', required: false, }), }; static description = 'Show the balance of an account on Base and Base Sepolia.'; static examples = [ '<%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> 0x1234...abcd', ]; static flags = {}; async run() { const { args } = await this.parse(AccountBalance); let { address } = args; if (!address) { // Use default account if no address is provided const defaultAddress = getDefaultAccount(); if (!defaultAddress) { this.log('No default account set. Please set a default account or provide an address.'); return; } address = defaultAddress; } this.log(`Fetching balances for: ${address}`); await Promise.all(supportedNetworks.map(async (network) => { const client = createPublicClient({ chain: network, transport: http(), }); try { const balance = await client.getBalance({ address: address, }); this.log(`${network.name}: ${formatEther(balance)} ETH`); } catch (error) { this.log(`${network.name}: Error fetching balance (${error})`); } })); } }