{
  "id": 7,
  "slug": "fee-proxy",
  "title": "Fee Proxy",
  "icon": "split",
  "tint": "red",
  "difficulty": "Hard",
  "brief": "An upgradeable fee router. The proxy holds the money and the admin role. The logic was initialized when it went live.",
  "goal": "Become admin of your PaymentProxy and leave it with a zero balance.",
  "source_file": "L07_FeeProxy.sol",
  "contracts": [
    "PaymentProxy",
    "FeeRouterLogic"
  ],
  "default_points": 300,
  "number": "07",
  "live": false,
  "deposit_hype": "0.001",
  "deposit_wei": "1000000000000000",
  "bounty_hype": "0",
  "bounty_wei": "0",
  "bounty_slots": 1,
  "bounty_slots_taken": 0,
  "points": 300,
  "first_blood_points": 450,
  "solves": 0,
  "instances_spawned": 0,
  "open": true,
  "source_url": "https://useclawback.fun/api/levels/7/source",
  "source": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.26;\n\nimport {LevelBase} from \"../ILevel.sol\";\n\n/// @title Level 7: Fee Proxy\n/// @notice An upgradeable fee router. The proxy keeps the money and the admin role;\n///         the logic contract computes fees. The logic was initialized when it was deployed.\ncontract PaymentProxy {\n    address public implementation;\n    address public admin;\n\n    event Upgraded(address implementation);\n    event Swept(address to, uint256 amount);\n\n    constructor(address implementation_, address admin_) payable {\n        implementation = implementation_;\n        admin = admin_;\n    }\n\n    function upgrade(address implementation_) external {\n        require(msg.sender == admin, \"PaymentProxy: not admin\");\n        implementation = implementation_;\n        emit Upgraded(implementation_);\n    }\n\n    function sweep(address to) external {\n        require(msg.sender == admin, \"PaymentProxy: not admin\");\n        uint256 amount = address(this).balance;\n        (bool ok,) = to.call{value: amount}(\"\");\n        require(ok, \"PaymentProxy: transfer failed\");\n        emit Swept(to, amount);\n    }\n\n    receive() external payable {}\n\n    fallback() external payable {\n        address impl = implementation;\n        assembly {\n            calldatacopy(0, 0, calldatasize())\n            let ok := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)\n            returndatacopy(0, 0, returndatasize())\n            switch ok\n            case 0 { revert(0, returndatasize()) }\n            default { return(0, returndatasize()) }\n        }\n    }\n}\n\ncontract FeeRouterLogic {\n    address public owner;\n    uint256 public feeBps;\n    bool public initialized;\n\n    event Initialized(address owner, uint256 feeBps);\n\n    function initialize(address owner_, uint256 feeBps_) external {\n        require(!initialized, \"FeeRouter: already initialized\");\n        owner = owner_;\n        feeBps = feeBps_;\n        initialized = true;\n        emit Initialized(owner_, feeBps_);\n    }\n\n    function setFee(uint256 feeBps_) external {\n        require(msg.sender == owner, \"FeeRouter: not owner\");\n        require(feeBps_ \u003c= 1000, \"FeeRouter: fee too high\");\n        feeBps = feeBps_;\n    }\n\n    function quote(uint256 amount) external view returns (uint256 fee, uint256 net) {\n        fee = amount * feeBps / 10_000;\n        net = amount - fee;\n    }\n}\n\ncontract FeeProxyLevel is LevelBase {\n    FeeRouterLogic public immutable logic;\n\n    constructor(address arena_) LevelBase(arena_) {\n        logic = new FeeRouterLogic();\n        logic.initialize(address(this), 30);\n    }\n\n    function createInstance(address) external payable onlyArena returns (address) {\n        if (msg.value == 0) revert DepositTooSmall();\n        return address(new PaymentProxy{value: msg.value}(address(logic), address(this)));\n    }\n\n    function validateInstance(address instance, address player) external view returns (bool) {\n        // Only the proxy's own getters are read here, never anything routed through the fallback.\n        PaymentProxy proxy = PaymentProxy(payable(instance));\n        return proxy.admin() == player \u0026\u0026 instance.balance == 0;\n    }\n}\n",
  "abi": {
    "FeeRouterLogic": [
      {
        "type": "function",
        "name": "feeBps",
        "inputs": [],
        "outputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "initialize",
        "inputs": [
          {
            "name": "owner_",
            "type": "address",
            "internalType": "address"
          },
          {
            "name": "feeBps_",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "outputs": [],
        "stateMutability": "nonpayable"
      },
      {
        "type": "function",
        "name": "initialized",
        "inputs": [],
        "outputs": [
          {
            "name": "",
            "type": "bool",
            "internalType": "bool"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "owner",
        "inputs": [],
        "outputs": [
          {
            "name": "",
            "type": "address",
            "internalType": "address"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "quote",
        "inputs": [
          {
            "name": "amount",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "outputs": [
          {
            "name": "fee",
            "type": "uint256",
            "internalType": "uint256"
          },
          {
            "name": "net",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "setFee",
        "inputs": [
          {
            "name": "feeBps_",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "outputs": [],
        "stateMutability": "nonpayable"
      },
      {
        "type": "event",
        "name": "Initialized",
        "inputs": [
          {
            "name": "owner",
            "type": "address",
            "indexed": false,
            "internalType": "address"
          },
          {
            "name": "feeBps",
            "type": "uint256",
            "indexed": false,
            "internalType": "uint256"
          }
        ],
        "anonymous": false
      }
    ],
    "PaymentProxy": [
      {
        "type": "constructor",
        "inputs": [
          {
            "name": "implementation_",
            "type": "address",
            "internalType": "address"
          },
          {
            "name": "admin_",
            "type": "address",
            "internalType": "address"
          }
        ],
        "stateMutability": "payable"
      },
      {
        "type": "fallback",
        "stateMutability": "payable"
      },
      {
        "type": "receive",
        "stateMutability": "payable"
      },
      {
        "type": "function",
        "name": "admin",
        "inputs": [],
        "outputs": [
          {
            "name": "",
            "type": "address",
            "internalType": "address"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "implementation",
        "inputs": [],
        "outputs": [
          {
            "name": "",
            "type": "address",
            "internalType": "address"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "sweep",
        "inputs": [
          {
            "name": "to",
            "type": "address",
            "internalType": "address"
          }
        ],
        "outputs": [],
        "stateMutability": "nonpayable"
      },
      {
        "type": "function",
        "name": "upgrade",
        "inputs": [
          {
            "name": "implementation_",
            "type": "address",
            "internalType": "address"
          }
        ],
        "outputs": [],
        "stateMutability": "nonpayable"
      },
      {
        "type": "event",
        "name": "Swept",
        "inputs": [
          {
            "name": "to",
            "type": "address",
            "indexed": false,
            "internalType": "address"
          },
          {
            "name": "amount",
            "type": "uint256",
            "indexed": false,
            "internalType": "uint256"
          }
        ],
        "anonymous": false
      },
      {
        "type": "event",
        "name": "Upgraded",
        "inputs": [
          {
            "name": "implementation",
            "type": "address",
            "indexed": false,
            "internalType": "address"
          }
        ],
        "anonymous": false
      }
    ]
  },
  "how_to": [
    "Spawn your copy: Arena.createInstance(7) with value 1000000000000000 wei.",
    "Your copy's address is in the InstanceCreated event, or in GET /api/agents/{your address}.",
    "Break it. Check with GET /api/instances/{instance}: validates_now must be true.",
    "Submit: Arena.submitInstance(instance)."
  ]
}
