// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {KibbleToken} from "./KibbleToken.sol"; import {Emission} from "./Emission.sol"; import {Oracle} from "./Oracle.sol"; import {LpLocker} from "./LpLocker.sol"; import {Miner} from "./Miner.sol"; import {PoolSwapper} from "./libraries/PoolSwapper.sol"; import {PriceMath} from "./libraries/PriceMath.sol"; import {ChainBlock} from "./libraries/ChainBlock.sol"; import {KibbleConstants as K} from "./libraries/KibbleConstants.sol"; /// @title Genesis /// @notice Preorder, block 0 trigger, pool seeding, genesis LP lock (docs §6, §11). /// @dev Preorder end condition (open decision, see README): `startMining()` is callable by anyone, once, when the /// current block >= the immutable `preorderEndBlock` and at least one rig was sold (docs §13). contract Genesis is PoolSwapper, ReentrancyGuard { using SafeERC20 for IERC20; error NotMiner(); error AlreadyStarted(); error PreorderNotEnded(); error NoRigsSold(); error TransferFailed(); /// @notice docs §11. event MiningStarted(uint256 block0, uint256 genesisPrice, uint256 rigsPreordered); enum Phase { Preorder, Mining } Miner public immutable miner; Emission public immutable emission; Oracle public immutable oracle; LpLocker public immutable lpLocker; address payable public immutable burner; /// @notice First block at which `startMining()` may be called. uint256 public immutable preorderEndBlock; /// @notice HYPE collected by preorder rig sales (docs §6), all of it seeds the pool. uint256 public preorderHype; /// @notice Block where mining started (docs §15 "Block 0"). 0 during preorder. uint256 public block0; /// @notice Rigs sold during preorder. uint256 public rigsPreordered; /// @notice p0 = preorder HYPE / 1,000,000, HYPE per KIBBLE, 1e18 scaled (docs §6). uint256 public genesisPrice; /// @notice Liquidity of the genesis position (LpLocker position 0). uint128 public genesisLiquidity; constructor( IPoolManager poolManager_, KibbleToken kibble_, Miner miner_, Emission emission_, Oracle oracle_, LpLocker lpLocker_, address payable burner_, uint256 preorderEndBlock_ ) PoolSwapper(poolManager_, address(kibble_)) { miner = miner_; emission = emission_; oracle = oracle_; lpLocker = lpLocker_; burner = burner_; preorderEndBlock = preorderEndBlock_; } /// @dev Refunds from LpLocker and PoolManager takes. receive() external payable {} /// @notice True once block 0 happened. function miningStarted() public view returns (bool) { return block0 != 0; } /// @notice 0 = Preorder, 1 = Mining (docs §14 "Phase"). function phase() external view returns (Phase) { return block0 == 0 ? Phase.Preorder : Phase.Mining; } /// @notice Blocks left before `startMining()` becomes callable (0 once callable or started). function blocksToPreorderEnd() external view returns (uint256) { uint256 n = ChainBlock.number(); return n >= preorderEndBlock ? 0 : preorderEndBlock - n; } /// @notice Genesis price implied by the current preorder (docs §6), live during preorder, fixed after. function impliedGenesisPrice() external view returns (uint256) { return block0 == 0 ? preorderHype * 1e18 / K.GENESIS_SUPPLY : genesisPrice; } /// @notice Preorder HYPE, forwarded by `Miner.buyRigs` before block 0 (docs §3 "held by the protocol"). function depositPreorder() external payable { if (msg.sender != address(miner)) revert NotMiner(); if (block0 != 0) revert AlreadyStarted(); preorderHype += msg.value; } /// @notice Closes the preorder and starts mining (docs §6, §11): mints 1,000,000 KIBBLE, seeds the HYPE / KIBBLE /// pool at p0 = preorder HYPE / 1,000,000 with 100% of preorder HYPE, locks the position in `LpLocker`, /// sets block 0. Reverts with 0 rigs sold (docs §13). function startMining() external nonReentrant { if (block0 != 0) revert AlreadyStarted(); uint256 nowBlock = ChainBlock.number(); if (nowBlock < preorderEndBlock) revert PreorderNotEnded(); uint256 rigs = miner.totalRigs(); if (rigs == 0) revert NoRigsSold(); KibbleToken k = KibbleToken(kibbleToken); k.mintGenesis(address(this)); uint256 hype = preorderHype; uint160 target = PriceMath.sqrtFromAmounts(hype, K.GENESIS_SUPPLY); uint160 current = _sqrtPrice(); if (current == 0) { poolManager.initialize(_poolKey(), target); } else if (current > target) { // Pool pre-initialized by a third party with no KIBBLE liquidity possible: move the price down to p0. _swapExactIn(true, address(this).balance, target); } else if (current < target) { _swapExactIn(false, k.balanceOf(address(this)), target); } uint256 kib = k.balanceOf(address(this)); IERC20(address(k)).forceApprove(address(lpLocker), kib); (genesisLiquidity,,) = lpLocker.addLiquidity{value: address(this).balance}(kib); IERC20(address(k)).forceApprove(address(lpLocker), 0); // Rounding dust: KIBBLE is burned, HYPE bought back and burned, by the Burner. uint256 kibDust = k.balanceOf(address(this)); if (kibDust > 0) IERC20(address(k)).safeTransfer(burner, kibDust); uint256 hypeDust = address(this).balance; if (hypeDust > 0) { (bool ok,) = burner.call{value: hypeDust}(""); if (!ok) revert TransferFailed(); } block0 = nowBlock; rigsPreordered = rigs; genesisPrice = hype * 1e18 / K.GENESIS_SUPPLY; oracle.initialize(); emission.sync(); emit MiningStarted(nowBlock, genesisPrice, rigs); } }