ArenaLevel 04Medium
Credit Shop
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.
- Points
- 200
- First blood
- 300
- Deposit
- 0.001 HYPE
- Bounty
- Points only
- Paid slots left
- None
- Solves
- 0
1// SPDX-License-Identifier: MIT2pragma solidity 0.8.26;34import {LevelBase} from "../ILevel.sol";56/// @title Level 4: Credit Shop7/// @notice Sells store credit with 18 decimals at a fixed price and buys it back at the same price.8contract CreditShop {9 uint256 public constant UNIT = 1e18;10 uint256 public immutable seeded;11 uint256 public immutable price; // wei per whole credit12 mapping(address => uint256) public credits;1314 event Bought(address indexed who, uint256 units, uint256 paid);15 event Sold(address indexed who, uint256 units, uint256 received);1617 constructor(uint256 price_) payable {18 seeded = msg.value;19 price = price_;20 }2122 function quoteBuy(uint256 units) public view returns (uint256) {23 return units / UNIT * price;24 }2526 function quoteSell(uint256 units) public view returns (uint256) {27 return units * price / UNIT;28 }2930 function buy(uint256 units) external payable {31 uint256 cost = quoteBuy(units);32 require(msg.value == cost, "CreditShop: wrong payment");33 credits[msg.sender] += units;34 emit Bought(msg.sender, units, cost);35 }3637 function sell(uint256 units) external {38 require(credits[msg.sender] >= units, "CreditShop: not enough credit");39 credits[msg.sender] -= units;40 uint256 amount = quoteSell(units);41 (bool ok,) = msg.sender.call{value: amount}("");42 require(ok, "CreditShop: transfer failed");43 emit Sold(msg.sender, units, amount);44 }45}4647contract CreditShopLevel is LevelBase {48 constructor(address arena_) LevelBase(arena_) {}4950 function createInstance(address) external payable onlyArena returns (address) {51 if (msg.value < 1e6) revert DepositTooSmall();52 // One whole credit costs a tenth of the shop float.53 return address(new CreditShop{value: msg.value}(msg.value / 10));54 }5556 function validateInstance(address instance, address) external view returns (bool) {57 return _drained(instance, CreditShop(instance).seeded());58 }59}