// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {IERC721Min, IPriceFeed} from "./Interfaces.sol"; import {Header, QuorumLib} from "./QuorumLib.sol"; contract BondVault { uint256 public constant UNBOND_DELAY = 7 days; uint256 public constant MAX_SLABS = 50; uint256 public constant MAX_AGE = 3 days; struct Slab { address collection; uint256 tokenId; uint256 unbondAt; // 0 = active, otherwise pending unbond, withdrawable at this time } struct Validator { bool registered; bool jailed; bytes blsPubkey; string endpoint; } IPriceFeed public immutable feed; uint256 public immutable l2ChainId; uint256 public immutable minBondUsd18; address public admin; mapping(address => bool) public allowedCollections; mapping(address => Validator) internal validators; mapping(address => Slab[]) internal slabs; address[] public validatorList; event Registered(address indexed v, bytes blsPubkey, string endpoint); event Bonded(address indexed v, address collection, uint256 tokenId); event UnbondRequested(address indexed v, address collection, uint256 tokenId, uint256 unbondAt); event Unbonded(address indexed v, address collection, uint256 tokenId); event Slashed(address indexed validator, address indexed reporter, uint256 slabCount); event CollectionAllowed(address indexed collection, bool ok); constructor(address feed_, uint256 l2ChainId_, uint256 minBondUsd18_, address admin_) { feed = IPriceFeed(feed_); l2ChainId = l2ChainId_; minBondUsd18 = minBondUsd18_; admin = admin_; } function allowCollection(address c, bool ok) external { require(msg.sender == admin, "not admin"); allowedCollections[c] = ok; emit CollectionAllowed(c, ok); } function register(bytes calldata blsPubkey, string calldata endpoint) external { Validator storage v = validators[msg.sender]; require(!v.jailed, "jailed"); if (!v.registered) { v.registered = true; validatorList.push(msg.sender); } v.blsPubkey = blsPubkey; v.endpoint = endpoint; emit Registered(msg.sender, blsPubkey, endpoint); } function bond(address collection, uint256 tokenId) external { Validator storage v = validators[msg.sender]; require(v.registered, "not registered"); require(!v.jailed, "jailed"); require(allowedCollections[collection], "collection not allowed"); require(slabs[msg.sender].length < MAX_SLABS, "too many slabs"); IERC721Min(collection).transferFrom(msg.sender, address(this), tokenId); slabs[msg.sender].push(Slab(collection, tokenId, 0)); emit Bonded(msg.sender, collection, tokenId); } function _find(address v, address collection, uint256 tokenId) internal view returns (uint256) { Slab[] storage list = slabs[v]; for (uint256 i = 0; i < list.length; i++) { if (list[i].collection == collection && list[i].tokenId == tokenId) return i; } revert("slab not bonded"); } function requestUnbond(address collection, uint256 tokenId) external { uint256 i = _find(msg.sender, collection, tokenId); Slab storage s = slabs[msg.sender][i]; require(s.unbondAt == 0, "already unbonding"); s.unbondAt = block.timestamp + UNBOND_DELAY; emit UnbondRequested(msg.sender, collection, tokenId, s.unbondAt); } function withdrawUnbonded(address collection, uint256 tokenId) external { uint256 i = _find(msg.sender, collection, tokenId); Slab[] storage list = slabs[msg.sender]; uint256 at = list[i].unbondAt; require(at != 0, "not unbonding"); require(block.timestamp >= at, "unbond delay"); list[i] = list[list.length - 1]; list.pop(); IERC721Min(collection).transferFrom(address(this), msg.sender, tokenId); emit Unbonded(msg.sender, collection, tokenId); } function bondValue(address v) public view returns (uint256 usd18) { Slab[] storage list = slabs[v]; for (uint256 i = 0; i < list.length; i++) { if (list[i].unbondAt != 0) continue; (uint256 p, uint256 at) = feed.priceOf(list[i].collection, list[i].tokenId); if (p == 0 || block.timestamp - at > MAX_AGE) continue; usd18 += p; } } function activeSet() external view returns (address[] memory addrs, uint256[] memory values) { uint256 n = validatorList.length; address[] memory a = new address[](n); uint256[] memory vals = new uint256[](n); uint256 count; for (uint256 i = 0; i < n; i++) { address v = validatorList[i]; if (validators[v].jailed) continue; uint256 val = bondValue(v); if (val == 0 || val < minBondUsd18) continue; a[count] = v; vals[count] = val; count++; } addrs = new address[](count); values = new uint256[](count); for (uint256 i = 0; i < count; i++) { addrs[i] = a[i]; values[i] = vals[i]; } } function validatorCount() external view returns (uint256) { return validatorList.length; } function validatorInfo(address v) external view returns (bytes memory blsPubkey, string memory endpoint, uint256 slabCount, bool jailed, uint256 value) { Validator storage x = validators[v]; return (x.blsPubkey, x.endpoint, slabs[v].length, x.jailed, bondValue(v)); } function bondedSlabs(address v) external view returns (address[] memory collections, uint256[] memory tokenIds) { Slab[] storage list = slabs[v]; collections = new address[](list.length); tokenIds = new uint256[](list.length); for (uint256 i = 0; i < list.length; i++) { collections[i] = list[i].collection; tokenIds[i] = list[i].tokenId; } } function slashDoubleSign(Header calldata a, Header calldata b, bytes calldata sigA, bytes calldata sigB) external { require(a.chainId == l2ChainId && b.chainId == l2ChainId, "wrong chain"); require(a.height == b.height, "different height"); require(a.proposer == b.proposer, "different proposer"); bytes32 ha = QuorumLib.headerHash(a); bytes32 hb = QuorumLib.headerHash(b); require(ha != hb, "same header"); address p = a.proposer; require(p != address(0), "zero proposer"); require(QuorumLib.recover(ha, sigA) == p, "bad sigA"); require(QuorumLib.recover(hb, sigB) == p, "bad sigB"); Slab[] storage list = slabs[p]; uint256 n = list.length; require(n > 0, "nothing bonded"); validators[p].jailed = true; Slab[] memory taken = list; delete slabs[p]; for (uint256 i = 0; i < n; i++) { IERC721Min(taken[i].collection).transferFrom(address(this), msg.sender, taken[i].tokenId); } emit Slashed(p, msg.sender, n); } }