Skip to content
Open
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
69 changes: 69 additions & 0 deletions src/commands/account/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {BaseAction, resolveNetwork} from "../../lib/actions/BaseAction";
import {formatEther} from "viem";
import {createClient} from "genlayer-js";
import type {GenLayerChain, Address} from "genlayer-js/types";
import {readFileSync, existsSync} from "fs";

export interface GetBalanceOptions {
rpc?: string;
account?: string;
}

export class GetBalanceAction extends BaseAction {
constructor() {
super();
}

private getNetwork(): GenLayerChain {
return resolveNetwork(this.getConfig().network);
}

async execute(options?: GetBalanceOptions): Promise<void> {
this.startSpinner("Fetching account balance...");

try {
if (options?.account) {
this.accountOverride = options.account;
}

const accountName = this.resolveAccountName();
const keystorePath = this.getKeystorePath(accountName);

if (!existsSync(keystorePath)) {
this.failSpinner(`Account '${accountName}' not found. Run 'genlayer account create --name ${accountName}' first.`);
return;
}

const keystoreData = JSON.parse(readFileSync(keystorePath, "utf-8"));

if (!this.isValidKeystoreFormat(keystoreData)) {
this.failSpinner("Invalid keystore format.");
return;
}

const rawAddr = keystoreData.address;
const address = (rawAddr.startsWith("0x") ? rawAddr : `0x${rawAddr}`) as Address;
const network = this.getNetwork();

const client = createClient({
chain: network,
account: address,
endpoint: options?.rpc,
});

const balance = await client.getBalance({address});
const formattedBalance = formatEther(balance);

const result = {
account: accountName,
address,
balance: `${formattedBalance} GEN`,
network: network.name || "localnet",
};

this.succeedSpinner("Account balance", result);
} catch (error: any) {
this.failSpinner("Failed to get account balance", error.message || error);
}
}
}
12 changes: 12 additions & 0 deletions src/commands/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {ExportAccountAction, ExportAccountOptions} from "./export";
import {UnlockAccountAction, UnlockAccountOptions} from "./unlock";
import {LockAccountAction, LockAccountOptions} from "./lock";
import {SendAction, SendOptions} from "./send";
import {GetBalanceAction, GetBalanceOptions} from "./balance";
import {FundAccountAction, FundAccountOptions} from "./fund";
Comment on lines +9 to +10
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check if fund.ts exists and if FundAccountAction is used anywhere
echo "=== Checking if fund.ts exists ==="
fd -t f 'fund.ts' src/commands/account/

echo ""
echo "=== Searching for FundAccountAction usage ==="
rg -n 'FundAccountAction' --type ts

Repository: genlayerlabs/genlayer-cli

Length of output: 245


🏁 Script executed:

#!/bin/bash
# Check if fund.ts exists anywhere in the project
echo "=== Searching for fund.ts anywhere in the project ==="
fd -t f 'fund\.ts' .

echo ""
echo "=== Checking structure of src/commands/account/ ==="
ls -la src/commands/account/

echo ""
echo "=== Checking for any fund-related patterns ==="
rg -i 'fund' src/commands/account/ --type ts

Repository: genlayerlabs/genlayer-cli

Length of output: 1138


Remove unused import on line 10.

FundAccountAction and FundAccountOptions are imported from a non-existent module ./fund. This import will cause a build error. Since the module doesn't exist and the imports are never used, remove the line.

Suggested diff
 import {GetBalanceAction, GetBalanceOptions} from "./balance";
-import {FundAccountAction, FundAccountOptions} from "./fund";
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/commands/account/index.ts` around lines 9 - 10, Remove the unused import
line that brings in FundAccountAction and FundAccountOptions from "./fund"
(these symbols don't exist and are not used); edit the imports in index.ts so
only GetBalanceAction and GetBalanceOptions are imported from "./balance" and
delete the import of FundAccountAction/FundAccountOptions, and if there are any
leftover references to FundAccountAction or FundAccountOptions elsewhere in this
module, remove or replace them accordingly.

import {ListAccountsAction} from "./list";
import {UseAccountAction} from "./use";
import {RemoveAccountAction} from "./remove";
Expand Down Expand Up @@ -38,6 +40,16 @@ export function initializeAccountCommands(program: Command) {
await showAction.execute(options);
});

accountCommand
.command("balance")
.description("Get account balance")
.option("--rpc <rpcUrl>", "RPC URL for the network")
.option("--account <name>", "Account to check balance for")
.action(async (options: GetBalanceOptions) => {
const balanceAction = new GetBalanceAction();
await balanceAction.execute(options);
});

accountCommand
.command("create")
.description("Create a new account with encrypted keystore")
Expand Down