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.

3.5 KB
Raw Blame
// `get_wallet_portfolio` - one KOL wallet's holdings card plus its real trading
// record. Read-only.
//
// Wraps GET /api/kol/wallets?addresses=<wallet>. Two independently-sourced halves
// come back in one row: current holdings from Birdeye (server-side key) and, when
// three.ws has trade history for the wallet, realized P&L / win rate / volume
// FIFO-computed from that wallet's own on-chain trades. Any P&L field can be
// null, which means "no trade history to measure", never "flat".

import { z } from 'zod';

import { apiRequest } from '../lib/api.js';

export const def = {
	name: 'get_wallet_portfolio',
	title: 'KOL wallet portfolio + P&L',
	annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: false, openWorldHint: true },
	description:
		"Pull one KOL trader's live portfolio card from three.ws. Returns current holdings " +
		'(total USD value, position count, and the single highest-value token the wallet ' +
		'holds) from the Birdeye proxy, plus realized P&L, win rate, trade count and volume ' +
		'over the last 30 days FIFO-computed from that wallet\'s own on-chain trades. Use this ' +
		'to size up a specific smart trader before copying or analyzing them. For ranking many ' +
		'traders at once use intel-mcp `kol_leaderboard`; this is the per-wallet deep dive. ' +
		'A null P&L field (with `pnl_source: null`) means three.ws has no trade history for ' +
		'that wallet in the window, which is an honest "unknown", NOT a flat or losing record: ' +
		'never report it as zero profit. `has_activity:false` means no holdings and no trades. ' +
		'If the holdings provider is down or rate-limited the call fails with `upstream_unavailable` ' +
		'rather than answering with an empty card, so an outage is never reported as a quiet wallet. ' +
		'Read-only live data.',
	inputSchema: {
		wallet: z
			.string()
			.min(1)
			.describe('The Solana wallet address of the KOL trader to pull a portfolio card for.'),
	},
	async handler(args) {
		const wallet = String(args?.wallet ?? '').trim();
		const data = await apiRequest('/api/kol/wallets', { query: { addresses: wallet } });
		// The proxy returns one row per requested address, and omits an address whose
		// upstream fetch failed rather than inventing a row for it. A wallet with no
		// holdings still gets a row (zeroed holdings, null P&L), so a MISSING row means
		// the holdings provider failed, not that the wallet is quiet. Say so: shaping
		// that gap into an all-null card would render an outage as "no activity", the
		// exact confusion the omission upstream exists to prevent.
		const rows = Array.isArray(data?.data) ? data.data : [];
		const row = rows.find((r) => r?.address === wallet);
		if (!row) {
			throw Object.assign(
				new Error(
					`three.ws returned no portfolio row for ${wallet}: the holdings provider is ` +
						'unavailable or rate-limited right now. That is an outage, not an empty wallet. Retry shortly.',
				),
				{ code: 'upstream_unavailable', status: 503 },
			);
		}

		const holdings = row.holdings ?? 0;
		const totalTrades = row.totalTrades ?? null;

		return {
			ok: true,
			wallet: row.address ?? wallet,
			has_activity: holdings > 0 || (totalTrades ?? 0) > 0,
			portfolio_value_usd: row.totalUsd ?? null,
			holdings,
			top_token: row.topToken ?? null,
			realized_pnl_usd: row.realizedPnl ?? null,
			win_rate: row.winRate ?? null,
			total_trades: totalTrades,
			volume_usd: row.volumeUsd ?? null,
			pnl_source: row.pnlSource ?? null,
			pnl_window: row.pnlWindow ?? null,
		};
	},
};