Skip to content

Limit orders display base-unit amounts for sub-dollar tokens #223

@premiumjibles

Description

@premiumjibles

Problem

When placing limit orders for sub-dollar tokens (e.g. ARB → USDC), SellAmount shows a massive base-unit number like "230,000,000,000..." instead of human-readable amounts. Users could sign orders with wildly wrong amounts.

tk ticket: sa-lhbw
Repro: Place a limit order for ARB → USDC (or any sub-dollar token).

Root Cause

Server-side code is structurally correct (summary.sellAsset.amount uses human-readable values). The bug is most likely an LLM reasoning error for sub-dollar tokens where:

  1. LLM passes base-unit value as sellAmount after seeing precision: 18
  2. orderParams.sellAmount (base unit) leaks into LLM's text response
  3. LLM inverts limitPrice for sub-dollar tokens, producing enormous numbers

Fix

  1. Input validation — reject sellAmount with >15 digits (defense in depth); applies to all 3 tools
  2. Sanity check — log WARN at >10x deviation from market price; hard error at >1000x; applies to createLimitOrder only (stopLoss uses USD trigger price, TWAP is market-price execution)
  3. Schema description — explicit base-unit warning with sub-dollar token examples on limitPrice field; never-invert note
  4. Reduce leakage via experimental_toToolResultContent — filter LLM-visible tool output without affecting the UI stream (which receives the raw execute() result separately):
    • createLimitOrder: strip orderParams (contains sellAmount/buyAmount in base units)
    • createStopLoss: strip sellAmountBaseUnit, sellPrecision, buyPrecision
    • createTwap: strip sellAmountBaseUnit, sellPrecision, buyPrecision
  5. Apply fixes 1 & 3 to createStopLoss.ts and createTwap.ts

Follow-up (separate ticket): getAssets exposes precision: 18 in its response — this is the upstream trigger that teaches the LLM about base units. Should be filtered from getAssets output.

Key Files

  • apps/agentic-server/src/tools/limitOrder/createLimitOrder.ts
  • apps/agentic-server/src/tools/stopLoss/createStopLoss.ts
  • apps/agentic-server/src/tools/twap/createTwap.ts
  • apps/agentic-server/src/routes/chat.ts

Acceptance Criteria

  • Input validation rejects >15 digit sellAmount values (all 3 tools)
  • Sanity check warns at >10x and errors at >1000x limitPrice deviation from market rate
  • Schema descriptions warn about base-unit vs human-readable with sub-dollar examples
  • experimental_toToolResultContent strips base-unit fields from LLM-visible output on all 3 tools; UI stream unaffected
  • Manual test: ARB → USDC limit order shows correct amounts
  • Manual test: meme → USDC limit order shows correct amounts
  • Manual test: any low value coin → USDC limit order shows correct amounts

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

Status

Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions