// SPDX-License-Identifier: MIT pragma solidity 0.8.28; /* * SEED distributor — Merkle claims, organised in seasons. * * A season holds one token. The poster opens it, funds it from tokens already sitting in this * contract, and publishes a Merkle root of cumulative amounts per account; the root may be replaced * while the season is open, but its declared total never goes down and never exceeds what was * funded. Freezing a season fixes the root, returns any funded-but-unallocated amount to the free * balance and, if the season has a claim window, starts the countdown to its deadline. * * Claims stop at the deadline. After it anyone can expire the season: what was not claimed goes * to the treasury. That is the only way season funds leave other than claims. A season * without a claim window never expires. Whatever the leaves of a root say, a season never pays out * more than its declared root total, and that total never exceeds what the season was funded with. * * Who receives a claim: the account itself or its beneficiary may claim to any address. Anyone else * may deliver a claim, but only to the beneficiary. With a deposits registry set, the beneficiary is * the account's operator there (an AntSeed buyer's operator); without one, it is the account. * * The contract has no owner, no upgrade path, no delegatecall and no arbitrary call. The poster and * the treasury hand their roles over in two steps (propose, then accept by the new address). */ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {MerkleProof} from "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol"; import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; interface IOperatorRegistry { function getOperator(address buyer) external view returns (address); } contract SeedDistributor is ReentrancyGuard { using SafeERC20 for IERC20; uint64 public constant MIN_CLAIM_WINDOW = 30 days; uint64 public constant MAX_CLAIM_WINDOW = 3650 days; struct Season { IERC20 token; uint64 claimWindow; // 0 = never expires uint64 deadline; // set by freeze(); 0 while open or when the season never expires bool frozen; bool expired; bytes32 root; uint256 rootTotal; // sum of the cumulative amounts in the current root uint256 funded; uint256 claimed; } struct ClaimItem { uint256 season; address account; uint256 cumulative; bytes32[] proof; } IOperatorRegistry public immutable registry; // address(0) = the account is its own beneficiary address public poster; address public pendingPoster; address public treasury; address public pendingTreasury; Season[] private _seasons; mapping(uint256 season => mapping(address account => uint256)) public claimedOf; mapping(address token => uint256) public reserved; // funded minus claimed, over seasons not expired event SeasonOpened(uint256 indexed season, address indexed token, uint64 claimWindow); event Funded(uint256 indexed season, uint256 amount, uint256 funded); event RootSet(uint256 indexed season, bytes32 root, uint256 total); event Frozen(uint256 indexed season, uint64 deadline, uint256 released); event Claimed(uint256 indexed season, address indexed account, address indexed to, address caller, uint256 amount); event Expired(uint256 indexed season, address indexed treasury, uint256 amount); event SweptUnallocated(address indexed token, address indexed treasury, uint256 amount); event PosterProposed(address indexed pending); event PosterChanged(address indexed previous, address indexed current); event TreasuryProposed(address indexed pending); event TreasuryChanged(address indexed previous, address indexed current); error ZeroAddress(); error NotPoster(); error NotTreasury(); error NotPending(); error NoSeason(); error SeasonFrozen(); error SeasonExpired(); error BadWindow(); error NotEnoughUnallocated(); error ZeroRoot(); error NoRoot(); error RootAboveFunded(); error RootTotalDecreased(); error BadProof(); error NoBeneficiary(); error OverClaim(); error InvalidDestination(); error NotExpirable(); error TooEarly(); modifier onlyPoster() { if (msg.sender != poster) revert NotPoster(); _; } constructor(address registry_, address poster_, address treasury_) { if (poster_ == address(0) || treasury_ == address(0)) revert ZeroAddress(); registry = IOperatorRegistry(registry_); poster = poster_; treasury = treasury_; } // ── seasons (poster) ───────────────────────────────────────────────────────────────────────── function openSeason(IERC20 token, uint64 claimWindow) external onlyPoster returns (uint256 id) { if (address(token) == address(0)) revert ZeroAddress(); if (claimWindow != 0 && (claimWindow < MIN_CLAIM_WINDOW || claimWindow > MAX_CLAIM_WINDOW)) revert BadWindow(); id = _seasons.length; _seasons.push(); Season storage s = _seasons[id]; s.token = token; s.claimWindow = claimWindow; emit SeasonOpened(id, address(token), claimWindow); } /// Assigns tokens already held by this contract, and not reserved by any season, to a season. function fund(uint256 id, uint256 amount) external onlyPoster { Season storage s = _openSeason(id); address t = address(s.token); if (amount == 0 || amount > unallocated(IERC20(t))) revert NotEnoughUnallocated(); s.funded += amount; reserved[t] += amount; emit Funded(id, amount, s.funded); } function setRoot(uint256 id, bytes32 root, uint256 total) external onlyPoster { Season storage s = _openSeason(id); if (root == bytes32(0)) revert ZeroRoot(); if (total > s.funded) revert RootAboveFunded(); if (total < s.rootTotal) revert RootTotalDecreased(); s.root = root; s.rootTotal = total; emit RootSet(id, root, total); } function freeze(uint256 id) external onlyPoster { Season storage s = _openSeason(id); if (s.root == bytes32(0)) revert NoRoot(); s.frozen = true; uint256 released = s.funded - s.rootTotal; if (released != 0) { s.funded = s.rootTotal; reserved[address(s.token)] -= released; } if (s.claimWindow != 0) s.deadline = uint64(block.timestamp) + s.claimWindow; emit Frozen(id, s.deadline, released); } /// Sends to the treasury what this contract holds of a token beyond what the seasons reserve. function sweepUnallocated(IERC20 token) external onlyPoster nonReentrant returns (uint256 amount) { amount = unallocated(token); if (amount != 0) token.safeTransfer(treasury, amount); emit SweptUnallocated(address(token), treasury, amount); } // ── claims (anyone, to the right place) ────────────────────────────────────────────────────── /// The account or its beneficiary may send the claim anywhere (`to` = 0 means the caller). /// Anyone else may deliver it, and then it goes to the beneficiary, whatever `to` says. function claim(uint256 id, address account, uint256 cumulative, bytes32[] calldata proof, address to) external nonReentrant returns (uint256) { return _claim(id, account, cumulative, proof, to); } /// Batch delivery (e.g. a push before a deadline): every item goes to its account's beneficiary /// unless the caller is that account or beneficiary. Reverts as a whole if any item fails. function claimMany(ClaimItem[] calldata items) external nonReentrant returns (uint256 total) { for (uint256 i = 0; i < items.length; i++) { total += _claim(items[i].season, items[i].account, items[i].cumulative, items[i].proof, address(0)); } } /// Anyone, after the deadline of a frozen season with a claim window. function expire(uint256 id) external nonReentrant returns (uint256 amount) { Season storage s = _season(id); if (!s.frozen || s.deadline == 0) revert NotExpirable(); if (s.expired) revert SeasonExpired(); if (block.timestamp <= s.deadline) revert TooEarly(); s.expired = true; amount = s.funded - s.claimed; if (amount != 0) { reserved[address(s.token)] -= amount; s.token.safeTransfer(treasury, amount); } emit Expired(id, treasury, amount); } // ── roles (two steps) ──────────────────────────────────────────────────────────────────────── function proposePoster(address next) external onlyPoster { pendingPoster = next; emit PosterProposed(next); } function acceptPoster() external { if (msg.sender != pendingPoster || msg.sender == address(0)) revert NotPending(); emit PosterChanged(poster, msg.sender); poster = msg.sender; pendingPoster = address(0); } function proposeTreasury(address next) external { if (msg.sender != treasury) revert NotTreasury(); pendingTreasury = next; emit TreasuryProposed(next); } function acceptTreasury() external { if (msg.sender != pendingTreasury || msg.sender == address(0)) revert NotPending(); emit TreasuryChanged(treasury, msg.sender); treasury = msg.sender; pendingTreasury = address(0); } // ── views ──────────────────────────────────────────────────────────────────────────────────── function seasonCount() external view returns (uint256) { return _seasons.length; } function season(uint256 id) external view returns (Season memory) { return _season(id); } function leaf(uint256 id, address account, uint256 cumulative) public view returns (bytes32) { return keccak256(bytes.concat(keccak256(abi.encode(address(this), block.chainid, id, account, cumulative)))); } function beneficiaryOf(address account) public view returns (address) { if (address(registry) == address(0)) return account; return registry.getOperator(account); } function unallocated(IERC20 token) public view returns (uint256) { return token.balanceOf(address(this)) - reserved[address(token)]; } // ── internals ──────────────────────────────────────────────────────────────────────────────── function _claim(uint256 id, address account, uint256 cumulative, bytes32[] calldata proof, address to) internal returns (uint256 amount) { Season storage s = _season(id); if (s.expired || (s.deadline != 0 && block.timestamp > s.deadline)) revert SeasonExpired(); if (s.root == bytes32(0)) revert NoRoot(); if (!MerkleProof.verifyCalldata(proof, s.root, leaf(id, account, cumulative))) revert BadProof(); uint256 done = claimedOf[id][account]; if (cumulative <= done) return 0; // already paid: no registry call, so a paid item never sinks a batch address dest = _destination(account, to); if (dest == address(this)) revert InvalidDestination(); amount = cumulative - done; if (s.claimed + amount > s.rootTotal) revert OverClaim(); claimedOf[id][account] = cumulative; s.claimed += amount; reserved[address(s.token)] -= amount; s.token.safeTransfer(dest, amount); emit Claimed(id, account, dest, msg.sender, amount); } function _destination(address account, address to) private view returns (address) { // the account's own claim never depends on the registry if (msg.sender == account) return to == address(0) ? msg.sender : to; address beneficiary = beneficiaryOf(account); if (beneficiary != address(0) && msg.sender == beneficiary) return to == address(0) ? msg.sender : to; if (beneficiary == address(0)) revert NoBeneficiary(); return beneficiary; } function _season(uint256 id) private view returns (Season storage) { if (id >= _seasons.length) revert NoSeason(); return _seasons[id]; } function _openSeason(uint256 id) private view returns (Season storage s) { s = _season(id); if (s.frozen) revert SeasonFrozen(); } }