Miner.t.sol
449 lines · sha256 25423b19fb0929635a915aa0c193e80df206ee843d39ebf10bdd481b495beb6d · Raw
| 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.26; |
| 3 | |
| 4 | import {KibbleBase} from "./utils/KibbleBase.sol"; |
| 5 | import {Miner} from "../src/Miner.sol"; |
| 6 | |
| 7 | contract MinerTest is KibbleBase { |
| 8 | event RigsBought(address user, uint256 amount, uint256 hypePaid); |
| 9 | event PowerDeposited(address user, uint256 hypeEquivalent, bool inKibble); |
| 10 | event PowerWithdrawn(address user, uint256 hypeEquivalent); |
| 11 | event RigsSwitched(address user, uint256 rigsOn); |
| 12 | event TankEmpty(address user, uint256 blockNumber); |
| 13 | event Claimed(address user, uint256 amount); |
| 14 | |
| 15 | uint256 internal constant ONE_BLOCK_UNITS = 1e18; // power units per rig per block |
| 16 | uint256 internal constant PER_BLOCK_E1 = uint256(4_500_000e18) / 216_000; // display only |
| 17 | |
| 18 | // ------------------------------------------------------------ buyRigs |
| 19 | |
| 20 | function test_buyRigs_preorder_goesToGenesis() public { |
| 21 | vm.deal(alice, 5e18); |
| 22 | vm.expectEmit(address(miner)); |
| 23 | emit RigsBought(alice, 5, 5e18); |
| 24 | vm.prank(alice); |
| 25 | miner.buyRigs{value: 5e18}(5); |
| 26 | assertEq(miner.rigsOf(alice), 5); |
| 27 | assertEq(miner.totalRigs(), 5); |
| 28 | assertEq(genesis.preorderHype(), 5e18); |
| 29 | assertEq(address(genesis).balance, 5e18); |
| 30 | assertEq(address(miner).balance, 0); |
| 31 | } |
| 32 | |
| 33 | function test_buyRigs_reverts() public { |
| 34 | vm.deal(alice, 10e18); |
| 35 | vm.startPrank(alice); |
| 36 | vm.expectRevert(Miner.ZeroAmount.selector); |
| 37 | miner.buyRigs{value: 0}(0); |
| 38 | vm.expectRevert(Miner.WrongPayment.selector); |
| 39 | miner.buyRigs{value: 1e18}(2); |
| 40 | vm.expectRevert(Miner.WrongPayment.selector); |
| 41 | miner.buyRigs{value: 3e18}(2); |
| 42 | vm.stopPrank(); |
| 43 | } |
| 44 | |
| 45 | function test_buyRigs_afterBlock0_zapsIntoLockedLp() public { |
| 46 | _launch(10_000); |
| 47 | uint256 posBefore = locker.positionCount(); |
| 48 | uint256 liqBefore = locker.totalLiquidity(); |
| 49 | uint256 lockedHypeBefore = locker.lockedHype(); |
| 50 | _roll(10); |
| 51 | _buy(bob, 100); |
| 52 | assertEq(locker.positionCount(), posBefore + 1); |
| 53 | assertGt(locker.totalLiquidity(), liqBefore); |
| 54 | // ~half of the HYPE paired into LP, the other half bought KIBBLE |
| 55 | assertApproxEqRel(locker.lockedHype() - lockedHypeBefore, 50e18, 0.02e18); |
| 56 | assertEq(genesis.preorderHype(), 10_000e18); // unchanged |
| 57 | // dust left in the zapper is tiny relative to the zap |
| 58 | assertLt(address(zapper).balance, 1e18); |
| 59 | assertEq(miner.rigsOf(bob), 100); |
| 60 | assertEq(address(miner).balance, 10_000 * 36e17); // only alice's tank |
| 61 | } |
| 62 | |
| 63 | function test_rigsAreNotTransferableNorRefundable() public { |
| 64 | // there is simply no function for it: rigs are a balance in Miner |
| 65 | _buy(alice, 3); |
| 66 | (bool ok,) = address(miner).call(abi.encodeWithSignature("transfer(address,uint256)", bob, 1)); |
| 67 | assertFalse(ok); |
| 68 | (ok,) = address(miner).call(abi.encodeWithSignature("sellRigs(uint256)", 1)); |
| 69 | assertFalse(ok); |
| 70 | assertEq(miner.rigsOf(alice), 3); |
| 71 | } |
| 72 | |
| 73 | // ------------------------------------------------------------ power |
| 74 | |
| 75 | function test_depositPower_hype() public { |
| 76 | _buy(alice, 2); |
| 77 | vm.deal(alice, 1e18); |
| 78 | vm.expectEmit(address(miner)); |
| 79 | emit PowerDeposited(alice, 1e18, false); |
| 80 | vm.prank(alice); |
| 81 | miner.depositPower{value: 1e18}(); |
| 82 | (uint256 h, uint256 kh, uint256 ka, uint256 t) = miner.tankOf(alice); |
| 83 | assertEq(h, 1e18); |
| 84 | assertEq(kh, 0); |
| 85 | assertEq(ka, 0); |
| 86 | assertEq(t, 1e18); |
| 87 | } |
| 88 | |
| 89 | function test_depositPower_zeroReverts() public { |
| 90 | vm.expectRevert(Miner.ZeroAmount.selector); |
| 91 | miner.depositPower{value: 0}(); |
| 92 | } |
| 93 | |
| 94 | function test_preorder_noDrawBeforeBlock0() public { |
| 95 | _buy(alice, 10); |
| 96 | _power(alice, 1e18); |
| 97 | _on(alice, 10); |
| 98 | _roll(500); |
| 99 | miner.checkpoint(); |
| 100 | (,,, uint256 t) = miner.tankOf(alice); |
| 101 | assertEq(t, 1e18); |
| 102 | assertEq(miner.pending(alice), 0); |
| 103 | assertEq(miner.rigsOnOf(alice), 10); |
| 104 | assertEq(miner.totalOnlineHashrate(), 10); |
| 105 | assertEq(miner.emptyBlockOf(alice), 0); // unknown before block 0 |
| 106 | assertEq(miner.blocksLeftOf(alice), 6000); // 1 HYPE / (10 x 1/60,000) |
| 107 | _start(); |
| 108 | assertEq(miner.emptyBlockOf(alice), genesis.block0() + 6000); |
| 109 | _roll(1); |
| 110 | (,,, t) = miner.tankOf(alice); |
| 111 | assertEq(t, 1e18 - uint256(10e18) / 60_000 - 1); // floored to wei |
| 112 | assertGt(miner.pending(alice), 0); |
| 113 | } |
| 114 | |
| 115 | function test_depositPowerKibble_beforeBlock0_reverts() public { |
| 116 | vm.prank(alice); |
| 117 | vm.expectRevert(Miner.MiningNotStarted.selector); |
| 118 | miner.depositPowerKibble(1e18); |
| 119 | } |
| 120 | |
| 121 | function test_depositPowerKibble_creditAtTwapOver09() public { |
| 122 | _launch(10_000); |
| 123 | _roll(100); |
| 124 | uint256 got = _claim(alice); |
| 125 | assertGt(got, 900e18); |
| 126 | uint256 twap = oracle.twap(); |
| 127 | assertApproxEqRel(twap, 0.01e18, 1e9); |
| 128 | vm.startPrank(alice); |
| 129 | token.approve(address(miner), 900e18); |
| 130 | miner.depositPowerKibble(900e18); |
| 131 | vm.stopPrank(); |
| 132 | (uint256 h, uint256 kh, uint256 ka,) = miner.tankOf(alice); |
| 133 | assertEq(ka, 900e18); |
| 134 | assertEq(kh, 900e18 * twap * 10 / 9 / 1e18); // 900 x 0.01 / 0.9 = 10 HYPE |
| 135 | assertApproxEqRel(kh, 10e18, 1e9); |
| 136 | assertGt(h, 0); |
| 137 | } |
| 138 | |
| 139 | /// docs §13: power deposited in KIBBLE, withdrawn later, returned in KIBBLE at the deposit rate. |
| 140 | function test_kibblePower_withdrawnAtDepositRate() public { |
| 141 | _launch(10_000); |
| 142 | _roll(100); |
| 143 | _claim(alice); |
| 144 | _buy(bob, 1); |
| 145 | vm.prank(alice); |
| 146 | token.transfer(bob, 500e18); |
| 147 | vm.startPrank(bob); |
| 148 | token.approve(address(miner), 500e18); |
| 149 | miner.depositPowerKibble(500e18); |
| 150 | vm.stopPrank(); |
| 151 | (, uint256 kh,,) = miner.tankOf(bob); |
| 152 | // price moves a lot |
| 153 | _marketSwap(carol, true, 2_000e18); |
| 154 | _roll(20_000); |
| 155 | oracle.update(); |
| 156 | _roll(20_000); |
| 157 | assertGt(oracle.twap(), 0.012e18); |
| 158 | vm.prank(bob); |
| 159 | miner.withdrawPower(kh); |
| 160 | assertEq(token.balanceOf(bob), 500e18); |
| 161 | (,,, uint256 t) = miner.tankOf(bob); |
| 162 | assertEq(t, 0); |
| 163 | } |
| 164 | |
| 165 | /// Consumption order: KIBBLE-backed power first, burned directly. |
| 166 | function test_consumptionOrder_kibbleFirst_drain() public { |
| 167 | _launch(10_000); |
| 168 | _roll(100); |
| 169 | _claim(alice); |
| 170 | _buy(bob, 1); |
| 171 | vm.prank(alice); |
| 172 | token.transfer(bob, 90e18); |
| 173 | vm.startPrank(bob); |
| 174 | token.approve(address(miner), 90e18); |
| 175 | miner.depositPowerKibble(90e18); |
| 176 | vm.stopPrank(); |
| 177 | _power(bob, 1e18); |
| 178 | _on(bob, 1); |
| 179 | (uint256 h0, uint256 kh0, uint256 ka0,) = miner.tankOf(bob); |
| 180 | _roll(30_000); // half of the KIBBLE-backed power |
| 181 | (uint256 h1, uint256 kh1, uint256 ka1,) = miner.tankOf(bob); |
| 182 | assertEq(h1, h0); // HYPE-backed untouched |
| 183 | assertLt(kh1, kh0); |
| 184 | assertApproxEqRel(ka1, ka0 / 2, 0.01e18); |
| 185 | vm.prank(bob); |
| 186 | miner.settle(bob); |
| 187 | assertApproxEqRel(miner.pendingBurnKibble(), ka0 - ka1, 1e6); |
| 188 | // burn the consumed KIBBLE directly |
| 189 | uint256 supply = token.totalSupply(); |
| 190 | uint256 burnedKib = miner.pendingBurnKibble(); |
| 191 | miner.flushBurn(); |
| 192 | assertEq(miner.pendingBurnKibble(), 0); |
| 193 | assertLe(token.totalSupply(), supply - burnedKib); |
| 194 | assertGe(burner.totalBurnedDirect(), burnedKib); |
| 195 | } |
| 196 | |
| 197 | function test_withdrawPower_partialAndAll() public { |
| 198 | _buy(alice, 1); |
| 199 | _power(alice, 2e18); |
| 200 | uint256 bal = alice.balance; |
| 201 | vm.expectEmit(address(miner)); |
| 202 | emit PowerWithdrawn(alice, 0.5e18); |
| 203 | vm.prank(alice); |
| 204 | miner.withdrawPower(0.5e18); |
| 205 | assertEq(alice.balance, bal + 0.5e18); |
| 206 | vm.prank(alice); |
| 207 | miner.withdrawPower(type(uint256).max); |
| 208 | assertEq(alice.balance, bal + 2e18); |
| 209 | (,,, uint256 t) = miner.tankOf(alice); |
| 210 | assertEq(t, 0); |
| 211 | } |
| 212 | |
| 213 | function test_withdrawPower_reverts() public { |
| 214 | _buy(alice, 1); |
| 215 | _power(alice, 1e18); |
| 216 | vm.startPrank(alice); |
| 217 | vm.expectRevert(Miner.ZeroAmount.selector); |
| 218 | miner.withdrawPower(0); |
| 219 | vm.expectRevert(Miner.InsufficientPower.selector); |
| 220 | miner.withdrawPower(2e18); |
| 221 | vm.stopPrank(); |
| 222 | } |
| 223 | |
| 224 | /// docs §13: user withdraws all power while rigs are on -> rigs switch off. |
| 225 | function test_withdrawAll_switchesOff() public { |
| 226 | _launch(10); |
| 227 | _roll(10); |
| 228 | assertEq(miner.totalOnlineHashrate(), 10); |
| 229 | vm.expectEmit(address(miner)); |
| 230 | emit RigsSwitched(alice, 0); |
| 231 | vm.prank(alice); |
| 232 | miner.withdrawPower(type(uint256).max); |
| 233 | assertEq(miner.rigsOnOf(alice), 0); |
| 234 | assertEq(miner.totalOnlineHashrate(), 0); |
| 235 | assertEq(miner.emptyBlockOf(alice), 0); |
| 236 | uint256 p = miner.pending(alice); |
| 237 | _roll(100); |
| 238 | assertEq(miner.pending(alice), p); // mines nothing once off |
| 239 | } |
| 240 | |
| 241 | function test_withdrawPartial_keepsRigsOn_newEmptyBlock() public { |
| 242 | _launch(1); |
| 243 | uint256 b0 = genesis.block0(); |
| 244 | assertEq(miner.emptyBlockOf(alice), b0 + ERA); |
| 245 | vm.prank(alice); |
| 246 | miner.withdrawPower(1.8e18); |
| 247 | assertEq(miner.rigsOnOf(alice), 1); |
| 248 | assertEq(miner.emptyBlockOf(alice), b0 + ERA / 2); |
| 249 | } |
| 250 | |
| 251 | // ------------------------------------------------------------ setRigsOn |
| 252 | |
| 253 | function test_setRigsOn_reverts() public { |
| 254 | _buy(alice, 2); |
| 255 | vm.startPrank(alice); |
| 256 | vm.expectRevert(Miner.TooManyRigs.selector); |
| 257 | miner.setRigsOn(3); |
| 258 | // docs §13: switch on with an empty tank reverts |
| 259 | vm.expectRevert(Miner.EmptyTank.selector); |
| 260 | miner.setRigsOn(1); |
| 261 | vm.stopPrank(); |
| 262 | // a tank that cannot power the rigs for a single block is empty too |
| 263 | _power(alice, 16666666666666); // < 1/60,000 HYPE |
| 264 | vm.prank(alice); |
| 265 | vm.expectRevert(Miner.EmptyTank.selector); |
| 266 | miner.setRigsOn(1); |
| 267 | _power(alice, 1); |
| 268 | _on(alice, 1); // exactly one block |
| 269 | assertEq(miner.blocksLeftOf(alice), 1); |
| 270 | } |
| 271 | |
| 272 | function test_setRigsOn_offAndOn() public { |
| 273 | _launch(4); |
| 274 | _roll(100); |
| 275 | vm.expectEmit(address(miner)); |
| 276 | emit RigsSwitched(alice, 1); |
| 277 | _on(alice, 1); |
| 278 | assertEq(miner.totalOnlineHashrate(), 1); |
| 279 | _on(alice, 0); |
| 280 | assertEq(miner.totalOnlineHashrate(), 0); |
| 281 | uint256 p = miner.pending(alice); |
| 282 | _roll(100); |
| 283 | assertEq(miner.pending(alice), p); |
| 284 | (,,, uint256 t) = miner.tankOf(alice); |
| 285 | _roll(100); |
| 286 | (,,, uint256 t2) = miner.tankOf(alice); |
| 287 | assertEq(t, t2); // off draws nothing |
| 288 | } |
| 289 | |
| 290 | // ------------------------------------------------------------ rewards |
| 291 | |
| 292 | /// docs §5: wallet share = reward x wallet hashrate / total hashrate. |
| 293 | function test_proRataShare() public { |
| 294 | _buy(alice, 3); |
| 295 | _buy(bob, 1); |
| 296 | _power(alice, 10e18); |
| 297 | _power(bob, 10e18); |
| 298 | _on(alice, 3); |
| 299 | _on(bob, 1); |
| 300 | _start(); |
| 301 | _roll(1000); |
| 302 | uint256 pa = miner.pending(alice); |
| 303 | uint256 pb = miner.pending(bob); |
| 304 | uint256 blockReward = uint256(1000) * 4_500_000e18 / 216_000; |
| 305 | assertApproxEqAbs(pa, blockReward * 3 / 4, 2); |
| 306 | assertApproxEqAbs(pb, blockReward / 4, 2); |
| 307 | assertEq(_claim(alice), pa); |
| 308 | assertEq(token.balanceOf(alice), pa); |
| 309 | assertEq(miner.pending(alice), 0); |
| 310 | } |
| 311 | |
| 312 | /// docs §13: no rig on during a block -> reward not minted, not carried over. |
| 313 | function test_noRigOn_noEmission() public { |
| 314 | _buy(alice, 1); |
| 315 | _power(alice, 1e18); |
| 316 | _start(); // alice is off |
| 317 | _roll(1000); |
| 318 | miner.checkpoint(); |
| 319 | assertEq(miner.totalAllocated(), 0); |
| 320 | _on(alice, 1); |
| 321 | _roll(10); |
| 322 | uint256 got = _claim(alice); |
| 323 | assertEq(got, uint256(10) * 4_500_000e18 / 216_000); |
| 324 | assertEq(miner.totalAllocated(), got); // the first 1000 blocks never exist |
| 325 | } |
| 326 | |
| 327 | /// docs §13: tank empties mid-update -> rigs counted until the exact empty block, then off. |
| 328 | function test_tankEmptiesMidUpdate_exactBlock() public { |
| 329 | _buy(alice, 1); |
| 330 | _buy(bob, 1); |
| 331 | _power(alice, 3e16); // 1,800 blocks |
| 332 | _power(bob, 1e18); |
| 333 | _on(alice, 1); |
| 334 | _on(bob, 1); |
| 335 | _start(); |
| 336 | uint256 b0 = genesis.block0(); |
| 337 | assertEq(miner.emptyBlockOf(alice), b0 + 1800); |
| 338 | vm.roll(b0 + 3000); |
| 339 | assertEq(miner.totalOnlineHashrate(), 1); |
| 340 | assertEq(miner.rigsOnOf(alice), 0); |
| 341 | uint256 e1 = 4_500_000e18; |
| 342 | uint256 expectA = 1800 * e1 / 216_000 / 2; |
| 343 | uint256 expectB = 1800 * e1 / 216_000 / 2 + 1200 * e1 / 216_000; |
| 344 | assertApproxEqAbs(miner.pending(alice), expectA, 2); |
| 345 | assertApproxEqAbs(miner.pending(bob), expectB, 2); |
| 346 | vm.expectEmit(address(miner)); |
| 347 | emit TankEmpty(alice, b0 + 1800); |
| 348 | _claim(alice); |
| 349 | (,,, uint256 t) = miner.tankOf(alice); |
| 350 | assertEq(t, 0); |
| 351 | assertEq(_claim(bob), miner.pending(bob) + token.balanceOf(bob)); |
| 352 | } |
| 353 | |
| 354 | function test_tankEmpty_reloadNeedsSwitchOn() public { |
| 355 | _buy(alice, 1); |
| 356 | _power(alice, 3e16); |
| 357 | _on(alice, 1); |
| 358 | _start(); |
| 359 | _roll(2000); |
| 360 | miner.settle(alice); |
| 361 | assertEq(miner.rigsOnOf(alice), 0); |
| 362 | _power(alice, 3e16); |
| 363 | assertEq(miner.rigsOnOf(alice), 0); // FAQ: switched back on after a refill |
| 364 | _on(alice, 1); |
| 365 | assertEq(miner.rigsOnOf(alice), 1); |
| 366 | } |
| 367 | |
| 368 | function test_manyWalletsExpiringAtDifferentBlocks() public { |
| 369 | uint256 n = 40; |
| 370 | for (uint256 i; i < n; ++i) { |
| 371 | address u = address(uint160(0x1000 + i)); |
| 372 | _buy(u, 1); |
| 373 | _power(u, (i + 1) * 1e15); // 60 x (i+1) blocks |
| 374 | _on(u, 1); |
| 375 | } |
| 376 | _start(); |
| 377 | assertEq(miner.totalOnlineHashrate(), n); |
| 378 | _roll(60 * 20); |
| 379 | assertEq(miner.totalOnlineHashrate(), n - 20); // wallets 0..19 expired (wallet 19 exactly at 1200) |
| 380 | _roll(1_000_000); |
| 381 | // bounded checkpoints |
| 382 | bool done = miner.checkpointPartial(10); |
| 383 | assertFalse(done); |
| 384 | assertEq(miner.onlineHashrateAccounted(), n - 10); |
| 385 | done = miner.checkpointPartial(100); |
| 386 | assertTrue(done); |
| 387 | assertEq(miner.onlineHashrateAccounted(), 0); |
| 388 | uint256 sum; |
| 389 | for (uint256 i; i < n; ++i) { |
| 390 | address u = address(uint160(0x1000 + i)); |
| 391 | sum += _claim(u); |
| 392 | } |
| 393 | assertLe(sum, miner.totalAllocated()); |
| 394 | assertApproxEqAbs(sum, miner.totalAllocated(), n * 2); |
| 395 | } |
| 396 | |
| 397 | function test_claim_nothing() public { |
| 398 | assertEq(_claim(alice), 0); |
| 399 | } |
| 400 | |
| 401 | function test_checkpoint_permissionless_and_era_event() public { |
| 402 | _launch(1); |
| 403 | _roll(ERA * 2 + 5); |
| 404 | vm.expectEmit(address(emission)); |
| 405 | emit EraStarted(2, 10_416666666666666666, genesis.block0() + ERA); |
| 406 | vm.prank(carol); |
| 407 | miner.checkpoint(); |
| 408 | assertEq(emission.lastAnnouncedEra(), 3); |
| 409 | } |
| 410 | |
| 411 | event EraStarted(uint256 era, uint256 rewardPerBlock, uint256 startBlock); |
| 412 | |
| 413 | function test_settle_realizesBurnForIdleUser() public { |
| 414 | _launch(10); |
| 415 | _roll(ERA + 1); |
| 416 | assertEq(miner.pendingBurnHypeUnits(), 0); |
| 417 | vm.prank(carol); |
| 418 | miner.settle(alice); |
| 419 | assertEq(miner.pendingBurnHypeUnits(), 36e18 * 60_000); |
| 420 | } |
| 421 | |
| 422 | // ------------------------------------------------------------ burn |
| 423 | |
| 424 | function test_flushBurn_buybackAndBurn() public { |
| 425 | _launch(10_000); |
| 426 | _roll(10_000); |
| 427 | miner.settle(alice); |
| 428 | uint256 hypeConsumed = miner.pendingBurnHypeUnits() / 60_000; |
| 429 | assertGt(hypeConsumed, 0); |
| 430 | uint256 supply = token.totalSupply(); |
| 431 | hypeConsumed += address(burner).balance; // genesis rounding dust already waiting there |
| 432 | miner.flushBurn(); |
| 433 | // 1,666 HYPE into a 10,000 HYPE pool would move the price > 10%: the buyback stops at TWAP x 1.1 |
| 434 | assertGt(burner.totalHypeSpent(), 0); |
| 435 | assertEq(burner.totalHypeSpent() + address(burner).balance, hypeConsumed); |
| 436 | assertLe(oracle.spotPrice(), oracle.twap() * 11_000 / 10_000 + 1e9); |
| 437 | assertGt(burner.totalBurned(), 0); |
| 438 | assertEq(token.totalSupply(), supply - burner.totalBurned()); |
| 439 | // once the TWAP catches up, the rest is bought back and burned |
| 440 | for (uint256 i; i < 20 && address(burner).balance > 0; ++i) { |
| 441 | _roll(5_000); |
| 442 | oracle.update(); |
| 443 | burner.burn(); |
| 444 | } |
| 445 | assertEq(address(burner).balance, 0); |
| 446 | assertEq(burner.totalHypeSpent(), hypeConsumed); |
| 447 | } |
| 448 | } |
| 449 |