Reading the record
A fill's cost by cause
Every fill is marked against the market price. That one gap holds three things: what the agent's own order did to the price on the pool, the pool's fee, and how far the pool already sat from the market price before the trade. The first two come from the price the router read from the pool right before its swap, so no oracle is read for them.
The three parts
- Agent's order
- The fill price without the pool fee against the pool's price right before the swap: the price the order moved on the pool, and the pool rounding the amounts to whole token units. On small fills of tokens with few decimals, such as cbBTC with 8, the rounding can be most of it.
- Pool fee
- The fill price against the same price without the pool fee. The pool takes its fee tier from the input: the USDG on a buy, so a 0.30% pool shows 30.09 bps, or the tokens on a sale.
- Market gap
- What is left: about the pool's price against the market price before the trade. The market price is the router's reference price, read from Chainlink, which updates when the price moves past a threshold or after a set time, so the timing of those updates sits in this part. So does any earlier move of the pool, the agent's own earlier fills included.
All three are in bps, positive when the part made the fill worse for the client, and they add up to the gap on record for the fill.
Where each part comes from
Every Fillmark router reads the pool's slot0 right before its swap and logs it in Filled as fill.sqrtPriceX96, next to the fill's exact USDG and token amounts. The router also logs prices.spot (that pool price as USDG per whole token), prices.premium (the pool price against the reference price) and prices.impact (the fill price against the pool price, fee included). The split works from the fill's amounts, the logged pool price and the pool's fee tier, which is read from the pool, and leaves the rest as the market gap.
- Fill price without the fee
- On a buy, the USDG in times (1 − fee) over the tokens out; on a sale, the USDG out over the tokens in times (1 − fee).
feeis the pool's fee tier. - Agent's order
- The fill price without the fee against the pool price the router logged.
- Pool fee
- The fill price against the same price without the fee. Agent's order and pool fee together are about the router's prices.impact, which also holds their product.
- Market gap
- The gap on record minus the two parts above. It differs from the router's prices.premium by products of the parts, within 0.1 bps on the fills so far.
A swap in the same pool earlier in the fill's block, its own transaction included, moves the price the router reads, so its move counts in the market gap. The API counts those swaps for each fill, and how many of them are the same agent's fills for other clients. It also says whether the pool's price right before a fill equals the price the agent's last earlier fill in that pool left. When an agent splits one order into fills one after another, this shows that nothing moved the pool between its fills, so the price the next fill met is the one its own earlier fill left.
In the API
curl https://api.fillmark.trade/v1/agents/26144/costsplit- impact
- The median and the largest
impactBpsover the agent's fills, and the transaction of the largest. - fills[].impactBps, poolFeeBps, marketGapBps
- The three parts, in bps rounded to 2 decimals; they add up to
bps, the gap on record. - fills[].poolPriceBefore
- The pool's price right before the swap, as the router logged it, in USDG per whole token with 8 decimals.
- fills[].sameBlockSwaps, sameBlockAgentFills
- Swaps in the pool earlier in the fill's block, its own transaction included, and how many of those were the same agent's fills.
- fills[].atOwnLastFillPrice
- True when the pool's price right before the fill equals the price the agent's last earlier fill in that pool left, so nothing moved the pool between the two; null when there was no such fill.
- fills[].pool, poolFeePips
- The pool the fill went through and its fee tier in millionths.
The first report for an agent takes about a minute: until it is ready the API answers 202. A report over ten minutes old is made again when the record holds fills it does not, and the last one is served with updating true meanwhile. When the day's read budget is used up, the 202 says waiting and when the split is made. The agent pages on fillmark.trade show the three parts under each fill.
How it is checked
cd record && npm install
RPC=<url> npx tsx test/costsplit.ts history
RPC=<url> npx tsx test/costsplit.ts state- history
- Every Fillmark fill on every router: the swap in its transaction is in its pool and carries its amounts, the price before equals the router's prices.spot, the pool fee matches the fee tier, the agent's part is never negative, the three parts add up to the gap on record, and they agree with the router's own prices.impact and prices.premium.
- state
- Also reads the pool's slot0 at the end of the block before each fill with no earlier swap in its block, and checks it equals the price the router logged.
The code is in the record folder: src/costsplit.ts.