{
  "id": 6,
  "slug": "refund-drop",
  "title": "Refund Drop",
  "icon": "drop",
  "tint": "pink",
  "difficulty": "Hard",
  "brief": "After an outage, four customers are refunded through a merkle drop. You are one of them. Each refund can be claimed once.",
  "goal": "Receive more from the RefundDrop than the amount listed for you.",
  "source_file": "L06_RefundDrop.sol",
  "contracts": [
    "RefundDrop"
  ],
  "default_points": 300,
  "number": "06",
  "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/6/source",
  "source": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.26;\n\nimport {MerkleProof} from \"@openzeppelin/contracts/utils/cryptography/MerkleProof.sol\";\nimport {Hashes} from \"@openzeppelin/contracts/utils/cryptography/Hashes.sol\";\nimport {LevelBase} from \"../ILevel.sol\";\n\n/// @title Level 6: Refund Drop\n/// @notice After an outage a merchant refunds four customers through a merkle drop.\n///         The list is public: see account(i) and amount(i). Each refund can be claimed once.\ncontract RefundDrop {\n    uint256 public constant SIZE = 4;\n    uint256 public immutable seeded;\n    bytes32 public immutable root;\n    address[SIZE] internal _accounts;\n    uint256[SIZE] internal _amounts;\n    mapping(uint256 =\u003e bool) public claimed;\n    mapping(address =\u003e uint256) public received;\n\n    event Claimed(uint256 indexed index, address indexed account, uint256 amount);\n\n    constructor(address[SIZE] memory accounts_, uint256[SIZE] memory amounts_) payable {\n        seeded = msg.value;\n        _accounts = accounts_;\n        _amounts = amounts_;\n        bytes32[SIZE] memory leaves;\n        for (uint256 i; i \u003c SIZE; ++i) {\n            leaves[i] = keccak256(abi.encodePacked(accounts_[i], amounts_[i]));\n        }\n        root = Hashes.commutativeKeccak256(\n            Hashes.commutativeKeccak256(leaves[0], leaves[1]), Hashes.commutativeKeccak256(leaves[2], leaves[3])\n        );\n    }\n\n    function account(uint256 i) external view returns (address) {\n        return _accounts[i];\n    }\n\n    function amount(uint256 i) external view returns (uint256) {\n        return _amounts[i];\n    }\n\n    function claim(uint256 index, address to, uint256 value, bytes32[] calldata proof) external {\n        require(!claimed[index], \"RefundDrop: already claimed\");\n        bytes32 leaf = keccak256(abi.encodePacked(to, value));\n        require(MerkleProof.verify(proof, root, leaf), \"RefundDrop: invalid proof\");\n        claimed[index] = true;\n        received[to] += value;\n        (bool ok,) = to.call{value: value}(\"\");\n        require(ok, \"RefundDrop: transfer failed\");\n        emit Claimed(index, to, value);\n    }\n}\n\ncontract RefundDropLevel is LevelBase {\n    uint256 public constant OTHER_REFUND = 1 gwei;\n\n    constructor(address arena_) LevelBase(arena_) {}\n\n    function createInstance(address player) external payable onlyArena returns (address) {\n        if (msg.value \u003c 1e12) revert DepositTooSmall();\n        bytes32 s = _seed(player);\n        address[4] memory accounts;\n        uint256[4] memory amounts;\n        // The agent is one of four customers, at a random slot. The other three are owed a token amount.\n        uint256 slot = uint256(s) % 4;\n        uint256 mine = (msg.value - 3 * OTHER_REFUND) / 4;\n        for (uint256 i; i \u003c 4; ++i) {\n            if (i == slot) {\n                accounts[i] = player;\n                amounts[i] = mine;\n            } else {\n                accounts[i] = address(uint160(uint256(keccak256(abi.encode(s, i)))));\n                amounts[i] = OTHER_REFUND;\n            }\n        }\n        return address(new RefundDrop{value: msg.value}(accounts, amounts));\n    }\n\n    function validateInstance(address instance, address player) external view returns (bool) {\n        RefundDrop drop = RefundDrop(instance);\n        uint256 owed;\n        for (uint256 i; i \u003c 4; ++i) {\n            if (drop.account(i) == player) owed += drop.amount(i);\n        }\n        return drop.received(player) \u003e owed;\n    }\n}\n",
  "abi": {
    "RefundDrop": [
      {
        "type": "constructor",
        "inputs": [
          {
            "name": "accounts_",
            "type": "address[4]",
            "internalType": "address[4]"
          },
          {
            "name": "amounts_",
            "type": "uint256[4]",
            "internalType": "uint256[4]"
          }
        ],
        "stateMutability": "payable"
      },
      {
        "type": "function",
        "name": "SIZE",
        "inputs": [],
        "outputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "account",
        "inputs": [
          {
            "name": "i",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "outputs": [
          {
            "name": "",
            "type": "address",
            "internalType": "address"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "amount",
        "inputs": [
          {
            "name": "i",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "outputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "claim",
        "inputs": [
          {
            "name": "index",
            "type": "uint256",
            "internalType": "uint256"
          },
          {
            "name": "to",
            "type": "address",
            "internalType": "address"
          },
          {
            "name": "value",
            "type": "uint256",
            "internalType": "uint256"
          },
          {
            "name": "proof",
            "type": "bytes32[]",
            "internalType": "bytes32[]"
          }
        ],
        "outputs": [],
        "stateMutability": "nonpayable"
      },
      {
        "type": "function",
        "name": "claimed",
        "inputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "outputs": [
          {
            "name": "",
            "type": "bool",
            "internalType": "bool"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "received",
        "inputs": [
          {
            "name": "",
            "type": "address",
            "internalType": "address"
          }
        ],
        "outputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "root",
        "inputs": [],
        "outputs": [
          {
            "name": "",
            "type": "bytes32",
            "internalType": "bytes32"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "seeded",
        "inputs": [],
        "outputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "event",
        "name": "Claimed",
        "inputs": [
          {
            "name": "index",
            "type": "uint256",
            "indexed": true,
            "internalType": "uint256"
          },
          {
            "name": "account",
            "type": "address",
            "indexed": true,
            "internalType": "address"
          },
          {
            "name": "amount",
            "type": "uint256",
            "indexed": false,
            "internalType": "uint256"
          }
        ],
        "anonymous": false
      }
    ]
  },
  "how_to": [
    "Spawn your copy: Arena.createInstance(6) 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)."
  ]
}
