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(market): price every mint through the shared chain, and say when a smart-money board is remembered rather than empty

nirholas5d230c5f0f0c
diff --git a/api/_lib/balances.js b/api/_lib/balances.js
index eba810e35..eb2e3471b 100644
--- a/api/_lib/balances.js
+++ b/api/_lib/balances.js
@@ -17,6 +17,8 @@
import { cacheGet, cacheSet, cacheDel } from './cache.js';
import { getMetadataForMints } from './token-metadata.js';
import { solPriceUsd } from './sol-price.js';
+import { fetchTokenPriceUsd } from './market/token-market.js';
+import { fetchCoinPriceUsdOrNull } from './market-fallbacks.js';
import { solanaRpcEndpoints, makeRotatingFetch, markEndpointCooldown } from './solana/connection.js';
const BALANCES_TTL_S = 60;
@@ -245,6 +247,16 @@ export async function solanaMintUsdPrice(mint) {
} catch (err) {
console.warn('[balances] jupiter single-mint price failed:', err?.message);
}
+ // The shared market chain (Birdeye, tokens.xyz, DexScreener, GeckoTerminal,
+ // each with its own stale tier) sits between Jupiter and the pump.fun curve
+ // read below. Without it a Jupiter outage dropped straight to a curve price
+ // that only exists for pump mints, so every other token priced at zero.
+ try {
+ const p = Number(await fetchTokenPriceUsd(mint)) || 0;
+ if (p > 0) return p;
+ } catch (err) {
+ console.warn('[balances] market-chain single-mint price failed:', err?.message);
+ }
try {
const p = await pumpFunMintUsd(mint);
if (p > 0) return p;
@@ -594,6 +606,15 @@ async function getEvmBalances(address) {
} catch {
// best-effort
}
+ // CoinGecko's keyless tier is shared per egress IP and Cloud Run's is busy,
+ // so this leg throttles on its own schedule. Falling back to the multi-source
+ // price chain keeps the ETH leg of an EVM balance sheet valued instead of
+ // rendering a wallet full of holdings with no USD against any of them. The
+ // 24h change has no second source and stays absent rather than invented.
+ if (!(ethUsdPrice > 0)) {
+ const fallbackEth = await fetchCoinPriceUsdOrNull('ethereum');
+ if (fallbackEth > 0) ethUsdPrice = fallbackEth;
+ }
const tokens = rawTokens.map((t, i) => {
const meta = metadataResults[i].status === 'fulfilled' ? metadataResults[i].value?.result : null;
diff --git a/api/_lib/gmgn-feed.js b/api/_lib/gmgn-feed.js
index 7257907b5..9d4b39f29 100644
--- a/api/_lib/gmgn-feed.js
+++ b/api/_lib/gmgn-feed.js
@@ -133,12 +133,28 @@ export async function gmgnSmartMoneySnapshot({
let result = await fetchRank({ chain, interval });
let source = 'gmgn';
// A 403 is Cloudflare refusing this egress IP, not an outage: fall back to the
- // DexScreener board exactly as the poller does.
- if (!result.ok && result.status === 403) {
+ // DexScreener board exactly as the poller does. Any other GMGN failure gets
+ // the same treatment, because "GMGN did not answer" and "GMGN answered 403"
+ // are the same thing from the caller's side and the board is right there.
+ if (!result.ok) {
result = await fetchDexScreener({ chain });
source = 'dexscreener';
}
if (!result.ok) {
+ // Both live sources are down. A remembered board is a far better answer
+ // than an empty one, because an empty smart-money feed reads as "nobody is
+ // buying anything" rather than "we cannot see". It ships marked stale so
+ // no caller can mistake the two.
+ const remembered = _lastGoodItems.get(`${chain}:${interval}`);
+ if (remembered && Date.now() - remembered.at < LAST_GOOD_MAX_MS) {
+ return {
+ ok: true,
+ source: remembered.source,
+ items: remembered.items.slice(0, limit),
+ stale: true,
+ as_of: new Date(remembered.at).toISOString(),
+ };
+ }
return { ok: false, source: null, items: [], status: result.status, error: result.error };
}
source = result.source || source;
@@ -152,7 +168,23 @@ export async function gmgnSmartMoneySnapshot({
items.push(normalize(item, smartCount, 0, chain, interval, source));
if (items.length >= limit) break;
}
- return { ok: true, source, items };
+ if (items.length) _lastGoodItems.set(`${chain}:${interval}`, { items, source, at: Date.now() });
+ return { ok: true, source, items, stale: false };
+}
+
+// Last live board per (chain, interval). Small and per-instance on purpose: it
+// exists to ride out a throttle, not to be a cache.
+const LAST_GOOD_MAX_MS = 15 * 60_000;
+const _lastGoodItems = new Map();
+
+/**
+ * Test seam: forget every remembered board, so a suite can exercise the truly
+ * cold failure path (no live source AND nothing remembered) after a case that
+ * populated it. Module state outlives a single test exactly as it outlives a
+ * single request in production.
+ */
+export function _resetGmgnLastGood() {
+ _lastGoodItems.clear();
}
let _pollerId = 0;
diff --git a/tests/gmgn-snapshot.test.js b/tests/gmgn-snapshot.test.js
index 64ea7d247..c65f1c500 100644
--- a/tests/gmgn-snapshot.test.js
+++ b/tests/gmgn-snapshot.test.js
@@ -10,7 +10,7 @@
*/
import { describe, it, expect, vi, afterEach } from 'vitest';
-import { gmgnSmartMoneySnapshot } from '../api/_lib/gmgn-feed.js';
+import { gmgnSmartMoneySnapshot, _resetGmgnLastGood } from '../api/_lib/gmgn-feed.js';
// $THREE, the only coin this platform references.
const THREE = 'FeMbDoX7R1Psc4GEcvJdsbNbZA3bfztcyDCatJVJpump';
@@ -72,7 +72,26 @@ describe('gmgnSmartMoneySnapshot', () => {
expect(snap.items).toEqual([]);
});
- it('reports ok:false when every upstream is down (failure path)', async () => {
+ it('serves the last live board, marked stale, when every upstream is down', async () => {
+ // An empty smart-money feed reads as "nobody is buying anything", which is a
+ // claim, not an absence of one. A remembered board says what it is.
+ globalThis.fetch = vi.fn(async () => jsonResponse({
+ data: { rank: [{ address: THREE, symbol: 'THREE', smart_buy_24h: 9 }] },
+ }));
+ const live = await gmgnSmartMoneySnapshot();
+ expect(live.ok).toBe(true);
+ expect(live.stale).toBe(false);
+
+ globalThis.fetch = vi.fn(async () => jsonResponse({}, 500));
+ const stale = await gmgnSmartMoneySnapshot();
+ expect(stale.ok).toBe(true);
+ expect(stale.stale).toBe(true);
+ expect(stale.as_of).toBeTruthy();
+ expect(stale.items).toHaveLength(1);
+ });
+
+ it('reports ok:false when every upstream is down and nothing was ever seen', async () => {
+ _resetGmgnLastGood();
globalThis.fetch = vi.fn(async () => jsonResponse({}, 500));
const snap = await gmgnSmartMoneySnapshot();