// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; import {ERC4626} from "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol"; import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; import {Math} from "@openzeppelin/contracts/utils/math/Math.sol"; import {IXBurn} from "./interfaces/IXBurn.sol"; import {IBurnToken} from "./interfaces/IBurnToken.sol"; import {BurnToken} from "./BurnToken.sol"; /// @title XBurn /// @notice Dividend receipt token + ERC4626-compatible wrapper. /// @dev /// ─── ERC4626 mapping ─── /// asset() = BURN (i.e. _asset) /// shares (xBURN) = 1:1 with the deposited BURN /// totalAssets() = totalSupply() (1:1 conceptual BURN backing) /// ┗ the actual BURN has been transferred to DEAD, so the vault's own balance is 0; /// using totalSupply as the backing keeps the 4626 math always 1:1 /// maxWithdraw / maxRedeem = 0 (one-way burn, not redeemable) /// /// ─── Dividend (decoupled from 4626) ─── /// MasterChef accumulator: /// · notifyDividend(amount) the hook feeds the fee into accDividendPerShare /// · claim(xBurnAmount) settles all pending + burns xBurnAmount /// · _update mint/burn/transfer automatically _settle both sides /// /// bootstrap: if there is an orphan dividend before the first deposit (a fee received while totalSupply=0), /// the first depositor absorbs it all at once, implemented by adjusting accDividendPerShare post-mint. contract XBurn is ERC4626, ReentrancyGuard, IXBurn { using SafeERC20 for IERC20; using Math for uint256; BurnToken public immutable burn; IERC20 public immutable override dividendToken; address public immutable hook; uint256 public override dividendPool; uint256 public accDividendPerShare; mapping(address => uint256) public rewardDebt; uint256 private constant ACC_PRECISION = 1e18; address public constant DEAD = 0x000000000000000000000000000000000000dEaD; error NotHook(); error ZeroAmount(); error TradingNotOpen(); event DividendSettled(address indexed user, uint256 amount); constructor(address _burn, address _dividendToken, address _hook) ERC4626(IERC20(_burn)) ERC20("Burned BURN Share", "xBURN") { require(_burn != address(0) && _dividendToken != address(0) && _hook != address(0), "zero addr"); burn = BurnToken(payable(_burn)); dividendToken = IERC20(_dividendToken); hook = _hook; } // ============ ERC4626 overrides ============ function totalAssets() public view override returns (uint256) { return totalSupply(); } function maxWithdraw(address) public pure override returns (uint256) { return 0; } function maxRedeem(address) public pure override returns (uint256) { return 0; } /// @dev EIP-4626 recommendation: when the user cannot withdraw, preview also returns 0 to avoid misleading the UI. function previewWithdraw(uint256) public pure override returns (uint256) { return 0; } function previewRedeem(uint256) public pure override returns (uint256) { return 0; } /// @dev OZ's _deposit defaults to transferFrom(caller, address(this), assets) + _mint(receiver, shares). /// We change it to: transferFrom(caller, DEAD, assets) + bootstrap orphan + _mint. /// _mint triggers _update → _settle, locking in the old rewardDebt before acc is adjusted. function _deposit(address caller, address receiver, uint256 assets, uint256 shares) internal override { if (assets == 0) revert ZeroAmount(); // Deposits open as soon as the pool exists (BONDED), i.e. during the launch cooldown, so that // minters can already hold xBURN when the first swap happens and its fee is shared pro rata // instead of going to whoever deposits first after trading opens. IBurnToken.Phase phase = burn.currentPhase(); if (phase != IBurnToken.Phase.BONDED && phase != IBurnToken.Phase.TRADING) revert TradingNotOpen(); bool bootstrap = (totalSupply() == 0 && dividendPool > 0); uint256 orphan = bootstrap ? dividendPool : 0; IERC20(asset()).safeTransferFrom(caller, DEAD, assets); _mint(receiver, shares); if (bootstrap) { // Key point: use += not =. // On the "supply goes nonzero → 0 → nonzero" path, the old accDividendPerShare is still > 0 // (accumulated by past notify calls while supply>0). During _mint, _update has already // set rewardDebt(caller) = shares · old_acc / 1e18. // Overwriting directly with "=" would, when new_acc < old_acc, make the later _settle compute // pending = shares·new_acc - shares·old_acc and underflow-revert. // Using "+=" instead: new_acc = old_acc + orphan·1e18/shares, // pending(caller) = shares·new_acc - shares·old_acc = orphan ✓ accDividendPerShare += orphan.mulDiv(ACC_PRECISION, assets); } emit Deposit(caller, receiver, assets, shares); emit Entered(receiver, assets, shares); // legacy IXBurn event } /// @dev The public 4626 entry points add nonReentrant to prevent the dividend token from re-entering during the _settle phase. function deposit(uint256 assets, address receiver) public override nonReentrant returns (uint256) { return super.deposit(assets, receiver); } function mint(uint256 shares, address receiver) public override nonReentrant returns (uint256) { return super.mint(shares, receiver); } // ============ IXBurn legacy API ============ /// @notice Legacy API, equivalent to `deposit(burnAmount, msg.sender)`. function enter(uint256 burnAmount) external override { deposit(burnAmount, msg.sender); } /// @notice Pushes msg.sender's accumulated pending dividend, and along the way burns `xBurnAmount` to give up the right to future dividends. function claim(uint256 xBurnAmount) external override nonReentrant { if (xBurnAmount == 0) revert ZeroAmount(); uint256 paid = _pendingDividend(msg.sender, balanceOf(msg.sender)); _burn(msg.sender, xBurnAmount); emit Claimed(msg.sender, xBurnAmount, paid); } function notifyDividend(uint256 amount) external override { if (msg.sender != hook) revert NotHook(); uint256 supply = totalSupply(); if (supply > 0) { accDividendPerShare += amount.mulDiv(ACC_PRECISION, supply); } unchecked { dividendPool += amount; } emit DividendReceived(amount, dividendPool); } // ============ Settlement plumbing ============ function _pendingDividend(address user, uint256 bal) internal view returns (uint256) { return bal.mulDiv(accDividendPerShare, ACC_PRECISION) - rewardDebt[user]; } function _settle(address user) internal { uint256 bal = balanceOf(user); uint256 pending = _pendingDividend(user, bal); if (pending > 0) { dividendPool -= pending; dividendToken.safeTransfer(user, pending); emit DividendSettled(user, pending); } rewardDebt[user] = bal.mulDiv(accDividendPerShare, ACC_PRECISION); } function _update(address from, address to, uint256 value) internal override { if (from != address(0)) _settle(from); if (to != address(0) && to != from) _settle(to); super._update(from, to, value); if (from != address(0)) { rewardDebt[from] = balanceOf(from).mulDiv(accDividendPerShare, ACC_PRECISION); } if (to != address(0) && to != from) { rewardDebt[to] = balanceOf(to).mulDiv(accDividendPerShare, ACC_PRECISION); } } // ============ Views ============ function pendingDividend(address user) external view override returns (uint256) { return _pendingDividend(user, balanceOf(user)); } function dividendPerShare() external view override returns (uint256) { return accDividendPerShare; } }