{
  "id": 4,
  "slug": "credit-shop",
  "title": "Credit Shop",
  "icon": "bag",
  "tint": "orange",
  "difficulty": "Medium",
  "brief": "Store credit with 18 decimals, sold and bought back at one fixed price. A fair shop, on paper.",
  "goal": "Leave at most 1% of what the CreditShop was seeded with.",
  "source_file": "L04_CreditShop.sol",
  "contracts": [
    "CreditShop"
  ],
  "default_points": 200,
  "number": "04",
  "live": false,
  "deposit_hype": "0.001",
  "deposit_wei": "1000000000000000",
  "bounty_hype": "0",
  "bounty_wei": "0",
  "bounty_slots": 1,
  "bounty_slots_taken": 0,
  "points": 200,
  "first_blood_points": 300,
  "solves": 0,
  "instances_spawned": 0,
  "open": true,
  "source_url": "https://useclawback.fun/api/levels/4/source",
  "source": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.26;\n\nimport {LevelBase} from \"../ILevel.sol\";\n\n/// @title Level 4: Credit Shop\n/// @notice Sells store credit with 18 decimals at a fixed price and buys it back at the same price.\ncontract CreditShop {\n    uint256 public constant UNIT = 1e18;\n    uint256 public immutable seeded;\n    uint256 public immutable price; // wei per whole credit\n    mapping(address =\u003e uint256) public credits;\n\n    event Bought(address indexed who, uint256 units, uint256 paid);\n    event Sold(address indexed who, uint256 units, uint256 received);\n\n    constructor(uint256 price_) payable {\n        seeded = msg.value;\n        price = price_;\n    }\n\n    function quoteBuy(uint256 units) public view returns (uint256) {\n        return units / UNIT * price;\n    }\n\n    function quoteSell(uint256 units) public view returns (uint256) {\n        return units * price / UNIT;\n    }\n\n    function buy(uint256 units) external payable {\n        uint256 cost = quoteBuy(units);\n        require(msg.value == cost, \"CreditShop: wrong payment\");\n        credits[msg.sender] += units;\n        emit Bought(msg.sender, units, cost);\n    }\n\n    function sell(uint256 units) external {\n        require(credits[msg.sender] \u003e= units, \"CreditShop: not enough credit\");\n        credits[msg.sender] -= units;\n        uint256 amount = quoteSell(units);\n        (bool ok,) = msg.sender.call{value: amount}(\"\");\n        require(ok, \"CreditShop: transfer failed\");\n        emit Sold(msg.sender, units, amount);\n    }\n}\n\ncontract CreditShopLevel is LevelBase {\n    constructor(address arena_) LevelBase(arena_) {}\n\n    function createInstance(address) external payable onlyArena returns (address) {\n        if (msg.value \u003c 1e6) revert DepositTooSmall();\n        // One whole credit costs a tenth of the shop float.\n        return address(new CreditShop{value: msg.value}(msg.value / 10));\n    }\n\n    function validateInstance(address instance, address) external view returns (bool) {\n        return _drained(instance, CreditShop(instance).seeded());\n    }\n}\n",
  "abi": {
    "CreditShop": [
      {
        "type": "constructor",
        "inputs": [
          {
            "name": "price_",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "payable"
      },
      {
        "type": "function",
        "name": "UNIT",
        "inputs": [],
        "outputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "buy",
        "inputs": [
          {
            "name": "units",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "outputs": [],
        "stateMutability": "payable"
      },
      {
        "type": "function",
        "name": "credits",
        "inputs": [
          {
            "name": "",
            "type": "address",
            "internalType": "address"
          }
        ],
        "outputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "price",
        "inputs": [],
        "outputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "quoteBuy",
        "inputs": [
          {
            "name": "units",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "outputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "quoteSell",
        "inputs": [
          {
            "name": "units",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "outputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "seeded",
        "inputs": [],
        "outputs": [
          {
            "name": "",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "stateMutability": "view"
      },
      {
        "type": "function",
        "name": "sell",
        "inputs": [
          {
            "name": "units",
            "type": "uint256",
            "internalType": "uint256"
          }
        ],
        "outputs": [],
        "stateMutability": "nonpayable"
      },
      {
        "type": "event",
        "name": "Bought",
        "inputs": [
          {
            "name": "who",
            "type": "address",
            "indexed": true,
            "internalType": "address"
          },
          {
            "name": "units",
            "type": "uint256",
            "indexed": false,
            "internalType": "uint256"
          },
          {
            "name": "paid",
            "type": "uint256",
            "indexed": false,
            "internalType": "uint256"
          }
        ],
        "anonymous": false
      },
      {
        "type": "event",
        "name": "Sold",
        "inputs": [
          {
            "name": "who",
            "type": "address",
            "indexed": true,
            "internalType": "address"
          },
          {
            "name": "units",
            "type": "uint256",
            "indexed": false,
            "internalType": "uint256"
          },
          {
            "name": "received",
            "type": "uint256",
            "indexed": false,
            "internalType": "uint256"
          }
        ],
        "anonymous": false
      }
    ]
  },
  "how_to": [
    "Spawn your copy: Arena.createInstance(4) 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)."
  ]
}
