// SPDX-License-Identifier: MIT pragma solidity 0.8.26; /// @dev Minimal ERC-20 surface this contract needs. interface IERC20 { function balanceOf(address account) external view returns (uint256); } /// @title KinetiqOptions /// @notice Covered calls on kHYPE, fully collateralised and physically settled. /// /// A writer locks kHYPE and names a strike, an expiry and a premium. /// A buyer pays the premium in native HYPE and receives the option. /// For 24 hours after expiry the holder may buy the locked kHYPE at the strike. /// If they do not, the writer takes the kHYPE back. /// /// The collateral stays kHYPE the whole time, so the writer keeps the Kinetiq /// staking yield until the option is exercised or reclaimed. /// /// There is no oracle, no liquidation, no owner, no pause and no upgrade path: /// every outcome is decided by the two parties and the clock. contract KinetiqOptions { enum State { None, Open, // written, waiting for a buyer Sold, // premium paid, option held by `holder` Exercised, // holder paid the strike and took the collateral Cancelled, // writer withdrew before anyone bought Reclaimed // exercise window passed, collateral returned to the writer } struct Option { address writer; address holder; uint128 amount; // collateral locked, in collateral units uint128 strike; // strike-asset units per 1e18 collateral units uint128 premium; // native HYPE, in wei, for the whole option uint64 expiry; // the exercise window opens at this timestamp State state; } /// @notice The exercise window after expiry. uint64 public constant EXERCISE_WINDOW = 1 days; /// @notice Shortest and longest life of an option at writing time. uint64 public constant MIN_DURATION = 1 hours; uint64 public constant MAX_DURATION = 180 days; /// @notice The protocol fee can never exceed 3% of the premium. uint16 public constant MAX_FEE_BPS = 300; address public immutable collateral; // kHYPE address public immutable strikeAsset; // USDC address public immutable treasury; uint16 public immutable feeBps; // share of each premium sent to the treasury Option[] private _options; uint256 private _entered = 1; event Written(uint256 indexed id, address indexed writer, uint128 amount, uint128 strike, uint64 expiry, uint128 premium); event Cancelled(uint256 indexed id); event Bought(uint256 indexed id, address indexed holder, uint256 premium, uint256 fee); event Transferred(uint256 indexed id, address indexed from, address indexed to); event Exercised(uint256 indexed id, address indexed holder, uint256 strikePaid); event Reclaimed(uint256 indexed id); error ZeroAddress(); error FeeTooHigh(); error ZeroAmount(); error BadExpiry(); error UnsupportedToken(); error UnknownOption(); error WrongState(); error NotWriter(); error NotHolder(); error Expired(); error WrongPremium(); error WindowClosed(); error WindowOpen(); error Reentrancy(); error TransferFailed(); modifier nonReentrant() { if (_entered != 1) revert Reentrancy(); _entered = 2; _; _entered = 1; } constructor(address collateral_, address strikeAsset_, address treasury_, uint16 feeBps_) { if (collateral_ == address(0) || strikeAsset_ == address(0) || treasury_ == address(0)) revert ZeroAddress(); if (feeBps_ > MAX_FEE_BPS) revert FeeTooHigh(); collateral = collateral_; strikeAsset = strikeAsset_; treasury = treasury_; feeBps = feeBps_; } // ───────────────────────────── writer ───────────────────────────── /// @notice Lock `amount` of collateral and offer a call on it. /// @param amount Collateral to lock. /// @param strike Strike-asset units the holder pays per 1e18 collateral units. /// @param expiry When the 24-hour exercise window opens. /// @param premium Native HYPE, in wei, a buyer pays for the whole option. function write(uint128 amount, uint128 strike, uint64 expiry, uint128 premium) external nonReentrant returns (uint256 id) { if (amount == 0 || strike == 0 || premium == 0) revert ZeroAmount(); if (expiry < block.timestamp + MIN_DURATION || expiry > block.timestamp + MAX_DURATION) revert BadExpiry(); // Tokens that take a fee on transfer would leave the option under-collateralised. uint256 before = IERC20(collateral).balanceOf(address(this)); _pull(collateral, msg.sender, address(this), amount); if (IERC20(collateral).balanceOf(address(this)) - before != amount) revert UnsupportedToken(); id = _options.length; _options.push( Option({ writer: msg.sender, holder: address(0), amount: amount, strike: strike, premium: premium, expiry: expiry, state: State.Open }) ); emit Written(id, msg.sender, amount, strike, expiry, premium); } /// @notice Withdraw an option nobody bought. The collateral returns to the writer. function cancel(uint256 id) external nonReentrant { Option storage o = _get(id); if (o.state != State.Open) revert WrongState(); if (msg.sender != o.writer) revert NotWriter(); o.state = State.Cancelled; emit Cancelled(id); _push(collateral, o.writer, o.amount); } /// @notice Return the collateral to the writer once the exercise window has passed unused. /// @dev Anyone may call it: the collateral can only go to the writer. function reclaim(uint256 id) external nonReentrant { Option storage o = _get(id); if (o.state != State.Sold) revert WrongState(); if (block.timestamp < uint256(o.expiry) + EXERCISE_WINDOW) revert WindowOpen(); o.state = State.Reclaimed; emit Reclaimed(id); _push(collateral, o.writer, o.amount); } // ───────────────────────────── holder ───────────────────────────── /// @notice Buy an open option by paying its premium in native HYPE. function buy(uint256 id) external payable nonReentrant { Option storage o = _get(id); if (o.state != State.Open) revert WrongState(); if (block.timestamp >= o.expiry) revert Expired(); if (msg.value != o.premium) revert WrongPremium(); o.state = State.Sold; o.holder = msg.sender; uint256 fee = (msg.value * feeBps) / 10_000; emit Bought(id, msg.sender, msg.value, fee); if (fee != 0) _send(treasury, fee); _send(o.writer, msg.value - fee); } /// @notice Hand the option to another address. function transferOption(uint256 id, address to) external { Option storage o = _get(id); if (o.state != State.Sold) revert WrongState(); if (msg.sender != o.holder) revert NotHolder(); if (to == address(0)) revert ZeroAddress(); o.holder = to; emit Transferred(id, msg.sender, to); } /// @notice During the window, pay the strike to the writer and take the collateral. function exercise(uint256 id) external nonReentrant { Option storage o = _get(id); if (o.state != State.Sold) revert WrongState(); if (msg.sender != o.holder) revert NotHolder(); if (block.timestamp < o.expiry) revert WindowClosed(); if (block.timestamp >= uint256(o.expiry) + EXERCISE_WINDOW) revert WindowClosed(); o.state = State.Exercised; uint256 cost = _cost(o.amount, o.strike); emit Exercised(id, msg.sender, cost); _pull(strikeAsset, msg.sender, o.writer, cost); _push(collateral, msg.sender, o.amount); } // ───────────────────────────── views ───────────────────────────── function count() external view returns (uint256) { return _options.length; } function getOption(uint256 id) external view returns (Option memory) { return _get(id); } /// @notice A page of options, for interfaces. function getOptions(uint256 start, uint256 max) external view returns (Option[] memory page) { uint256 n = _options.length; if (start >= n) return new Option[](0); uint256 end = start + max > n ? n : start + max; page = new Option[](end - start); for (uint256 i = start; i < end; ++i) { page[i - start] = _options[i]; } } /// @notice What the holder pays, in strike-asset units, to exercise option `id`. function exerciseCost(uint256 id) external view returns (uint256) { Option storage o = _get(id); return _cost(o.amount, o.strike); } // ───────────────────────────── internals ───────────────────────────── function _get(uint256 id) private view returns (Option storage o) { if (id >= _options.length) revert UnknownOption(); o = _options[id]; } /// @dev Rounded up, so a holder can never underpay the writer by a unit. function _cost(uint256 amount, uint256 strike) private pure returns (uint256) { return (amount * strike + 1e18 - 1) / 1e18; } function _send(address to, uint256 value) private { (bool ok,) = to.call{value: value}(""); if (!ok) revert TransferFailed(); } function _push(address token, address to, uint256 value) private { _call(token, abi.encodeWithSelector(0xa9059cbb, to, value)); // transfer(address,uint256) } function _pull(address token, address from, address to, uint256 value) private { _call(token, abi.encodeWithSelector(0x23b872dd, from, to, value)); // transferFrom(address,address,uint256) } /// @dev Accepts tokens that return true and tokens that return nothing. function _call(address token, bytes memory data) private { if (token.code.length == 0) revert TransferFailed(); (bool ok, bytes memory ret) = token.call(data); if (!ok || (ret.length != 0 && !abi.decode(ret, (bool)))) revert TransferFailed(); } }