// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; /// @title NoN $n0n Fee Hook (Uniswap v4) /// @notice The fee layer of NoN Bond, written to the current v4 hook spec. /// On every swap in the $n0n pool the hook takes the protocol fee in /// $n0n, burns 80% of it to the dead address, and forwards 20% to the /// prover fund. Bond NFT settlement rides the same rail. /// /// @dev DEPLOYMENT STATUS: written and frozen, deploys with the engine. /// Production build compiles against @uniswap/v4-core and inherits /// BaseHook from v4-periphery; the minimal types below mirror those /// interfaces so this file documents the exact logic that ships. /// The deployed address must encode AFTER_SWAP_FLAG (and /// AFTER_SWAP_RETURNS_DELTA_FLAG) per the v4 address-mining rules, and /// getHookPermissions() must stay in sync with the callbacks used. interface IERC20 { function transfer(address to, uint256 amount) external returns (bool); function balanceOf(address a) external view returns (uint256); } // ---- minimal mirrors of the v4 types this hook touches ---- type BalanceDelta is int256; struct PoolKey { address currency0; address currency1; uint24 fee; int24 tickSpacing; address hooks; } struct SwapParams { bool zeroForOne; int256 amountSpecified; uint160 sqrtPriceLimitX96; } struct HookPermissions { bool beforeInitialize; bool afterInitialize; bool beforeAddLiquidity; bool afterAddLiquidity; bool beforeRemoveLiquidity; bool afterRemoveLiquidity; bool beforeSwap; bool afterSwap; bool beforeDonate; bool afterDonate; bool beforeSwapReturnDelta; bool afterSwapReturnDelta; bool afterAddLiquidityReturnDelta; bool afterRemoveLiquidityReturnDelta; } contract NonV4FeeHook { address public constant DEAD = 0x000000000000000000000000000000000000dEaD; IERC20 public immutable non; // $n0n address public immutable poolManager; // v4 singleton on the target chain address public immutable proverFund; // 20% destination uint256 public constant BURN_BPS = 8000; // 80% of every fee burns uint256 public constant FEE_BPS = 30; // protocol fee: 0.30% of swap output in $n0n uint256 public totalBurned; uint256 public totalToProvers; event FeeSettled(uint256 fee, uint256 burned, uint256 toProvers); modifier onlyPoolManager() { require(msg.sender == poolManager, "not pool manager"); _; } constructor(address nonToken, address manager, address provers) { non = IERC20(nonToken); poolManager = manager; proverFund = provers; } function getHookPermissions() public pure returns (HookPermissions memory p) { p.afterSwap = true; p.afterSwapReturnDelta = true; } /// @dev v4 afterSwap callback shape: (sender, key, params, delta, hookData) /// returns (selector, fee delta taken by the hook in the unspecified currency). function afterSwap( address, PoolKey calldata key, SwapParams calldata params, BalanceDelta delta, bytes calldata ) external onlyPoolManager returns (bytes4, int128) { // fee applies only when $n0n is the output currency of the swap uint256 nonOut = _nonOutput(key, params, delta); if (nonOut == 0) return (this.afterSwap.selector, 0); uint256 fee = (nonOut * FEE_BPS) / 10_000; uint256 burn = (fee * BURN_BPS) / 10_000; uint256 provers = fee - burn; // production build: fee is taken from the pool via poolManager.take() // and settled here; the transfers below are the settlement itself. non.transfer(DEAD, burn); non.transfer(proverFund, provers); unchecked { totalBurned += burn; totalToProvers += provers; } emit FeeSettled(fee, burn, provers); return (this.afterSwap.selector, int128(int256(fee))); } function _nonOutput(PoolKey calldata key, SwapParams calldata params, BalanceDelta delta) private view returns (uint256) { int256 d = BalanceDelta.unwrap(delta); int128 amount0 = int128(d >> 128); int128 amount1 = int128(d); if (key.currency0 == address(non) && params.zeroForOne == false && amount0 > 0) { return uint256(uint128(amount0)); } if (key.currency1 == address(non) && params.zeroForOne == true && amount1 > 0) { return uint256(uint128(amount1)); } return 0; } }