Fee

This document is better viewed on the docs page.

Hooks for managing and customizing pool fees, including dynamic fee adjustments, fee overrides, and post-swap fee calculations.

  • BaseDynamicFee: Hook to apply a manual dynamic fee via the Uniswap’s PoolManager contract.

  • BaseOverrideFee: Hook that overrides and applies a fee before swapping automatically.

  • BaseDynamicAfterFee: Hook that overrides and applies a fee based on a delta after swapping.

Hooks

BaseDynamicFee

import "uniswap-hooks/src/fee/BaseDynamicFee.sol";

Base implementation to apply a dynamic fee via the PoolManager’s `updateDynamicLPFee function.

This is experimental software and is provided on an "as is" and "as available" basis. We do not give any warranties and will not be liable for any losses incurred through any use of this code base.

Available since v0.1.0

constructor(contract IPoolManager _poolManager) internal

Set the PoolManager address.

_getFee(struct PoolKey key) → uint24 internal

Returns a fee, denominated in hundredths of a bip, to be applied to the pool after it is initialized.

_afterInitialize(address, struct PoolKey key, uint160, int24) → bytes4 internal

Set the fee after the pool is initialized.

poke(struct PoolKey key) external

Updates the dynamic LP fee for the given pool, which must have a key that contains this hook’s address.

This function can be called by anyone at any time. If _getFee implementation depends on external conditions (e.g., oracle prices, other pool states, token balances), it may be vulnerable to manipulation. An attacker could potentially: 1. Manipulate the external conditions that _getFee depends on 2. Call poke() to update the fee to a more favorable rate 3. Execute trades at the manipulated fee rate

Inheriting contracts should consider implementing access controls on this function, make the logic in _getFee resistant to short-term manipulation, or accept the risk of fee manipulation.

getHookPermissions() → struct Hooks.Permissions permissions public

Set the hook permissions, specifically afterInitialize.

NotDynamicFee() error

The hook was attempted to be initialized with a non-dynamic fee.

BaseOverrideFee

import "uniswap-hooks/src/fee/BaseOverrideFee.sol";

Base implementation for automatic dynamic fees applied before swaps.

This is experimental software and is provided on an "as is" and "as available" basis. We do not give any warranties and will not be liable for any losses incurred through any use of this code base.

Available since v0.1.0

constructor(contract IPoolManager _poolManager) internal

Set the PoolManager address.

_afterInitialize(address, struct PoolKey key, uint160, int24) → bytes4 internal

Check that the pool key has a dynamic fee.

_getFee(address sender, struct PoolKey key, struct IPoolManager.SwapParams params, bytes hookData) → uint24 internal

Returns a fee, denominated in hundredths of a bip, to be applied to a swap.

_beforeSwap(address sender, struct PoolKey key, struct IPoolManager.SwapParams params, bytes hookData) → bytes4, BeforeSwapDelta, uint24 internal

Set the fee before the swap is processed using the override fee flag.

getHookPermissions() → struct Hooks.Permissions permissions public

Set the hook permissions, specifically afterInitialize and beforeSwap.

NotDynamicFee() error

The hook was attempted to be initialized with a non-dynamic fee.

BaseDynamicAfterFee

import "uniswap-hooks/src/fee/BaseDynamicAfterFee.sol";

Base implementation for dynamic fees applied after swaps.

In order to use this hook, the inheriting contract must define the _getTargetOutput and _afterSwapHandler functions. The _getTargetOutput function returns the target output to apply to the swap depending on the given apply flag. The _afterSwapHandler function is called after the target output is applied to the swap and currency amount is received.

This is experimental software and is provided on an "as is" and "as available" basis. We do not give any warranties and will not be liable for any losses incurred through any use of this code base.

Available since v0.1.0

constructor(contract IPoolManager _poolManager) internal

Set the PoolManager address.

_beforeSwap(address sender, struct PoolKey key, struct IPoolManager.SwapParams params, bytes hookData) → bytes4, BeforeSwapDelta, uint24 internal

Sets the target output and apply flag to be used in the afterSwap hook.

The target output is reset to 0 in the afterSwap hook regardless of the apply flag.

_afterSwap(address sender, struct PoolKey key, struct IPoolManager.SwapParams params, BalanceDelta delta, bytes) → bytes4, int128 internal

Apply the target output to the unspecified currency of the swap using fees. The fees are minted as ERC-6909 tokens, which can then be redeemed in the _afterSwapHandler function. Note that if the underlying unspecified currency is native, the implementing contract must ensure that it can receive native tokens when redeeming.

The target output is reset to 0, both when the apply flag is set to false and when set to true.

_getTargetOutput(address sender, struct PoolKey key, struct IPoolManager.SwapParams params, bytes hookData) → uint256 targetOutput, bool applyTargetOutput internal

Return the target output to be enforced by the afterSwap hook using fees.

The swap will revert if the target output exceeds the output unspecified amount from the swap. In order to consume all of the output from the swap, set the target output to equal the output unspecified amount and set the apply flag to true.

_afterSwapHandler(struct PoolKey key, struct IPoolManager.SwapParams params, BalanceDelta delta, uint256 targetOutput, uint256 feeAmount) internal

Handler called after applying the target output to a swap and receiving the currency amount.

getHookPermissions() → struct Hooks.Permissions permissions public

Set the hook permissions, specifically {beforeSwap}, {afterSwap} and {afterSwapReturnDelta}.

TargetOutputExceeds() error

Target output exceeds swap amount.

uint256 _targetOutput internal

bool _applyTargetOutput internal