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
Add vault peak-share-price migration and live KOL leaderboard; refine vault trade/transfer
nirholas29ac31e2bd26
diff --git a/api/_lib/migrations/20260628140000_agent_vaults_peak_share_price.sql b/api/_lib/migrations/20260628140000_agent_vaults_peak_share_price.sqlnew file mode 100644index 000000000..bc141bbbc--- /dev/null+++ b/api/_lib/migrations/20260628140000_agent_vaults_peak_share_price.sql@@ -0,0 +1,10 @@+begin;++-- Rename peak_nav_atomics → peak_share_price_e6 on agent_vaults.+-- The original migration (20260628130000_agent_vaults.sql) was edited in place+-- to track peak share price (NAV per share, scaled 1e6) instead of raw NAV; this+-- forward migration brings the live schema in sync.+alter table agent_vaults+ rename column peak_nav_atomics to peak_share_price_e6;++commit;diff --git a/api/_lib/vault-trade.js b/api/_lib/vault-trade.jsindex 3ba94f018..84aaed842 100644--- a/api/_lib/vault-trade.js+++ b/api/_lib/vault-trade.js@@ -79,22 +79,26 @@ async function signSendConfirm(conn, tx, keypair) {export async function enforceBreaker(vault, { userId = null, reason = 'post_trade' } = {}) {const positions = await getOpenPositions(vault.id);const nav = await computeVaultNav(vault, positions);- const peak = nextPeak(vault.peak_nav_atomics, nav.navAtomics);+ // The breaker measures SHARE PRICE drawdown (NAV per share), not raw NAV — so+ // deposits/redemptions, which move NAV but not price, can never trip it. Only+ // real trading losses move the share price down.const priceE6 = sharePriceE6(nav.navAtomics, vault.total_shares);+ const peak = nextPeak(vault.peak_share_price_e6, priceE6);// Ratchet the peak (only rises). Done regardless of breach.await applyVaultShareDelta(vault.id, 0n, peak);let halted = false;- if (nav.priced && vault.status === 'open' && isDrawdownBreached(peak, nav.navAtomics, vault.max_drawdown_bps)) {+ if (nav.priced && vault.status === 'open' && toBig(vault.total_shares) > 0n+ && isDrawdownBreached(peak, priceE6, vault.max_drawdown_bps)) {await setVaultStatus(vault.id, 'paused', { haltReason: 'drawdown' });await recordVaultEvent({vaultId: vault.id, type: 'drawdown_halt', userId,navAtomics: nav.navAtomics, sharePriceE6: priceE6,reason: 'drawdown circuit breaker tripped — autonomous trading halted to protect capital',- meta: { peak_nav_atomics: String(peak), nav_atomics: String(nav.navAtomics), max_drawdown_bps: vault.max_drawdown_bps, trigger: reason },+ meta: { peak_share_price_e6: String(peak), share_price_e6: String(priceE6), nav_atomics: String(nav.navAtomics), max_drawdown_bps: vault.max_drawdown_bps, trigger: reason },});- logAudit({ userId, action: 'vault.drawdown_halt', resourceId: vault.id, meta: { peak: String(peak), nav: String(nav.navAtomics), max_drawdown_bps: vault.max_drawdown_bps } });+ logAudit({ userId, action: 'vault.drawdown_halt', resourceId: vault.id, meta: { peak_share_price_e6: String(peak), share_price_e6: String(priceE6), max_drawdown_bps: vault.max_drawdown_bps } });halted = true;}return { nav, peak, sharePriceE6: priceE6, halted };diff --git a/api/_lib/vault-transfer.js b/api/_lib/vault-transfer.jsindex 71e8d54ee..8cfb802a8 100644--- a/api/_lib/vault-transfer.js+++ b/api/_lib/vault-transfer.js@@ -130,7 +130,10 @@ export async function depositToVault({ vault, backerAgent, userId, usdcAtomics,vaultId: vault.id, userId, backerAgentId: backerAgent.id,sharesDelta: minted, basisDelta: amount, depositedDelta: amount,});- const updated = await applyVaultShareDelta(vault.id, minted, nextPeak(vault.peak_nav_atomics, nav.navAtomics));+ // Ratchet the share-price peak (a deposit mints at the current price, so this+ // is a no-op on price — it just seeds the peak on the very first deposit).+ const priceAfter = sharePriceE6(nav.navAtomics, toBig(vault.total_shares) + minted);+ const updated = await applyVaultShareDelta(vault.id, minted, nextPeak(vault.peak_share_price_e6, priceAfter));logAudit({ userId, action: 'vault.deposit', resourceId: vault.id, meta: { agent_id: vault.agent_id, usdc_atomics: String(amount), shares: String(minted), signature } });diff --git a/src/kol/kolscan-live.js b/src/kol/kolscan-live.jsnew file mode 100644index 000000000..a74f3768d--- /dev/null+++ b/src/kol/kolscan-live.js@@ -0,0 +1,168 @@+// Live KOL leaderboard — kolscan.io.+//+// kolscan.io publishes a real, public leaderboard of named Solana traders ranked+// by realized SOL profit over a daily / weekly / monthly window. Its JSON API is+// auth-gated, but the /leaderboard page server-renders ALL THREE windows into its+// Next.js flight data in a single response — so one HTML GET yields the full+// board. This module fetches it, parses the embedded `initLeaderboard` array, and+// maps it onto the wallet-centric shape the KOL surfaces already speak+// ({ wallet, pnlSol, pnlUsd, winRate, trades }).+//+// We deliberately drop the trader names/handles kolscan attaches: the existing+// leaderboard schema is address-keyed, and surfacing arbitrary third-party social+// handles is neither needed nor something we want to render verbatim.+//+// Honest degradation (mirrors gmgn-feed.js / smart-money.js): a network error,+// a bot-challenge, a layout change, or a price-feed outage resolves to `null`,+// never a throw and never fabricated rows — the caller decides how to degrade.+//+// Coin-agnostic analytics over real on-chain traders. $THREE is the only coin+// three.ws promotes; this reads wallets + realized PnL, no token is referenced.++import { solToUsd } from '../shared/usd-price.js';+import { cacheGet, cacheSet } from '../../api/_lib/cache.js';++const LEADERBOARD_URL = 'https://kolscan.io/leaderboard';+const FETCH_TIMEOUT_MS = 9_000;+const CACHE_KEY = 'kol:kolscan:leaderboard:v1';+const CACHE_TTL_S = 120; // the board moves slowly; spare the upstream + the edge++// kolscan's `timeframe` field → the window vocabulary the KOL surfaces use.+const TIMEFRAME_TO_WINDOW = { 1: '24h', 7: '7d', 30: '30d' };++const BROWSER_HEADERS = {+ 'user-agent':+ 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',+ accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',+ 'accept-language': 'en-US,en;q=0.9',+ referer: 'https://kolscan.io/',+};++const num = (v) => (Number.isFinite(Number(v)) ? Number(v) : 0);++// Reassemble the Next.js RSC flight stream the page ships as a series of+// `self.__next_f.push([1,"<escaped-json-string>"])` calls. Each call's payload is+// a JSON string literal; decoding and concatenating them yields the full blob the+// server rendered, which contains the `initLeaderboard` array verbatim.+function decodeFlight(html) {+ const re = /self\.__next_f\.push\(\[1,"((?:[^"\\]|\\.)*)"\]\)/g;+ let blob = '';+ let m;+ while ((m = re.exec(html)) !== null) {+ try {+ blob += JSON.parse(`"${m[1]}"`);+ } catch {+ /* skip a malformed chunk rather than abandon the whole parse */+ }+ }+ return blob;+}++// Pull the JSON array that follows a `"<key>":` marker via brace/bracket matching,+// so embedded strings or nested arrays can't truncate it the way a greedy regex+// would. Returns the parsed array, or null if the marker/array isn't well-formed.+function extractJsonArray(blob, key) {+ const at = blob.indexOf(`"${key}":`);+ if (at === -1) return null;+ const start = blob.indexOf('[', at);+ if (start === -1) return null;+ let depth = 0;+ let inStr = false;+ let esc = false;+ for (let i = start; i < blob.length; i++) {+ const ch = blob[i];+ if (inStr) {+ if (esc) esc = false;+ else if (ch === '\\') esc = true;+ else if (ch === '"') inStr = false;+ continue;+ }+ if (ch === '"') inStr = true;+ else if (ch === '[') depth++;+ else if (ch === ']' && --depth === 0) {+ try {+ return JSON.parse(blob.slice(start, i + 1));+ } catch {+ return null;+ }+ }+ }+ return null;+}++/**+ * Parse a kolscan /leaderboard HTML document into per-window rows. Pure: no I/O,+ * no USD pricing (that needs a live feed) — just the SOL-denominated truth the+ * page rendered. Returns null when the leaderboard payload can't be found.+ *+ * @param {string} html+ * @returns {null | { '24h': Row[], '7d': Row[], '30d': Row[] }}+ * Row = { wallet, pnlSol, winRate, trades }+ */+export function parseKolscanLeaderboard(html) {+ if (typeof html !== 'string' || html.length === 0) return null;+ const blob = decodeFlight(html);+ const entries = extractJsonArray(blob, 'initLeaderboard');+ if (!Array.isArray(entries) || entries.length === 0) return null;++ const windows = { '24h': [], '7d': [], '30d': [] };+ for (const e of entries) {+ const window = TIMEFRAME_TO_WINDOW[e?.timeframe];+ const wallet = e?.wallet_address;+ if (!window || typeof wallet !== 'string' || wallet.length < 32) continue;+ const wins = num(e.wins);+ const losses = num(e.losses);+ const trades = wins + losses;+ windows[window].push({+ wallet,+ pnlSol: num(e.profit),+ winRate: trades > 0 ? wins / trades : 0,+ trades,+ });+ }+ // A payload that parsed but yielded no usable rows is a layout change, not data.+ if (!windows['24h'].length && !windows['7d'].length && !windows['30d'].length) return null;+ return windows;+}++// Attach a USD figure to every row from a single live SOL/USD read. If the price+// feed is down, solToUsd returns null and we surface pnlUsd: null — the caller+// treats a board with no USD as "no live data" and degrades accordingly.+async function priceWindows(windows) {+ const solPrice = await solToUsd(1); // dollars per 1 SOL, or null on feed outage+ const apply = (rows) =>+ rows.map((r) => ({ ...r, pnlUsd: solPrice == null ? null : r.pnlSol * solPrice }));+ return { '24h': apply(windows['24h']), '7d': apply(windows['7d']), '30d': apply(windows['30d']) };+}++/**+ * Fetch the live kolscan leaderboard for all windows, priced in USD, cached+ * briefly. Resolves to null on any failure (network, challenge, parse, pricing).+ *+ * @returns {Promise<null | { '24h': Row[], '7d': Row[], '30d': Row[] }>}+ * Row = { wallet, pnlSol, pnlUsd, winRate, trades }+ */+export async function fetchKolscanLeaderboard() {+ const cached = await cacheGet(CACHE_KEY).catch(() => null);+ if (cached) return cached;++ const ctrl = new AbortController();+ const timer = setTimeout(() => ctrl.abort(), FETCH_TIMEOUT_MS);+ let html;+ try {+ const res = await fetch(LEADERBOARD_URL, { headers: BROWSER_HEADERS, signal: ctrl.signal });+ if (!res.ok) return null;+ html = await res.text();+ } catch {+ return null;+ } finally {+ clearTimeout(timer);+ }++ const parsed = parseKolscanLeaderboard(html);+ if (!parsed) return null;++ const priced = await priceWindows(parsed);+ await cacheSet(CACHE_KEY, priced, CACHE_TTL_S).catch(() => {});+ return priced;+}