The VolumeBasedFeeHook hook implements dynamic fees based on swap volume. The hook adjusts fees in a tiered structure based on swap amounts:
- Below minAmount: Uses
defaultFee - Between minAmount and maxAmount: Uses linear interpolation between
feeAtMinAmountandfeeAtMaxAmount - Above maxAmount: Uses
feeAtMaxAmount
Fee relationships must maintain:
feeAtMinAmount < defaultFeefeeAtMaxAmount < feeAtMinAmountminAmount < maxAmount
Clone the repository and install dependencies:
forge install- Source:
src/VolumeBasedFeeHook.sol - Interface:
src/interfaces/IVolumeBasedFeeHook.sol
The VolumeBasedFeeHook hook is designed to be deployed with a set of parameters encapsulated in the IVolumeBasedFeeHook.VolumeBasedFeeHookParams struct.
IVolumeBasedFeeHook.VolumeBasedFeeHookParams memory params = IVolumeBasedFeeHook.VolumeBasedFeeHookParams({
defaultFee: 3000, // 0.3%
feeAtMinAmount0: 2700, // 0.27%
feeAtMaxAmount0: 2400, // 0.24%
feeAtMinAmount1: 2100, // 0.21%
feeAtMaxAmount1: 2000, // 0.20%
minAmount0: 1e18, // 1 token0
maxAmount0: 10e18, // 10 token0
minAmount1: 1e18, // 1 token1
maxAmount1: 10e18 // 10 token1
});Several scripts are provided in the script/ folder to deploy and interact with the VolumeBasedFeeHook hook. These include:
- Deployment via CREATE2:
VolumeBasedFeeHook.s.sol - Local lifecycle testing (pool initialization, liquidity provision, swaps):
Anvil.s.sol
Use the following command to deploy the VolumeBasedFeeHook hook (via CREATE2) on your local network:
forge script script/VolumeBasedFeeHook.s.sol:VolumeBasedFeeHookScript --rpc-url http://localhost:8545 --private-key <YOUR_PRIVATE_KEY> --broadcastThis script:
- Mines the correct salt (using
HookMiner) for the given parameters and flags - Deploys the VolumeBasedFeeHook hook with the provided parameters
- Logs the deployed hook address
You can also run a full deployment and test lifecycle (pool creation, liquidity addition, and swapping) using:
forge script script/Anvil.s.sol:VolumeBasedFeeHookScript --rpc-url http://localhost:8545 --private-key <YOUR_PRIVATE_KEY> --broadcastThe Anvil.s.sol script:
- Deploys a pool manager and the VolumeBasedFeeHook hook
- Sets up additional helper contracts (like the Position Manager and Routers)
- Initializes a pool using the VolumeBasedFeeHook hook
- Adds liquidity and performs a test swap for full-end-to-end verification
Two important files help configure and bootstrap the system:
script/base/Constants.sol
The Constants file contains shared, immutable parameters used across scripts. These include:
-
Deployer Addresses:
The address from which CREATE2 deployments are performed (e.g.,CREATE2_DEPLOYER). -
Pool Manager & Other Contract Addresses:
Pre-set addresses for key contracts such as the Pool Manager, Position Manager, and the Permit2 contract, which help in setting up the system on a local Anvil network.
This file centralizes addresses to keep scripts consistent and to easily change deployment configuration if needed.
script/base/Config.sol
The Config file (if present) is used to configure and fine-tune deployment parameters for different environments. It may include:
- Network-specific configurations
- Pool fee settings and tick spacing
- Default token amounts for liquidity, price parameters, and more
Using these files helps in keeping the deployment scripts clean—by abstracting environment-specific configuration details away from the logic in the scripts themselves.
-
Hook Deployment Failures:
- Verify that the deployment flags passed to
HookMiner.findmatch the ones returned bygetHookCalls()in your hook. - Ensure that the salt mining logic uses the same deployer address (in scripts, this should be the CREATE2 deployer at
0x4e59b44847b379578588920cA78FbF26c0B4956C).
- Verify that the deployment flags passed to
-
Permission Denied / SSH Key Issues:
- If you encounter Github SSH errors, ensure your SSH keys are correctly added to your agent. Refer to GitHub’s SSH setup guide.
-
Incorrect Fee or Parameter Issues:
- Ensure that the fee parameters follow the valid relationships:
feeAtMinAmount < defaultFeefeeAtMaxAmount < feeAtMinAmountminAmount < maxAmount
- You can add extra debugging logs within your hook to monitor fee calculations if needed.
- Ensure that the fee parameters follow the valid relationships:
- Use
console.logstatements in your script to output key variable values. - Run your tests on Anvil locally and inspect the transaction logs via Foundry’s output.
- Check the v4-core and v4-periphery repositories for more detailed implementation examples.