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’sPoolManager
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
_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.
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
_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 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 SwapParams params, bytes hookData) → bytes4, BeforeSwapDelta, uint24
internal
Set the fee before the swap is processed using the override fee flag.
BaseDynamicAfterFee
import "uniswap-hooks/src/fee/BaseDynamicAfterFee.sol";
Base implementation for dynamic target hook fees applied after swaps.
Enables to enforce a dynamic target determined by _getTargetUnspecified
for the unspecified currency of the swap
during _beforeSwap
, where if the swap outcome results better than the target, any positive difference is taken
as a hook fee, being posteriorily handled or distributed by the hook via _afterSwapHandler
.
In order to use this hook, the inheriting contract must implement _getTargetUnspecified to determine the target,
and _afterSwapHandler to handle accumulated fees.
|
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
_transientTargetUnspecifiedAmount() → uint256
internal
The target unspecified amount to be enforced by the afterSwap
hook.
_transientApplyTarget() → bool
internal
Whether the target unspecified amount should be enforced by the afterSwap
hook.
_setTransientTargetUnspecifiedAmount(uint256 value)
internal
Set the target unspecified amount to be enforced by the afterSwap
hook.
_beforeSwap(address sender, struct PoolKey key, struct SwapParams params, bytes hookData) → bytes4, BeforeSwapDelta, uint24
internal
Sets the target unspecified amount and apply flag to be used in the afterSwap
hook.
The target unspecified amount and the apply flag are reset in the afterSwap hook.
|
_afterSwap(address sender, struct PoolKey key, struct SwapParams params, BalanceDelta delta, bytes) → bytes4, int128
internal
Enforce the target unspecified amount to the unspecified currency of the swap.
When the swap is exactInput
and the unspecified target is surpassed, the difference is decreased from the
output as a hook fee. Accordingly, when the swap is exactOutput
and the unspecified target is not reached, the
difference is increased to the input as a hook fee. Note that the fee is always applied to the unspecified
currency of the swap, regardless of the swap direction.
The fees are minted to this hook as ERC-6909 tokens, which can then be distributed in _afterSwapHandler
The target unspecified amount and the apply flag are reset on purpose to avoid state overlapping across swaps. |
_getTargetUnspecified(address sender, struct PoolKey key, struct SwapParams params, bytes hookData) → uint256 targetUnspecifiedAmount, bool applyTarget
internal
Return the target unspecified amount to be enforced by the afterSwap
hook.