Agent builders
Getting paid
An agent is paid the performance fee each client set, in USDG to the agent wallet recorded at install, on result the router has counted.
The rate
Each permission carries perfFeeBps, from 0 to 3000 (30%), in configs(account) and in the Installed event. The rate is the client's choice. An agent that asks for a higher one should skip accounts below it; the reference agent asks for 0 and skips none.
Collecting it
When a sale makes a gain, the router keeps the fee that gain could owe (the rate times the gain, rounded up) out of the sale's USDG, so a fee that comes due is already set aside. settle(account) works out the fee for the account's current agent and pays it to the agent wallet recorded at install: first from what was set aside for gains that now count, then from the account's USDG. Set-aside USDG that no fee can need goes back to the account in the same call. Anyone can send it; the agent usually does, after a gain has counted or on a schedule. Each fee charged is also written by the client's account to the Reputation Registry with tag fillmark-agentfee.
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 = "0x6F01e2bE7d1D31E88dF177bEC23392D1905FC8Aa";
const ACCOUNT = "0x..."; // a client account this agent trades
const routerAbi = parseAbi([
"function settle(address account) returns (uint256 fee)",
"function bookOf(address account, uint256 agentId) view returns ((int128 realized, int128 peak, uint128 debt, uint128 paid, uint128 escMatured, uint128 outLoss, address wallet, uint16 perfFeeBps, bool closing, uint64 closingAt))",
]);
const sender = privateKeyToAccount(process.env.AGENT_KEY as `0x${string}`); // any key with ETH for gas
const pub = createPublicClient({ chain: robinhood, transport: http() });
const wallet = createWalletClient({ account: sender, chain: robinhood, transport: http() });
const AGENT_ID = 26144n;
// Simulate first: it reverts while a position's price is not live. It also collects open debt
// and returns to the account the held fee no charge needs.
const { request, result: fee } = await pub.simulateContract({ address: ROUTER, abi: routerAbi, functionName: "settle", args: [ACCOUNT], account: sender });
// Sent whenever the simulation passes: matured gains move into the book only inside settle, so a
// read of the book before it cannot tell whether held USDG would go back to the account.
// A fifth above the estimate: the estimate is exact for one block only.
const gas = ((await pub.estimateContractGas({ address: ROUTER, abi: routerAbi, functionName: "settle", args: [ACCOUNT], account: sender })) * 6n) / 5n;
await pub.waitForTransactionReceipt({ hash: await wallet.writeContract({ ...request, gas }) });
const book = await pub.readContract({ address: ROUTER, abi: routerAbi, functionName: "bookOf", args: [ACCOUNT, AGENT_ID] });
console.log({ fee, paid: book.paid, debt: book.debt });Settling values every position at the reference price, so it fails while a position's price is not live, for example a stock token over the weekend. release(account) prices nothing: it pays open debt from set-aside USDG and returns what no fee can need, and charges no fee. Send either with gas above the estimate: the router gives the client account a fixed allowance for each payment and stops with LowGas when less is left. A payment the router cannot deliver, for example to a wallet USDG has frozen, is kept for that address; claim(address) sends it once it can be delivered.
What counts
- Base
- Realized result: every loss at once, and gains whose wait is over. No fee is charged while the positions below their cost at the reference price are, added up, more than 0.01 USDG below it; gains on other positions do not offset that.
- Peak
- The fee is the rate times the base above the highest base already charged, which then becomes the new peak.
- Gain wait
- A sale's gain counts 7 days after it. Gains from sales made while an earlier gain of the same asset still waits wait together, and their 7 days restart on each such sale. Buying back what was sold cancels waiting gains first, and the USDG set aside for them goes back to the account.
- Set aside
- What is set aside for a gain stays with the router until the gain counts and settle or release runs. While positions are below cost no fee is charged, so set-aside USDG can stay held for as long as they are.
- Only router trades
- Positions are what the agent bought through the router. Assets the client already held carry no result.
- Withdrawals
- Units the client moves out count as sold at the reference price. Their gain waits too, under one deadline per asset for all moved-out units, which each new gain moves toward its own 7 days in proportion to its size. Units that come back count as bought again at the reference price.
- Removal
- When the client removes the permission, every position counts at the reference price and all waiting gains count. Any fee due is paid from what was set aside, the rest of that goes back to the account, and what is still owed becomes open debt. Without live prices the book waits; anyone can count it with closeOut once they are live, and after 30 days a position counts at its cost if its feed has been silent all that time, or if it cannot be priced for a reason other than a closed market or an out-of-date price. Until it is counted, the account cannot give any agent permission on this router.
Open debt
What the account cannot pay stays as debt in the book. While there is debt the router refuses the agent's buys with InDebt, and each sale pays it from its USDG after the fill fee and the share set aside for the sale's own gain. After a removal the router cannot move the client's funds any more: the client pays with payDebt or not at all, and cannot give the same agent permission again until it is paid. Read it with bookOf(account, agentId).