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(agent-orders): scale a raw size_tokens sell by the mint decimals before firing

nirholase5fe5ef4cdf3
diff --git a/api/_lib/orders.js b/api/_lib/orders.js
index 4b292c354..945890810 100644
--- a/api/_lib/orders.js
+++ b/api/_lib/orders.js
@@ -323,7 +323,9 @@ function fmt(n) {
function sizeLabel(o) {
if (o.side === 'buy') return `${o.size_sol} SOL`;
if (o.sell_pct != null) return `${o.sell_pct}% of the holding`;
- return `${o.size_tokens} tokens`;
+ // size_tokens is raw base units, not whole tokens; label it as what it is so
+ // the readback can't be misread as a 1e6x larger sell on a 6-decimal mint.
+ return `${o.size_tokens} base units`;
}
/** A plain-language readback of an order — what it does, in one line. */
@@ -347,7 +349,7 @@ export function describeOrder(o) {
}
case 'twap': {
const every = humanInterval(o.schedule?.interval_seconds);
- const total = o.side === 'buy' ? `${o.schedule?.total_sol} SOL` : (o.schedule?.total_pct != null ? `${o.schedule.total_pct}%` : `${o.schedule?.total_tokens} tokens`);
+ const total = o.side === 'buy' ? `${o.schedule?.total_sol} SOL` : (o.schedule?.total_pct != null ? `${o.schedule.total_pct}%` : `${o.schedule?.total_tokens} base units`);
return `TWAP: ${verb.toLowerCase()} ${total} of ${sym} sliced over ${o.schedule?.slices} fills, one every ${every}.`;
}
case 'conditional':
diff --git a/tests/agent-orders-sweep.test.js b/tests/agent-orders-sweep.test.js
index e67424a57..ab013d386 100644
--- a/tests/agent-orders-sweep.test.js
+++ b/tests/agent-orders-sweep.test.js
@@ -94,7 +94,7 @@ const { loadConfig } = await import('../workers/agent-orders/config.js');
const AGENT_ID = '11111111-1111-4111-8111-111111111111';
const USER_ID = '22222222-2222-4222-8222-222222222222';
const MINT = 'FeMbDoX7R1Psc4GEcvJdsbNbZA3bfztcyDCatJVJpump'; // $THREE
-const AGENT_ADDRESS = '9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM';
+const AGENT_ADDRESS = 'THREEsyntheticAgentWallet11111111111111111111'; // never validated: getHolding is stubbed
const cfg = { network: 'mainnet', mode: 'simulate', concurrency: 4, staleFiringMs: 180_000 };
@@ -267,6 +267,32 @@ describe('runOrderSweep: blocked fires', () => {
expect(release).toBeTruthy();
});
+ it('scales a raw size_tokens sell by the mint decimals before handing it to the trade path', async () => {
+ // size_tokens is persisted in raw base units; executeAgentTrade takes whole
+ // tokens. Unscaled, this would ask for 1e6x the size and bounce off
+ // insufficient_token_balance (a clearable code) on every sweep forever.
+ dbState.activeOrders = [order({ type: 'limit', side: 'sell', size_sol: null, size_tokens: 2_500_000, limit_price: 40_000 })];
+ priceAt(45_000);
+ marketState.holding = { whole: 1_000, raw: 1_000_000_000n, decimals: 6 };
+
+ await runOrderSweep(cfg);
+
+ expect(tradeState.calls).toHaveLength(1);
+ expect(tradeState.calls[0].input.amount).toBe(2.5);
+ });
+
+ it('holds a size_tokens sell when the mint decimals cannot be read', async () => {
+ dbState.activeOrders = [order({ type: 'limit', side: 'sell', size_sol: null, size_tokens: 2_500_000, limit_price: 40_000 })];
+ priceAt(45_000);
+ marketState.holding = null; // RPC could not resolve the mint this sweep
+
+ await runOrderSweep(cfg);
+
+ expect(tradeState.calls).toHaveLength(0);
+ const release = dbState.calls.find((c) => c.kind === 'set_status' && c.values[1] === 'no_balance_or_size');
+ expect(release).toBeTruthy();
+ });
+
it('sizes a percentage sell off the live holding', async () => {
dbState.activeOrders = [order({ type: 'limit', side: 'sell', size_sol: null, sell_pct: 25, limit_price: 40_000 })];
priceAt(45_000);
diff --git a/workers/agent-orders/market.js b/workers/agent-orders/market.js
index dfbf57fea..fc427615f 100644
--- a/workers/agent-orders/market.js
+++ b/workers/agent-orders/market.js
@@ -132,8 +132,10 @@ export function metricValue(market, signals, metric) {
}
/**
- * Live token holding for an owner. Used to size percentage sells (sell N% of the
- * bag). Returns whole-token + raw base-unit balance + decimals, or null if the
+ * Live token holding for an owner. Sizes percentage sells (sell N% of the bag)
+ * and supplies the mint decimals that convert a raw `size_tokens` order into the
+ * whole-token amount the trade path expects. Returns whole-token + raw
+ * base-unit balance + decimals, or null if the
* owner holds none / the mint can't be read. Mirrors the resolveHolding logic the
* trade endpoint uses so a percentage sell is priced against the same balance.
*/
diff --git a/workers/agent-orders/sweep.js b/workers/agent-orders/sweep.js
index 9e41603fa..84422103d 100644
--- a/workers/agent-orders/sweep.js
+++ b/workers/agent-orders/sweep.js
@@ -115,7 +115,17 @@ async function buildBody(order, agent) {
const amt = holding.whole * (order.sell_pct / 100);
return amt > 0 ? { ...base, amount: amt } : null;
}
- if (Number(order.size_tokens) > 0) return { ...base, amount: Number(order.size_tokens) };
+ if (Number(order.size_tokens) > 0) {
+ // size_tokens is stored in RAW base units (see the orders migration and
+ // docs/agent-wallet-api.md) while the trade path takes WHOLE tokens, so it
+ // has to be scaled by the mint's own decimals. Passing the raw figure
+ // through would ask for 10^decimals times the intended size and bounce off
+ // insufficient_token_balance, a clearable code, on every sweep forever.
+ const holding = await getHolding({ network: order.network, mint: order.mint, owner: agent.meta.solana_address });
+ if (!holding) return null; // no decimals to scale by: hold, never guess
+ const whole = Number(order.size_tokens) / 10 ** holding.decimals;
+ return whole > 0 ? { ...base, amount: whole } : null;
+ }
return null;
}