Trading copilot
An agent that executes trades suggested by an LLM on a single whitelisted DEX, capped by weekly spend.
Policy
const policies = [
{ type: "spend", token: "USDC", amount: 200, period: "weekly" },
{ type: "contract", whitelist: ["uniswap.eth"] },
{ type: "rate", count: 5, period: "daily" },
{ type: "expires", at: "2026-12-31" },
] as const;200 USDC weekly cap. Uniswap-only. At most five trades a day. Hard expiry at end of year.
Implementation
import { createAgent } from "@agensai/sdk";
const agent = await createAgent({
name: "my-agent",
chainId: 8453,
apiKey: process.env.AGENSAI_API_KEY!,
ownerPrivateKey: process.env.AGENSAI_OWNER_PRIVATE_KEY! as `0x${string}`,
policies: [
{ type: "spend", token: "USDC", amount: 200, period: "weekly" },
{ type: "contract", whitelist: ["uniswap.eth"] },
{ type: "rate", count: 5, period: "daily" },
{ type: "expires", at: "2026-12-31" },
],
});
// LLM produces a trade idea; we encode it.
const swapCalldata = encodeSwap({ tokenIn: "USDC", tokenOut: "WETH", amountIn: 50 });
const tx = await agent.execute({
to: "uniswap.eth",
value: 0n,
data: swapCalldata,
});
console.log(`swap submitted: ${tx}`);Why this is safe
If the LLM hallucinates a trade twice the size you intended, the spend cap catches it. If the LLM tries to send to a different protocol, the contract whitelist catches it. If the LLM gets stuck in a tight loop, the rate cap catches it. If you forget to revoke before going on vacation for a year, the expiry catches it.
The smart account is your last line of defense. Your code can be wrong; the wallet still won't.