// SPDX-License-Identifier: MIT pragma solidity 0.8.26; import {LevelBase} from "../ILevel.sol"; /// @title Level 4: Credit Shop /// @notice Sells store credit with 18 decimals at a fixed price and buys it back at the same price. contract CreditShop { uint256 public constant UNIT = 1e18; uint256 public immutable seeded; uint256 public immutable price; // wei per whole credit mapping(address => uint256) public credits; event Bought(address indexed who, uint256 units, uint256 paid); event Sold(address indexed who, uint256 units, uint256 received); constructor(uint256 price_) payable { seeded = msg.value; price = price_; } function quoteBuy(uint256 units) public view returns (uint256) { return units / UNIT * price; } function quoteSell(uint256 units) public view returns (uint256) { return units * price / UNIT; } function buy(uint256 units) external payable { uint256 cost = quoteBuy(units); require(msg.value == cost, "CreditShop: wrong payment"); credits[msg.sender] += units; emit Bought(msg.sender, units, cost); } function sell(uint256 units) external { require(credits[msg.sender] >= units, "CreditShop: not enough credit"); credits[msg.sender] -= units; uint256 amount = quoteSell(units); (bool ok,) = msg.sender.call{value: amount}(""); require(ok, "CreditShop: transfer failed"); emit Sold(msg.sender, units, amount); } } contract CreditShopLevel is LevelBase { constructor(address arena_) LevelBase(arena_) {} function createInstance(address) external payable onlyArena returns (address) { if (msg.value < 1e6) revert DepositTooSmall(); // One whole credit costs a tenth of the shop float. return address(new CreditShop{value: msg.value}(msg.value / 10)); } function validateInstance(address instance, address) external view returns (bool) { return _drained(instance, CreditShop(instance).seeded()); } }