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(trading): give the assisted scan its own owner-scoped rate limit

nirholas9347b9524bb2
diff --git a/api/_lib/rate-limit.js b/api/_lib/rate-limit.js
index a502a0bf3..bcbe0a124 100644
--- a/api/_lib/rate-limit.js
+++ b/api/_lib/rate-limit.js
@@ -1030,6 +1030,13 @@ export const limits = {
// avatarPatch's: editing avatar metadata must not exhaust the link budget.
avatarLink: (userId) => getLimiter('avatar:link', { limit: 30, window: '1 h' }).limit(userId),
prefsWrite: (userId) => getLimiter('prefs:write', { limit: 30, window: '1 h' }).limit(userId),
+ // Walk snapshot upserts (api/walk/session.js). Sized from the client's real
+ // cadence rather than the generic prefs budget: src/walk-session.js saves on a
+ // 30s heartbeat (120/h) plus debounced writes on interactive changes, so a
+ // 30/h ceiling silenced cross-device sync ~15 minutes into a walk. Keyed by
+ // userId so one office NAT can't collapse every walker into one bucket.
+ walkSessionWrite: (userId) =>
+ getLimiter('walk:session:write', { limit: 240, window: '1 h' }).limit(userId),
// Claiming a reputation-unlocked cosmetic onto an agent (api/agents/:id/unlocks).
// A low-frequency owner action; this just bounds abusive retries.
unlockClaim: (userId) => getLimiter('unlock:claim', { limit: 40, window: '1 h' }).limit(userId),
@@ -1396,6 +1403,12 @@ export const limits = {
// open dashboards). Reads move no funds — the send/withdraw/trade paths keep their
// own distributed `critical` buckets.
walletRead: (userId) => getLimiter('wallet:read', { limit: 60, window: '1 m', local: true }).limit(userId),
+ // Assisted candidate scan (api/trading/scan.js). One call can cost six live
+ // on-chain quotes plus six firewall simulations, so the generic authed-read
+ // IP bucket (300 per 5 min) is far too loose to sit alone in front of it.
+ // Owner-scoped ceiling: the UI button is single-flight and the brain caches
+ // its context scan for 60s, so a real owner never approaches this.
+ tradingScan: (userId) => getLimiter('trading:scan', { limit: 30, window: '5 m', local: true }).limit(userId),
// Public cross-origin wallet embed card (GET /api/agents/wallet-embed). Served
// CORS:* so a stranger's blog can mount the wallet chip — keyed per IP and
// generous (a page with several embeds hydrates them all on load) but bounded
diff --git a/api/trading/scan.js b/api/trading/scan.js
index 5852d41e8..01714c4b8 100644
--- a/api/trading/scan.js
+++ b/api/trading/scan.js
@@ -1,24 +1,26 @@
/**
- * Trading Brain — assisted candidate scan (P4)
- * ============================================
+ * Trading Brain, assisted candidate scan (P4)
+ * ===========================================
*
* POST /api/trading/scan { agent_id, config }
- * → live launches that match the rule's entry conditions RIGHT NOW, each with
+ * -> live launches that match the rule's entry conditions RIGHT NOW, each with
* a real on-chain quote (expected tokens out, price impact) and a real
- * rug/honeypot firewall verdict. NEVER executes — this is the assisted
+ * rug/honeypot firewall verdict. NEVER executes. This is the assisted
* mode's "what would my agent buy?" preview. The owner confirms a candidate
* with the existing discretionary trade endpoint, which re-runs every guard.
*
* Read-only over real data: the same launch feed (recentPumpLaunches), the same
* entry gate (matchesEntry), the same quote function (quoteTrade), and the same
- * firewall (assessTradeSafety) the autonomous runner uses — so what the owner
+ * firewall (assessTradeSafety) the autonomous runner uses, so what the owner
* sees here is exactly what the engine would act on. No synthetic candidates, no
* fabricated fills, no fake P&L.
*
- * Owner-scoped + rate-limited. CSRF-exempt because it moves no funds and never
- * touches the custodial key (like the trade endpoint's preview path).
+ * Owner-scoped + rate-limited (per-IP and per-owner, because every match costs
+ * an on-chain quote and a firewall simulation). CSRF-exempt because it moves no
+ * funds and never touches the custodial key (like the trade endpoint's preview
+ * path).
*
- * Coin rule: coin-agnostic plumbing — it scans whatever real launches the live
+ * Coin rule: coin-agnostic plumbing. It scans whatever real launches the live
* pump.fun feed returns against the owner's runtime filters. It hardcodes,
* markets, and recommends no specific mint. $THREE remains the only coin three.ws
* promotes.
@@ -41,6 +43,10 @@ const LAMPORTS_PER_SOL = 1_000_000_000;
// so an owner's scan stays fast and never hammers the node.
const MAX_PRICED = 6;
const MAX_LAUNCHES = 60;
+// Entry-gate failure codes that only the creator stats can decide. A launch
+// rejected for any OTHER reason (age, market cap, liquidity, socials) is already
+// out, so paying for its creator lookup would be a wasted round-trip.
+const CREATOR_GATE_FAILURE = /^(creator_launches|creator_graduated_below):/;
async function resolveUser(req) {
const session = await getSessionUser(req);
@@ -59,6 +65,8 @@ export default wrap(async (req, res) => {
const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
+ const ownerRl = await limits.tradingScan(user.id);
+ if (!ownerRl.success) return rateLimited(res, ownerRl, 'too many scans; wait a moment before scanning again');
let body;
try {
@@ -85,8 +93,11 @@ export default wrap(async (req, res) => {
return json(res, 200, { data: { network, scanned: 0, matched: 0, candidates: [], note: 'Live launch scanning is available on mainnet only.' } });
}
- const payerAddr = agent.meta?.solana_address || null;
- const payer = payerAddr ? new PublicKey(payerAddr) : null;
+ // meta.solana_address is owner-writable through PATCH /api/agents/:id, so it is
+ // not guaranteed to parse. Hand the firewall the raw string: it resolves the
+ // key itself and downgrades an unparsable one to a skipped round-trip probe,
+ // where decoding it here would throw and turn a preview into a 500.
+ const payer = agent.meta?.solana_address || null;
const conn = solanaConnection(network);
const nowMs = Date.now();
const amountSol = config.sizing.amount_sol;