import { describe, expect, it } from 'vitest' import { ERA1_REWARD, ERA_LENGTH, MINABLE_SUPPLY, accruedBetween, blockShare, blocksToNextHalving, calculate, calibrationCost, emptyBlock, eraOf, eraTable, genesisPrice, kibblePowerCredit, maxMinedAtBlock, powerPerRigPerEra, productionCost, rewardPerBlock, spread, totalMinedAtEraEnd, } from './index' describe('docs §2 core parameters', () => { it('era 1 reward is 20.8333 per block, 4,500,000 over the era', () => { expect(ERA1_REWARD).toBeCloseTo(20.8333, 4) expect(ERA1_REWARD * ERA_LENGTH).toBeCloseTo(4_500_000, 6) }) it('power draw per rig per era is 3.6 HYPE', () => { expect(powerPerRigPerEra()).toBeCloseTo(3.6, 12) }) }) describe('docs §5 era table', () => { const expected = [ [1, 20.8333, 4_500_000, 4_500_000, 0.5], [2, 10.4167, 2_250_000, 6_750_000, 0.75], [3, 5.2083, 1_125_000, 7_875_000, 0.875], [4, 2.6042, 562_500, 8_437_500, 0.9375], [5, 1.3021, 281_250, 8_718_750, 0.96875], [6, 0.651, 140_625, 8_859_375, 0.984375], ] as const const table = eraTable(6) it.each(expected)('era %i', (era, reward, emitted, total, share) => { const row = table[era - 1]! expect(row.rewardPerBlock).toBeCloseTo(reward, 3) expect(row.emitted).toBeCloseTo(emitted, 6) expect(row.totalMined).toBeCloseTo(total, 6) expect(row.shareOfMinable).toBeCloseTo(share, 6) }) it('approaches 9,000,000 and never reaches it', () => { expect(totalMinedAtEraEnd(30)).toBeLessThan(MINABLE_SUPPLY) expect(totalMinedAtEraEnd(30)).toBeGreaterThan(MINABLE_SUPPLY - 0.01) }) it('era boundaries are counted in blocks', () => { expect(eraOf(0)).toBe(1) expect(eraOf(ERA_LENGTH - 1)).toBe(1) expect(eraOf(ERA_LENGTH)).toBe(2) expect(blocksToNextHalving(0)).toBe(ERA_LENGTH) expect(blocksToNextHalving(ERA_LENGTH - 1)).toBe(1) expect(maxMinedAtBlock(ERA_LENGTH)).toBeCloseTo(4_500_000, 6) expect(maxMinedAtBlock(ERA_LENGTH + 1)).toBeCloseTo(4_500_000 + rewardPerBlock(2), 6) }) }) describe('docs §7 worked example: 10,000 rigs preordered', () => { const rigs = 10_000 it('genesis price 0.01 HYPE', () => { expect(genesisPrice(rigs)).toBeCloseTo(0.01, 12) }) it('cost per KIBBLE 0.008 HYPE, 80% of genesis price, 25% margin', () => { expect(calibrationCost(rigs)).toBeCloseTo(0.008, 12) expect(calibrationCost(rigs) / genesisPrice(rigs)).toBeCloseTo(0.8, 12) expect(spread(genesisPrice(rigs), calibrationCost(rigs))).toBeCloseTo(0.25, 12) }) it('450 KIBBLE per rig in era 1, worth 4.5 HYPE at genesis price', () => { const perRig = accruedBetween(0, ERA_LENGTH, 1, rigs) expect(perRig).toBeCloseTo(450, 6) expect(perRig * genesisPrice(rigs)).toBeCloseTo(4.5, 9) }) it('cost doubles in era 2 at the same hashrate', () => { expect(productionCost(rigs, 2)).toBeCloseTo(0.016, 12) }) it('calibration holds for any N', () => { for (const n of [1, 37, 1_000, 123_456]) { expect(calibrationCost(n) / genesisPrice(n)).toBeCloseTo(0.8, 12) } }) }) describe('docs §12 accounting', () => { it('splits rewards per era segment across a halving', () => { const r = accruedBetween(ERA_LENGTH - 10, ERA_LENGTH + 10, 1, 1) expect(r).toBeCloseTo(10 * rewardPerBlock(1) + 10 * rewardPerBlock(2), 9) }) it('no rig on means nothing accrues', () => { expect(accruedBetween(0, 100, 0, 0)).toBe(0) expect(productionCost(0, 1)).toBe(0) expect(blockShare(5, 0)).toBe(0) }) it('empty block from tank balance', () => { expect(emptyBlock(100, 1, 1)).toBe(100 + 60_000) expect(emptyBlock(100, 1, 2)).toBe(100 + 30_000) expect(emptyBlock(100, 1, 0)).toBe(Number.POSITIVE_INFINITY) }) }) describe('docs §4 KIBBLE power', () => { it('credits KIBBLE × TWAP / 0.9', () => { expect(kibblePowerCredit(90, 0.01)).toBeCloseTo(1, 12) }) }) describe('calculator', () => { it('matches the worked example', () => { const [e1, e2] = calculate({ rigs: 1, price: 0.01, otherRigsOnline: 9_999 }) expect(e1!.kibblePerEra).toBeCloseTo(450, 6) expect(e1!.powerCostPerEra).toBeCloseTo(3.6, 12) expect(e1!.costPerKibble).toBeCloseTo(0.008, 12) expect(e1!.margin).toBeCloseTo(0.25, 12) expect(e2!.costPerKibble).toBeCloseTo(0.016, 12) }) })