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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(wallet): tighten treasury autopilot, wallet intents and bazaar client handling, with Jupiter quote param tests
nirholas96a75a6ef57b
diff --git a/api/_lib/treasury-autopilot.js b/api/_lib/treasury-autopilot.jsindex 90b9b8633..ebe5439fa 100644--- a/api/_lib/treasury-autopilot.js+++ b/api/_lib/treasury-autopilot.js@@ -566,11 +566,38 @@ async function tokenUiBalance(conn, owner, mint) {* Jupiter, returning the signature. Throws on devnet, no route, or any failure —* the executor turns that into an honest pause, never a guess.*/+// Build the Jupiter quote query from validated parts. The mint was interpolated+// raw, so a mint string carrying `&slippageBps=10000` or `&amount=` rewrote the+// quote the agent then signed. A mint must be a real base58 pubkey, slippage a+// bounded integer, and every value is encoded.+export function jupiterQuoteParams({ outputMint, lamports, slippageBps }) {+ let mint;+ try {+ mint = new PublicKey(String(outputMint)).toBase58();+ } catch {+ mint = null;+ }+ if (!mint || mint !== outputMint) {+ throw Object.assign(new Error('output mint is not a valid Solana address'), { code: 'invalid_mint' });+ }+ const bps = Math.trunc(Number(slippageBps));+ if (!Number.isFinite(bps) || bps < 0 || bps > 10_000) {+ throw Object.assign(new Error('slippage must be 0-10000 bps'), { code: 'invalid_slippage' });+ }+ return new URLSearchParams({+ inputMint: WSOL_MINT,+ outputMint: mint,+ amount: BigInt(lamports).toString(),+ slippageBps: String(bps),+ swapMode: 'ExactIn',+ }).toString();+}+async function swapSolToToken({ keypair, lamports, outputMint, slippageBps, network, conn }) {if (network !== 'mainnet') {throw Object.assign(new Error('token swaps are mainnet-only — this wallet is on devnet'), { code: 'devnet_no_swap' });}- const qUrl = `${JUPITER_BASE}/quote?inputMint=${WSOL_MINT}&outputMint=${outputMint}&amount=${lamports.toString()}&slippageBps=${slippageBps}&swapMode=ExactIn`;+ const qUrl = `${JUPITER_BASE}/quote?${jupiterQuoteParams({ outputMint, lamports, slippageBps })}`;// The quote is an idempotent GET: a 429/5xx/network blip is retried with// jittered backoff inside the same 15s per-attempt deadline. A 4xx (no route// for this pair) is an answer and comes straight back as `no_route`, which thediff --git a/api/_lib/wallet-intents.js b/api/_lib/wallet-intents.jsindex c15118176..0a09ecccf 100644--- a/api/_lib/wallet-intents.js+++ b/api/_lib/wallet-intents.js@@ -951,9 +951,36 @@ async function buyToken({ ctx, intent, discriminator, mint, lamports, slippagePc});}+// Build the Jupiter quote query from validated parts. The mint was interpolated+// raw, so a mint string carrying `&slippageBps=10000` or `&amount=` rewrote the+// quote the agent then signed. A mint must be a real base58 pubkey, slippage a+// bounded integer, and every value is encoded.+export function jupiterQuoteParams({ outputMint, lamports, slippageBps }) {+ let mint;+ try {+ mint = new PublicKey(String(outputMint)).toBase58();+ } catch {+ mint = null;+ }+ if (!mint || mint !== outputMint) {+ throw Object.assign(new Error('output mint is not a valid Solana address'), { code: 'invalid_mint' });+ }+ const bps = Math.trunc(Number(slippageBps));+ if (!Number.isFinite(bps) || bps < 0 || bps > 10_000) {+ throw Object.assign(new Error('slippage must be 0-10000 bps'), { code: 'invalid_slippage' });+ }+ return new URLSearchParams({+ inputMint: WSOL_MINT,+ outputMint: mint,+ amount: BigInt(lamports).toString(),+ slippageBps: String(bps),+ swapMode: 'ExactIn',+ }).toString();+}+async function swapSolToToken({ keypair, lamports, outputMint, slippageBps, conn }) {const { VersionedTransaction } = await import('@solana/web3.js');- const qUrl = `${JUPITER_BASE}/quote?inputMint=${WSOL_MINT}&outputMint=${outputMint}&amount=${lamports.toString()}&slippageBps=${slippageBps}&swapMode=ExactIn`;+ const qUrl = `${JUPITER_BASE}/quote?${jupiterQuoteParams({ outputMint, lamports, slippageBps })}`;const qRes = await fetchWithTimeout(qUrl, {}, 15_000);if (!qRes.ok) throw Object.assign(new Error(`no swap route (${qRes.status})`), { code: 'no_route' });const quote = await qRes.json();diff --git a/api/_lib/x402/bazaar-client.js b/api/_lib/x402/bazaar-client.jsindex 98aba0ccf..58ecac09a 100644--- a/api/_lib/x402/bazaar-client.js+++ b/api/_lib/x402/bazaar-client.js@@ -286,6 +286,24 @@ async function listOneFacilitator(facilitatorUrl, { type, limit, maxItems, untruconst CATALOG_TTL_MS = Math.max(0, Number(process.env.BAZAAR_CATALOG_TTL_MS) || 60_000);const catalogMemo = new Map();+// The memo key carries caller-chosen facilitators, limit and maxItems, and+// /api/bazaar/list is anonymous, so without a bound every distinct query would+// pin its own multi-MB catalog in memory until the process fell over. The+// platform's own views use a handful of keys; anything past this cap evicts the+// oldest entry (Map iteration is insertion order).+const CATALOG_MEMO_MAX = 16;++function rememberCatalog(key, entry, now) {+ for (const [k, v] of catalogMemo) {+ if (now - v.at >= CATALOG_TTL_MS) catalogMemo.delete(k);+ }+ catalogMemo.delete(key);+ while (catalogMemo.size >= CATALOG_MEMO_MAX) {+ catalogMemo.delete(catalogMemo.keys().next().value);+ }+ catalogMemo.set(key, entry);+}+// Callers filter and sort what they get back, so hand out fresh array wrappers// around the shared item objects rather than the memo's own arrays.function shallowCopy(result) {@@ -385,7 +403,7 @@ export class Bazaar {const now = Date.now();if (hit && now - hit.at < CATALOG_TTL_MS) return shallowCopy(await hit.promise);const promise = this.list({ type, limit, maxItems });- catalogMemo.set(key, { at: now, promise });+ rememberCatalog(key, { at: now, promise }, now);try {return shallowCopy(await promise);} catch (e) {diff --git a/tests/jupiter-quote-params.test.js b/tests/jupiter-quote-params.test.jsnew file mode 100644index 000000000..3b13e2413--- /dev/null+++ b/tests/jupiter-quote-params.test.js@@ -0,0 +1,34 @@+// Jupiter quote URLs are built from a validated mint and encoded parameters.+//+// The autopilot and wallet-intent swap paths interpolated the output mint raw+// into the quote query, so a mint string like `<mint>&slippageBps=10000` rewrote+// the slippage (or amount) of the quote the agent wallet then signed.++import { describe, it, expect, vi } from 'vitest';++vi.mock('../api/_lib/db.js', () => ({ sql: vi.fn(async () => []) }));++const THREE = 'FeMbDoX7R1Psc4GEcvJdsbNbZA3bfztcyDCatJVJpump';++for (const mod of ['../api/_lib/treasury-autopilot.js', '../api/_lib/wallet-intents.js']) {+ describe(`jupiterQuoteParams in ${mod}`, async () => {+ const { jupiterQuoteParams } = await import(mod);++ it('builds an encoded query for a real mint', () => {+ const q = new URLSearchParams(jupiterQuoteParams({ outputMint: THREE, lamports: 1000n, slippageBps: 300 }));+ expect(q.get('outputMint')).toBe(THREE);+ expect(q.get('slippageBps')).toBe('300');+ expect(q.get('amount')).toBe('1000');+ expect(q.getAll('slippageBps')).toHaveLength(1);+ });++ it('refuses a mint carrying extra query parameters', () => {+ expect(() => jupiterQuoteParams({ outputMint: `${THREE}&slippageBps=10000`, lamports: 1000n, slippageBps: 50 }))+ .toThrow(/valid Solana address/);+ });++ it('refuses an out-of-range slippage', () => {+ expect(() => jupiterQuoteParams({ outputMint: THREE, lamports: 1n, slippageBps: 20_000 })).toThrow(/slippage/);+ });+ });+}