Verified contractPartialMIT
WeiBoard
Address0xa1ab6880beec5937d59b8cebbd1ccc7376ee4f32
- Balance
0.030002 ETH
- Transactions
4
- Compiler
solc 0.8.28
- Same code
Only this contract
Details
- Compiler
- solc 0.8.28+commit.7893614a
- Language
- solidity
- Optimization
- Yes, 10,000 runs
- EVM version
- cancun
- License
- MIT
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,104,381
- Creation transaction
- 0x85d283e8d8…d6715bb4c6
src/WeiBoard.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
/// @title WeiBoard
/// @notice A 100 × 100 grid of blocks (10 × 10 px each). Rectangular plots are claimed once and
/// are then always for sale at the price their owner sets. Owners pay a fee on that price
/// from a prepaid ETH deposit. A plot whose deposit has run out is foreclosed: the next
/// buyer pays the minimum price, to the treasury.
/// @dev No proxy, no admin powers over plots. The treasury address can only withdraw the
/// treasury balance and hand that role over. Sale proceeds are credited and withdrawn
/// (pull payments) so a seller that rejects ETH can never block a sale.
/// Narrowing casts (uint96 prices/deposits, uint40 timestamps, uint128 row masks) are
/// always preceded by a bounds check: `_checkTerms`, the rectangle check, or an explicit
/// `type(uint96).max` comparison.
contract WeiBoard {
// ───────────────────────────────────────── Parameters
uint256 public constant GRID_SIZE = 100;
uint256 public constant MAX_PLOT_SIDE = 20;
/// @notice Harberger fee per period, in basis points of the listed price (5% a month).
uint256 public constant FEE_BPS = 500;
uint256 public constant FEE_PERIOD = 30 days;
/// @notice Share of every non-foreclosure sale that goes to the treasury.
uint256 public constant SALE_FEE_BPS = 500;
/// @notice After a claim or purchase, the plot cannot be bought for this long.
uint256 public constant PROTECTION_PERIOD = 24 hours;
uint256 private constant BPS = 10_000;
/// @notice Price of one empty block; also the minimum listed price per block.
uint256 public immutable blockPrice;
// ───────────────────────────────────────── State
/// @dev Two storage slots per plot.
struct Plot {
address owner;
uint8 x;
uint8 y;
uint8 w;
uint8 h;
uint40 lastSettled;
uint96 price;
uint96 deposit;
uint40 protectedUntil;
}
/// @notice Plot state as seen at the current block, with fees owed already deducted.
struct PlotView {
uint256 id;
address owner;
uint8 x;
uint8 y;
uint8 w;
uint8 h;
uint256 price;
uint256 deposit;
uint256 protectedUntil;
/// @dev Timestamp at which the deposit runs (or ran) out at the current price.
uint256 paidThrough;
bool foreclosed;
/// @dev What a buyer pays right now: `price`, or the minimum price if foreclosed.
uint256 salePrice;
}
/// @dev One 100-bit occupancy mask per grid row; bit `x` of row `y` is block (x, y).
uint128[GRID_SIZE] private _rows;
/// @notice Plots by id. Ids start at 1 and are never reused.
mapping(uint256 id => Plot) private _plots;
uint256 public plotCount;
/// @notice ETH withdrawable by each account (sale proceeds and returned deposits).
mapping(address account => uint256) public credits;
address public treasury;
address public pendingTreasury;
uint256 public treasuryBalance;
bool private transient _locked;
// ───────────────────────────────────────── Events
event Claimed(
uint256 indexed id,
address indexed owner,
uint8 x,
uint8 y,
uint8 w,
uint8 h,
uint256 price,
uint256 deposit
);
event Bought(
uint256 indexed id,
address indexed seller,
address indexed buyer,
uint256 salePrice,
uint256 newPrice,
uint256 deposit,
bool foreclosed
);
event PriceChanged(uint256 indexed id, uint256 price);
event DepositChanged(uint256 indexed id, uint256 deposit);
event Foreclosed(uint256 indexed id);
event CreditsWithdrawn(address indexed account, uint256 amount);
event TreasuryWithdrawn(address indexed to, uint256 amount);
event TreasuryTransferStarted(address indexed current, address indexed pending);
event TreasuryTransferred(address indexed previous, address indexed current);
// ───────────────────────────────────────── Errors
error InvalidRect();
error Overlap();
error NoSuchPlot();
error NotOwner();
error NotTreasury();
error PlotForeclosed();
error Protected(uint256 until);
error PriceTooLow(uint256 minimum);
error PriceTooHigh();
error PriceAboveMax(uint256 salePrice);
error DepositTooLow(uint256 minimum);
error DepositTooHigh();
error InsufficientPayment(uint256 required);
error InvalidAmount();
error ZeroAddress();
error TransferFailed();
error Reentrancy();
// ───────────────────────────────────────── Modifiers
modifier nonReentrant() {
if (_locked) revert Reentrancy();
_locked = true;
_;
_locked = false;
}
modifier onlyTreasury() {
if (msg.sender != treasury) revert NotTreasury();
_;
}
constructor(uint256 blockPrice_, address treasury_) {
if (blockPrice_ == 0) revert InvalidAmount();
if (treasury_ == address(0)) revert ZeroAddress();
blockPrice = blockPrice_;
treasury = treasury_;
}
// ───────────────────────────────────────── Plot actions
/// @notice Claim a rectangle of empty blocks.
/// @dev `msg.value` must be `w * h * blockPrice` plus the deposit (at least one period of fees).
function claim(uint8 x, uint8 y, uint8 w, uint8 h, uint256 price)
external
payable
returns (uint256 id)
{
if (!_validRect(x, y, w, h)) revert InvalidRect();
uint256 blocks = uint256(w) * h;
uint256 cost = blocks * blockPrice;
if (msg.value < cost) revert InsufficientPayment(cost);
uint256 deposit = msg.value - cost;
_checkTerms(blocks, price, deposit);
uint128 mask = uint128(((uint256(1) << w) - 1) << x);
uint256 end = uint256(y) + h;
for (uint256 r = y; r < end; ++r) {
uint128 row = _rows[r];
if (row & mask != 0) revert Overlap();
_rows[r] = row | mask;
}
id = ++plotCount;
_plots[id] = Plot({
owner: msg.sender,
x: x,
y: y,
w: w,
h: h,
lastSettled: uint40(block.timestamp),
price: uint96(price),
deposit: uint96(deposit),
protectedUntil: uint40(block.timestamp + PROTECTION_PERIOD)
});
treasuryBalance += cost;
emit Claimed(id, msg.sender, x, y, w, h, price, deposit);
}
/// @notice Buy a plot at its current sale price and relist it at `newPrice`.
/// @param maxPrice The most the buyer accepts to pay; protects against price changes
/// between signing and inclusion.
/// @dev `msg.value` must be the sale price plus the new deposit.
function buy(uint256 id, uint256 maxPrice, uint256 newPrice) external payable {
Plot storage p = _existing(id);
if (block.timestamp < p.protectedUntil) revert Protected(p.protectedUntil);
_settle(id, p);
uint256 blocks = uint256(p.w) * p.h;
bool foreclosed = p.deposit == 0;
uint256 salePrice = foreclosed ? blocks * blockPrice : p.price;
if (salePrice > maxPrice) revert PriceAboveMax(salePrice);
if (msg.value < salePrice) revert InsufficientPayment(salePrice);
uint256 deposit = msg.value - salePrice;
_checkTerms(blocks, newPrice, deposit);
address seller = p.owner;
if (foreclosed) {
treasuryBalance += salePrice;
} else {
uint256 fee = salePrice * SALE_FEE_BPS / BPS;
treasuryBalance += fee;
credits[seller] += salePrice - fee + p.deposit;
}
p.owner = msg.sender;
p.price = uint96(newPrice);
p.deposit = uint96(deposit);
p.protectedUntil = uint40(block.timestamp + PROTECTION_PERIOD);
emit Bought(id, seller, msg.sender, salePrice, newPrice, deposit, foreclosed);
}
/// @notice Change the listed price. The deposit must still cover one period at the new price.
function setPrice(uint256 id, uint256 newPrice) external {
Plot storage p = _ownedActive(id);
_checkTerms(uint256(p.w) * p.h, newPrice, p.deposit);
p.price = uint96(newPrice);
emit PriceChanged(id, newPrice);
}
/// @notice Add to a plot's deposit. Anyone may top up any active plot.
function topUp(uint256 id) external payable {
if (msg.value == 0) revert InvalidAmount();
Plot storage p = _existing(id);
_settle(id, p);
if (p.deposit == 0) revert PlotForeclosed();
uint256 deposit = p.deposit + msg.value;
if (deposit > type(uint96).max) revert DepositTooHigh();
p.deposit = uint96(deposit);
emit DepositChanged(id, deposit);
}
/// @notice Withdraw part of a plot's deposit, leaving at least one period of fees.
function withdrawDeposit(uint256 id, uint256 amount) external nonReentrant {
Plot storage p = _ownedActive(id);
if (amount == 0 || amount > p.deposit) revert InvalidAmount();
uint256 remaining = p.deposit - amount;
uint256 minimum = _periodFee(p.price);
if (remaining < minimum) revert DepositTooLow(minimum);
p.deposit = uint96(remaining);
emit DepositChanged(id, remaining);
_send(msg.sender, amount);
}
/// @notice Realize fees owed by a plot. Anyone may call; it never changes ownership.
function settle(uint256 id) external {
_settle(id, _existing(id));
}
/// @notice Withdraw the caller's credited ETH.
function withdraw() external nonReentrant {
uint256 amount = credits[msg.sender];
if (amount == 0) revert InvalidAmount();
credits[msg.sender] = 0;
emit CreditsWithdrawn(msg.sender, amount);
_send(msg.sender, amount);
}
// ───────────────────────────────────────── Treasury
function withdrawTreasury() external onlyTreasury nonReentrant {
uint256 amount = treasuryBalance;
if (amount == 0) revert InvalidAmount();
treasuryBalance = 0;
emit TreasuryWithdrawn(msg.sender, amount);
_send(msg.sender, amount);
}
/// @notice Start handing the treasury role to `next`; it takes effect when `next` accepts.
function transferTreasury(address next) external onlyTreasury {
if (next == address(0)) revert ZeroAddress();
pendingTreasury = next;
emit TreasuryTransferStarted(msg.sender, next);
}
function acceptTreasury() external {
if (msg.sender != pendingTreasury) revert NotTreasury();
emit TreasuryTransferred(treasury, msg.sender);
treasury = msg.sender;
pendingTreasury = address(0);
}
// ───────────────────────────────────────── Views
function getPlot(uint256 id) external view returns (PlotView memory) {
return _view(id, _existing(id));
}
/// @notice Up to `count` plots starting at id `fromId` (ids start at 1).
function getPlots(uint256 fromId, uint256 count) external view returns (PlotView[] memory out) {
if (fromId == 0) fromId = 1;
uint256 last = plotCount;
if (fromId > last || count == 0) return out;
uint256 n = last - fromId + 1;
if (n > count) n = count;
out = new PlotView[](n);
for (uint256 i; i < n; ++i) {
uint256 id = fromId + i;
out[i] = _view(id, _plots[id]);
}
}
/// @notice Whether `claim` could take this rectangle: valid and every block unclaimed.
function isFree(uint8 x, uint8 y, uint8 w, uint8 h) external view returns (bool) {
if (!_validRect(x, y, w, h)) return false;
uint128 mask = uint128(((uint256(1) << w) - 1) << x);
uint256 end = uint256(y) + h;
for (uint256 r = y; r < end; ++r) {
if (_rows[r] & mask != 0) return false;
}
return true;
}
/// @notice Minimum deposit for a listed price: one fee period.
function minDeposit(uint256 price) external pure returns (uint256) {
return _periodFee(price);
}
// ───────────────────────────────────────── Internals
/// @dev On the grid, non-empty and at most `MAX_PLOT_SIDE` on each side.
function _validRect(uint8 x, uint8 y, uint8 w, uint8 h) private pure returns (bool) {
return w != 0 && h != 0 && w <= MAX_PLOT_SIDE && h <= MAX_PLOT_SIDE
&& uint256(x) + w <= GRID_SIZE && uint256(y) + h <= GRID_SIZE;
}
function _existing(uint256 id) private view returns (Plot storage p) {
p = _plots[id];
if (p.owner == address(0)) revert NoSuchPlot();
}
function _ownedActive(uint256 id) private returns (Plot storage p) {
p = _existing(id);
if (p.owner != msg.sender) revert NotOwner();
_settle(id, p);
if (p.deposit == 0) revert PlotForeclosed();
}
/// @dev Moves fees owed since the last settlement from the deposit to the treasury.
/// A deposit that cannot cover them is taken in full and the plot is foreclosed.
function _settle(uint256 id, Plot storage p) private {
uint256 owed = _owed(p);
uint256 deposit = p.deposit;
if (owed >= deposit) {
if (deposit != 0) {
treasuryBalance += deposit;
p.deposit = 0;
emit Foreclosed(id);
}
} else if (owed != 0) {
treasuryBalance += owed;
p.deposit = uint96(deposit - owed);
}
p.lastSettled = uint40(block.timestamp);
}
function _owed(Plot storage p) private view returns (uint256) {
return uint256(p.price) * FEE_BPS * (block.timestamp - p.lastSettled) / (BPS * FEE_PERIOD);
}
/// @dev One period of fees, rounded up so a minimum deposit always lasts a full period.
function _periodFee(uint256 price) private pure returns (uint256) {
return (price * FEE_BPS + BPS - 1) / BPS;
}
function _checkTerms(uint256 blocks, uint256 price, uint256 deposit) private view {
uint256 minPrice = blocks * blockPrice;
if (price < minPrice) revert PriceTooLow(minPrice);
if (price > type(uint96).max) revert PriceTooHigh();
uint256 minimum = _periodFee(price);
if (deposit < minimum) revert DepositTooLow(minimum);
if (deposit > type(uint96).max) revert DepositTooHigh();
}
function _view(uint256 id, Plot storage p) private view returns (PlotView memory v) {
uint256 owed = _owed(p);
bool foreclosed = owed >= p.deposit;
v = PlotView({
id: id,
owner: p.owner,
x: p.x,
y: p.y,
w: p.w,
h: p.h,
price: p.price,
deposit: foreclosed ? 0 : p.deposit - owed,
protectedUntil: p.protectedUntil,
paidThrough: p.lastSettled + uint256(p.deposit) * BPS * FEE_PERIOD
/ (uint256(p.price) * FEE_BPS),
foreclosed: foreclosed,
salePrice: foreclosed ? uint256(p.w) * p.h * blockPrice : p.price
});
}
function _send(address to, uint256 amount) private {
(bool ok,) = to.call{value: amount}("");
if (!ok) revert TransferFailed();
}
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "none",
"useLiteralContent": false
},
"optimizer": {
"runs": 10000,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"forge-std/=lib/forge-std/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.