Verified contractExactMIT
CMDHook
Address0x7b0d132c6bf845d5a072b6cc9201546f0af8a044
- Balance
0.000012 ETH
- Transactions
3
- Compiler
solc 0.8.30
- Same code
Only this contract
Details
- Compiler
- solc 0.8.30+commit.73712a01
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- prague
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 24,972,332
- Creation transaction
- 0x19124d3144…97ad0c2676
47 files
The source code is large: one file is shown at a time. Choose a file in the list.
- src/CMDHook.solmain30.44 KB
- lib/solady/src/auth/Ownable.sol12.55 KB
- lib/solady/src/tokens/ERC20.sol31.12 KB
- lib/solady/src/utils/CallContextChecker.sol4.49 KB
- lib/solady/src/utils/Initializable.sol8.48 KB
- lib/solady/src/utils/LibClone.sol156.44 KB
- lib/solady/src/utils/ReentrancyGuard.sol2.33 KB
- lib/solady/src/utils/SafeTransferLib.sol32.86 KB
- lib/solady/src/utils/UUPSUpgradeable.sol5.21 KB
- lib/v4-core/src/interfaces/IExtsload.sol1.02 KB
- lib/v4-core/src/interfaces/IExttload.sol699 B
- lib/v4-core/src/interfaces/IHooks.sol8.23 KB
- lib/v4-core/src/interfaces/IPoolManager.sol12.84 KB
- lib/v4-core/src/interfaces/IProtocolFees.sol2.49 KB
- lib/v4-core/src/interfaces/callback/IUnlockCallback.sol488 B
- lib/v4-core/src/interfaces/external/IERC20Minimal.sol3.04 KB
- lib/v4-core/src/interfaces/external/IERC6909Claims.sol3.16 KB
- lib/v4-core/src/libraries/BitMath.sol2.61 KB
- lib/v4-core/src/libraries/CustomRevert.sol5.17 KB
- lib/v4-core/src/libraries/FixedPoint128.sol297 B
- lib/v4-core/src/libraries/FixedPoint96.sol366 B
- lib/v4-core/src/libraries/FullMath.sol5.46 KB
- lib/v4-core/src/libraries/Hooks.sol16.51 KB
- lib/v4-core/src/libraries/LPFeeLibrary.sol3.6 KB
- lib/v4-core/src/libraries/LiquidityMath.sol742 B
- lib/v4-core/src/libraries/ParseBytes.sol1.2 KB
- lib/v4-core/src/libraries/Position.sol4.81 KB
- lib/v4-core/src/libraries/SafeCast.sol2.27 KB
- lib/v4-core/src/libraries/StateLibrary.sol16.03 KB
- lib/v4-core/src/libraries/TickMath.sol12.38 KB
- lib/v4-core/src/types/BalanceDelta.sol2.34 KB
- lib/v4-core/src/types/BeforeSwapDelta.sol1.47 KB
- lib/v4-core/src/types/Currency.sol5.46 KB
- lib/v4-core/src/types/PoolId.sol539 B
- lib/v4-core/src/types/PoolKey.sol805 B
- lib/v4-core/src/types/PoolOperation.sol907 B
- lib/v4-core/test/utils/CurrencySettler.sol2.41 KB
- lib/v4-periphery/src/base/ImmutableState.sol818 B
- lib/v4-periphery/src/interfaces/IImmutableState.sol365 B
- lib/v4-periphery/src/interfaces/IMsgSender.sol605 B
- lib/v4-periphery/src/libraries/LiquidityAmounts.sol3.77 KB
- lib/v4-periphery/src/utils/BaseHook.sol7.1 KB
- lib/v4-router/src/interfaces/IUniswapV4Router04.sol7 KB
- lib/v4-router/src/libraries/PathKey.sol1.45 KB
- src/CMD.sol17.58 KB
- src/CMDCommandments.sol27.04 KB
- src/Interfaces.sol104 B
src/CMDHook.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import {BaseHook} from "@uniswap/v4-periphery/src/utils/BaseHook.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol";
import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {CurrencySettler} from "@uniswap/v4-core/test/utils/CurrencySettler.sol";
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {Ownable} from "solady/auth/Ownable.sol";
import {ReentrancyGuard} from "solady/utils/ReentrancyGuard.sol";
import {SwapParams} from "@uniswap/v4-core/src/types/PoolOperation.sol";
import {IMsgSender} from "@uniswap/v4-periphery/src/interfaces/IMsgSender.sol";
import {CMDCommandments} from "./CMDCommandments.sol";
/// @title CMDHook - Uniswap V4 hook implementing CMD commandments II, V, IX and routing for I + VI
/// @notice Collects sell-side fees, runs the adaptive dampener (V) and cooldown (IX), distributes
/// fees per-swap, and forwards the LP slice (I) to CMDCommandments, which owns the locked
/// position and runs `harvestAndBurn` (VI). Hook is swappable via `cmd.updateHookAddress`.
contract CMDHook is BaseHook, Ownable, ReentrancyGuard {
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ ™™™™™™™™™™™ ™™™™™™™™™™™ */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ ™™™™™™™™™™™™™ ™™™™™™™™™™ */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ ™™™™™™™™™™™™™ ™™™™™™™™™™™ */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ ™™™™™™™™™™™™™™ ™™™™™™™™™™™ */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ ™™™™™™™™™™™™™™™ ™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™™ ™™™™™™™™™™™™™™™ ™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™™™™™™™™™ ™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™™™™™™™™™ ™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™™™™™™™™™™™ ™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™™ ™™™™™™™™™ ™™™™™™™™™ ™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™ ™™™™™™™™™ ™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™ ™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™™ ™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™™™™™™™™™™™ ™™™™™™™™™™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™™™™™™™™™ ™™™™™™™™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™™™™™™™™™ ™™™™™™™™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™™™™™™™ ™™™™™™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™™™™™ ™™™™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™™™™™ ™™™™™™™™™™™™™™ */
/* ™™™™™™™™™™™ ™™™™™™™™™™™™ ™™™™™™™™™™™™ */
using PoolIdLibrary for PoolKey;
using StateLibrary for IPoolManager;
using CurrencySettler for Currency;
using SafeCast for uint256;
using SafeCast for int128;
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* CONSTANTS */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
uint128 private constant TOTAL_BIPS = 10000;
uint128 private constant RESTING_FEE = 100; // 1% resting (both directions)
uint128 private constant STARTING_BUY_FEE = 9900; // 99% starting buy fee (decays 1%/block)
uint128 private constant BURN_BPS = 200; // 2% burn target on sells (II)
uint128 private constant LOCK_LP_BPS = 25; // 0.25% of sells reserved for locked LP (I)
uint128 private constant COOLDOWN_BPS = 500; // 5% surcharge on sells inside cooldown (IX)
uint128 private constant DAMPENER_CAP_BPS = 300; // 3% surcharge cap (V)
uint128 private constant DAMPENER_SLOPE = 2; // bps of surcharge per bps over threshold (V)
uint128 private constant DAMPENER_THRESHOLD_BPS = 25; // 0.25% of supply sold in 24h (V)
uint128 private constant SELL_CAP_BPS = 1000; // 10% combined sell-side cap (II)
uint256 private constant COOLDOWN_SECONDS = 6 hours; // cooldown window (IX)
uint256 private constant BUCKET_COUNT = 24; // ring-buffer slots (V)
uint256 private constant BUCKET_SECONDS = 1 hours; // each slot = 1 hour (V)
uint160 private constant MAX_PRICE_LIMIT = TickMath.MAX_SQRT_PRICE - 1;
uint160 private constant MIN_PRICE_LIMIT = TickMath.MIN_SQRT_PRICE + 1;
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* STATE */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
uint256 public deploymentBlock;
bool public poolInitialized;
address public feeAddress;
address public buybackAddress;
CMDCommandments public immutable cmd;
// V — hourly sell-volume ring buffer, each slot tagged with its hour number
struct Bucket {
uint256 hour;
uint256 volume; // CMD
}
Bucket[BUCKET_COUNT] public sellBuckets;
// IX — per-user last sell timestamp
mapping(address => uint48) public lastSellAt;
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* ERRORS */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
error PoolAlreadyInitialized();
error NotOwner();
error ExactOutputNotAllowed();
error ZeroAddress();
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* EVENTS */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
event HookFee(bytes32 indexed id, address indexed sender, uint128 feeAmount0, uint128 feeAmount1);
event Trade(uint160 sqrtPriceX96, int128 ethAmount, int128 tokenAmount);
event SellTaxes(
bytes32 indexed id,
address indexed user,
uint128 restingBps,
uint128 burnBps,
uint128 lpBps,
uint128 cooldownBps,
uint128 dampenerBps
);
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* CONSTRUCTOR */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
constructor(
IPoolManager _poolManager,
CMDCommandments _cmd,
address _owner,
address _feeAddress,
address _buybackAddress
) BaseHook(_poolManager) {
_initializeOwner(_owner);
cmd = _cmd;
feeAddress = _feeAddress;
buybackAddress = _buybackAddress;
}
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* ADMIN */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
function updateFeeAddress(address _feeAddress) external onlyOwner {
if (_feeAddress == address(0)) revert ZeroAddress();
feeAddress = _feeAddress;
}
function updateBuybackAddress(address _buybackAddress) external onlyOwner {
if (_buybackAddress == address(0)) revert ZeroAddress();
buybackAddress = _buybackAddress;
}
receive() external payable {}
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* HOOK PERMISSIONS */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: false,
beforeAddLiquidity: false,
afterAddLiquidity: false,
beforeRemoveLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: false,
afterSwap: true,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: true,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* _beforeInitialize */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
function _beforeInitialize(address sender, PoolKey calldata key, uint160) internal override returns (bytes4) {
if (poolInitialized) revert PoolAlreadyInitialized();
if (sender != owner() && sender != address(cmd)) revert NotOwner();
require(key.currency0.isAddressZero(), "Only ETH/token pools are supported");
poolInitialized = true;
deploymentBlock = block.number;
// CMDCommandments owns the locked LP; tell it the pool config (writes are one-shot).
int24 _tickLower = TickMath.minUsableTick(key.tickSpacing);
int24 _tickUpper = TickMath.maxUsableTick(key.tickSpacing);
cmd.configurePool(key, _tickLower, _tickUpper);
return BaseHook.beforeInitialize.selector;
}
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* FEE VIEWS */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/// @notice Both buy and sell fees decay 1% per block from 99% to 1% during the launch window.
/// Once the decay completes (block `deploymentBlock + 98`), the fee floors at 1% and the
/// sell-side bundle (burn / LP / cooldown / dampener) starts applying on top; buys just
/// keep the 1% resting rate with no bundle. The boolean argument is retained for ABI
/// compatibility; both directions follow the same decay curve.
function calculateFee(bool) public view returns (uint128) {
uint256 deployedAt = deploymentBlock;
if (deployedAt == 0) return RESTING_FEE;
uint256 blocksPassed = block.number - deployedAt;
uint256 feeReductions = blocksPassed * 100;
uint256 maxReducible = STARTING_BUY_FEE - RESTING_FEE;
if (feeReductions >= maxReducible) return RESTING_FEE;
return uint128(STARTING_BUY_FEE - feeReductions);
}
struct SellBundle {
uint128 restingBps;
uint128 burnBps;
uint128 lpBps;
uint128 cooldownBps;
uint128 dampenerBps;
uint128 totalBps;
}
function quoteSellBundle(address user) public view returns (SellBundle memory) {
return _computeBundle(_cooldownSurcharge(user), _dampenerBps());
}
/// @notice Pure helper computing the bundle given the two variable surcharges. Exposed for fuzz
/// testing of the cap / sum invariants without needing to fabricate dampener / cooldown state.
function previewBundle(uint128 cooldownBps, uint128 dampenerBps) external pure returns (SellBundle memory) {
return _computeBundle(cooldownBps, dampenerBps);
}
function _computeBundle(uint128 cooldownBps, uint128 dampenerBps) internal pure returns (SellBundle memory b) {
b.restingBps = RESTING_FEE;
b.lpBps = LOCK_LP_BPS;
b.cooldownBps = cooldownBps;
b.dampenerBps = dampenerBps;
uint128 others = b.restingBps + b.lpBps + cooldownBps + dampenerBps;
if (others >= SELL_CAP_BPS) {
b.burnBps = 0;
} else {
uint128 room = SELL_CAP_BPS - others;
b.burnBps = room >= BURN_BPS ? BURN_BPS : room;
}
b.totalBps = others + b.burnBps;
}
function _cooldownSurcharge(address user) internal view returns (uint128) {
uint48 last = lastSellAt[user];
if (last == 0) return 0;
return block.timestamp - last < COOLDOWN_SECONDS ? COOLDOWN_BPS : 0;
}
/// @notice Resolve the real user behind a swap via IMsgSender, falling back to tx.origin.
function _resolveUser(address sender) internal view returns (address) {
if (sender == address(this)) return address(this);
try IMsgSender(sender).msgSender() returns (address u) {
if (u != address(0)) return u;
} catch {}
return tx.origin;
}
function _dampenerBps() internal view returns (uint128) {
uint256 supply = cmd.totalSupply();
if (supply == 0) return 0;
uint256 nowHour = block.timestamp / BUCKET_SECONDS;
uint256 threshold = nowHour + 1 >= BUCKET_COUNT ? nowHour + 1 - BUCKET_COUNT : 0;
uint256 sum;
for (uint256 i = 0; i < BUCKET_COUNT; i++) {
Bucket memory b = sellBuckets[i];
if (b.hour >= threshold && b.hour <= nowHour) sum += b.volume;
}
uint256 shareBps = (sum * uint256(TOTAL_BIPS)) / supply;
if (shareBps <= DAMPENER_THRESHOLD_BPS) return 0;
uint256 bps = (shareBps - DAMPENER_THRESHOLD_BPS) * DAMPENER_SLOPE;
if (bps > DAMPENER_CAP_BPS) return DAMPENER_CAP_BPS;
return uint128(bps);
}
function _recordSellVolume(uint256 cmdAmount) internal {
uint256 h = block.timestamp / BUCKET_SECONDS;
uint256 idx = h % BUCKET_COUNT;
Bucket storage b = sellBuckets[idx];
if (b.hour != h) {
b.hour = h;
b.volume = cmdAmount;
} else {
b.volume += cmdAmount;
}
}
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* _afterSwap */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
function _afterSwap(
address sender,
PoolKey calldata key,
SwapParams calldata params,
BalanceDelta delta,
bytes calldata
) internal override returns (bytes4, int128) {
// Self-call guard. Covers:
// - the hook's own internal swaps (burn slice + LP slice inside _handleSell, and the
// _swapCmdToEth conversion in _handleBuy);
// - CMDCommandments's TWAP buyback swap — the protocol's own buyback shouldn't pay the
// hook's 1% fee on top of the pool's LP fee.
if (sender == address(this) || sender == address(cmd)) {
return (BaseHook.afterSwap.selector, 0);
}
if (params.amountSpecified > 0) {
revert ExactOutputNotAllowed();
}
bool specifiedTokenIs0 = (params.amountSpecified < 0 == params.zeroForOne);
int128 swapAmount = specifiedTokenIs0 ? delta.amount1() : delta.amount0();
if (swapAmount < 0) swapAmount = -swapAmount;
int128 feeDelta;
uint256 allowanceForRouter;
if (params.zeroForOne) {
feeDelta = _handleBuy(key, uint128(swapAmount), sender);
allowanceForRouter = uint256(uint128(swapAmount)) - uint256(uint128(feeDelta));
} else {
uint256 cmdInput = uint256(int256(-delta.amount1()));
feeDelta = _handleSell(key, uint128(swapAmount), cmdInput, sender);
allowanceForRouter = cmdInput;
}
if (allowanceForRouter > 0) cmd.increaseTransferAllowance(allowanceForRouter);
emit Trade(_getCurrentPrice(key), delta.amount0(), delta.amount1());
return (BaseHook.afterSwap.selector, feeDelta);
}
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* BUY-SIDE HANDLER */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
function _handleBuy(PoolKey calldata key, uint128 swapAmount, address sender) internal returns (int128) {
uint128 currentFee = calculateFee(true);
uint256 totalFee = uint256(swapAmount) * currentFee / TOTAL_BIPS;
if (totalFee == 0) return 0;
uint256 twFee = uint256(swapAmount) * RESTING_FEE / TOTAL_BIPS;
// Pool→hook CMD fee take.
cmd.increaseTransferAllowance(totalFee);
poolManager.take(key.currency1, address(this), totalFee);
emit HookFee(PoolId.unwrap(key.toId()), _resolveUser(sender), 0, uint128(totalFee));
uint256 ethReceived = _swapCmdToEth(key, totalFee);
uint256 twEth = (ethReceived * twFee) / totalFee;
uint256 buybackEth = ethReceived - twEth;
SafeTransferLib.forceSafeTransferETH(feeAddress, twEth);
if (buybackEth > 0) {
SafeTransferLib.forceSafeTransferETH(buybackAddress, buybackEth);
}
return totalFee.toInt128();
}
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* SELL-SIDE HANDLER */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
function _handleSell(PoolKey calldata key, uint128 swapAmount, uint256 cmdInput, address sender)
internal
returns (int128)
{
address user = _resolveUser(sender);
// Launch decay phase: sells mirror the buy-fee curve. The bundle (burn / LP / cooldown /
// dampener) and its 10% cap don't apply yet — the anti-sniper decay is the only mechanism.
uint128 decayFee = calculateFee(false);
if (decayFee > RESTING_FEE) {
return _handleSellDecay(key, uint256(swapAmount), decayFee, user);
}
SellBundle memory b = quoteSellBundle(user);
uint256 totalFeeEth = (uint256(swapAmount) * b.totalBps) / TOTAL_BIPS;
if (totalFeeEth == 0) {
_recordSellVolume(cmdInput);
lastSellAt[user] = uint48(block.timestamp);
return 0;
}
poolManager.take(key.currency0, address(this), totalFeeEth);
emit HookFee(PoolId.unwrap(key.toId()), user, uint128(totalFeeEth), 0);
uint256 restingEth = totalFeeEth * b.restingBps / b.totalBps;
uint256 cooldownEth = totalFeeEth * b.cooldownBps / b.totalBps;
uint256 dampenerEth = totalFeeEth * b.dampenerBps / b.totalBps;
uint256 lpEth = totalFeeEth * b.lpBps / b.totalBps;
uint256 burnEth = totalFeeEth - restingEth - cooldownEth - dampenerEth - lpEth;
if (restingEth > 0) SafeTransferLib.forceSafeTransferETH(feeAddress, restingEth);
uint256 buybackEthOut = cooldownEth + dampenerEth;
if (buybackEthOut > 0) SafeTransferLib.forceSafeTransferETH(buybackAddress, buybackEthOut);
// Burn and LP-accum both need ETH→CMD. Do ONE combined internal swap and split the output
// CMD in proportion to the ETH contributed by each component. Saves ~150k gas per sell vs.
// running two separate internal swaps.
uint256 ethForLpHold = lpEth / 2; // ETH half of the LP slice, stays as ETH
uint256 ethForLpSwap = lpEth - ethForLpHold; // ETH half to be converted to CMD
uint256 ethToConvert = burnEth + ethForLpSwap;
uint256 cmdForLpAccum;
if (ethToConvert > 0) {
uint256 totalCmdOut = _swapEthToCmd(key, ethToConvert, address(this));
if (totalCmdOut > 0) {
// Split proportionally by ETH input weight. LP-accum share first; burn gets the rest
// (any rounding dust lands in the burn portion).
cmdForLpAccum = (totalCmdOut * ethForLpSwap) / ethToConvert;
uint256 cmdForBurn = totalCmdOut - cmdForLpAccum;
if (cmdForBurn > 0) {
cmd.burn(address(this), cmdForBurn);
}
}
}
if (lpEth > 0) {
if (ethForLpHold > 0) {
SafeTransferLib.forceSafeTransferETH(address(cmd), ethForLpHold);
}
if (cmdForLpAccum > 0) {
// hook → cmd transfer passes via cmd-self distributor bypass; no allowance needed.
cmd.transfer(address(cmd), cmdForLpAccum);
}
cmd.registerLpFunds(ethForLpHold, cmdForLpAccum);
}
emit SellTaxes(
PoolId.unwrap(key.toId()),
user,
b.restingBps,
b.burnBps,
b.lpBps,
b.cooldownBps,
b.dampenerBps
);
_recordSellVolume(cmdInput);
lastSellAt[user] = uint48(block.timestamp);
return totalFeeEth.toInt128();
}
/// @notice Sell path during the launch decay window. Takes the full decayed fee in ETH and
/// splits it the same way a buy does: 1%-of-swap to TokenWorks, the decay excess to
/// buyback. No burn / LP / cooldown / dampener activity — those activate only after
/// the decay completes.
function _handleSellDecay(PoolKey calldata key, uint256 swapAmount, uint128 decayFee, address user)
internal
returns (int128)
{
uint256 totalFeeEth = (swapAmount * decayFee) / TOTAL_BIPS;
if (totalFeeEth == 0) return 0;
poolManager.take(key.currency0, address(this), totalFeeEth);
emit HookFee(PoolId.unwrap(key.toId()), user, uint128(totalFeeEth), 0);
uint256 twFeeEth = (swapAmount * RESTING_FEE) / TOTAL_BIPS; // 1% of the swap size
// Clamp (shouldn't trigger given decayFee > RESTING_FEE, but defensive for rounding edges).
if (twFeeEth > totalFeeEth) twFeeEth = totalFeeEth;
uint256 buybackEth = totalFeeEth - twFeeEth;
if (twFeeEth > 0) SafeTransferLib.forceSafeTransferETH(feeAddress, twFeeEth);
if (buybackEth > 0) SafeTransferLib.forceSafeTransferETH(buybackAddress, buybackEth);
return totalFeeEth.toInt128();
}
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* INTERNAL SWAPS */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/// @notice Swaps CMD held by the hook into ETH via the pool. Returns ETH received.
function _swapCmdToEth(PoolKey memory key, uint256 cmdAmount) internal returns (uint256) {
uint256 ethBefore = address(this).balance;
BalanceDelta d = poolManager.swap(
key,
SwapParams({
zeroForOne: false,
amountSpecified: -int256(cmdAmount),
sqrtPriceLimitX96: MAX_PRICE_LIMIT
}),
""
);
uint256 cmdToSettle = uint256(int256(-d.amount1()));
// Hook→pool CMD settle: strict rules require allowance (hook is not a distributor).
if (cmdToSettle > 0) {
cmd.increaseTransferAllowance(cmdToSettle);
}
key.currency1.settle(poolManager, address(this), cmdToSettle, false);
key.currency0.take(poolManager, address(this), uint256(int256(d.amount0())), false);
return address(this).balance - ethBefore;
}
/// @notice Swaps ETH held by the hook into CMD via the pool. Output CMD is delivered to `recipient`.
/// When `recipient == address(cmd)`, the pool→cmd transfer needs no allowance because
/// CMDCommandments marks itself a local distributor. Otherwise we bump allowance.
function _swapEthToCmd(PoolKey memory key, uint256 ethAmount, address recipient) internal returns (uint256) {
BalanceDelta d = poolManager.swap(
key,
SwapParams({
zeroForOne: true,
amountSpecified: -int256(ethAmount),
sqrtPriceLimitX96: MIN_PRICE_LIMIT
}),
""
);
key.currency0.settle(poolManager, address(this), uint256(int256(-d.amount0())), false);
uint256 cmdOut = uint256(int256(d.amount1()));
if (cmdOut > 0) {
if (recipient != address(cmd)) {
cmd.increaseTransferAllowance(cmdOut);
}
key.currency1.take(poolManager, recipient, cmdOut, false);
}
return cmdOut;
}
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
/* VIEWS */
/* ™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™™ */
function _getCurrentPrice(PoolKey calldata key) internal view returns (uint160) {
(uint160 sqrtPriceX96,,,) = poolManager.getSlot0(key.toId());
return sqrtPriceX96;
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "prague",
"remappings": [
"solady/=lib/solady/src/",
"@uniswap/v4-core/=lib/v4-core/",
"@uniswap/v4-periphery/=lib/v4-periphery/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"v4-router/=lib/v4-router/src/",
"@v4/=lib/v4-core/",
"@ensdomains/=lib/v4-core/node_modules/@ensdomains/",
"@forge/=lib/v4-router/lib/forge-std/src/",
"@openzeppelin/uniswap-hooks/=lib/v4-router/lib/v4-template/lib/uniswap-hooks/",
"@permit2/=lib/v4-router/lib/permit2/src/",
"@solady/=lib/v4-router/lib/solady/",
"@uniswap/v2-core/=lib/v4-router/lib/universal-router/node_modules/@uniswap/v2-core/",
"@uniswap/v3-core/=lib/v4-router/lib/universal-router/node_modules/@uniswap/v3-core/",
"@uniswap/v3-periphery/=lib/v4-router/lib/universal-router/lib/v3-periphery/",
"@universal-router/=lib/v4-router/lib/universal-router/contracts/",
"@v4-periphery/=lib/v4-router/lib/v4-periphery/",
"@v4-template/=lib/v4-router/lib/v4-template/",
"ds-test/=lib/v4-core/lib/forge-std/lib/ds-test/src/",
"erc4626-tests/=lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
"forge-gas-snapshot/=lib/v4-router/lib/forge-gas-snapshot/src/",
"forge-std/=lib/forge-std/src/",
"hardhat/=lib/v4-core/node_modules/hardhat/",
"hookmate/=lib/v4-router/lib/v4-template/lib/hookmate/src/",
"openzeppelin-contracts/=lib/v4-core/lib/openzeppelin-contracts/",
"openzeppelin/=lib/v4-router/lib/v4-template/lib/uniswap-hooks/lib/v4-core/lib/openzeppelin-contracts/contracts/",
"permit2/=lib/v4-periphery/lib/permit2/",
"solmate/=lib/v4-core/lib/solmate/",
"uniswap-hooks/=lib/v4-router/lib/v4-template/lib/uniswap-hooks/src/",
"universal-router/=lib/v4-router/lib/universal-router/",
"v3-periphery/=lib/v4-router/lib/universal-router/lib/v3-periphery/contracts/",
"v4-core/=lib/v4-core/src/",
"v4-periphery/=lib/v4-periphery/",
"v4-template/=lib/v4-router/lib/v4-template/",
"lib/permit2/test:openzeppelin-contracts/contracts/=lib/v4-router/lib/permit2/lib/openzeppelin-contracts/contracts/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.