Building a tokenized-stock agent on Base where the guard, not the agent, decides whether money moves.
The problem, stated plainly
A trading agent on Base is a loop: read price, decide, send tx. Every failure mode hides in "read price."
A ticker paused for a split still returns a price. A Chainlink feed outside 24/5 hours still returns a price — yesterday's. An agent that trusts those numbers will happily buy at the wrong one, and nobody finds out until settlement.
Reserve inverts the default. Money never moves because the agent decided. It moves because a separate contract agreed.
Three contracts, one seam
-
ReserveVault— holds USDC and the tokenized stock, keeps the book, exposesexecute(action, asset, amount). -
Guard— pure policy. Takes an intent, returnsbytes32— zero for OK, or a reason hash likeFEED_STALE. -
IMarketData— a three-value view:(multiplierWad, pending, effectiveAt).
That third one is the whole portability trick. Testnet and mainnet read completely different infrastructure, but the shape the Guard sees is identical, so Guard.sol and ReserveVault.sol compile to the same bytecode on both chains. Only the adapter swaps: mocks on Sepolia, Chainlink + Coinbase's onchain registry on mainnet.
The multiplier matters more than it looks. B20 tokenized stocks don't rebase — a dividend or split arrives as a multiplier that changes your displayed balance while balanceOf stays put. There's a window where the registry has announced the multiplier but hasn't applied it. Pricing a trade inside that window is pricing a number that's about to change.
Refusal as a state machine
Four checks, all before any transfer:
-
pausedFeatures()non-empty →PAUSED_CORPORATE_ACTION - multiplier announced, not yet effective →
MULTIPLIER_PENDING - feed timestamp older than its heartbeat →
FEED_STALE - post-trade liquid balance under the operating floor →
RESERVE_FLOOR_BREACH
One rule outranks all of them: repay is always allowed. A guard that blocks de-risking is worse than no guard.
And refusals are not exceptions — they're transactions. The agent submits them onchain and the contract emits an event with the reason hash. Which produces the demo's central number: 17 intents, 14 refusals, 3 settlements.
A refusal you can only find in a log file isn't auditable; one that's a tx with a block number is. That's the design decision I'd defend hardest. It would have been cheaper to return early offchain and log to a JSON file. It would also have meant the product's main claim — that the no is verifiable — was a screenshot.
The agent
A viem loop, ~120 lines. Each cycle: read the book, propose an intent, ask the Guard, submit either way, append the receipt to runs.jsonl. Nothing clever. The interesting part is that the loop never decides whether to trade safely — it decides what it wants, and the Guard decides whether it's allowed.
I hit a real bug here that's worth naming: the intent amount is 8-decimal USD, the vault transfer is 6-decimal USDC. For a while the loop was passing 2e8 into a token call expecting 2e6 — a 100× error that looked like a book imbalance. Fixed by keeping two constants and never letting them meet.
Paying for its own data
The risk verdict is also sold over x402. GET /verdict/:asset returns 402 with a PAYMENT-REQUIRED header, the agent signs an exact-EVM USDC transfer, retries, gets 200 with a PAYMENT-RESPONSE receipt. Settled for $0.001 — real tx, block 60921570.
The satisfying detail: the verdict it buys back is safe: false, FEED_STALE. The oracle sells refusals, not prices. That's the product in one response.
// seller, x402 v2
const server = new x402ResourceServer(new HTTPFacilitatorClient(...))
.register("eip155:84532", new ExactEvmScheme());
app.use(paymentMiddleware({
"GET /verdict/:asset": { price: "$0.001", network: "eip155:84532" }
}, server));
The dashboard is a build artifact
Base's public Sepolia RPC caps eth_getLogs at 200 blocks, and the ledger spans thousands. So scripts/snapshot.mjs doesn't query logs at all — it walks the tx hashes from runs.jsonl plus a hand-maintained list, fetches each receipt, and classifies by topic. Output is a static state.json that the page renders with no client-side RPC.
Ugly, and correct. It also means the dashboard can't lie about live state: if the snapshot script fails, the page shows old numbers rather than inventing new ones.
What's faked, on purpose
Sepolia has no B20 ticker — Coinbase's /chains endpoint lists exactly one chain, 8453, and the mainnet registry contracts have zero code on 84532. So the stock and the price feed are mocks. That's disclosed in the README, on the dashboard, and in the video. The Guard and the vault aren't mocked, which is the part being judged.
18 Foundry tests, 5 of them running against live Base mainnet state.
What I'd do next
The borrow leg. Supplying stock as collateral and refusing to borrow when a corporate action starts mid-position is where the refusal model stops being a demo and starts being infrastructure.
Live: https://reserve-book.vercel.app
Code: https://github.com/harishkotra/reserve
Top comments (0)