Skip to content
Docs

Agent builders

Signing and sending a fill

A fill is a FillIntent signed by the agent wallet and sent to the router's execute function by anyone.

The intent

The agent wallet signs a FillIntent with EIP-712. Domain: name Fillmark, version 1, chain id 4663, verifying contract the router. A buy has USDG as tokenIn; a sell has USDG as tokenOut.

The nonce is the agent's choice and can fill once per account, so an agent that derives it from the account, asset and day can never fill the same daily buy twice. refSqrtPriceX96 is the pool price the intent was built on; the fill reverts if the pool has moved more than maxDevBps from it.

TypeScript (viem): sign and send a fill
import { createPublicClient, createWalletClient, defineChain, http, parseAbi } from "viem";
import { privateKeyToAccount } from "viem/accounts";

const robinhood = defineChain({
  id: 4663,
  name: "Robinhood Chain",
  nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
  rpcUrls: { default: { http: ["https://rpc.mainnet.chain.robinhood.com"] } },
});
const ROUTER = "0xe542a7c8779962b8f819c4352d1436e08bc2d388";
const POOL = "0x...";         // the Uniswap v3 pool for USDG and the asset at this fee tier
const pub = createPublicClient({ chain: robinhood, transport: http() });
const [sqrtPriceX96] = await pub.readContract({
  address: POOL,
  abi: parseAbi(["function slot0() view returns (uint160, int24, uint16, uint16, uint16, uint8, bool)"]),
  functionName: "slot0",
});
const agent = privateKeyToAccount(process.env.AGENT_KEY as `0x${string}`);

const domain = { name: "Fillmark", version: "1", chainId: 4663, verifyingContract: ROUTER } as const;
const types = {
  FillIntent: [
    { name: "agentId", type: "uint256" },
    { name: "account", type: "address" },
    { name: "installId", type: "uint64" },
    { name: "tokenIn", type: "address" },
    { name: "tokenOut", type: "address" },
    { name: "fee", type: "uint24" },
    { name: "amountIn", type: "uint256" },
    { name: "minOut", type: "uint256" },
    { name: "refSqrtPriceX96", type: "uint160" },
    { name: "maxDevBps", type: "uint16" },
    { name: "sqrtPriceLimitX96", type: "uint160" },
    { name: "nonce", type: "uint256" },
    { name: "deadline", type: "uint64" },
  ],
} as const;

const intent = {
  agentId: 26144n,
  account: "0x...",           // the client account
  installId: 1n,              // configs(account).installId
  tokenIn: "0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168", // USDG in = a buy
  tokenOut: "0x...",          // the asset token
  fee: 500,                   // Uniswap v3 fee tier of the pool
  amountIn: 10_000000n,       // 10 USDG
  minOut: 0n,                 // set from a quote (Uniswap QuoterV2) less a small buffer
  refSqrtPriceX96: sqrtPriceX96, // the pool price the intent was built on
  maxDevBps: 50,              // how far the pool may move before the fill
  sqrtPriceLimitX96: 0n,      // 0 = no limit
  nonce: 1n,                  // any number not used before for this account
  deadline: BigInt(Math.floor(Date.now() / 1000) + 300),
} as const;

const signature = await agent.signTypedData({ domain, types, primaryType: "FillIntent", message: intent });

const wallet = createWalletClient({ account: agent, chain: robinhood, transport: http() });
const executeAbi = parseAbi([
  "function execute((uint256 agentId, address account, uint64 installId, address tokenIn, address tokenOut, uint24 fee, uint256 amountIn, uint256 minOut, uint160 refSqrtPriceX96, uint16 maxDevBps, uint160 sqrtPriceLimitX96, uint256 nonce, uint64 deadline) it, bytes signature) returns (uint256 amountOut)",
]);
// Simulate first: a rejected fill reverts with one of the router's errors.
const hash = await wallet.writeContract({ address: ROUTER, abi: executeAbi, functionName: "execute", args: [intent, signature] });

Sending it

execute(intent, signature) can be sent by anyone; the sender pays the gas. The router swaps in the Uniswap v3 pool for the pair and fee tier, takes exactly amountIn from the account and sends the output to the account. A fill is all or nothing: a swap that stops at the price limit reverts. A fill of about 10 USDG measured about 0.0000125 ETH in gas.

The reference agent quotes every fee tier with Uniswap's QuoterV2, simulates the best one and sends it with a fixed gas cap. Its source is a working example of everything on this page: agent on GitHub.

Loading the search index…