index.test.ts
124 lines · sha256 539c7e4d05297e13ac84cc982749b398276f58527914c4161ed77de4f5fd5662 · Raw
| 1 | import { describe, expect, it } from 'vitest' |
| 2 | import { |
| 3 | ERA1_REWARD, |
| 4 | ERA_LENGTH, |
| 5 | MINABLE_SUPPLY, |
| 6 | accruedBetween, |
| 7 | blockShare, |
| 8 | blocksToNextHalving, |
| 9 | calculate, |
| 10 | calibrationCost, |
| 11 | emptyBlock, |
| 12 | eraOf, |
| 13 | eraTable, |
| 14 | genesisPrice, |
| 15 | kibblePowerCredit, |
| 16 | maxMinedAtBlock, |
| 17 | powerPerRigPerEra, |
| 18 | productionCost, |
| 19 | rewardPerBlock, |
| 20 | spread, |
| 21 | totalMinedAtEraEnd, |
| 22 | } from './index' |
| 23 | |
| 24 | describe('docs §2 core parameters', () => { |
| 25 | it('era 1 reward is 20.8333 per block, 4,500,000 over the era', () => { |
| 26 | expect(ERA1_REWARD).toBeCloseTo(20.8333, 4) |
| 27 | expect(ERA1_REWARD * ERA_LENGTH).toBeCloseTo(4_500_000, 6) |
| 28 | }) |
| 29 | it('power draw per rig per era is 3.6 HYPE', () => { |
| 30 | expect(powerPerRigPerEra()).toBeCloseTo(3.6, 12) |
| 31 | }) |
| 32 | }) |
| 33 | |
| 34 | describe('docs §5 era table', () => { |
| 35 | const expected = [ |
| 36 | [1, 20.8333, 4_500_000, 4_500_000, 0.5], |
| 37 | [2, 10.4167, 2_250_000, 6_750_000, 0.75], |
| 38 | [3, 5.2083, 1_125_000, 7_875_000, 0.875], |
| 39 | [4, 2.6042, 562_500, 8_437_500, 0.9375], |
| 40 | [5, 1.3021, 281_250, 8_718_750, 0.96875], |
| 41 | [6, 0.651, 140_625, 8_859_375, 0.984375], |
| 42 | ] as const |
| 43 | const table = eraTable(6) |
| 44 | it.each(expected)('era %i', (era, reward, emitted, total, share) => { |
| 45 | const row = table[era - 1]! |
| 46 | expect(row.rewardPerBlock).toBeCloseTo(reward, 3) |
| 47 | expect(row.emitted).toBeCloseTo(emitted, 6) |
| 48 | expect(row.totalMined).toBeCloseTo(total, 6) |
| 49 | expect(row.shareOfMinable).toBeCloseTo(share, 6) |
| 50 | }) |
| 51 | it('approaches 9,000,000 and never reaches it', () => { |
| 52 | expect(totalMinedAtEraEnd(30)).toBeLessThan(MINABLE_SUPPLY) |
| 53 | expect(totalMinedAtEraEnd(30)).toBeGreaterThan(MINABLE_SUPPLY - 0.01) |
| 54 | }) |
| 55 | it('era boundaries are counted in blocks', () => { |
| 56 | expect(eraOf(0)).toBe(1) |
| 57 | expect(eraOf(ERA_LENGTH - 1)).toBe(1) |
| 58 | expect(eraOf(ERA_LENGTH)).toBe(2) |
| 59 | expect(blocksToNextHalving(0)).toBe(ERA_LENGTH) |
| 60 | expect(blocksToNextHalving(ERA_LENGTH - 1)).toBe(1) |
| 61 | expect(maxMinedAtBlock(ERA_LENGTH)).toBeCloseTo(4_500_000, 6) |
| 62 | expect(maxMinedAtBlock(ERA_LENGTH + 1)).toBeCloseTo(4_500_000 + rewardPerBlock(2), 6) |
| 63 | }) |
| 64 | }) |
| 65 | |
| 66 | describe('docs §7 worked example: 10,000 rigs preordered', () => { |
| 67 | const rigs = 10_000 |
| 68 | it('genesis price 0.01 HYPE', () => { |
| 69 | expect(genesisPrice(rigs)).toBeCloseTo(0.01, 12) |
| 70 | }) |
| 71 | it('cost per KIBBLE 0.008 HYPE, 80% of genesis price, 25% margin', () => { |
| 72 | expect(calibrationCost(rigs)).toBeCloseTo(0.008, 12) |
| 73 | expect(calibrationCost(rigs) / genesisPrice(rigs)).toBeCloseTo(0.8, 12) |
| 74 | expect(spread(genesisPrice(rigs), calibrationCost(rigs))).toBeCloseTo(0.25, 12) |
| 75 | }) |
| 76 | it('450 KIBBLE per rig in era 1, worth 4.5 HYPE at genesis price', () => { |
| 77 | const perRig = accruedBetween(0, ERA_LENGTH, 1, rigs) |
| 78 | expect(perRig).toBeCloseTo(450, 6) |
| 79 | expect(perRig * genesisPrice(rigs)).toBeCloseTo(4.5, 9) |
| 80 | }) |
| 81 | it('cost doubles in era 2 at the same hashrate', () => { |
| 82 | expect(productionCost(rigs, 2)).toBeCloseTo(0.016, 12) |
| 83 | }) |
| 84 | it('calibration holds for any N', () => { |
| 85 | for (const n of [1, 37, 1_000, 123_456]) { |
| 86 | expect(calibrationCost(n) / genesisPrice(n)).toBeCloseTo(0.8, 12) |
| 87 | } |
| 88 | }) |
| 89 | }) |
| 90 | |
| 91 | describe('docs §12 accounting', () => { |
| 92 | it('splits rewards per era segment across a halving', () => { |
| 93 | const r = accruedBetween(ERA_LENGTH - 10, ERA_LENGTH + 10, 1, 1) |
| 94 | expect(r).toBeCloseTo(10 * rewardPerBlock(1) + 10 * rewardPerBlock(2), 9) |
| 95 | }) |
| 96 | it('no rig on means nothing accrues', () => { |
| 97 | expect(accruedBetween(0, 100, 0, 0)).toBe(0) |
| 98 | expect(productionCost(0, 1)).toBe(0) |
| 99 | expect(blockShare(5, 0)).toBe(0) |
| 100 | }) |
| 101 | it('empty block from tank balance', () => { |
| 102 | expect(emptyBlock(100, 1, 1)).toBe(100 + 60_000) |
| 103 | expect(emptyBlock(100, 1, 2)).toBe(100 + 30_000) |
| 104 | expect(emptyBlock(100, 1, 0)).toBe(Number.POSITIVE_INFINITY) |
| 105 | }) |
| 106 | }) |
| 107 | |
| 108 | describe('docs §4 KIBBLE power', () => { |
| 109 | it('credits KIBBLE × TWAP / 0.9', () => { |
| 110 | expect(kibblePowerCredit(90, 0.01)).toBeCloseTo(1, 12) |
| 111 | }) |
| 112 | }) |
| 113 | |
| 114 | describe('calculator', () => { |
| 115 | it('matches the worked example', () => { |
| 116 | const [e1, e2] = calculate({ rigs: 1, price: 0.01, otherRigsOnline: 9_999 }) |
| 117 | expect(e1!.kibblePerEra).toBeCloseTo(450, 6) |
| 118 | expect(e1!.powerCostPerEra).toBeCloseTo(3.6, 12) |
| 119 | expect(e1!.costPerKibble).toBeCloseTo(0.008, 12) |
| 120 | expect(e1!.margin).toBeCloseTo(0.25, 12) |
| 121 | expect(e2!.costPerKibble).toBeCloseTo(0.016, 12) |
| 122 | }) |
| 123 | }) |
| 124 |