// SPDX-License-Identifier: MIT pragma solidity 0.8.28; /* * SEED staking — points by SEED × time, kept on-chain. * * Anyone can stake SEED and unstake it at any time. While SEED is staked the account earns * points: one point per wei of SEED per second (shown as SEED-days off-chain). Points already * earned are never taken away; what stays staked keeps earning. The contract records points, * nothing else: it knows nothing about what the project may later share out by points, so it * never needs to change when that policy does. * * Accounting (per account and globally): before any balance changes, the points earned by the * OLD balance since the last update are added and the timestamp is moved; only then does the * balance move. Everything is uint256 with checked arithmetic. Events carry the block timestamp, * so the full history can be rebuilt from logs alone and checked against pointsOf(). * * The contract has no owner, no pause, no upgrade path, no delegatecall and no arbitrary call. * There is no cap and no minimum. SEED sent to this contract by plain transfer is not a stake: * it earns nothing and cannot be recovered (there is no rescue function, by design). */ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; contract SeedStaking is ReentrancyGuard { using SafeERC20 for IERC20; struct Account { uint256 stake; // SEED staked now, in wei uint256 points; // points settled up to `last` (wei × seconds) uint64 last; // timestamp of the last settlement (0 before the first interaction; harmless, stake is 0 then) } IERC20 public immutable seed; uint64 public immutable deployedAt; uint256 public totalStake; uint256 private totalPointsSettled; // global points settled up to `lastGlobal` uint64 private lastGlobal; mapping(address => Account) private accounts; event Staked( address indexed account, uint256 amount, uint256 stake, uint256 points, uint256 totalStake, uint256 totalPoints, uint64 timestamp ); event Unstaked( address indexed account, uint256 amount, uint256 stake, uint256 points, uint256 totalStake, uint256 totalPoints, uint64 timestamp ); error ZeroAmount(); error InsufficientStake(uint256 stake, uint256 amount); error NotAContract(); constructor(IERC20 seed_) { if (address(seed_).code.length == 0) revert NotAContract(); seed = seed_; deployedAt = uint64(block.timestamp); lastGlobal = uint64(block.timestamp); } // ---- actions --------------------------------------------------------------------------- function stake(uint256 amount) external nonReentrant { if (amount == 0) revert ZeroAmount(); Account storage a = _settle(msg.sender); a.stake += amount; totalStake += amount; emit Staked(msg.sender, amount, a.stake, a.points, totalStake, totalPointsSettled, uint64(block.timestamp)); seed.safeTransferFrom(msg.sender, address(this), amount); } function unstake(uint256 amount) external nonReentrant { if (amount == 0) revert ZeroAmount(); Account storage a = _settle(msg.sender); if (amount > a.stake) revert InsufficientStake(a.stake, amount); a.stake -= amount; totalStake -= amount; emit Unstaked(msg.sender, amount, a.stake, a.points, totalStake, totalPointsSettled, uint64(block.timestamp)); seed.safeTransfer(msg.sender, amount); } // ---- views ----------------------------------------------------------------------------- /// Points of `account` as of now: settled points plus what the current stake earned since `last`. /// An account that never interacted has stake 0 and points 0, so this is 0 without a sentinel. function pointsOf(address account) external view returns (uint256) { Account storage a = accounts[account]; return a.points + a.stake * (block.timestamp - a.last); } /// Sum of every account's points as of now. Equals the sum of pointsOf() over all accounts. function totalPoints() external view returns (uint256) { return totalPointsSettled + totalStake * (block.timestamp - lastGlobal); } function stakeOf(address account) external view returns (uint256) { return accounts[account].stake; } /// Raw settled state of an account (points settled up to `last`, not including time since). function accountOf(address account) external view returns (uint256 stake_, uint256 points, uint64 last) { Account storage a = accounts[account]; return (a.stake, a.points, a.last); } // ---- internal -------------------------------------------------------------------------- /// Settles points earned by the OLD balances up to now, for the account and globally, and moves /// both timestamps. Called before any balance changes. No sentinel for "never interacted": such /// an account has stake 0, so the product is 0 whatever `last` is. function _settle(address account) private returns (Account storage a) { uint64 now_ = uint64(block.timestamp); a = accounts[account]; a.points += a.stake * (now_ - a.last); a.last = now_; totalPointsSettled += totalStake * (now_ - lastGlobal); lastGlobal = now_; } }