DocsVectors.t.sol
119 lines · sha256 30f03cf255c080db6448cbc2d0a60d464b939dff7cdf6400b9cb9127e3ca0e42 · Raw
| 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.26; |
| 3 | |
| 4 | import {KibbleBase} from "./utils/KibbleBase.sol"; |
| 5 | |
| 6 | /// @notice Numbers quoted in the docs (§2, §5, §7) reproduced on chain. |
| 7 | contract DocsVectorsTest is KibbleBase { |
| 8 | function test_eraTable_docs5() public view { |
| 9 | // reward per block |
| 10 | assertEq(emission.rewardPerBlockOf(1), 20_833333333333333333); // 20.8333 |
| 11 | assertEq(emission.rewardPerBlockOf(2), 10_416666666666666666); // 10.4167 |
| 12 | assertEq(emission.rewardPerBlockOf(3), 5_208333333333333333); // 5.2083 |
| 13 | assertEq(emission.rewardPerBlockOf(4), 2_604166666666666666); // 2.6042 |
| 14 | assertEq(emission.rewardPerBlockOf(5), 1_302083333333333333); // 1.3021 |
| 15 | assertEq(emission.rewardPerBlockOf(6), 651041666666666666); // 0.6510 |
| 16 | // emitted in era |
| 17 | assertEq(emission.eraEmission(1), 4_500_000e18); |
| 18 | assertEq(emission.eraEmission(2), 2_250_000e18); |
| 19 | assertEq(emission.eraEmission(3), 1_125_000e18); |
| 20 | assertEq(emission.eraEmission(4), 562_500e18); |
| 21 | assertEq(emission.eraEmission(5), 281_250e18); |
| 22 | assertEq(emission.eraEmission(6), 140_625e18); |
| 23 | // total mined |
| 24 | assertEq(emission.cumulativeEmission(1), 4_500_000e18); |
| 25 | assertEq(emission.cumulativeEmission(2), 6_750_000e18); |
| 26 | assertEq(emission.cumulativeEmission(3), 7_875_000e18); |
| 27 | assertEq(emission.cumulativeEmission(4), 8_437_500e18); |
| 28 | assertEq(emission.cumulativeEmission(5), 8_718_750e18); |
| 29 | assertEq(emission.cumulativeEmission(6), 8_859_375e18); |
| 30 | // approaches, never reaches 9,000,000 |
| 31 | uint256 total = emission.cumulativeEmission(300); |
| 32 | assertLt(total, 9_000_000e18); |
| 33 | assertGt(total, 9_000_000e18 - 1e18); |
| 34 | assertEq(emission.eraEmission(83), 0); |
| 35 | assertEq(emission.eraEmission(82), 1); |
| 36 | // 1,000,000 genesis + emission stays below the 10,000,000 max supply |
| 37 | assertLt(1_000_000e18 + total, token.maxSupply()); |
| 38 | } |
| 39 | |
| 40 | function test_powerDrawRatio() public view { |
| 41 | (uint256 num, uint256 den) = miner.powerDrawPerRigPerBlock(); |
| 42 | assertEq(num, 1e18); |
| 43 | assertEq(den, 60_000); |
| 44 | // full era is exactly 3.6 HYPE per rig |
| 45 | assertEq(ERA * num / den, 3.6e18); |
| 46 | } |
| 47 | |
| 48 | /// docs §7 worked example: 10,000 rigs preordered. |
| 49 | function test_workedExample_10000Rigs() public { |
| 50 | _launch(10_000); |
| 51 | assertEq(genesis.preorderHype(), 10_000e18); |
| 52 | assertEq(genesis.genesisPrice(), 0.01e18); // p0 = 0.01 HYPE |
| 53 | assertApproxEqRel(oracle.spotPrice(), 0.01e18, 1e9); // pool spot = p0 (sqrt rounding only) |
| 54 | assertEq(miner.totalOnlineHashrate(), 10_000); |
| 55 | assertEq(miner.productionCost(), 0.008e18); // 0.8 x p0 |
| 56 | assertEq(miner.productionCost() * 10 / 8, genesis.genesisPrice()); // 25% margin |
| 57 | |
| 58 | uint256 b0 = genesis.block0(); |
| 59 | assertEq(miner.emptyBlockOf(alice), b0 + ERA); // 3.6 HYPE/rig lasts exactly one era |
| 60 | |
| 61 | vm.roll(b0 + ERA); |
| 62 | vm.expectEmit(address(miner)); |
| 63 | emit TankEmpty(alice, b0 + ERA); |
| 64 | uint256 got = _claim(alice); |
| 65 | assertEq(got, 4_500_000e18); // the whole era 1 |
| 66 | assertEq(got / 10_000, 450e18); // 450 KIBBLE per rig |
| 67 | (,,, uint256 total) = miner.tankOf(alice); |
| 68 | assertEq(total, 0); // 3.6 HYPE drawn per rig = 36,000 HYPE |
| 69 | assertEq(miner.pendingBurnHypeUnits(), 36_000e18 * 60_000); |
| 70 | assertEq(miner.rigsOnOf(alice), 0); |
| 71 | |
| 72 | // era 2, same hashrate: cost doubles to 0.016 |
| 73 | _power(alice, 36_000e18); |
| 74 | _on(alice, 10_000); |
| 75 | assertEq(emission.era(), 2); |
| 76 | assertEq(miner.productionCost(), 0.016e18); |
| 77 | } |
| 78 | |
| 79 | event TankEmpty(address user, uint256 blockNumber); |
| 80 | |
| 81 | /// docs §7: cost at block 0 = 0.8 x genesis price for any N. |
| 82 | function testFuzz_calibration_anyN(uint256 n) public { |
| 83 | n = bound(n, 1, 5_000_000); |
| 84 | _buy(alice, n); |
| 85 | _power(alice, n * 1e16); |
| 86 | _on(alice, n); |
| 87 | _start(); |
| 88 | assertEq(genesis.genesisPrice(), n * 1e12); |
| 89 | assertEq(miner.productionCost(), n * 8e11); |
| 90 | assertEq(miner.productionCost() * 10, genesis.genesisPrice() * 8); |
| 91 | } |
| 92 | |
| 93 | /// Era 1 emitted with frequent checkpoints: only sub-wei dust lost per update. |
| 94 | function test_fullEra_manyCheckpoints() public { |
| 95 | _launch(10_000); |
| 96 | uint256 b0 = genesis.block0(); |
| 97 | for (uint256 i = 1; i <= 216; ++i) { |
| 98 | vm.roll(b0 + i * 1000); |
| 99 | miner.checkpoint(); |
| 100 | } |
| 101 | uint256 got = _claim(alice); |
| 102 | assertApproxEqAbs(got, 4_500_000e18, 216); |
| 103 | assertLe(got, 4_500_000e18); |
| 104 | } |
| 105 | |
| 106 | /// docs §5 + §12: two eras at constant hashrate, claimed once across the boundary. |
| 107 | function test_halvingSplit() public { |
| 108 | _buy(alice, 4); |
| 109 | _power(alice, 4 * 72e17); // 2 eras |
| 110 | _on(alice, 4); |
| 111 | _start(); |
| 112 | vm.roll(genesis.block0() + ERA + ERA / 2); |
| 113 | uint256 got = _claim(alice); |
| 114 | assertEq(got, 4_500_000e18 + 1_125_000e18); |
| 115 | assertEq(emission.totalMinted(), got); |
| 116 | assertEq(emission.era(), 2); |
| 117 | } |
| 118 | } |
| 119 |