// SPDX-License-Identifier: MIT pragma solidity 0.8.24; import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; /// @title Elysium City Fee Market vault: a builder points its sequencer fee rebates here and sells shares. /// Every HYPE that arrives is split pro rata across all shares; holders claim at any time. /// The builder keeps `builderShares`; the rest are sold at a fixed `price` per share, paid straight to the builder. contract RevenueShareVault is ERC20, ReentrancyGuard { uint256 private constant MAG = 2 ** 128; address public immutable builder; uint256 public immutable price; // wei per whole share (1e18 units) uint256 public sharesForSale; // units still for sale, held by this contract uint256 public totalReceived; uint256 private magPerShare; mapping(address => int256) private corrections; mapping(address => uint256) public withdrawn; event Received(address indexed from, uint256 amount); event Bought(address indexed buyer, uint256 shares, uint256 paid); event Claimed(address indexed holder, uint256 amount); constructor(string memory name_, string memory symbol_, uint256 totalShares, uint256 builderShares, uint256 price_, address builder_) ERC20(name_, symbol_) { require(totalShares > 0 && builderShares <= totalShares, "shares"); require(builder_ != address(0), "zero address"); builder = builder_; price = price_; _mint(builder_, builderShares * 1e18); sharesForSale = (totalShares - builderShares) * 1e18; _mint(address(this), sharesForSale); } /// Rebates (or anyone) pay in here. receive() external payable { _distribute(msg.value); } function _distribute(uint256 amount) internal { if (amount == 0) return; totalReceived += amount; magPerShare += (amount * MAG) / totalSupply(); emit Received(msg.sender, amount); } /// Buy `units` share units (1e18 = one share) at the fixed price. Proceeds go to the builder. function buy(uint256 units) external payable nonReentrant { require(units > 0 && units <= sharesForSale, "amount"); uint256 cost = (units * price) / 1e18; require(cost > 0 && msg.value == cost, "payment"); sharesForSale -= units; _transfer(address(this), msg.sender, units); (bool ok,) = payable(builder).call{value: msg.value}(""); require(ok, "pay builder"); emit Bought(msg.sender, units, msg.value); } function claimable(address holder) public view returns (uint256) { if (holder == address(this)) return 0; int256 acc = int256(magPerShare * balanceOf(holder)) + corrections[holder]; return uint256(acc) / MAG - withdrawn[holder]; } function claim() external nonReentrant { uint256 amount = claimable(msg.sender); require(amount > 0, "nothing"); withdrawn[msg.sender] += amount; (bool ok,) = payable(msg.sender).call{value: amount}(""); require(ok, "transfer failed"); emit Claimed(msg.sender, amount); } /// Unsold shares earn too; their part goes to the builder. function claimUnsold() external nonReentrant { int256 acc = int256(magPerShare * balanceOf(address(this))) + corrections[address(this)]; uint256 amount = uint256(acc) / MAG - withdrawn[address(this)]; require(amount > 0, "nothing"); withdrawn[address(this)] += amount; (bool ok,) = payable(builder).call{value: amount}(""); require(ok, "transfer failed"); emit Claimed(builder, amount); } function _update(address from, address to, uint256 value) internal override { // Shares sent back to the vault would be stranded; only the constructor mints to it. require(to != address(this) || from == address(0), "vault cannot receive shares"); super._update(from, to, value); int256 c = int256(magPerShare * value); if (from != address(0)) corrections[from] += c; if (to != address(0)) corrections[to] -= c; } }