🔥
BurnHookBNB Chain
Contract Source
Full source of every core contract, identical to the version deployed and verified on BscScan. All contracts are non-upgradeable with no admin after launch.
src/BurnSwapRouter.sol
Swap router: minimal V4 router used by the frontend
243 linessolc 0.8.26 · via-ir · optimizer 200sha256 3a26210ea3736a92…Not deployed yet
1// SPDX-License-Identifier: MIT2pragma solidity ^0.8.26;4import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";5import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";6import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";7import {Address} from "@openzeppelin/contracts/utils/Address.sol";8import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";10import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";11import {IUnlockCallback} from "v4-core/src/interfaces/callback/IUnlockCallback.sol";12import {IHooks} from "v4-core/src/interfaces/IHooks.sol";13import {PoolKey} from "v4-core/src/types/PoolKey.sol";14import {PoolId, PoolIdLibrary} from "v4-core/src/types/PoolId.sol";15import {Currency} from "v4-core/src/types/Currency.sol";16import {SwapParams} from "v4-core/src/types/PoolOperation.sol";17import {TickMath} from "v4-core/src/libraries/TickMath.sol";18import {TransientStateLibrary} from "v4-core/src/libraries/TransientStateLibrary.sol";19import {StateLibrary} from "v4-core/src/libraries/StateLibrary.sol";20import {IWETH9} from "v4-periphery/src/interfaces/external/IWETH9.sol";22import {BurnHook} from "./BurnHook.sol";24/// @title BurnSwapRouter25/// @notice Minimal, dedicated exact-input swap router for the protocol's own Uniswap V4 BURN/WETH pool.26/// @dev Lets a frontend trade against the hook pool with a plain `approve` + `swap(amountIn, minOut)`27/// flow — no Universal Router / Permit2 / command encoding needed.28///29/// It reuses the V4 unlock/callback + flash-accounting pattern: pull the input token, `unlock`30/// the PoolManager, `swap`, then settle/take the exact amounts read from the transient31/// `currencyDelta` (which already accounts for the BurnHook's deterministic 1% WETH fee).32///33/// Slippage is the caller's responsibility via `minOut` (the frontend gets a figure from the34/// V4 Quoter and applies its tolerance). Pass `deadline` (0 to disable) for stale-tx protection.35/// The pool only accepts swaps in the TRADING phase; the hook reverts otherwise.36contract BurnSwapRouter is IUnlockCallback, ReentrancyGuard {37 using SafeERC20 for IERC20;38 using TransientStateLibrary for IPoolManager;39 using StateLibrary for IPoolManager;40 using PoolIdLibrary for PoolKey;41 using Address for address payable;43 uint256 private constant BPS = 10_000;44 uint256 private constant HOOK_FEE_BPS = 100; // BurnHook takes 1% of the WETH leg on every swap46 IPoolManager public immutable poolManager;47 BurnHook public immutable hook;48 address public immutable burn;49 address public immutable weth;51 // PoolKey fields cached at deploy (the pool is already created by the time the router is deployed).52 address public immutable currency0;53 address public immutable currency1;54 uint24 public immutable fee;55 int24 public immutable tickSpacing;57 error NotPoolManager();58 error NotFinalized();59 error ZeroAmount();60 error ZeroAddress();61 error Expired();62 error Slippage();64 event Swap(65 address indexed sender,66 address indexed to,67 address tokenIn,68 address tokenOut,69 uint256 amountIn,70 uint256 amountOut71 );73 constructor(BurnHook _hook) {74 if (!_hook.finalized()) revert NotFinalized();75 hook = _hook;76 poolManager = _hook.poolManager();77 burn = address(_hook.burn());78 weth = address(_hook.wrappedNative());80 (Currency c0, Currency c1, uint24 f, int24 ts,) = _hook.poolKey();81 currency0 = Currency.unwrap(c0);82 currency1 = Currency.unwrap(c1);83 fee = f;84 tickSpacing = ts;85 }87 // ============ Public swap entries (exact input) ============89 /// @notice Swap an exact amount of WETH for BURN. Caller must `approve` WETH to this router first.90 function swapExactWethForBurn(uint256 amountIn, uint256 minBurnOut, address to, uint256 deadline)91 external92 nonReentrant93 returns (uint256 burnOut)94 {95 address recipient = _prep(to, deadline, amountIn);96 IERC20(weth).safeTransferFrom(msg.sender, address(this), amountIn);97 burnOut = _swap(weth, burn, amountIn, minBurnOut);98 IERC20(burn).safeTransfer(recipient, burnOut);99 emit Swap(msg.sender, recipient, weth, burn, amountIn, burnOut);100 }102 /// @notice Swap an exact amount of BURN for WETH. Caller must `approve` BURN to this router first.103 function swapExactBurnForWeth(uint256 amountIn, uint256 minWethOut, address to, uint256 deadline)104 external105 nonReentrant106 returns (uint256 wethOut)107 {108 address recipient = _prep(to, deadline, amountIn);109 IERC20(burn).safeTransferFrom(msg.sender, address(this), amountIn);110 wethOut = _swap(burn, weth, amountIn, minWethOut);111 IERC20(weth).safeTransfer(recipient, wethOut);112 emit Swap(msg.sender, recipient, burn, weth, amountIn, wethOut);113 }115 /// @notice Swap native ETH for BURN: the router wraps msg.value to WETH, then swaps.116 function swapExactEthForBurn(uint256 minBurnOut, address to, uint256 deadline)117 external118 payable119 nonReentrant120 returns (uint256 burnOut)121 {122 address recipient = _prep(to, deadline, msg.value);123 IWETH9(weth).deposit{value: msg.value}();124 burnOut = _swap(weth, burn, msg.value, minBurnOut);125 IERC20(burn).safeTransfer(recipient, burnOut);126 emit Swap(msg.sender, recipient, weth, burn, msg.value, burnOut);127 }129 /// @notice Swap an exact amount of BURN for native ETH: swap to WETH then unwrap. Caller must `approve` BURN first.130 function swapExactBurnForEth(uint256 amountIn, uint256 minEthOut, address to, uint256 deadline)131 external132 nonReentrant133 returns (uint256 ethOut)134 {135 address recipient = _prep(to, deadline, amountIn);136 IERC20(burn).safeTransferFrom(msg.sender, address(this), amountIn);137 ethOut = _swap(burn, weth, amountIn, minEthOut);138 IWETH9(weth).withdraw(ethOut);139 payable(recipient).sendValue(ethOut);140 emit Swap(msg.sender, recipient, burn, weth, amountIn, ethOut);141 }143 // ============ Quote (display only) ============145 /// @notice Spot-price estimate of the exact-input output, after the hook's 1% WETH fee.146 /// @dev DISPLAY ONLY — ignores price impact, so actual output is lower (more so for large147 /// swaps relative to liquidity). The frontend derives `minOut` from this with a slippage148 /// tolerance; on-chain execution is protected by that `minOut`, never by this estimate.149 /// @param tokenIn must be WETH or BURN150 /// @param amountIn exact input amount151 function quoteExactInput(address tokenIn, uint256 amountIn) external view returns (uint256 estOut) {152 if (amountIn == 0) return 0;153 require(tokenIn == weth || tokenIn == burn, "bad tokenIn");154 address tokenOut = tokenIn == weth ? burn : weth;156 (uint160 sqrtPriceX96,,,) = poolManager.getSlot0(_poolKey().toId());157 uint256 p = uint256(sqrtPriceX96);158 uint256 q96 = 1 << 96;160 // 1% hook fee applies to the WETH leg: deduct from input if paying WETH, else from output below.161 uint256 effIn = tokenIn == weth ? amountIn * (BPS - HOOK_FEE_BPS) / BPS : amountIn;163 bool inIsToken0 = tokenIn < tokenOut; // lower address is currency0164 uint256 gross = inIsToken0165 ? Math.mulDiv(Math.mulDiv(effIn, p, q96), p, q96) // out = in · P^2 / 2^192166 : Math.mulDiv(Math.mulDiv(effIn, q96, p), q96, p); // out = in · 2^192 / P^2168 estOut = tokenOut == weth ? gross * (BPS - HOOK_FEE_BPS) / BPS : gross;169 }171 // ============ Internal ============173 function _prep(address to, uint256 deadline, uint256 amountIn) internal view returns (address recipient) {174 if (deadline != 0 && block.timestamp > deadline) revert Expired();175 if (amountIn == 0) revert ZeroAmount();176 recipient = to == address(0) ? msg.sender : to;177 }179 function _swap(address tokenIn, address tokenOut, uint256 amountIn, uint256 minOut)180 internal181 returns (uint256 out)182 {183 out = abi.decode(184 poolManager.unlock(abi.encode(tokenIn, tokenOut, amountIn, minOut)),185 (uint256)186 );187 }189 /// @notice V4 unlock callback — only reachable inside a swap initiated by this router.190 function unlockCallback(bytes calldata data) external override returns (bytes memory) {191 if (msg.sender != address(poolManager)) revert NotPoolManager();192 (address tokenIn, address tokenOut, uint256 amountIn, uint256 minOut) =193 abi.decode(data, (address, address, uint256, uint256));195 // zeroForOne = swapping currency0 → currency1 = (tokenIn is currency0) = (tokenIn address is the lower one)196 bool zeroForOne = tokenIn < tokenOut;198 poolManager.swap(199 _poolKey(),200 SwapParams({201 zeroForOne: zeroForOne,202 amountSpecified: -int256(amountIn), // negative = exact input203 sqrtPriceLimitX96: zeroForOne ? TickMath.MIN_SQRT_PRICE + 1 : TickMath.MAX_SQRT_PRICE - 1204 }),205 ""206 );208 Currency inCur = Currency.wrap(tokenIn);209 Currency outCur = Currency.wrap(tokenOut);211 // Settle exactly what the transient ledger says (auto-accounts for the hook's 1% WETH fee).212 int256 inDelta = poolManager.currencyDelta(address(this), inCur); // < 0 = router owes213 int256 outDelta = poolManager.currencyDelta(address(this), outCur); // > 0 = router can take215 if (inDelta < 0) {216 poolManager.sync(inCur);217 IERC20(tokenIn).safeTransfer(address(poolManager), uint256(-inDelta));218 poolManager.settle();219 }221 if (outDelta <= 0) revert Slippage();222 uint256 out = uint256(outDelta);223 if (out < minOut) revert Slippage();224 poolManager.take(outCur, address(this), out);226 return abi.encode(out);227 }229 function _poolKey() internal view returns (PoolKey memory) {230 return PoolKey({231 currency0: Currency.wrap(currency0),232 currency1: Currency.wrap(currency1),233 fee: fee,234 tickSpacing: tickSpacing,235 hooks: IHooks(address(hook))236 });237 }239 /// @dev Only accept ETH from the WETH contract (during unwrap in swapExactBurnForEth).240 receive() external payable {241 if (msg.sender != weth) revert ZeroAddress();242 }243}