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.5 KB
Raw Blame
import {
  ComputeBudgetProgram,
  PublicKey,
  TransactionMessage,
  VersionedTransaction,
  type Connection,
  type TransactionInstruction,
} from "@solana/web3.js";
import { SimulationError } from "../errors.js";

export async function estimatePriorityFee(connection: Connection): Promise<number> {
  const fees = await connection.getRecentPrioritizationFees();
  if (fees.length === 0) return 1_000;
  const sorted = fees.map((f) => f.prioritizationFee).sort((a, b) => a - b);
  return sorted[Math.floor(sorted.length * 0.75)] ?? 1_000;
}

export async function estimateComputeUnits(
  connection: Connection,
  instructions: TransactionInstruction[],
  payer: PublicKey,
): Promise<number> {
  const { blockhash } = await connection.getLatestBlockhash();
  const msg = new TransactionMessage({
    payerKey: payer,
    recentBlockhash: blockhash,
    instructions,
  }).compileToV0Message();

  const sim = await connection.simulateTransaction(
    new VersionedTransaction(msg),
    { sigVerify: false },
  );

  if (sim.value.err) throw new SimulationError(sim.value.err);
  const units = sim.value.unitsConsumed;
  if (!units) {
    return Math.min(200_000, 10_000 + instructions.length * 20_000);
  }
  return Math.ceil(units * 1.1);
}

export function priorityFeeIx(microLamports: number): TransactionInstruction {
  return ComputeBudgetProgram.setComputeUnitPrice({ microLamports });
}

export function computeUnitIx(units: number): TransactionInstruction {
  return ComputeBudgetProgram.setComputeUnitLimit({ units });
}