Skip to content

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.

1.3 KB
Raw Blame
import { Connection, PublicKey } from "@solana/web3.js";
import { getAssociatedTokenAddressSync, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID } from "@solana/spl-token";

export interface TokenBalanceResult {
  mint: string;
  amount: string;
  decimals: number;
  uiAmount: string;
  ata: string;
}

function formatUiAmount(rawAmount: string, decimals: number): string {
  const big = BigInt(rawAmount);
  const divisor = BigInt(10 ** decimals);
  const whole = big / divisor;
  const frac = big % divisor;
  if (frac === 0n) return whole.toString();
  const fracStr = frac.toString().padStart(decimals, "0").replace(/0+$/, "");
  return `${whole}.${fracStr}`;
}

export async function getTokenBalance(
  connection: Connection,
  owner: PublicKey,
  mint: PublicKey | string,
): Promise<TokenBalanceResult | null> {
  const mintPk = typeof mint === "string" ? new PublicKey(mint) : mint;
  const ata = getAssociatedTokenAddressSync(mintPk, owner, false, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID);

  try {
    const balance = await connection.getTokenAccountBalance(ata);
    const { amount, decimals } = balance.value;
    return {
      mint: mintPk.toBase58(),
      amount,
      decimals,
      uiAmount: formatUiAmount(amount, decimals),
      ata: ata.toBase58(),
    };
  } catch {
    return null;
  }
}