Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
Feature: getTokenBalance and getTokenAccounts on SolanaAgent
Context
/workspaces/3D-Agent/solana-agent-sdk is a TypeScript Solana agent SDK (@three-ws/solana-agent).
Build command: cd /workspaces/3D-Agent/solana-agent-sdk && npm run build
The build must pass with zero errors before this task is complete.
src/agent.ts contains the SolanaAgent class. It currently has:
getBalance()— returns SOL balance in lamportstransferSol(),transferSpl(),swap(),getOrCreateAta()
It is missing basic token balance reads.
What to Build
Add two methods to SolanaAgent and two standalone functions to src/actions/:
Standalone functions
src/actions/get-token-balance.ts
export interface TokenBalanceResult {
mint: string;
amount: string; // raw base units
decimals: number;
uiAmount: string; // human readable
ata: string; // ATA address
}
export async function getTokenBalance(
connection: Connection,
owner: PublicKey,
mint: PublicKey | string,
): Promise<TokenBalanceResult | null>
- Derives the ATA for
owner+mint - Fetches the token account via
connection.getTokenAccountBalance(ata) - Returns
nullif the account doesn't exist (not an error) - Returns
TokenBalanceResultwith all fields populated uiAmountshould use BigInt-safe division (same pattern asfix-bigint-precision-swap.mdrecommends —whole.fracstring format, noNumber()on large values)
src/actions/get-token-accounts.ts
export interface TokenAccount {
mint: string;
ata: string;
amount: string; // raw base units
decimals: number;
uiAmount: string;
}
export async function getTokenAccounts(
connection: Connection,
owner: PublicKey,
): Promise<TokenAccount[]>
- Uses
connection.getParsedTokenAccountsByOwner(owner, { programId: TOKEN_PROGRAM_ID }) - Returns all token accounts with non-zero balance
- Filters out zero-balance accounts
- Uses
@solana/spl-token'sTOKEN_PROGRAM_IDfor the filter uiAmountuses BigInt-safe division
src/actions/index.ts
Add exports:
export { getTokenBalance } from "./get-token-balance.js";
export type { TokenBalanceResult } from "./get-token-balance.js";
export { getTokenAccounts } from "./get-token-accounts.js";
export type { TokenAccount } from "./get-token-accounts.js";
src/agent.ts
Add two methods:
/** Get SPL token balance. Returns null if the wallet has no account for that mint. */
getTokenBalance(mint: PublicKey | string): Promise<TokenBalanceResult | null> {
return getTokenBalance(this.connection, this.wallet.publicKey, mint);
}
/** List all SPL token accounts with non-zero balance. */
getTokenAccounts(): Promise<TokenAccount[]> {
return getTokenAccounts(this.connection, this.wallet.publicKey);
}
Import the functions at the top of agent.ts.
src/index.ts
Add exports for the new types and functions:
export { getTokenBalance } from "./actions/get-token-balance.js";
export type { TokenBalanceResult } from "./actions/get-token-balance.js";
export { getTokenAccounts } from "./actions/get-token-accounts.js";
export type { TokenAccount } from "./actions/get-token-accounts.js";
Verification
npm run buildpasses with zero errorsSolanaAgenthas bothgetTokenBalance(mint)andgetTokenAccounts()methods- Both standalone functions are exported from the main index
getTokenBalancereturnsnull(not an error) when the ATA doesn't existuiAmountfields use BigInt-safe arithmetic — noNumber(bigint)