Skip to content
LogoLogo

DynamicFeeOrdering

Git Source

Title: Dynamic fee ordering on pool-liquidity fills

Linked library called by AsyncMatching in the hook's context.

Constants

ORDERING_WINDOW

Blocks a pool fill's starting tick stays the reference that later fills are measured against.

uint256 internal constant ORDERING_WINDOW = 2;

Functions

charge

Dynamic fee on a pool fill that backruns a price move, taken from its output. The first pool fill in a window of ORDERING_WINDOW blocks records its starting tick as the reference. A later fill in the window that moves the price back toward the reference pays half the price gap it closes: amountOut * avgGap / 2, with the gap averaged over the fill's start and end ticks (one tick is about 0.01%), so splitting a backrun does not lower its total. A fill that crosses the reference pays only on the share of its output from the side before the reference, in proportion to the ticks on each side. Half of the fee is donated to in-range LPs and half credited to the FeeVault as claims.

function charge(
        AsyncFiller.State storage self,
        PoolKey calldata key,
        bytes32 id,
        Currency output,
        int24 tickBefore,
        uint256 amountOut
    ) public returns (uint256 fee);

Parameters

NameTypeDescription
selfAsyncFiller.State
keyPoolKey
idbytes32
outputCurrency
tickBeforeint24
amountOutuint256

Returns

NameTypeDescription
feeuint256

_pay

Half of fee to in-range LPs (all of it to the FeeVault if none are in range), the rest to the FeeVault as claims.

function _pay(AsyncFiller.State storage self, PoolKey calldata key, bytes32 id, Currency output, uint256 fee)
        private;

Parameters

NameTypeDescription
selfAsyncFiller.State
keyPoolKey
idbytes32
outputCurrency
feeuint256

Events

DynamicFeeCharged

A pool fill paid the dynamic fee, in currency (the fill's output): toLps donated to in-range LPs and toFeeVault credited to the FeeVault, measured against refTick.

event DynamicFeeCharged(
        PoolId indexed poolId,
        bytes32 indexed orderId,
        Currency indexed currency,
        uint256 toLps,
        uint256 toFeeVault,
        int24 refTick
    );

Parameters

NameTypeDescription
poolIdPoolId
orderIdbytes32
currencyCurrency
toLpsuint256
toFeeVaultuint256
refTickint24

PoolPriceObserved

The pool tick before and after a completed pool fill, with the reference used for fee ordering.

Indexers can use these events as the price-movement log; fee calculation uses compact on-chain state.

event PoolPriceObserved(
        PoolId indexed poolId,
        bytes32 indexed orderId,
        int24 tickBefore,
        int24 tickAfter,
        int24 refTick,
        uint64 observedBlock
    );

Parameters

NameTypeDescription
poolIdPoolId
orderIdbytes32
tickBeforeint24
tickAfterint24
refTickint24
observedBlockuint64