This describes a pre-release version of LBAMM. Interfaces and behavior may change.
Verify the exact repository commit before building production integrations.
Fee Ordering & Settlement
This page defines the deterministic ordering and settlement semantics of fee surfaces in LBAMM.
It separates:
- Execution-level adjustments
- Hop-level pool mechanics
- Hook-level fee accrual
- Final settlement
This separation is critical for reasoning about multi-hop swaps and composability.
Swap Execution Ordering
Swap execution consists of distinct phases.
Execution-Level Adjustments (Applied Once)
Execution-level adjustments apply once per singleSwap or multiSwap call, regardless of hop count.
These include:
- Fee-on-top (flat input adjustment)
- Exchange fee (BPS-based)
Swap-by-Input
Given an input amount X:
- Fee-on-top is removed from
X - Exchange fee is computed on the remaining input
- The resulting net input proceeds into hop execution
Swap-by-Output
Given a target output amount:
- Pool math determines required net input
- Exchange fee scales input upward
- Fee-on-top is added to the required input
These adjustments:
- Do not apply per hop
- Do not compound across route segments
- Apply only at the execution boundary
Hop-Level Pool Mechanics (Per Hop)
After execution-level adjustments:
- Swap proceeds hop-by-hop
- Each hop:
- Applies its LP fee
- Updates reserves
- Emits
Swapevent - Produces an output used as the next hop’s input
Execution-level exchange fee and fee-on-top do not reapply at this layer.
Token Hook Fees (Per Hop)
For each hop:
beforeSwap(if enabled)- Pool swap logic
afterSwap(if enabled)
Token hook fees:
- Are computed per hop
- May cause swap to revert
- Are accrued in core
- Are not transferred immediately during hook execution
There is no explicit max parameter for swap hook fees, but:
- Hooks cannot overconsume the trade
- Excessive fees may trigger swap limit validation reverts
Liquidity Operation Ordering
Liquidity operations (addLiquidity, removeLiquidity, collectFees) follow a deterministic sequence.
Hook Validation Order
- Token hooks (token0, token1 sides if enabled)
- Position hook (if provided)
- Pool hook (if configured)
Each hook may:
- Revert
- Return hook fees
Hook Fee Aggregation
For liquidity operations:
- All hook fees for
token0are summed - All hook fees for
token1are summed - Final totals are compared against:
maxHookFee0maxHookFee1
Hooks are unaware of each other’s fee returns.
Enforcement occurs on the aggregated totals.
Netting Semantics
Hook fees may:
- Increase required deposit
- Reduce withdrawal amount
In edge cases:
- A withdrawal may be reduced enough that the provider must supply additional funds
Flashloan Ordering
Flashloan execution:
- Loan token hook
beforeFlashloan- Returns
(feeToken, feeAmount)
- Returns
- Optional validation if
feeTokendiffers from loan token - Loan is issued
- Loan is repaid
- Fee is accounted
Flashloan fee:
- Is accrued using the token-hook fee accounting model
- Emitted via
Flashloanevent - Is not subject to explicit protocol cap
Settlement Model
LBAMM separates:
- Calculation
- Accrual
- Transfer
Execution Atomicity
All operations are atomic:
- If any hook reverts, the entire operation reverts
- No fee surface partially commits
Exchange Fee and Fee-on-Top Settlement
For swaps:
- Exchange fee and fee-on-top are transferred by core
- If a transfer handler is used:
- Handler must supply total input (including fees)
- If no handler:
- Input tokens are collected from executor directly
Exchange fee and fee-on-top are not emitted in swap events today.
Hook Fee Accrual
Hook fees:
- Are accrued in core balances
- May be collected by the hook later
- May be queued for collection during execution depending on hook configuration
Queued transfers:
- Execute after primary operation finalizes
- Process FIFO
- Cannot interfere with swap/liquidity accounting
Protocol Fee Settlement
Protocol fees:
- Are taken according to configured rates
- May apply to:
- Hop-level token fees
- LP fees
- Exchange fee
- Fee-on-top
- Are emitted via:
event ProtocolFeeTaken(address indexed token, uint256 amount);
Protocol fee accounting is internal but fee rates are queryable.
Multi-Hop Invariants
For multiSwap:
- Exchange fee applies once
- Fee-on-top applies once
- Transfer handler (if any) cannot change mid-route
- LP fees apply per hop
- Token hook swap fees apply per hop
- Protocol hop minimum fees (if configured) apply per hop
These invariants prevent compounding of execution-level fees.
Key Invariants Summary
- Execution-level fees are applied once.
- LP fees are hop-scoped.
- Hook fees are accrued and settled post-operation.
- Liquidity hook fees are bounded by aggregated max limits.
- Flashloan fees are governed by token hook logic.
- All operations are atomic.
