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.

feat(oracle): build the wallet trading profile from reputation and recent coin rows

nirholasc8311ac58187
diff --git a/api/_lib/oracle/sources.js b/api/_lib/oracle/sources.js
index e815ea01d..074277209 100644
--- a/api/_lib/oracle/sources.js
+++ b/api/_lib/oracle/sources.js
@@ -336,19 +336,35 @@ export async function recentMints({ network = 'mainnet', limit = 100, sinceSecon
return rows.map((r) => r.mint);
}
+/** How many recent coin appearances the wallet profile loads. */
+export const WALLET_PROFILE_COIN_LIMIT = 60;
+
/**
* A wallet's reputation + recent footprint, for the wallet profile endpoint.
+ *
+ * Unlike the conviction-engine readers above, this one does NOT swallow query
+ * faults into an empty result. A wallet profile IS the query result, so an
+ * unreachable database has to read as "we could not answer", never as "this
+ * trader has no record": the caller publishes that answer to a CDN, and a
+ * 30-second Neon blip would otherwise pin a false "wallet not indexed" over a
+ * real trader's track record for the life of the cache entry. Let it throw and
+ * let `wrap()` turn it into a 503 no-store.
+ *
* @param {string} wallet
* @param {string} network
*/
export async function walletProfile(wallet, network = 'mainnet') {
- const rep = await tryRow(() => sql`
+ const repRows = await sql`
select wallet, coins_traded, early_entries, wins, early_wins, duds, dumps,
creator_count, creator_wins, win_rate, early_win_rate, dump_rate,
smart_money_score, label, first_seen_at, last_active_at
from wallet_reputation where wallet = ${wallet} and network = ${network} limit 1
- `);
- const recent = await tryRows(() => sql`
+ `;
+ // pump_coin_wallets carries no network column, so a coin's network comes from
+ // its intel row. Without this filter a devnet query answered with the wallet's
+ // mainnet trades. The `is null` arm keeps a coin the indexer has observed but
+ // not yet classified, so the default mainnet read never silently drops history.
+ const recent = await sql`
select w.mint, w.buy_count, w.sell_count, w.buy_lamports, w.sell_lamports,
w.base_bought, w.base_sold, w.is_creator, w.first_seen_at, w.last_seen_at,
i.symbol, i.name, i.image_uri, i.category, i.quality_score, i.narrative,
@@ -357,10 +373,11 @@ export async function walletProfile(wallet, network = 'mainnet') {
left join pump_coin_intel i on i.mint = w.mint
left join pump_coin_outcomes o on o.mint = w.mint
where w.wallet = ${wallet}
+ and (i.network is null or i.network = ${network})
order by w.last_seen_at desc
- limit 60
- `);
- return { rep, recent };
+ limit ${WALLET_PROFILE_COIN_LIMIT}
+ `;
+ return { rep: repRows?.[0] || null, recent: recent || [] };
}
/**
diff --git a/api/traders/preview.js b/api/traders/preview.js
index ad61ef28c..e732649c0 100644
--- a/api/traders/preview.js
+++ b/api/traders/preview.js
@@ -4,11 +4,14 @@
* GET /api/traders/preview?wallet=<base58>&network=mainnet
*
* Returns the wallet's brain reputation (win rate, smart-money score, label,
- * trade count) and their 20 most recent pump.fun coin appearances. This powers
- * the /claim-wallet page where a KOL or trader can see their verified track
- * record before signing in to claim it.
+ * trade count) and their most recent pump.fun coin appearances
+ * (WALLET_PROFILE_COIN_LIMIT of them). This powers the /claim-wallet page where
+ * a KOL or trader can see their verified track record before signing in to
+ * claim it.
*
- * Public, IP rate-limited, 2-minute CDN cache.
+ * Public, IP rate-limited, 2-minute CDN cache. A database fault propagates out
+ * of walletProfile() so wrap() answers 503 no-store: an unreachable brain must
+ * never be published to the CDN as "this wallet has no track record".
*/
import { cors, json, method, wrap, error, rateLimited } from '../_lib/http.js';
@@ -89,12 +92,20 @@ export default wrap(async (req, res) => {
const totalBuy = coins.reduce((a, c) => a + (c.buy_sol || 0), 0);
const totalSell = coins.reduce((a, c) => a + (c.sell_sol || 0), 0);
- const wins = coins.filter((c) => c.pnl_sol != null && c.pnl_sol > 0).length;
- const losses = coins.filter((c) => c.pnl_sol != null && c.pnl_sol <= 0).length;
// ROI distribution buckets — the GMGN "Distribution (Token N)" panel. Each
// closed position lands in exactly one bucket by realized return.
const closed = coins.filter((c) => c.roi != null && !c.open);
+ // Win/loss is a statement about REALIZED outcomes, so only closed positions
+ // vote. Counting every non-positive pnl called an untouched open bag a loss:
+ // a sniper holding 60 fresh positions read as "0 wins / 60 losses" on the
+ // card while win_rate_window said null, and the client's own aggregate()
+ // (closed-only) disagreed with the server on the same numbers.
+ const wins = closed.filter((c) => c.pnl_sol > 0).length;
+ const losses = closed.length - wins;
+ // Realized PnL closes the same gap in the money column: net_pnl_sol books an
+ // open bag's buy as a total loss, so it is the floor, not the outcome.
+ const realizedPnl = closed.reduce((a, c) => a + (c.pnl_sol || 0), 0);
const dist = { x5: 0, x2: 0, up: 0, down: 0, rug: 0 };
for (const c of closed) {
if (c.roi >= 5) dist.x5++;
@@ -136,8 +147,13 @@ export default wrap(async (req, res) => {
last_active_at: rep.last_active_at ? new Date(rep.last_active_at).toISOString() : null,
} : null;
+ // `known` means the rollup has graded this wallet. `claimable` means there is
+ // a real on-chain record to claim, which is true the moment the indexer has
+ // seen the wallet trade — the rollup lags by design. Gating claimable on
+ // known made /claim-wallet answer "no record found" to wallets carrying
+ // thousands of indexed pump.fun trades, purely because the scorer hadn't run.
const known = !!profile;
- const claimable = known && (Number(profile.coins_traded) > 0 || coins.length > 0);
+ const claimable = coins.length > 0 || (known && Number(profile.coins_traded) > 0);
return json(res, 200, {
wallet,
@@ -158,6 +174,7 @@ export default wrap(async (req, res) => {
total_sell_sol: Math.round(totalSell * 1000) / 1000,
total_volume_sol: Math.round((totalBuy + totalSell) * 1000) / 1000,
net_pnl_sol: Math.round((totalSell - totalBuy) * 1000) / 1000,
+ realized_pnl_sol: Math.round(realizedPnl * 1000) / 1000,
avg_buy_sol: coins.length ? Math.round((totalBuy / coins.length) * 1000) / 1000 : 0,
avg_sell_sol: coins.length ? Math.round((totalSell / coins.length) * 1000) / 1000 : 0,
total_tx: totalTx,