Index / FeeRouter
pattern · MIT · last updated 2026-08-27
FeeRouter
afterSwap skims protocolBps of output to an immutable treasury address.
First-party teaching hook for fee collect-and-route on every swap. afterSwap takes protocolBps of the swap output token via take + ERC20 transfer to treasury. Similar shape to referral-fee.yml but fixed recipient instead of hookData referrer. Experimental. Forge tests in test/FeeRouter.t.sol. Listing is not an audit.
Permission bits
These bits must match the deployed address. Confirm on-chain before you route.
afterSwapafterSwapReturnDelta
Solidity
Excerpt from contracts/FeeRouter.sol. Copy the full file to implement this. Not an audit.
// SPDX-License-Identifier: MIT
function _afterSwap(...) internal override returns (bytes4, int128) {
uint256 pay = (uint256(int256(out)) * protocolBps) / 10_000;
outCur.take(poolManager, address(this), pay, false);
IERC20(Currency.unwrap(outCur)).transfer(treasury, pay);
return (this.afterSwap.selector, pay.toInt128());
}Spec
| Kind | pattern |
|---|---|
| Status | experimental |
| License | MIT |
| Source | https://github.com/CryptoGnome/v4hooks |
| Categories | Creator economyDynamic fees |
| Properties | Vanilla swap |
| Chains | Ethereum |
FAQ
Can I paste this into production? The snippet is an excerpt. Use the full file at the source URL, match flags to the address, and treat this page as a map, not a guarantee.
How do I build this safely? Start with secure v4 hooks and the OpenZeppelin hooks guide.