// SPDX-License-Identifier: MIT pragma solidity 0.8.26; import {LevelBase} from "../ILevel.sol"; /// @title Level 7: Fee Proxy /// @notice An upgradeable fee router. The proxy keeps the money and the admin role; /// the logic contract computes fees. The logic was initialized when it was deployed. contract PaymentProxy { address public implementation; address public admin; event Upgraded(address implementation); event Swept(address to, uint256 amount); constructor(address implementation_, address admin_) payable { implementation = implementation_; admin = admin_; } function upgrade(address implementation_) external { require(msg.sender == admin, "PaymentProxy: not admin"); implementation = implementation_; emit Upgraded(implementation_); } function sweep(address to) external { require(msg.sender == admin, "PaymentProxy: not admin"); uint256 amount = address(this).balance; (bool ok,) = to.call{value: amount}(""); require(ok, "PaymentProxy: transfer failed"); emit Swept(to, amount); } receive() external payable {} fallback() external payable { address impl = implementation; assembly { calldatacopy(0, 0, calldatasize()) let ok := delegatecall(gas(), impl, 0, calldatasize(), 0, 0) returndatacopy(0, 0, returndatasize()) switch ok case 0 { revert(0, returndatasize()) } default { return(0, returndatasize()) } } } } contract FeeRouterLogic { address public owner; uint256 public feeBps; bool public initialized; event Initialized(address owner, uint256 feeBps); function initialize(address owner_, uint256 feeBps_) external { require(!initialized, "FeeRouter: already initialized"); owner = owner_; feeBps = feeBps_; initialized = true; emit Initialized(owner_, feeBps_); } function setFee(uint256 feeBps_) external { require(msg.sender == owner, "FeeRouter: not owner"); require(feeBps_ <= 1000, "FeeRouter: fee too high"); feeBps = feeBps_; } function quote(uint256 amount) external view returns (uint256 fee, uint256 net) { fee = amount * feeBps / 10_000; net = amount - fee; } } contract FeeProxyLevel is LevelBase { FeeRouterLogic public immutable logic; constructor(address arena_) LevelBase(arena_) { logic = new FeeRouterLogic(); logic.initialize(address(this), 30); } function createInstance(address) external payable onlyArena returns (address) { if (msg.value == 0) revert DepositTooSmall(); return address(new PaymentProxy{value: msg.value}(address(logic), address(this))); } function validateInstance(address instance, address player) external view returns (bool) { // Only the proxy's own getters are read here, never anything routed through the fallback. PaymentProxy proxy = PaymentProxy(payable(instance)); return proxy.admin() == player && instance.balance == 0; } }