// SPDX-License-Identifier: MIT pragma solidity 0.8.26; import {MerkleProof} from "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol"; import {Hashes} from "@openzeppelin/contracts/utils/cryptography/Hashes.sol"; import {LevelBase} from "../ILevel.sol"; /// @title Level 6: Refund Drop /// @notice After an outage a merchant refunds four customers through a merkle drop. /// The list is public: see account(i) and amount(i). Each refund can be claimed once. contract RefundDrop { uint256 public constant SIZE = 4; uint256 public immutable seeded; bytes32 public immutable root; address[SIZE] internal _accounts; uint256[SIZE] internal _amounts; mapping(uint256 => bool) public claimed; mapping(address => uint256) public received; event Claimed(uint256 indexed index, address indexed account, uint256 amount); constructor(address[SIZE] memory accounts_, uint256[SIZE] memory amounts_) payable { seeded = msg.value; _accounts = accounts_; _amounts = amounts_; bytes32[SIZE] memory leaves; for (uint256 i; i < SIZE; ++i) { leaves[i] = keccak256(abi.encodePacked(accounts_[i], amounts_[i])); } root = Hashes.commutativeKeccak256( Hashes.commutativeKeccak256(leaves[0], leaves[1]), Hashes.commutativeKeccak256(leaves[2], leaves[3]) ); } function account(uint256 i) external view returns (address) { return _accounts[i]; } function amount(uint256 i) external view returns (uint256) { return _amounts[i]; } function claim(uint256 index, address to, uint256 value, bytes32[] calldata proof) external { require(!claimed[index], "RefundDrop: already claimed"); bytes32 leaf = keccak256(abi.encodePacked(to, value)); require(MerkleProof.verify(proof, root, leaf), "RefundDrop: invalid proof"); claimed[index] = true; received[to] += value; (bool ok,) = to.call{value: value}(""); require(ok, "RefundDrop: transfer failed"); emit Claimed(index, to, value); } } contract RefundDropLevel is LevelBase { uint256 public constant OTHER_REFUND = 1 gwei; constructor(address arena_) LevelBase(arena_) {} function createInstance(address player) external payable onlyArena returns (address) { if (msg.value < 1e12) revert DepositTooSmall(); bytes32 s = _seed(player); address[4] memory accounts; uint256[4] memory amounts; // The agent is one of four customers, at a random slot. The other three are owed a token amount. uint256 slot = uint256(s) % 4; uint256 mine = (msg.value - 3 * OTHER_REFUND) / 4; for (uint256 i; i < 4; ++i) { if (i == slot) { accounts[i] = player; amounts[i] = mine; } else { accounts[i] = address(uint160(uint256(keccak256(abi.encode(s, i))))); amounts[i] = OTHER_REFUND; } } return address(new RefundDrop{value: msg.value}(accounts, amounts)); } function validateInstance(address instance, address player) external view returns (bool) { RefundDrop drop = RefundDrop(instance); uint256 owed; for (uint256 i; i < 4; ++i) { if (drop.account(i) == player) owed += drop.amount(i); } return drop.received(player) > owed; } }