/** * KIBBLE protocol math. Every constant and formula is taken from kibble-docs.md. * Units: HYPE and KIBBLE as plain numbers (not wei). Blocks are counted from block 0. */ /** docs §2 */ export const MAX_SUPPLY = 10_000_000 export const GENESIS_LP_KIBBLE = 1_000_000 export const MINABLE_SUPPLY = 9_000_000 export const RIG_PRICE_HYPE = 1 /** docs §2: exactly 1/60,000 HYPE per rig per block */ export const POWER_DRAW_DENOMINATOR = 60_000 export const POWER_PER_RIG_PER_BLOCK = 1 / POWER_DRAW_DENOMINATOR export const ERA_LENGTH = 216_000 export const ERA1_EMISSION = 4_500_000 export const ERA1_REWARD = ERA1_EMISSION / ERA_LENGTH /** docs §4: KIBBLE accepted as power at a 10% discount to TWAP */ export const KIBBLE_POWER_FACTOR = 0.9 export const TWAP_WINDOW_BLOCKS = 10_000 /** docs §7: production cost at block 0 equals 80% of the genesis price */ export const LAUNCH_CALIBRATION = 0.8 /** Era of a block counted from block 0, 1 based (docs §5). */ export function eraOf(blocksSinceGenesis: number): number { if (blocksSinceGenesis < 0) return 1 return Math.floor(blocksSinceGenesis / ERA_LENGTH) + 1 } /** Reward per block in a given era: halved at each new era (docs §5). */ export function rewardPerBlock(era: number): number { if (era < 1) throw new RangeError('era starts at 1') return ERA1_REWARD / 2 ** (era - 1) } /** KIBBLE emitted over a full era if at least one rig is on every block. */ export function eraEmission(era: number): number { return ERA1_EMISSION / 2 ** (era - 1) } /** Total mined at the end of an era, maximum schedule. Approaches 9,000,000, never reaches it. */ export function totalMinedAtEraEnd(era: number): number { let total = 0 for (let e = 1; e <= era; e++) total += eraEmission(e) return total } /** Share of the minable supply mined at the end of an era. */ export function minableShareAtEraEnd(era: number): number { return totalMinedAtEraEnd(era) / MINABLE_SUPPLY } /** Maximum mined after `blocks` blocks since block 0 (every block with at least one rig on). */ export function maxMinedAtBlock(blocks: number): number { if (blocks <= 0) return 0 const era = eraOf(blocks - 1) const inEra = blocks - (era - 1) * ERA_LENGTH return totalMinedAtEraEnd(era - 1) + inEra * rewardPerBlock(era) } export function blocksToNextHalving(blocksSinceGenesis: number): number { if (blocksSinceGenesis < 0) return ERA_LENGTH return ERA_LENGTH - (blocksSinceGenesis % ERA_LENGTH) } /** Fraction of the current era already elapsed, 0..1. */ export function eraProgress(blocksSinceGenesis: number): number { if (blocksSinceGenesis < 0) return 0 return (blocksSinceGenesis % ERA_LENGTH) / ERA_LENGTH } export interface EraRow { era: number rewardPerBlock: number emitted: number totalMined: number shareOfMinable: number } /** The era table of docs §5. */ export function eraTable(eras = 6): EraRow[] { return Array.from({ length: eras }, (_, i) => { const era = i + 1 return { era, rewardPerBlock: rewardPerBlock(era), emitted: eraEmission(era), totalMined: totalMinedAtEraEnd(era), shareOfMinable: minableShareAtEraEnd(era), } }) } /** HYPE drawn by `rigsOn` rigs over `blocks` blocks (docs §4). */ export function powerDrawn(rigsOn: number, blocks: number): number { return (rigsOn * blocks) / POWER_DRAW_DENOMINATOR } /** HYPE drawn per rig over a full era: 3.6 HYPE (docs §2). */ export function powerPerRigPerEra(): number { return powerDrawn(1, ERA_LENGTH) } /** * docs §7: cost per KIBBLE = power per rig per block × online hashrate / block reward. * Returns 0 when no rig is on (nothing is being produced). */ export function productionCost(onlineHashrate: number, era: number): number { if (onlineHashrate <= 0) return 0 return (POWER_PER_RIG_PER_BLOCK * onlineHashrate) / rewardPerBlock(era) } /** docs §6: genesis price = preorder HYPE / 1,000,000 */ export function genesisPrice(preorderHype: number): number { return preorderHype / GENESIS_LP_KIBBLE } /** docs §7: cost at block 0 with N preordered rigs all on = 0.8 × genesis price. */ export function calibrationCost(rigsPreordered: number): number { return productionCost(rigsPreordered, 1) } /** Spread of price over cost: +0.25 means price is 25% above cost. */ export function spread(price: number, cost: number): number { if (cost <= 0) return 0 return price / cost - 1 } /** docs §5: wallet share = wallet online hashrate / total online hashrate. */ export function blockShare(walletOnline: number, totalOnline: number): number { if (totalOnline <= 0 || walletOnline <= 0) return 0 return Math.min(1, walletOnline / totalOnline) } /** docs §4: power credited (HYPE) = KIBBLE deposited × TWAP / 0.9 */ export function kibblePowerCredit(kibbleDeposited: number, twap: number): number { return (kibbleDeposited * twap) / KIBBLE_POWER_FACTOR } /** docs §12: empty block = last update block + tank balance / (power draw × rigs on). */ export function emptyBlock(lastUpdateBlock: number, tankHype: number, rigsOn: number): number { if (rigsOn <= 0) return Number.POSITIVE_INFINITY return lastUpdateBlock + Math.floor(tankHype / (POWER_PER_RIG_PER_BLOCK * rigsOn)) } /** Blocks of power left in the tank at the current draw. */ export function blocksOfPower(tankHype: number, rigsOn: number): number { if (rigsOn <= 0) return Number.POSITIVE_INFINITY return Math.floor(tankHype / (POWER_PER_RIG_PER_BLOCK * rigsOn)) } /** * docs §12: rewards accrued between two blocks, split per era segment when a halving * falls in between. Hashrate assumed constant over the range. */ export function accruedBetween( fromBlock: number, toBlock: number, walletOnline: number, totalOnline: number, ): number { if (toBlock <= fromBlock || totalOnline <= 0 || walletOnline <= 0) return 0 let total = 0 let cursor = Math.max(0, fromBlock) while (cursor < toBlock) { const era = eraOf(cursor) const eraEnd = era * ERA_LENGTH const segEnd = Math.min(toBlock, eraEnd) total += (segEnd - cursor) * rewardPerBlock(era) cursor = segEnd } return (total * walletOnline) / totalOnline } export interface CalculatorInput { /** rigs the user runs, all switched on */ rigs: number /** price assumption, HYPE per KIBBLE */ price: number /** other rigs online on the network, assumed constant */ otherRigsOnline: number eras?: number } export interface CalculatorRow { era: number share: number kibblePerEra: number powerCostPerEra: number costPerKibble: number breakEvenPrice: number /** price / cost - 1; docs §7: cost at 0.8 × price is a 25% margin */ margin: number valueAtPrice: number } /** Calculator outputs, each from the formulas above. Not a prediction. */ export function calculate(input: CalculatorInput): CalculatorRow[] { const { rigs, price, otherRigsOnline, eras = 4 } = input const total = rigs + Math.max(0, otherRigsOnline) const share = blockShare(rigs, total) return Array.from({ length: eras }, (_, i) => { const era = i + 1 const kibblePerEra = share * eraEmission(era) const powerCostPerEra = powerDrawn(rigs, ERA_LENGTH) const costPerKibble = productionCost(total, era) return { era, share, kibblePerEra, powerCostPerEra, costPerKibble, breakEvenPrice: costPerKibble, margin: spread(price, costPerKibble), valueAtPrice: kibblePerEra * price, } }) }