Genesis.sol
154 lines · sha256 c5a5a66b88eccd8e2c253396cdc777df533fb3506f567736125b41ec5628259c · Raw
| 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.26; |
| 3 | |
| 4 | import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; |
| 5 | import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; |
| 6 | import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 7 | import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 8 | import {KibbleToken} from "./KibbleToken.sol"; |
| 9 | import {Emission} from "./Emission.sol"; |
| 10 | import {Oracle} from "./Oracle.sol"; |
| 11 | import {LpLocker} from "./LpLocker.sol"; |
| 12 | import {Miner} from "./Miner.sol"; |
| 13 | import {PoolSwapper} from "./libraries/PoolSwapper.sol"; |
| 14 | import {PriceMath} from "./libraries/PriceMath.sol"; |
| 15 | import {ChainBlock} from "./libraries/ChainBlock.sol"; |
| 16 | import {KibbleConstants as K} from "./libraries/KibbleConstants.sol"; |
| 17 | |
| 18 | /// @title Genesis |
| 19 | /// @notice Preorder, block 0 trigger, pool seeding, genesis LP lock (docs §6, §11). |
| 20 | /// @dev Preorder end condition (open decision, see README): `startMining()` is callable by anyone, once, when the |
| 21 | /// current block >= the immutable `preorderEndBlock` and at least one rig was sold (docs §13). |
| 22 | contract Genesis is PoolSwapper, ReentrancyGuard { |
| 23 | using SafeERC20 for IERC20; |
| 24 | |
| 25 | error NotMiner(); |
| 26 | error AlreadyStarted(); |
| 27 | error PreorderNotEnded(); |
| 28 | error NoRigsSold(); |
| 29 | error TransferFailed(); |
| 30 | |
| 31 | /// @notice docs §11. |
| 32 | event MiningStarted(uint256 block0, uint256 genesisPrice, uint256 rigsPreordered); |
| 33 | |
| 34 | enum Phase { |
| 35 | Preorder, |
| 36 | Mining |
| 37 | } |
| 38 | |
| 39 | Miner public immutable miner; |
| 40 | Emission public immutable emission; |
| 41 | Oracle public immutable oracle; |
| 42 | LpLocker public immutable lpLocker; |
| 43 | address payable public immutable burner; |
| 44 | /// @notice First block at which `startMining()` may be called. |
| 45 | uint256 public immutable preorderEndBlock; |
| 46 | |
| 47 | /// @notice HYPE collected by preorder rig sales (docs §6), all of it seeds the pool. |
| 48 | uint256 public preorderHype; |
| 49 | /// @notice Block where mining started (docs §15 "Block 0"). 0 during preorder. |
| 50 | uint256 public block0; |
| 51 | /// @notice Rigs sold during preorder. |
| 52 | uint256 public rigsPreordered; |
| 53 | /// @notice p0 = preorder HYPE / 1,000,000, HYPE per KIBBLE, 1e18 scaled (docs §6). |
| 54 | uint256 public genesisPrice; |
| 55 | /// @notice Liquidity of the genesis position (LpLocker position 0). |
| 56 | uint128 public genesisLiquidity; |
| 57 | |
| 58 | constructor( |
| 59 | IPoolManager poolManager_, |
| 60 | KibbleToken kibble_, |
| 61 | Miner miner_, |
| 62 | Emission emission_, |
| 63 | Oracle oracle_, |
| 64 | LpLocker lpLocker_, |
| 65 | address payable burner_, |
| 66 | uint256 preorderEndBlock_ |
| 67 | ) PoolSwapper(poolManager_, address(kibble_)) { |
| 68 | miner = miner_; |
| 69 | emission = emission_; |
| 70 | oracle = oracle_; |
| 71 | lpLocker = lpLocker_; |
| 72 | burner = burner_; |
| 73 | preorderEndBlock = preorderEndBlock_; |
| 74 | } |
| 75 | |
| 76 | /// @dev Refunds from LpLocker and PoolManager takes. |
| 77 | receive() external payable {} |
| 78 | |
| 79 | /// @notice True once block 0 happened. |
| 80 | function miningStarted() public view returns (bool) { |
| 81 | return block0 != 0; |
| 82 | } |
| 83 | |
| 84 | /// @notice 0 = Preorder, 1 = Mining (docs §14 "Phase"). |
| 85 | function phase() external view returns (Phase) { |
| 86 | return block0 == 0 ? Phase.Preorder : Phase.Mining; |
| 87 | } |
| 88 | |
| 89 | /// @notice Blocks left before `startMining()` becomes callable (0 once callable or started). |
| 90 | function blocksToPreorderEnd() external view returns (uint256) { |
| 91 | uint256 n = ChainBlock.number(); |
| 92 | return n >= preorderEndBlock ? 0 : preorderEndBlock - n; |
| 93 | } |
| 94 | |
| 95 | /// @notice Genesis price implied by the current preorder (docs §6), live during preorder, fixed after. |
| 96 | function impliedGenesisPrice() external view returns (uint256) { |
| 97 | return block0 == 0 ? preorderHype * 1e18 / K.GENESIS_SUPPLY : genesisPrice; |
| 98 | } |
| 99 | |
| 100 | /// @notice Preorder HYPE, forwarded by `Miner.buyRigs` before block 0 (docs §3 "held by the protocol"). |
| 101 | function depositPreorder() external payable { |
| 102 | if (msg.sender != address(miner)) revert NotMiner(); |
| 103 | if (block0 != 0) revert AlreadyStarted(); |
| 104 | preorderHype += msg.value; |
| 105 | } |
| 106 | |
| 107 | /// @notice Closes the preorder and starts mining (docs §6, §11): mints 1,000,000 KIBBLE, seeds the HYPE / KIBBLE |
| 108 | /// pool at p0 = preorder HYPE / 1,000,000 with 100% of preorder HYPE, locks the position in `LpLocker`, |
| 109 | /// sets block 0. Reverts with 0 rigs sold (docs §13). |
| 110 | function startMining() external nonReentrant { |
| 111 | if (block0 != 0) revert AlreadyStarted(); |
| 112 | uint256 nowBlock = ChainBlock.number(); |
| 113 | if (nowBlock < preorderEndBlock) revert PreorderNotEnded(); |
| 114 | uint256 rigs = miner.totalRigs(); |
| 115 | if (rigs == 0) revert NoRigsSold(); |
| 116 | |
| 117 | KibbleToken k = KibbleToken(kibbleToken); |
| 118 | k.mintGenesis(address(this)); |
| 119 | |
| 120 | uint256 hype = preorderHype; |
| 121 | uint160 target = PriceMath.sqrtFromAmounts(hype, K.GENESIS_SUPPLY); |
| 122 | uint160 current = _sqrtPrice(); |
| 123 | if (current == 0) { |
| 124 | poolManager.initialize(_poolKey(), target); |
| 125 | } else if (current > target) { |
| 126 | // Pool pre-initialized by a third party with no KIBBLE liquidity possible: move the price down to p0. |
| 127 | _swapExactIn(true, address(this).balance, target); |
| 128 | } else if (current < target) { |
| 129 | _swapExactIn(false, k.balanceOf(address(this)), target); |
| 130 | } |
| 131 | |
| 132 | uint256 kib = k.balanceOf(address(this)); |
| 133 | IERC20(address(k)).forceApprove(address(lpLocker), kib); |
| 134 | (genesisLiquidity,,) = lpLocker.addLiquidity{value: address(this).balance}(kib); |
| 135 | IERC20(address(k)).forceApprove(address(lpLocker), 0); |
| 136 | |
| 137 | // Rounding dust: KIBBLE is burned, HYPE bought back and burned, by the Burner. |
| 138 | uint256 kibDust = k.balanceOf(address(this)); |
| 139 | if (kibDust > 0) IERC20(address(k)).safeTransfer(burner, kibDust); |
| 140 | uint256 hypeDust = address(this).balance; |
| 141 | if (hypeDust > 0) { |
| 142 | (bool ok,) = burner.call{value: hypeDust}(""); |
| 143 | if (!ok) revert TransferFailed(); |
| 144 | } |
| 145 | |
| 146 | block0 = nowBlock; |
| 147 | rigsPreordered = rigs; |
| 148 | genesisPrice = hype * 1e18 / K.GENESIS_SUPPLY; |
| 149 | oracle.initialize(); |
| 150 | emission.sync(); |
| 151 | emit MiningStarted(nowBlock, genesisPrice, rigs); |
| 152 | } |
| 153 | } |
| 154 |