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.

fix(payments): route every Solana read on a checkout path through the shared guards

nirholas5c64528ea46f
diff --git a/api/agents/payments/[action].js b/api/agents/payments/[action].js
index cc7d347af..b3259d2af 100644
--- a/api/agents/payments/[action].js
+++ b/api/agents/payments/[action].js
@@ -13,6 +13,7 @@
import { z } from 'zod';
import { solanaConnection } from '../../_lib/solana/connection.js';
+import { blockhashKey, getRecentBlockhashInfo } from '../../_lib/solana/read-guards.js';
import { PublicKey, Transaction, ComputeBudgetProgram } from '@solana/web3.js';
import {
PumpAgentOffline,
@@ -117,7 +118,7 @@ async function handleCreatePrep(req, res) {
authority: owner,
});
- const { blockhash, lastValidBlockHeight } = await conn.getLatestBlockhash('confirmed');
+ const { blockhash, lastValidBlockHeight } = await getRecentBlockhashInfo(conn, blockhashKey({ network: body.cluster }));
const tx = new Transaction({ feePayer: owner, blockhash, lastValidBlockHeight }).add(
ComputeBudgetProgram.setComputeUnitLimit({
units: PumpAgentOffline.DEFAULT_COMPUTE_UNIT_LIMIT_FOR_AGENT_PAYMENTS,
@@ -322,7 +323,7 @@ async function handlePayPrep(req, res) {
endTime: new BN(endTime),
});
- const { blockhash, lastValidBlockHeight } = await conn.getLatestBlockhash('confirmed');
+ const { blockhash, lastValidBlockHeight } = await getRecentBlockhashInfo(conn, blockhashKey({ network: body.cluster }));
const tx = new Transaction({
feePayer: payerPk,
blockhash,
@@ -536,7 +537,7 @@ async function handleDistributePrep(req, res) {
user: wallet,
currencyMint: new PublicKey(body.currency_mint),
});
- const { blockhash, lastValidBlockHeight } = await conn.getLatestBlockhash('confirmed');
+ const { blockhash, lastValidBlockHeight } = await getRecentBlockhashInfo(conn, blockhashKey({ network: body.cluster }));
const tx = new Transaction({ feePayer: wallet, blockhash, lastValidBlockHeight }).add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 120_000 }),
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 100_000 }),
@@ -615,7 +616,7 @@ async function handleWithdrawPrep(req, res) {
: spl.getAssociatedTokenAddressSync(currency, authority);
const offline = PumpAgentOffline.load(new PublicKey(body.mint), conn);
const ix = await offline.withdraw({ authority, currencyMint: currency, receiverAta });
- const { blockhash, lastValidBlockHeight } = await conn.getLatestBlockhash('confirmed');
+ const { blockhash, lastValidBlockHeight } = await getRecentBlockhashInfo(conn, blockhashKey({ network: body.cluster }));
const tx = new Transaction({ feePayer: authority, blockhash, lastValidBlockHeight }).add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 100_000 }),
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 100_000 }),
diff --git a/api/marketplace/purchase-as-agent.js b/api/marketplace/purchase-as-agent.js
index 667b81824..78dc6e025 100644
--- a/api/marketplace/purchase-as-agent.js
+++ b/api/marketplace/purchase-as-agent.js
@@ -21,12 +21,12 @@
*/
import { Keypair, PublicKey } from '@solana/web3.js';
import { solanaConnection } from '../_lib/solana/connection.js';
+import { mintDecimals } from '../_lib/solana/read-guards.js';
import { submitProtected } from '../_lib/execution-engine.js';
import {
getAssociatedTokenAddressSync,
createTransferCheckedInstruction,
createAssociatedTokenAccountIdempotentInstruction,
- getMint,
} from '@solana/spl-token';
import { sql } from '../_lib/db.js';
@@ -232,7 +232,7 @@ export default wrap(async (req, res) => {
const connection = solanaConnection({ url: SOLANA_RPC, commitment: 'confirmed' });
const mintKey = new PublicKey(price.currency_mint);
const recipKey = new PublicKey(recipientAddr);
- const mintInfo = await getMint(connection, mintKey);
+ const decimals = await mintDecimals(connection, mintKey);
const fromAta = getAssociatedTokenAddressSync(mintKey, buyerKeypair.publicKey);
const toAta = getAssociatedTokenAddressSync(mintKey, recipKey);
@@ -245,7 +245,7 @@ export default wrap(async (req, res) => {
];
const transferIx = createTransferCheckedInstruction(
fromAta, mintKey, toAta, buyerKeypair.publicKey,
- BigInt(price.amount), mintInfo.decimals,
+ BigInt(price.amount), decimals,
);
transferIx.keys.push({ pubkey: referenceKey, isSigner: false, isWritable: false });
ixs.push(transferIx);
diff --git a/api/portfolio/[action].js b/api/portfolio/[action].js
index 8a205772a..ce22dcd35 100644
--- a/api/portfolio/[action].js
+++ b/api/portfolio/[action].js
@@ -12,6 +12,7 @@
import { cors, json, method, readJson, wrap, error, validationError, rateLimited } from '../_lib/http.js';
import { solanaConnection } from '../_lib/solana/connection.js';
+import { ataExists, blockhashKey, getRecentBlockhashInfo } from '../_lib/solana/read-guards.js';
import { evmFallbackProvider } from '../_lib/evm/rpc.js';
import { limits } from '../_lib/rate-limit.js';
import { getSessionUser, isSameSiteOrigin } from '../_lib/auth.js';
@@ -368,15 +369,18 @@ async function sendSolana({ agent, asset, recipient, amount, userId }) {
const senderAta = await getAssociatedTokenAddress(mintPk, kp.publicKey);
const recipientAta = await getAssociatedTokenAddress(mintPk, recipientPk);
- const recipientAccount = await conn.getAccountInfo(recipientAta);
- if (!recipientAccount) {
+ // Fails open to "missing" when the chain cannot be read: the worst case is
+ // an extra create instruction for an account that already exists, which the
+ // runtime treats as a no-op, whereas throwing here loses the whole transfer.
+ const recipientAccountExists = await ataExists(conn, recipientAta);
+ if (!recipientAccountExists) {
tx.add(createAssociatedTokenAccountInstruction(kp.publicKey, recipientAta, recipientPk, mintPk));
}
const amountUnits = parseAmountToBaseUnits(amount, decimals);
tx.add(createTransferInstruction(senderAta, recipientAta, kp.publicKey, amountUnits));
}
- const { blockhash } = await conn.getLatestBlockhash();
+ const { blockhash } = await getRecentBlockhashInfo(conn, blockhashKey({ url: env.SOLANA_RPC_URL }), { commitment: 'finalized' });
tx.feePayer = kp.publicKey;
tx.recentBlockhash = blockhash;
tx.sign(kp);
diff --git a/api/purchase/skill.js b/api/purchase/skill.js
index e78cf9ac0..14260348c 100644
--- a/api/purchase/skill.js
+++ b/api/purchase/skill.js
@@ -42,6 +42,7 @@ import { cors, error, json, method, readBody, wrap, rateLimited } from '../_lib/
import { clientIp, limits } from '../_lib/rate-limit.js';
import { logEvent, resolvePayoutAddress } from '../_lib/purchase-confirm.js';
import { solanaConnection } from '../_lib/solana/connection.js';
+import { blockhashKey, getRecentBlockhashInfo } from '../_lib/solana/read-guards.js';
import { resolveMarketplacePayer } from '../_lib/solana/gasless-tx.js';
const REFERENCE_RE = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/; // base58 Pubkey
@@ -235,7 +236,7 @@ export default wrap(async (req, res) => {
creatorIx.keys.push({ pubkey: referenceKey, isSigner: false, isWritable: false });
instructions.push(creatorIx);
- const { blockhash } = await connection.getLatestBlockhash('confirmed');
+ const { blockhash } = await getRecentBlockhashInfo(connection, blockhashKey({ network: 'mainnet' }));
const tx = new Transaction({ feePayer, recentBlockhash: blockhash }).add(...instructions);
// Sponsored checkout: the platform signs as fee payer so a buyer holding only
// USDC can still pay. The buyer adds the authority signature in their wallet.
diff --git a/api/subscriptions/subscribe.js b/api/subscriptions/subscribe.js
index e1abc67f9..9a0d02e0a 100644
--- a/api/subscriptions/subscribe.js
+++ b/api/subscriptions/subscribe.js
@@ -24,11 +24,11 @@ import {
import {
getAssociatedTokenAddressSync,
createTransferCheckedInstruction,
- getMint,
} from '@solana/spl-token';
import { z } from 'zod';
import { solanaConnection } from '../_lib/solana/connection.js';
+import { blockhashKey, getRecentBlockhashInfo, mintDecimals } from '../_lib/solana/read-guards.js';
import { sql } from '../_lib/db.js';
import { authenticateBearer, extractBearer, getSessionUser } from '../_lib/auth.js';
import { cors, error, json, method, readJson, wrap, rateLimited } from '../_lib/http.js';
@@ -183,10 +183,12 @@ export default wrap(async (req, res) => {
// Build the SPL transfer (creator leg + reference, fee leg) the buyer signs.
const connection = solanaConnection({ url: SOLANA_RPC, commitment: 'confirmed' });
- const { blockhash } = await connection.getLatestBlockhash('confirmed');
+ const { blockhash } = await getRecentBlockhashInfo(connection, blockhashKey({ url: SOLANA_RPC }));
const mintKey = new PublicKey(checkout.currency_mint);
- const mintInfo = await getMint(connection, mintKey);
- const decimals = mintInfo.decimals;
+ // USDC and the other settlement mints have fixed, known decimals, so the
+ // common case costs no network read at all and a dead RPC cannot stall a
+ // checkout over a constant.
+ const decimals = await mintDecimals(connection, mintKey);
const buyer = new PublicKey(buyerPublicKey);
const fromAta = getAssociatedTokenAddressSync(mintKey, buyer);
diff --git a/api/x402/pay-by-name.js b/api/x402/pay-by-name.js
index 6d57a15d2..6dc808978 100644
--- a/api/x402/pay-by-name.js
+++ b/api/x402/pay-by-name.js
@@ -42,6 +42,7 @@ import { env } from '../_lib/env.js';
import { cors, error, json, method, readJson, wrap, rateLimited } from '../_lib/http.js';
import { limits, clientIp } from '../_lib/rate-limit.js';
import { solanaConnection, loadAgentForSigning } from '../_lib/agent-pumpfun.js';
+import { blockhashKey, getRecentBlockhashInfo } from '../_lib/solana/read-guards.js';
import { getSpendLimits } from '../_lib/agent-trade-guards.js';
import { PARENT_LABEL } from '../_lib/threews-sns.js';
import {
@@ -323,7 +324,7 @@ async function handlePrep(req, res, body) {
}
const conn = solanaConnection('mainnet');
const ixs = await buildTransferIxs({ payer, recipient, amountAtoms });
- const { blockhash, lastValidBlockHeight } = await conn.getLatestBlockhash('confirmed');
+ const { blockhash, lastValidBlockHeight } = await getRecentBlockhashInfo(conn, blockhashKey({ network: 'mainnet' }));
const msg = new TransactionMessage({
payerKey: payer,
recentBlockhash: blockhash,