🔥
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/XBurn.sol
xBURN: burn BURN 1:1, earn trading dividends
195 linessolc 0.8.26 · via-ir · optimizer 200sha256 a9b521022c13bc8b…Not deployed yet
1// SPDX-License-Identifier: MIT2pragma solidity ^0.8.26;4import {ERC4626} from "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol";5import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";6import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";7import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";8import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";9import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";10import {IXBurn} from "./interfaces/IXBurn.sol";11import {IBurnToken} from "./interfaces/IBurnToken.sol";12import {BurnToken} from "./BurnToken.sol";14/// @title XBurn15/// @notice Dividend receipt token + ERC4626-compatible wrapper.16/// @dev17/// ─── ERC4626 mapping ───18/// asset() = BURN (i.e. _asset)19/// shares (xBURN) = 1:1 with the deposited BURN20/// totalAssets() = totalSupply() (1:1 conceptual BURN backing)21/// ┗ the actual BURN has been transferred to DEAD, so the vault's own balance is 0;22/// using totalSupply as the backing keeps the 4626 math always 1:123/// maxWithdraw / maxRedeem = 0 (one-way burn, not redeemable)24///25/// ─── Dividend (decoupled from 4626) ───26/// MasterChef accumulator:27/// · notifyDividend(amount) the hook feeds the fee into accDividendPerShare28/// · claim(xBurnAmount) settles all pending + burns xBurnAmount29/// · _update mint/burn/transfer automatically _settle both sides30///31/// bootstrap: if there is an orphan dividend before the first deposit (a fee received while totalSupply=0),32/// the first depositor absorbs it all at once, implemented by adjusting accDividendPerShare post-mint.33contract XBurn is ERC4626, ReentrancyGuard, IXBurn {34 using SafeERC20 for IERC20;35 using Math for uint256;37 BurnToken public immutable burn;38 IERC20 public immutable override dividendToken;39 address public immutable hook;41 uint256 public override dividendPool;42 uint256 public accDividendPerShare;43 mapping(address => uint256) public rewardDebt;45 uint256 private constant ACC_PRECISION = 1e18;46 address public constant DEAD = 0x000000000000000000000000000000000000dEaD;48 error NotHook();49 error ZeroAmount();50 error TradingNotOpen();52 event DividendSettled(address indexed user, uint256 amount);54 constructor(address _burn, address _dividendToken, address _hook)55 ERC4626(IERC20(_burn))56 ERC20("Burned BURN Share", "xBURN")57 {58 require(_burn != address(0) && _dividendToken != address(0) && _hook != address(0), "zero addr");59 burn = BurnToken(payable(_burn));60 dividendToken = IERC20(_dividendToken);61 hook = _hook;62 }64 // ============ ERC4626 overrides ============66 function totalAssets() public view override returns (uint256) {67 return totalSupply();68 }70 function maxWithdraw(address) public pure override returns (uint256) {71 return 0;72 }74 function maxRedeem(address) public pure override returns (uint256) {75 return 0;76 }78 /// @dev EIP-4626 recommendation: when the user cannot withdraw, preview also returns 0 to avoid misleading the UI.79 function previewWithdraw(uint256) public pure override returns (uint256) {80 return 0;81 }83 function previewRedeem(uint256) public pure override returns (uint256) {84 return 0;85 }87 /// @dev OZ's _deposit defaults to transferFrom(caller, address(this), assets) + _mint(receiver, shares).88 /// We change it to: transferFrom(caller, DEAD, assets) + bootstrap orphan + _mint.89 /// _mint triggers _update → _settle, locking in the old rewardDebt before acc is adjusted.90 function _deposit(address caller, address receiver, uint256 assets, uint256 shares) internal override {91 if (assets == 0) revert ZeroAmount();92 // Deposits open as soon as the pool exists (BONDED), i.e. during the launch cooldown, so that93 // minters can already hold xBURN when the first swap happens and its fee is shared pro rata94 // instead of going to whoever deposits first after trading opens.95 IBurnToken.Phase phase = burn.currentPhase();96 if (phase != IBurnToken.Phase.BONDED && phase != IBurnToken.Phase.TRADING) revert TradingNotOpen();98 bool bootstrap = (totalSupply() == 0 && dividendPool > 0);99 uint256 orphan = bootstrap ? dividendPool : 0;101 IERC20(asset()).safeTransferFrom(caller, DEAD, assets);102 _mint(receiver, shares);104 if (bootstrap) {105 // Key point: use += not =.106 // On the "supply goes nonzero → 0 → nonzero" path, the old accDividendPerShare is still > 0107 // (accumulated by past notify calls while supply>0). During _mint, _update has already108 // set rewardDebt(caller) = shares · old_acc / 1e18.109 // Overwriting directly with "=" would, when new_acc < old_acc, make the later _settle compute110 // pending = shares·new_acc - shares·old_acc and underflow-revert.111 // Using "+=" instead: new_acc = old_acc + orphan·1e18/shares,112 // pending(caller) = shares·new_acc - shares·old_acc = orphan ✓113 accDividendPerShare += orphan.mulDiv(ACC_PRECISION, assets);114 }116 emit Deposit(caller, receiver, assets, shares);117 emit Entered(receiver, assets, shares); // legacy IXBurn event118 }120 /// @dev The public 4626 entry points add nonReentrant to prevent the dividend token from re-entering during the _settle phase.121 function deposit(uint256 assets, address receiver) public override nonReentrant returns (uint256) {122 return super.deposit(assets, receiver);123 }125 function mint(uint256 shares, address receiver) public override nonReentrant returns (uint256) {126 return super.mint(shares, receiver);127 }129 // ============ IXBurn legacy API ============131 /// @notice Legacy API, equivalent to `deposit(burnAmount, msg.sender)`.132 function enter(uint256 burnAmount) external override {133 deposit(burnAmount, msg.sender);134 }136 /// @notice Pushes msg.sender's accumulated pending dividend, and along the way burns `xBurnAmount` to give up the right to future dividends.137 function claim(uint256 xBurnAmount) external override nonReentrant {138 if (xBurnAmount == 0) revert ZeroAmount();139 uint256 paid = _pendingDividend(msg.sender, balanceOf(msg.sender));140 _burn(msg.sender, xBurnAmount);141 emit Claimed(msg.sender, xBurnAmount, paid);142 }144 function notifyDividend(uint256 amount) external override {145 if (msg.sender != hook) revert NotHook();147 uint256 supply = totalSupply();148 if (supply > 0) {149 accDividendPerShare += amount.mulDiv(ACC_PRECISION, supply);150 }151 unchecked { dividendPool += amount; }152 emit DividendReceived(amount, dividendPool);153 }155 // ============ Settlement plumbing ============157 function _pendingDividend(address user, uint256 bal) internal view returns (uint256) {158 return bal.mulDiv(accDividendPerShare, ACC_PRECISION) - rewardDebt[user];159 }161 function _settle(address user) internal {162 uint256 bal = balanceOf(user);163 uint256 pending = _pendingDividend(user, bal);164 if (pending > 0) {165 dividendPool -= pending;166 dividendToken.safeTransfer(user, pending);167 emit DividendSettled(user, pending);168 }169 rewardDebt[user] = bal.mulDiv(accDividendPerShare, ACC_PRECISION);170 }172 function _update(address from, address to, uint256 value) internal override {173 if (from != address(0)) _settle(from);174 if (to != address(0) && to != from) _settle(to);176 super._update(from, to, value);178 if (from != address(0)) {179 rewardDebt[from] = balanceOf(from).mulDiv(accDividendPerShare, ACC_PRECISION);180 }181 if (to != address(0) && to != from) {182 rewardDebt[to] = balanceOf(to).mulDiv(accDividendPerShare, ACC_PRECISION);183 }184 }186 // ============ Views ============188 function pendingDividend(address user) external view override returns (uint256) {189 return _pendingDividend(user, balanceOf(user));190 }192 function dividendPerShare() external view override returns (uint256) {193 return accDividendPerShare;194 }195}