04 · Developers
Gas
What a hook adds to a swap, and how to keep that smaller than the trade.
The pool swap is the expensive part. A hook should add a few storage reads and, only when the rule requires it, one storage write. It should not add a write that the trade did not need.
What every swap already pays
An exact-input ERC-20 swap pays for the pool math, two token transfers, and the router's unlock. That cost is there with or without Talos. Native ETH is not supported, so you do not also pay a WETH wrap inside these contracts. Wrap first, if you must.
What the hub adds
Each module that opted into a callback is one external call. A hook with only beforeSwap is one call. A hook with beforeSwap and afterSwap is two. Eight modules that all use both is sixteen calls. Calls are hundreds of gas each once the contract is warm, plus whatever the module itself writes.
The hub's pull counter and the router's reentrancy guard use transient storage. They last for the transaction and do not cost a permanent storage write on every trade. Clearing them at the end of the call still matters: transient data survives until the transaction ends, so a later call in the same transaction must not see a stale pull lock.
What actually costs
- A new storage slot is the costly write, on the order of 20,000 gas the first time and far less when the slot is already warm or is reset to zero. Cohort, sell delay, conviction, buyer share, refill, pioneer seats, and dump weight write a slot the first time a wallet trades. Stabilizer writes a claim balance only when a counter-trade accrues a rebate. That is the rule. Do not add a second write that records the same fact.
- Fee hooks that only return a number are the cheap ones: decay, band, clip, and wave. They read configuration and revert or return. They do not pay a wallet slot.
- Calm, milestone, floor, drawdown, and circulation write market-wide state, not a slot per wallet. One warm write per swap, not one per trader.
- Match chest adds an ERC-20 transfer from the hook to the buyer when the chest has inventory. Budget for that transfer. An empty chest skips it.
- Buyback is a full swap. Use it when the quote amount is large next to gas. On a cheap chain the bar is low. Do not buy back dust on an expensive chain.
- Several fee overrides all execute. Only the highest number is charged. You pay every module's gas for one fee. Stack one fee hook.
A practical stack
Wave, cohort, and one fee hook is three modules. A first-time buyer pays the cohort's new slot once. Later buys from that wallet update a warm slot. A sell that the cohort ignores does not write the cap. If a trade is so small that even one warm write dominates its value, the trade is dust. Do not design the hook around dust, and do not invite dust with a stack of eight writers.
The forge suite checks behavior, not a gas snapshot in these pages. Before a launch, run forge test --gas-report on the stack you will actually register and compare that overhead with the smallest swap you are willing to accept. A report from a different stack is not your number.
The smallest contract that can join a stack, and the mistakes that unbalance the pool.