🔥
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/BurnHookLens.sol
Lens: read-only aggregator — protocol state, wallet position and mint eligibility in one call; the on-chain entry point for agents
354 linessolc 0.8.26 · via-ir · optimizer 200sha256 c775ceadd5fcc167…On-chain 0x4Cb05beBbfF35c15FE32952D34579F7E1d940cdC · BscScan ↗ · Sourcify (verified source) ↗
1// SPDX-License-Identifier: MIT2pragma solidity ^0.8.26;4import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";5import {IERC4626} from "@openzeppelin/contracts/interfaces/IERC4626.sol";6import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";8import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";9import {PoolId} from "v4-core/src/types/PoolId.sol";10import {StateLibrary} from "v4-core/src/libraries/StateLibrary.sol";12import {BurnToken} from "./BurnToken.sol";13import {IBurnToken} from "./interfaces/IBurnToken.sol";14import {BurnHook} from "./BurnHook.sol";15import {XBurn} from "./XBurn.sol";17/// @dev Minimal views the Lens needs from the periphery, used to validate `setPeriphery`.18interface IWrappedXBurnView {19 function asset() external view returns (address);20 function xBurnContract() external view returns (address);21}23interface IBurnSwapRouterView {24 function hook() external view returns (address);25 function burn() external view returns (address);26}28/// @title BurnHookLens29/// @notice Read-only aggregator for agents and frontends: one `eth_call` returns the whole protocol30/// state, a wallet's position, or a "can this wallet mint right now, and if not why" answer.31/// @dev Holds no funds and has no effect on the core contracts — it only reads them.32/// The core contracts do not know about the Lens, so it can be (re)deployed at any time.33///34/// Address discovery: BurnToken is the root. `hook = burn.bonder()`, `xBurn = hook.xBurn()`.35/// WrappedXBurn and BurnSwapRouter are deployed separately after finalize and are not36/// referenced by any core contract, so they are registered here ONCE via `setPeriphery`37/// (validated against the core, then the admin role is cleared for good).38contract BurnHookLens {39 using StateLibrary for IPoolManager;41 address internal constant DEAD = 0x000000000000000000000000000000000000dEaD;43 BurnToken public immutable burn;44 BurnHook public immutable hook;46 /// @notice Set once via `setPeriphery`; address(0) until then.47 address public wxBurn;48 address public router;50 /// @notice May call `setPeriphery` exactly once; address(0) afterwards.51 address public admin;53 error NotAdmin();54 error BonderNotSet();55 error NotFinalized();56 error BadWxBurn();57 error BadRouter();59 event PeripherySet(address indexed wxBurn, address indexed router);61 // ============ Return types ============63 struct Addresses {64 address burnToken;65 address burnHook;66 address xBurn; // address(0) before finalize67 address wxBurn; // address(0) until setPeriphery68 address router; // address(0) until setPeriphery69 address wrappedNative; // WBNB on BSC70 address agentId; // address(0) = no Agent ID gate71 address dev;72 address poolManager;73 }75 struct Constants {76 uint256 mintPrice;77 uint256 mintAmount;78 uint256 maxMints;79 uint256 maxMintsPerWallet;80 uint256 mintDuration;81 uint256 blockDelay;82 uint256 minMintsToInvite;83 uint256 directRewardBps;84 uint256 indirectRewardBps;85 uint256 devRewardBps;86 uint256 lpReserve;87 uint256 totalSupplyCap;88 }90 struct ProtocolState {91 uint8 phase; // IBurnToken.Phase: 0 PENDING, 1 MINTING, 2 MINT_SUCCESS, 3 BONDED, 4 TRADING, 5 MINT_FAILED92 uint256 blockNumber;93 uint256 timestamp;94 // mint95 uint256 mintCount;96 uint256 mintsRemaining;97 uint256 mintDeadline; // unix seconds; 0 before startMint98 uint256 timeRemaining; // seconds; 0 unless MINTING99 uint256 nativeRaised; // wei100 uint256 totalReferralReward; // wei reserved for referrers (unclaimed)101 uint256 devReward; // wei reserved for DEV (unclaimed)102 // pool103 bool finalized;104 uint256 tradingStartBlock; // bondedBlock + BLOCK_DELAY; 0 before finalize105 uint160 sqrtPriceX96; // 0 before finalize106 uint128 poolLiquidity; // 0 before finalize107 uint256 nativePerBurn; // spot price: wei of wrapped native per 1e18 BURN (before the 1% hook fee); 0 before finalize108 // supply109 uint256 burnTotalSupply;110 uint256 burnBurned; // BURN held by DEAD111 // xBURN / wxBURN112 uint256 xBurnTotalSupply;113 uint256 dividendPool; // wrapped native held by xBURN, not yet paid out114 uint256 wxBurnTotalAssets; // xBURN held by the vault115 uint256 wxBurnTotalSupply;116 uint256 vaultPendingDividend; // wrapped native the vault could harvest via compoundFromDividend117 }119 /// @dev `code` values returned by `mintStatus`.120 uint8 public constant MINT_OK = 0;121 uint8 public constant MINT_NOT_MINTING = 1;122 uint8 public constant MINT_SOLD_OUT = 2;123 uint8 public constant MINT_AGENT_ID_REQUIRED = 3;124 uint8 public constant MINT_WALLET_LIMIT = 4;125 uint8 public constant MINT_INVITER_REQUIRED = 5;126 uint8 public constant MINT_SELF_INVITE = 6;127 uint8 public constant MINT_INVITER_NOT_QUALIFIED = 7;128 uint8 public constant MINT_INSUFFICIENT_BALANCE = 8;130 struct MintStatus {131 bool canMint;132 uint8 code; // one of the MINT_* constants133 string reason; // human/agent readable name of `code`134 uint256 value; // exact msg.value to send (MINT_PRICE)135 address inviterToPass; // the argument to pass to mint(): address(0) when no inviter is needed136 bool inviterNeeded; // true only for a wallet's first mint when it is not the first minter137 uint256 walletMintsUsed;138 uint256 walletMintsLeft; // min(per-wallet remaining, global remaining)139 }141 struct Position {142 uint256 nativeBalance;143 uint256 burnBalance;144 uint256 xBurnBalance;145 uint256 wxBurnBalance;146 uint256 wxBurnAsXBurn; // xBURN redeemable for the wxBURN balance147 uint256 pendingDividend; // wrapped native claimable from xBURN via claim()148 uint256 referralReward; // wei accrued as a referrer149 bool referralClaimable; // true once the mint has succeeded and referralReward > 0150 uint256 mintsOf;151 address inviter;152 bool hasAgentId;153 bool canInvite;154 uint256 refundableBurn; // BURN that can be refunded now (multiple of MINT_AMOUNT); 0 unless MINT_FAILED155 uint256 refundableNative; // wei returned for refundableBurn156 }158 // ============ Setup ============160 /// @param _burn the BurnToken (protocol root). Its bonder (the hook) must already be set.161 constructor(address _burn) {162 burn = BurnToken(payable(_burn));163 address bonder = burn.bonder();164 if (bonder == address(0)) revert BonderNotSet();165 hook = BurnHook(payable(bonder));166 admin = msg.sender;167 }169 /// @notice Register WrappedXBurn and BurnSwapRouter. Callable once by the admin, after finalize.170 /// Both are checked against the core contracts; afterwards the admin role is cleared.171 function setPeriphery(address _wxBurn, address _router) external {172 if (msg.sender != admin) revert NotAdmin();173 address x = address(hook.xBurn());174 if (x == address(0)) revert NotFinalized();175 if (176 _wxBurn.code.length == 0177 || IWrappedXBurnView(_wxBurn).asset() != x178 || IWrappedXBurnView(_wxBurn).xBurnContract() != x179 ) revert BadWxBurn();180 if (181 _router.code.length == 0182 || IBurnSwapRouterView(_router).hook() != address(hook)183 || IBurnSwapRouterView(_router).burn() != address(burn)184 ) revert BadRouter();185 wxBurn = _wxBurn;186 router = _router;187 admin = address(0);188 emit PeripherySet(_wxBurn, _router);189 }191 // ============ Views ============193 function addresses() public view returns (Addresses memory a) {194 a.burnToken = address(burn);195 a.burnHook = address(hook);196 a.xBurn = address(hook.xBurn());197 a.wxBurn = wxBurn;198 a.router = router;199 a.wrappedNative = address(hook.wrappedNative());200 a.agentId = burn.AGENT_ID();201 a.dev = burn.DEV();202 a.poolManager = address(hook.poolManager());203 }205 function constants() external view returns (Constants memory c) {206 c.mintPrice = burn.MINT_PRICE();207 c.mintAmount = burn.MINT_AMOUNT();208 c.maxMints = burn.MAX_MINTS();209 c.maxMintsPerWallet = burn.MAX_MINTS_PER_WALLET();210 c.mintDuration = burn.MINT_DURATION();211 c.blockDelay = burn.BLOCK_DELAY();212 c.minMintsToInvite = burn.MIN_MINTS_TO_INVITE();213 c.directRewardBps = burn.DIRECT_REWARD_BPS();214 c.indirectRewardBps = burn.INDIRECT_REWARD_BPS();215 c.devRewardBps = burn.DEV_REWARD_BPS();216 c.lpReserve = burn.LP_RESERVE();217 c.totalSupplyCap = burn.TOTAL_SUPPLY();218 }220 function protocolState() external view returns (ProtocolState memory s) {221 s.phase = uint8(burn.currentPhase());222 s.blockNumber = block.number;223 s.timestamp = block.timestamp;225 s.mintCount = burn.mintCount();226 s.mintsRemaining = burn.mintsRemaining();227 s.mintDeadline = burn.mintDeadline();228 s.timeRemaining = burn.timeRemaining();229 s.nativeRaised = burn.ethRaised();230 s.totalReferralReward = burn.totalReferralReward();231 s.devReward = burn.devReward();233 s.burnTotalSupply = burn.totalSupply();234 s.burnBurned = burn.balanceOf(DEAD);236 s.finalized = hook.finalized();237 if (s.finalized) {238 s.tradingStartBlock = burn.bondedBlock() + burn.BLOCK_DELAY();240 IPoolManager pm = hook.poolManager();241 PoolId id = hook.poolId();242 (s.sqrtPriceX96,,,) = pm.getSlot0(id);243 s.poolLiquidity = pm.getLiquidity(id);244 s.nativePerBurn = _nativePerBurn(s.sqrtPriceX96);246 XBurn x = hook.xBurn();247 s.xBurnTotalSupply = x.totalSupply();248 s.dividendPool = x.dividendPool();250 address w = wxBurn;251 if (w != address(0)) {252 s.wxBurnTotalAssets = IERC4626(w).totalAssets();253 s.wxBurnTotalSupply = IERC20(w).totalSupply();254 s.vaultPendingDividend = x.pendingDividend(w) + x.dividendToken().balanceOf(w);255 }256 }257 }259 /// @notice Would `user.mint(inviter)` succeed right now? Mirrors BurnToken.mint's checks in order.260 /// @param inviter the referrer the user intends to pass; ignored when no inviter is needed.261 function mintStatus(address user, address inviter) external view returns (MintStatus memory m) {262 uint256 perWallet = burn.MAX_MINTS_PER_WALLET();263 m.value = burn.MINT_PRICE();264 m.walletMintsUsed = burn.mintsOf(user);266 uint256 walletLeft = m.walletMintsUsed >= perWallet ? 0 : perWallet - m.walletMintsUsed;267 m.walletMintsLeft = Math.min(walletLeft, burn.mintsRemaining());269 uint256 count = burn.mintCount();270 // An inviter is required only for a wallet's first mint, unless it is (or becomes) the first minter.271 m.inviterNeeded = count != 0 && user != burn.firstMinter() && burn.inviter(user) == address(0);272 m.inviterToPass = m.inviterNeeded ? inviter : address(0);274 if (burn.currentPhase() != IBurnToken.Phase.MINTING) {275 m.code = MINT_NOT_MINTING;276 } else if (!burn.hasAgentId(user)) {277 m.code = MINT_AGENT_ID_REQUIRED;278 } else if (walletLeft == 0) {279 m.code = MINT_WALLET_LIMIT;280 } else if (burn.mintsRemaining() == 0) {281 m.code = MINT_SOLD_OUT;282 } else if (m.inviterNeeded && inviter == address(0)) {283 m.code = MINT_INVITER_REQUIRED;284 } else if (m.inviterNeeded && inviter == user) {285 m.code = MINT_SELF_INVITE;286 } else if (m.inviterNeeded && !burn.canInvite(inviter)) {287 m.code = MINT_INVITER_NOT_QUALIFIED;288 } else if (user.balance < m.value) {289 m.code = MINT_INSUFFICIENT_BALANCE;290 } else {291 m.code = MINT_OK;292 }293 m.canMint = m.code == MINT_OK;294 m.reason = _mintReason(m.code);295 }297 function position(address user) external view returns (Position memory p) {298 IBurnToken.Phase phase = burn.currentPhase();300 p.nativeBalance = user.balance;301 p.burnBalance = burn.balanceOf(user);302 p.mintsOf = burn.mintsOf(user);303 p.inviter = burn.inviter(user);304 p.hasAgentId = burn.hasAgentId(user);305 p.canInvite = burn.canInvite(user);307 p.referralReward = burn.referralReward(user);308 p.referralClaimable = p.referralReward > 0309 && (phase == IBurnToken.Phase.MINT_SUCCESS || phase == IBurnToken.Phase.BONDED || phase == IBurnToken.Phase.TRADING);311 if (phase == IBurnToken.Phase.MINT_FAILED) {312 uint256 unit = burn.MINT_AMOUNT();313 uint256 units = p.burnBalance / unit;314 p.refundableBurn = units * unit;315 p.refundableNative = units * burn.MINT_PRICE();316 }318 XBurn x = hook.xBurn();319 if (address(x) != address(0)) {320 p.xBurnBalance = x.balanceOf(user);321 p.pendingDividend = x.pendingDividend(user);322 }323 address w = wxBurn;324 if (w != address(0)) {325 p.wxBurnBalance = IERC20(w).balanceOf(user);326 p.wxBurnAsXBurn = IERC4626(w).convertToAssets(p.wxBurnBalance);327 }328 }330 // ============ Internals ============332 /// @dev sqrtPriceX96 = sqrt(token1/token0)·2^96. Returns wei of wrapped native per 1e18 BURN.333 function _nativePerBurn(uint160 sqrtPriceX96) internal view returns (uint256) {334 if (sqrtPriceX96 == 0) return 0;335 uint256 p = uint256(sqrtPriceX96);336 uint256 q96 = 1 << 96;337 bool burnIsToken0 = address(burn) < address(hook.wrappedNative());338 return burnIsToken0339 ? Math.mulDiv(Math.mulDiv(1e18, p, q96), p, q96) // native/BURN = P^2 / 2^192340 : Math.mulDiv(Math.mulDiv(1e18, q96, p), q96, p); // native/BURN = 2^192 / P^2341 }343 function _mintReason(uint8 code) internal pure returns (string memory) {344 if (code == MINT_OK) return "OK";345 if (code == MINT_NOT_MINTING) return "NOT_MINTING";346 if (code == MINT_SOLD_OUT) return "SOLD_OUT";347 if (code == MINT_AGENT_ID_REQUIRED) return "AGENT_ID_REQUIRED";348 if (code == MINT_WALLET_LIMIT) return "WALLET_LIMIT";349 if (code == MINT_INVITER_REQUIRED) return "INVITER_REQUIRED";350 if (code == MINT_SELF_INVITE) return "SELF_INVITE";351 if (code == MINT_INVITER_NOT_QUALIFIED) return "INVITER_NOT_QUALIFIED";352 return "INSUFFICIENT_BALANCE";353 }354}