// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; import {KibbleBase} from "./utils/KibbleBase.sol"; import {Miner} from "../src/Miner.sol"; contract MinerTest is KibbleBase { event RigsBought(address user, uint256 amount, uint256 hypePaid); event PowerDeposited(address user, uint256 hypeEquivalent, bool inKibble); event PowerWithdrawn(address user, uint256 hypeEquivalent); event RigsSwitched(address user, uint256 rigsOn); event TankEmpty(address user, uint256 blockNumber); event Claimed(address user, uint256 amount); uint256 internal constant ONE_BLOCK_UNITS = 1e18; // power units per rig per block uint256 internal constant PER_BLOCK_E1 = uint256(4_500_000e18) / 216_000; // display only // ------------------------------------------------------------ buyRigs function test_buyRigs_preorder_goesToGenesis() public { vm.deal(alice, 5e18); vm.expectEmit(address(miner)); emit RigsBought(alice, 5, 5e18); vm.prank(alice); miner.buyRigs{value: 5e18}(5); assertEq(miner.rigsOf(alice), 5); assertEq(miner.totalRigs(), 5); assertEq(genesis.preorderHype(), 5e18); assertEq(address(genesis).balance, 5e18); assertEq(address(miner).balance, 0); } function test_buyRigs_reverts() public { vm.deal(alice, 10e18); vm.startPrank(alice); vm.expectRevert(Miner.ZeroAmount.selector); miner.buyRigs{value: 0}(0); vm.expectRevert(Miner.WrongPayment.selector); miner.buyRigs{value: 1e18}(2); vm.expectRevert(Miner.WrongPayment.selector); miner.buyRigs{value: 3e18}(2); vm.stopPrank(); } function test_buyRigs_afterBlock0_zapsIntoLockedLp() public { _launch(10_000); uint256 posBefore = locker.positionCount(); uint256 liqBefore = locker.totalLiquidity(); uint256 lockedHypeBefore = locker.lockedHype(); _roll(10); _buy(bob, 100); assertEq(locker.positionCount(), posBefore + 1); assertGt(locker.totalLiquidity(), liqBefore); // ~half of the HYPE paired into LP, the other half bought KIBBLE assertApproxEqRel(locker.lockedHype() - lockedHypeBefore, 50e18, 0.02e18); assertEq(genesis.preorderHype(), 10_000e18); // unchanged // dust left in the zapper is tiny relative to the zap assertLt(address(zapper).balance, 1e18); assertEq(miner.rigsOf(bob), 100); assertEq(address(miner).balance, 10_000 * 36e17); // only alice's tank } function test_rigsAreNotTransferableNorRefundable() public { // there is simply no function for it: rigs are a balance in Miner _buy(alice, 3); (bool ok,) = address(miner).call(abi.encodeWithSignature("transfer(address,uint256)", bob, 1)); assertFalse(ok); (ok,) = address(miner).call(abi.encodeWithSignature("sellRigs(uint256)", 1)); assertFalse(ok); assertEq(miner.rigsOf(alice), 3); } // ------------------------------------------------------------ power function test_depositPower_hype() public { _buy(alice, 2); vm.deal(alice, 1e18); vm.expectEmit(address(miner)); emit PowerDeposited(alice, 1e18, false); vm.prank(alice); miner.depositPower{value: 1e18}(); (uint256 h, uint256 kh, uint256 ka, uint256 t) = miner.tankOf(alice); assertEq(h, 1e18); assertEq(kh, 0); assertEq(ka, 0); assertEq(t, 1e18); } function test_depositPower_zeroReverts() public { vm.expectRevert(Miner.ZeroAmount.selector); miner.depositPower{value: 0}(); } function test_preorder_noDrawBeforeBlock0() public { _buy(alice, 10); _power(alice, 1e18); _on(alice, 10); _roll(500); miner.checkpoint(); (,,, uint256 t) = miner.tankOf(alice); assertEq(t, 1e18); assertEq(miner.pending(alice), 0); assertEq(miner.rigsOnOf(alice), 10); assertEq(miner.totalOnlineHashrate(), 10); assertEq(miner.emptyBlockOf(alice), 0); // unknown before block 0 assertEq(miner.blocksLeftOf(alice), 6000); // 1 HYPE / (10 x 1/60,000) _start(); assertEq(miner.emptyBlockOf(alice), genesis.block0() + 6000); _roll(1); (,,, t) = miner.tankOf(alice); assertEq(t, 1e18 - uint256(10e18) / 60_000 - 1); // floored to wei assertGt(miner.pending(alice), 0); } function test_depositPowerKibble_beforeBlock0_reverts() public { vm.prank(alice); vm.expectRevert(Miner.MiningNotStarted.selector); miner.depositPowerKibble(1e18); } function test_depositPowerKibble_creditAtTwapOver09() public { _launch(10_000); _roll(100); uint256 got = _claim(alice); assertGt(got, 900e18); uint256 twap = oracle.twap(); assertApproxEqRel(twap, 0.01e18, 1e9); vm.startPrank(alice); token.approve(address(miner), 900e18); miner.depositPowerKibble(900e18); vm.stopPrank(); (uint256 h, uint256 kh, uint256 ka,) = miner.tankOf(alice); assertEq(ka, 900e18); assertEq(kh, 900e18 * twap * 10 / 9 / 1e18); // 900 x 0.01 / 0.9 = 10 HYPE assertApproxEqRel(kh, 10e18, 1e9); assertGt(h, 0); } /// docs §13: power deposited in KIBBLE, withdrawn later, returned in KIBBLE at the deposit rate. function test_kibblePower_withdrawnAtDepositRate() public { _launch(10_000); _roll(100); _claim(alice); _buy(bob, 1); vm.prank(alice); token.transfer(bob, 500e18); vm.startPrank(bob); token.approve(address(miner), 500e18); miner.depositPowerKibble(500e18); vm.stopPrank(); (, uint256 kh,,) = miner.tankOf(bob); // price moves a lot _marketSwap(carol, true, 2_000e18); _roll(20_000); oracle.update(); _roll(20_000); assertGt(oracle.twap(), 0.012e18); vm.prank(bob); miner.withdrawPower(kh); assertEq(token.balanceOf(bob), 500e18); (,,, uint256 t) = miner.tankOf(bob); assertEq(t, 0); } /// Consumption order: KIBBLE-backed power first, burned directly. function test_consumptionOrder_kibbleFirst_drain() public { _launch(10_000); _roll(100); _claim(alice); _buy(bob, 1); vm.prank(alice); token.transfer(bob, 90e18); vm.startPrank(bob); token.approve(address(miner), 90e18); miner.depositPowerKibble(90e18); vm.stopPrank(); _power(bob, 1e18); _on(bob, 1); (uint256 h0, uint256 kh0, uint256 ka0,) = miner.tankOf(bob); _roll(30_000); // half of the KIBBLE-backed power (uint256 h1, uint256 kh1, uint256 ka1,) = miner.tankOf(bob); assertEq(h1, h0); // HYPE-backed untouched assertLt(kh1, kh0); assertApproxEqRel(ka1, ka0 / 2, 0.01e18); vm.prank(bob); miner.settle(bob); assertApproxEqRel(miner.pendingBurnKibble(), ka0 - ka1, 1e6); // burn the consumed KIBBLE directly uint256 supply = token.totalSupply(); uint256 burnedKib = miner.pendingBurnKibble(); miner.flushBurn(); assertEq(miner.pendingBurnKibble(), 0); assertLe(token.totalSupply(), supply - burnedKib); assertGe(burner.totalBurnedDirect(), burnedKib); } function test_withdrawPower_partialAndAll() public { _buy(alice, 1); _power(alice, 2e18); uint256 bal = alice.balance; vm.expectEmit(address(miner)); emit PowerWithdrawn(alice, 0.5e18); vm.prank(alice); miner.withdrawPower(0.5e18); assertEq(alice.balance, bal + 0.5e18); vm.prank(alice); miner.withdrawPower(type(uint256).max); assertEq(alice.balance, bal + 2e18); (,,, uint256 t) = miner.tankOf(alice); assertEq(t, 0); } function test_withdrawPower_reverts() public { _buy(alice, 1); _power(alice, 1e18); vm.startPrank(alice); vm.expectRevert(Miner.ZeroAmount.selector); miner.withdrawPower(0); vm.expectRevert(Miner.InsufficientPower.selector); miner.withdrawPower(2e18); vm.stopPrank(); } /// docs §13: user withdraws all power while rigs are on -> rigs switch off. function test_withdrawAll_switchesOff() public { _launch(10); _roll(10); assertEq(miner.totalOnlineHashrate(), 10); vm.expectEmit(address(miner)); emit RigsSwitched(alice, 0); vm.prank(alice); miner.withdrawPower(type(uint256).max); assertEq(miner.rigsOnOf(alice), 0); assertEq(miner.totalOnlineHashrate(), 0); assertEq(miner.emptyBlockOf(alice), 0); uint256 p = miner.pending(alice); _roll(100); assertEq(miner.pending(alice), p); // mines nothing once off } function test_withdrawPartial_keepsRigsOn_newEmptyBlock() public { _launch(1); uint256 b0 = genesis.block0(); assertEq(miner.emptyBlockOf(alice), b0 + ERA); vm.prank(alice); miner.withdrawPower(1.8e18); assertEq(miner.rigsOnOf(alice), 1); assertEq(miner.emptyBlockOf(alice), b0 + ERA / 2); } // ------------------------------------------------------------ setRigsOn function test_setRigsOn_reverts() public { _buy(alice, 2); vm.startPrank(alice); vm.expectRevert(Miner.TooManyRigs.selector); miner.setRigsOn(3); // docs §13: switch on with an empty tank reverts vm.expectRevert(Miner.EmptyTank.selector); miner.setRigsOn(1); vm.stopPrank(); // a tank that cannot power the rigs for a single block is empty too _power(alice, 16666666666666); // < 1/60,000 HYPE vm.prank(alice); vm.expectRevert(Miner.EmptyTank.selector); miner.setRigsOn(1); _power(alice, 1); _on(alice, 1); // exactly one block assertEq(miner.blocksLeftOf(alice), 1); } function test_setRigsOn_offAndOn() public { _launch(4); _roll(100); vm.expectEmit(address(miner)); emit RigsSwitched(alice, 1); _on(alice, 1); assertEq(miner.totalOnlineHashrate(), 1); _on(alice, 0); assertEq(miner.totalOnlineHashrate(), 0); uint256 p = miner.pending(alice); _roll(100); assertEq(miner.pending(alice), p); (,,, uint256 t) = miner.tankOf(alice); _roll(100); (,,, uint256 t2) = miner.tankOf(alice); assertEq(t, t2); // off draws nothing } // ------------------------------------------------------------ rewards /// docs §5: wallet share = reward x wallet hashrate / total hashrate. function test_proRataShare() public { _buy(alice, 3); _buy(bob, 1); _power(alice, 10e18); _power(bob, 10e18); _on(alice, 3); _on(bob, 1); _start(); _roll(1000); uint256 pa = miner.pending(alice); uint256 pb = miner.pending(bob); uint256 blockReward = uint256(1000) * 4_500_000e18 / 216_000; assertApproxEqAbs(pa, blockReward * 3 / 4, 2); assertApproxEqAbs(pb, blockReward / 4, 2); assertEq(_claim(alice), pa); assertEq(token.balanceOf(alice), pa); assertEq(miner.pending(alice), 0); } /// docs §13: no rig on during a block -> reward not minted, not carried over. function test_noRigOn_noEmission() public { _buy(alice, 1); _power(alice, 1e18); _start(); // alice is off _roll(1000); miner.checkpoint(); assertEq(miner.totalAllocated(), 0); _on(alice, 1); _roll(10); uint256 got = _claim(alice); assertEq(got, uint256(10) * 4_500_000e18 / 216_000); assertEq(miner.totalAllocated(), got); // the first 1000 blocks never exist } /// docs §13: tank empties mid-update -> rigs counted until the exact empty block, then off. function test_tankEmptiesMidUpdate_exactBlock() public { _buy(alice, 1); _buy(bob, 1); _power(alice, 3e16); // 1,800 blocks _power(bob, 1e18); _on(alice, 1); _on(bob, 1); _start(); uint256 b0 = genesis.block0(); assertEq(miner.emptyBlockOf(alice), b0 + 1800); vm.roll(b0 + 3000); assertEq(miner.totalOnlineHashrate(), 1); assertEq(miner.rigsOnOf(alice), 0); uint256 e1 = 4_500_000e18; uint256 expectA = 1800 * e1 / 216_000 / 2; uint256 expectB = 1800 * e1 / 216_000 / 2 + 1200 * e1 / 216_000; assertApproxEqAbs(miner.pending(alice), expectA, 2); assertApproxEqAbs(miner.pending(bob), expectB, 2); vm.expectEmit(address(miner)); emit TankEmpty(alice, b0 + 1800); _claim(alice); (,,, uint256 t) = miner.tankOf(alice); assertEq(t, 0); assertEq(_claim(bob), miner.pending(bob) + token.balanceOf(bob)); } function test_tankEmpty_reloadNeedsSwitchOn() public { _buy(alice, 1); _power(alice, 3e16); _on(alice, 1); _start(); _roll(2000); miner.settle(alice); assertEq(miner.rigsOnOf(alice), 0); _power(alice, 3e16); assertEq(miner.rigsOnOf(alice), 0); // FAQ: switched back on after a refill _on(alice, 1); assertEq(miner.rigsOnOf(alice), 1); } function test_manyWalletsExpiringAtDifferentBlocks() public { uint256 n = 40; for (uint256 i; i < n; ++i) { address u = address(uint160(0x1000 + i)); _buy(u, 1); _power(u, (i + 1) * 1e15); // 60 x (i+1) blocks _on(u, 1); } _start(); assertEq(miner.totalOnlineHashrate(), n); _roll(60 * 20); assertEq(miner.totalOnlineHashrate(), n - 20); // wallets 0..19 expired (wallet 19 exactly at 1200) _roll(1_000_000); // bounded checkpoints bool done = miner.checkpointPartial(10); assertFalse(done); assertEq(miner.onlineHashrateAccounted(), n - 10); done = miner.checkpointPartial(100); assertTrue(done); assertEq(miner.onlineHashrateAccounted(), 0); uint256 sum; for (uint256 i; i < n; ++i) { address u = address(uint160(0x1000 + i)); sum += _claim(u); } assertLe(sum, miner.totalAllocated()); assertApproxEqAbs(sum, miner.totalAllocated(), n * 2); } function test_claim_nothing() public { assertEq(_claim(alice), 0); } function test_checkpoint_permissionless_and_era_event() public { _launch(1); _roll(ERA * 2 + 5); vm.expectEmit(address(emission)); emit EraStarted(2, 10_416666666666666666, genesis.block0() + ERA); vm.prank(carol); miner.checkpoint(); assertEq(emission.lastAnnouncedEra(), 3); } event EraStarted(uint256 era, uint256 rewardPerBlock, uint256 startBlock); function test_settle_realizesBurnForIdleUser() public { _launch(10); _roll(ERA + 1); assertEq(miner.pendingBurnHypeUnits(), 0); vm.prank(carol); miner.settle(alice); assertEq(miner.pendingBurnHypeUnits(), 36e18 * 60_000); } // ------------------------------------------------------------ burn function test_flushBurn_buybackAndBurn() public { _launch(10_000); _roll(10_000); miner.settle(alice); uint256 hypeConsumed = miner.pendingBurnHypeUnits() / 60_000; assertGt(hypeConsumed, 0); uint256 supply = token.totalSupply(); hypeConsumed += address(burner).balance; // genesis rounding dust already waiting there miner.flushBurn(); // 1,666 HYPE into a 10,000 HYPE pool would move the price > 10%: the buyback stops at TWAP x 1.1 assertGt(burner.totalHypeSpent(), 0); assertEq(burner.totalHypeSpent() + address(burner).balance, hypeConsumed); assertLe(oracle.spotPrice(), oracle.twap() * 11_000 / 10_000 + 1e9); assertGt(burner.totalBurned(), 0); assertEq(token.totalSupply(), supply - burner.totalBurned()); // once the TWAP catches up, the rest is bought back and burned for (uint256 i; i < 20 && address(burner).balance > 0; ++i) { _roll(5_000); oracle.update(); burner.burn(); } assertEq(address(burner).balance, 0); assertEq(burner.totalHypeSpent(), hypeConsumed); } }