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(token-metadata): name a mint Helius cannot, and stop passing a slice of the mint off as a ticker

nirholas6cbd92c69515
diff --git a/api/_lib/balances.js b/api/_lib/balances.js
index fed03b1c5..eba810e35 100644
--- a/api/_lib/balances.js
+++ b/api/_lib/balances.js
@@ -313,7 +313,9 @@ async function getSolanaBalancesViaDas(address) {
if (!rawBalance) return null;
const amount = rawBalance / Math.pow(10, decimals);
const md = a?.content?.metadata || {};
- const symbol = ti.symbol || md.symbol || a.id.slice(0, 6);
+ // No mint-slice placeholder: an unnamed mint reports null so callers
+ // (and /api/crypto/wallet consumers) can tell "unknown" from a real ticker.
+ const symbol = ti.symbol || md.symbol || null;
const name = md.name || symbol;
const logo = a?.content?.links?.image || a?.content?.files?.[0]?.uri || null;
const priceFromHelius = ti.price_info?.price_per_token ?? null;
@@ -322,20 +324,35 @@ async function getSolanaBalancesViaDas(address) {
.filter(Boolean)
.filter((t) => t.amount > 0);
+ // DAS knows most mints but not all. Anything it returned unnamed goes through
+ // the shared resolver, whose keyless Jupiter rung names it, so the DAS path
+ // never reports a holding the fallback path could have identified.
+ const unnamed = fungible.filter((t) => !t.symbol).map((t) => t.mint);
+ if (unnamed.length > 0) {
+ const filled = await getMetadataForMints(unnamed);
+ for (const t of fungible) {
+ const md = filled.get(t.mint);
+ if (!md) continue;
+ t.symbol = t.symbol || md.symbol || null;
+ t.name = t.name || md.name || md.symbol || null;
+ t.logo = t.logo || md.logo || null;
+ }
+ }
+
// Persist metadata for everything we just resolved so other callers
- // (which may not use DAS) can hit cache instead of re-fetching.
- const metaPayload = fungible.map((t) => ({
- mint: t.mint,
- symbol: t.symbol,
- name: t.name,
- logo: t.logo,
- decimals: t.decimals,
- }));
+ // (which may not use DAS) can hit cache instead of re-fetching. Rows with no
+ // symbol, name, or logo are skipped: caching "unknown" would shadow the
+ // Jupiter rung on every later read.
+ const metaPayload = fungible
+ .filter((t) => t.symbol || t.name || t.logo)
+ .map((t) => ({
+ mint: t.mint,
+ symbol: t.symbol,
+ name: t.name,
+ logo: t.logo,
+ decimals: t.decimals,
+ }));
if (metaPayload.length > 0) {
- // Fire-and-forget — never block balance response on cache write.
- import('./token-metadata.js')
- .then((mod) => mod.getMetadataForMints([])) // ensure module loaded
- .catch(() => {});
// Direct write (cheaper than going through getMetadataForMints):
try {
const { sql, sqlValues } = await import('./db.js');
@@ -455,8 +472,8 @@ async function getSolanaBalancesFallback(address) {
const priceInfo = jupPrices?.[t.mint] || {};
const price = Number(priceInfo.usdPrice ?? priceInfo.price ?? 0);
return {
- symbol: md.symbol || t.mint.slice(0, 6),
- name: md.name || md.symbol || t.mint.slice(0, 6),
+ symbol: md.symbol || null,
+ name: md.name || md.symbol || null,
mint: t.mint,
decimals: t.decimals,
amount: t.amount,
@@ -587,8 +604,8 @@ async function getEvmBalances(address) {
const price = priceInfo.usd ?? 0;
const change24h = priceInfo.usd_24h_change ?? null;
return {
- symbol: meta?.symbol || t.contractAddress.slice(0, 8),
- name: meta?.name || meta?.symbol || t.contractAddress.slice(0, 8),
+ symbol: meta?.symbol || null,
+ name: meta?.name || meta?.symbol || null,
contract: t.contractAddress,
decimals,
amount,
diff --git a/api/_lib/token-metadata.js b/api/_lib/token-metadata.js
index 4c106503e..0d7c0a9b8 100644
--- a/api/_lib/token-metadata.js
+++ b/api/_lib/token-metadata.js
@@ -1,17 +1,20 @@
-// Solana token metadata resolver — Postgres cache primary, Helius DAS on miss.
+// Solana token metadata resolver: Postgres cache primary, Helius DAS on miss,
+// keyless Jupiter as the rung that keeps working when Helius does not.
//
// Why: getAsset/searchAssets are the most expensive Helius calls (~10 credits
// each). Mint metadata (symbol, name, logo, decimals) is effectively immutable,
// so we resolve a mint *once* and serve from `token_metadata` forever after.
//
// Cache flow for getMetadataForMints(mints[]):
-// 1. SELECT cached rows from token_metadata
+// 1. SELECT cached rows from token_metadata (fabricated placeholders re-resolve)
// 2. Resolve cache misses via Helius getAssetBatch (single RPC, up to 1000 mints)
-// 3. INSERT new rows (ON CONFLICT DO UPDATE — refresh stale logos)
+// 3. Resolve whatever Helius could not name via Jupiter token search (keyless)
+// 4. INSERT new rows (ON CONFLICT DO UPDATE, refreshing stale logos)
//
// Falls back gracefully:
-// - no Helius key → returns only cached + bare {mint, symbol: mint.slice(0,6)}
-// - DB unreachable → in-memory map for the request lifetime
+// - no Helius key or exhausted Helius quota: Jupiter names the mint instead
+// - no source knows the mint: symbol/name are null, never a slice of the mint
+// - DB unreachable: in-memory map for the request lifetime
import { sql, sqlValues } from './db.js';
@@ -22,8 +25,21 @@ function heliusRpcUrl() {
return key ? `https://mainnet.helius-rpc.com/?api-key=${key}` : null;
}
+// A mint no source could name. symbol/name stay null rather than a slice of the
+// mint: a fabricated "So1111" reads as a real ticker to every downstream caller
+// and to any agent consuming /api/crypto/wallet. Presentation layers
+// (src/portfolio.js, api/_lib/portfolio-overview.js) already derive a short-mint
+// label from a null symbol, so the honest null costs nothing on screen.
function bareEntry(mint) {
- return { mint, symbol: mint.slice(0, 6), name: mint.slice(0, 6), logo: null, decimals: null };
+ return { mint, symbol: null, name: null, logo: null, decimals: null };
+}
+
+// Rows written before the Jupiter rung existed (and any Helius row that fell back
+// to a mint slice) carry a fabricated symbol. Treat those as cache misses so the
+// cache heals itself on the next read instead of serving the fake forever.
+function isFabricated(entry) {
+ if (!entry?.symbol) return false;
+ return entry.symbol === entry.mint.slice(0, 6) || entry.symbol === entry.mint.slice(0, 8);
}
async function fetchFromCache(mints) {
@@ -38,14 +54,16 @@ async function fetchFromCache(mints) {
const now = Date.now();
for (const r of rows) {
const age = now - new Date(r.refreshed_at).getTime();
- if (age > REFRESH_AFTER_MS) continue; // stale — re-resolve
- out.set(r.mint, {
+ if (age > REFRESH_AFTER_MS) continue; // stale, re-resolve
+ const entry = {
mint: r.mint,
symbol: r.symbol,
name: r.name,
logo: r.logo,
decimals: r.decimals,
- });
+ };
+ if (isFabricated(entry)) continue; // mint-slice placeholder, re-resolve
+ out.set(r.mint, entry);
}
return out;
} catch (err) {
@@ -54,12 +72,12 @@ async function fetchFromCache(mints) {
}
}
-async function persist(entries) {
+async function persist(entries, source) {
if (entries.length === 0) return;
try {
const now = new Date();
const rows = entries.map((e) => [
- e.mint, 'solana', e.symbol, e.name, e.logo, e.decimals, 'helius-das', now,
+ e.mint, 'solana', e.symbol, e.name, e.logo, e.decimals, source, now,
]);
await sql`
INSERT INTO token_metadata (mint, chain, symbol, name, logo, decimals, source, refreshed_at)
@@ -103,18 +121,59 @@ async function resolveViaHelius(mints) {
const decimals = asset?.token_info?.decimals ?? null;
return {
mint: asset.id,
- symbol: md.symbol || asset.id.slice(0, 6),
- name: md.name || md.symbol || asset.id.slice(0, 6),
+ symbol: md.symbol || null,
+ name: md.name || md.symbol || null,
logo: asset?.content?.links?.image || asset?.content?.files?.[0]?.uri || null,
decimals,
};
- });
+ })
+ .filter((e) => e.symbol || e.name || e.logo);
} catch (err) {
console.warn('[token-metadata] helius resolve failed:', err?.message);
return [];
}
}
+// Keyless metadata rung. Jupiter's token search names any mint that has ever
+// traded on a Solana AMM, needs no key, and is the same host already used for
+// prices in api/_lib/balances.js. It is what keeps symbols real when the Helius
+// quota is exhausted (which returns HTTP 429 "max usage reached" on every call,
+// including getAssetBatch) or when no HELIUS_API_KEY is configured at all.
+const JUPITER_SEARCH_BATCH = 100;
+
+async function resolveViaJupiter(mints) {
+ if (mints.length === 0) return [];
+ const out = [];
+ for (let i = 0; i < mints.length; i += JUPITER_SEARCH_BATCH) {
+ const chunk = mints.slice(i, i + JUPITER_SEARCH_BATCH);
+ try {
+ const r = await fetch(
+ `https://lite-api.jup.ag/tokens/v2/search?query=${chunk.join(',')}`,
+ { signal: AbortSignal.timeout(15_000) },
+ );
+ if (!r.ok) {
+ console.warn('[token-metadata] jupiter search failed:', r.status);
+ continue;
+ }
+ const rows = await r.json();
+ if (!Array.isArray(rows)) continue;
+ for (const t of rows) {
+ if (!t?.id || !(t.symbol || t.name)) continue;
+ out.push({
+ mint: t.id,
+ symbol: t.symbol || null,
+ name: t.name || t.symbol || null,
+ logo: t.icon || null,
+ decimals: Number.isFinite(t.decimals) ? t.decimals : null,
+ });
+ }
+ } catch (err) {
+ console.warn('[token-metadata] jupiter chunk failed:', err?.message);
+ }
+ }
+ return out;
+}
+
/**
* Resolve metadata for a list of mints. Always returns one entry per input mint
* (bare placeholder if everything else fails).
@@ -126,10 +185,10 @@ export async function getMetadataForMints(mints) {
if (unique.length === 0) return new Map();
const cached = await fetchFromCache(unique);
- const missing = unique.filter((m) => !cached.has(m));
+ let missing = unique.filter((m) => !cached.has(m));
if (missing.length > 0) {
- // Helius getAssetBatch caps at 1000 per request — chunk safely.
+ // Helius getAssetBatch caps at 1000 per request, so chunk safely.
const resolved = [];
for (let i = 0; i < missing.length; i += 1000) {
const chunk = missing.slice(i, i + 1000);
@@ -137,9 +196,20 @@ export async function getMetadataForMints(mints) {
resolved.push(...part);
}
if (resolved.length > 0) {
- await persist(resolved);
+ await persist(resolved, 'helius-das');
for (const r of resolved) cached.set(r.mint, r);
}
+ missing = missing.filter((m) => !cached.has(m));
+ }
+
+ // Anything Helius could not name (no key, exhausted quota, or a mint DAS has
+ // no metadata for) gets the keyless Jupiter rung before we give up on it.
+ if (missing.length > 0) {
+ const viaJup = await resolveViaJupiter(missing);
+ if (viaJup.length > 0) {
+ await persist(viaJup, 'jupiter');
+ for (const r of viaJup) cached.set(r.mint, r);
+ }
}
// Fill any remaining gaps with bare placeholders so the caller gets a stable shape.