index.ts
218 lines · sha256 2a5f90903a56c4d3476aaeacb23747775f091835214ca6f1be55a87994f4ad49 · Raw
| 1 | /** |
| 2 | * KIBBLE protocol math. Every constant and formula is taken from kibble-docs.md. |
| 3 | * Units: HYPE and KIBBLE as plain numbers (not wei). Blocks are counted from block 0. |
| 4 | */ |
| 5 | |
| 6 | /** docs §2 */ |
| 7 | export const MAX_SUPPLY = 10_000_000 |
| 8 | export const GENESIS_LP_KIBBLE = 1_000_000 |
| 9 | export const MINABLE_SUPPLY = 9_000_000 |
| 10 | export const RIG_PRICE_HYPE = 1 |
| 11 | /** docs §2: exactly 1/60,000 HYPE per rig per block */ |
| 12 | export const POWER_DRAW_DENOMINATOR = 60_000 |
| 13 | export const POWER_PER_RIG_PER_BLOCK = 1 / POWER_DRAW_DENOMINATOR |
| 14 | export const ERA_LENGTH = 216_000 |
| 15 | export const ERA1_EMISSION = 4_500_000 |
| 16 | export const ERA1_REWARD = ERA1_EMISSION / ERA_LENGTH |
| 17 | /** docs §4: KIBBLE accepted as power at a 10% discount to TWAP */ |
| 18 | export const KIBBLE_POWER_FACTOR = 0.9 |
| 19 | export const TWAP_WINDOW_BLOCKS = 10_000 |
| 20 | /** docs §7: production cost at block 0 equals 80% of the genesis price */ |
| 21 | export const LAUNCH_CALIBRATION = 0.8 |
| 22 | |
| 23 | /** Era of a block counted from block 0, 1 based (docs §5). */ |
| 24 | export function eraOf(blocksSinceGenesis: number): number { |
| 25 | if (blocksSinceGenesis < 0) return 1 |
| 26 | return Math.floor(blocksSinceGenesis / ERA_LENGTH) + 1 |
| 27 | } |
| 28 | |
| 29 | /** Reward per block in a given era: halved at each new era (docs §5). */ |
| 30 | export function rewardPerBlock(era: number): number { |
| 31 | if (era < 1) throw new RangeError('era starts at 1') |
| 32 | return ERA1_REWARD / 2 ** (era - 1) |
| 33 | } |
| 34 | |
| 35 | /** KIBBLE emitted over a full era if at least one rig is on every block. */ |
| 36 | export function eraEmission(era: number): number { |
| 37 | return ERA1_EMISSION / 2 ** (era - 1) |
| 38 | } |
| 39 | |
| 40 | /** Total mined at the end of an era, maximum schedule. Approaches 9,000,000, never reaches it. */ |
| 41 | export function totalMinedAtEraEnd(era: number): number { |
| 42 | let total = 0 |
| 43 | for (let e = 1; e <= era; e++) total += eraEmission(e) |
| 44 | return total |
| 45 | } |
| 46 | |
| 47 | /** Share of the minable supply mined at the end of an era. */ |
| 48 | export function minableShareAtEraEnd(era: number): number { |
| 49 | return totalMinedAtEraEnd(era) / MINABLE_SUPPLY |
| 50 | } |
| 51 | |
| 52 | /** Maximum mined after `blocks` blocks since block 0 (every block with at least one rig on). */ |
| 53 | export function maxMinedAtBlock(blocks: number): number { |
| 54 | if (blocks <= 0) return 0 |
| 55 | const era = eraOf(blocks - 1) |
| 56 | const inEra = blocks - (era - 1) * ERA_LENGTH |
| 57 | return totalMinedAtEraEnd(era - 1) + inEra * rewardPerBlock(era) |
| 58 | } |
| 59 | |
| 60 | export function blocksToNextHalving(blocksSinceGenesis: number): number { |
| 61 | if (blocksSinceGenesis < 0) return ERA_LENGTH |
| 62 | return ERA_LENGTH - (blocksSinceGenesis % ERA_LENGTH) |
| 63 | } |
| 64 | |
| 65 | /** Fraction of the current era already elapsed, 0..1. */ |
| 66 | export function eraProgress(blocksSinceGenesis: number): number { |
| 67 | if (blocksSinceGenesis < 0) return 0 |
| 68 | return (blocksSinceGenesis % ERA_LENGTH) / ERA_LENGTH |
| 69 | } |
| 70 | |
| 71 | export interface EraRow { |
| 72 | era: number |
| 73 | rewardPerBlock: number |
| 74 | emitted: number |
| 75 | totalMined: number |
| 76 | shareOfMinable: number |
| 77 | } |
| 78 | |
| 79 | /** The era table of docs §5. */ |
| 80 | export function eraTable(eras = 6): EraRow[] { |
| 81 | return Array.from({ length: eras }, (_, i) => { |
| 82 | const era = i + 1 |
| 83 | return { |
| 84 | era, |
| 85 | rewardPerBlock: rewardPerBlock(era), |
| 86 | emitted: eraEmission(era), |
| 87 | totalMined: totalMinedAtEraEnd(era), |
| 88 | shareOfMinable: minableShareAtEraEnd(era), |
| 89 | } |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | /** HYPE drawn by `rigsOn` rigs over `blocks` blocks (docs §4). */ |
| 94 | export function powerDrawn(rigsOn: number, blocks: number): number { |
| 95 | return (rigsOn * blocks) / POWER_DRAW_DENOMINATOR |
| 96 | } |
| 97 | |
| 98 | /** HYPE drawn per rig over a full era: 3.6 HYPE (docs §2). */ |
| 99 | export function powerPerRigPerEra(): number { |
| 100 | return powerDrawn(1, ERA_LENGTH) |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * docs §7: cost per KIBBLE = power per rig per block × online hashrate / block reward. |
| 105 | * Returns 0 when no rig is on (nothing is being produced). |
| 106 | */ |
| 107 | export function productionCost(onlineHashrate: number, era: number): number { |
| 108 | if (onlineHashrate <= 0) return 0 |
| 109 | return (POWER_PER_RIG_PER_BLOCK * onlineHashrate) / rewardPerBlock(era) |
| 110 | } |
| 111 | |
| 112 | /** docs §6: genesis price = preorder HYPE / 1,000,000 */ |
| 113 | export function genesisPrice(preorderHype: number): number { |
| 114 | return preorderHype / GENESIS_LP_KIBBLE |
| 115 | } |
| 116 | |
| 117 | /** docs §7: cost at block 0 with N preordered rigs all on = 0.8 × genesis price. */ |
| 118 | export function calibrationCost(rigsPreordered: number): number { |
| 119 | return productionCost(rigsPreordered, 1) |
| 120 | } |
| 121 | |
| 122 | /** Spread of price over cost: +0.25 means price is 25% above cost. */ |
| 123 | export function spread(price: number, cost: number): number { |
| 124 | if (cost <= 0) return 0 |
| 125 | return price / cost - 1 |
| 126 | } |
| 127 | |
| 128 | /** docs §5: wallet share = wallet online hashrate / total online hashrate. */ |
| 129 | export function blockShare(walletOnline: number, totalOnline: number): number { |
| 130 | if (totalOnline <= 0 || walletOnline <= 0) return 0 |
| 131 | return Math.min(1, walletOnline / totalOnline) |
| 132 | } |
| 133 | |
| 134 | /** docs §4: power credited (HYPE) = KIBBLE deposited × TWAP / 0.9 */ |
| 135 | export function kibblePowerCredit(kibbleDeposited: number, twap: number): number { |
| 136 | return (kibbleDeposited * twap) / KIBBLE_POWER_FACTOR |
| 137 | } |
| 138 | |
| 139 | /** docs §12: empty block = last update block + tank balance / (power draw × rigs on). */ |
| 140 | export function emptyBlock(lastUpdateBlock: number, tankHype: number, rigsOn: number): number { |
| 141 | if (rigsOn <= 0) return Number.POSITIVE_INFINITY |
| 142 | return lastUpdateBlock + Math.floor(tankHype / (POWER_PER_RIG_PER_BLOCK * rigsOn)) |
| 143 | } |
| 144 | |
| 145 | /** Blocks of power left in the tank at the current draw. */ |
| 146 | export function blocksOfPower(tankHype: number, rigsOn: number): number { |
| 147 | if (rigsOn <= 0) return Number.POSITIVE_INFINITY |
| 148 | return Math.floor(tankHype / (POWER_PER_RIG_PER_BLOCK * rigsOn)) |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * docs §12: rewards accrued between two blocks, split per era segment when a halving |
| 153 | * falls in between. Hashrate assumed constant over the range. |
| 154 | */ |
| 155 | export function accruedBetween( |
| 156 | fromBlock: number, |
| 157 | toBlock: number, |
| 158 | walletOnline: number, |
| 159 | totalOnline: number, |
| 160 | ): number { |
| 161 | if (toBlock <= fromBlock || totalOnline <= 0 || walletOnline <= 0) return 0 |
| 162 | let total = 0 |
| 163 | let cursor = Math.max(0, fromBlock) |
| 164 | while (cursor < toBlock) { |
| 165 | const era = eraOf(cursor) |
| 166 | const eraEnd = era * ERA_LENGTH |
| 167 | const segEnd = Math.min(toBlock, eraEnd) |
| 168 | total += (segEnd - cursor) * rewardPerBlock(era) |
| 169 | cursor = segEnd |
| 170 | } |
| 171 | return (total * walletOnline) / totalOnline |
| 172 | } |
| 173 | |
| 174 | export interface CalculatorInput { |
| 175 | /** rigs the user runs, all switched on */ |
| 176 | rigs: number |
| 177 | /** price assumption, HYPE per KIBBLE */ |
| 178 | price: number |
| 179 | /** other rigs online on the network, assumed constant */ |
| 180 | otherRigsOnline: number |
| 181 | eras?: number |
| 182 | } |
| 183 | |
| 184 | export interface CalculatorRow { |
| 185 | era: number |
| 186 | share: number |
| 187 | kibblePerEra: number |
| 188 | powerCostPerEra: number |
| 189 | costPerKibble: number |
| 190 | breakEvenPrice: number |
| 191 | /** price / cost - 1; docs §7: cost at 0.8 × price is a 25% margin */ |
| 192 | margin: number |
| 193 | valueAtPrice: number |
| 194 | } |
| 195 | |
| 196 | /** Calculator outputs, each from the formulas above. Not a prediction. */ |
| 197 | export function calculate(input: CalculatorInput): CalculatorRow[] { |
| 198 | const { rigs, price, otherRigsOnline, eras = 4 } = input |
| 199 | const total = rigs + Math.max(0, otherRigsOnline) |
| 200 | const share = blockShare(rigs, total) |
| 201 | return Array.from({ length: eras }, (_, i) => { |
| 202 | const era = i + 1 |
| 203 | const kibblePerEra = share * eraEmission(era) |
| 204 | const powerCostPerEra = powerDrawn(rigs, ERA_LENGTH) |
| 205 | const costPerKibble = productionCost(total, era) |
| 206 | return { |
| 207 | era, |
| 208 | share, |
| 209 | kibblePerEra, |
| 210 | powerCostPerEra, |
| 211 | costPerKibble, |
| 212 | breakEvenPrice: costPerKibble, |
| 213 | margin: spread(price, costPerKibble), |
| 214 | valueAtPrice: kibblePerEra * price, |
| 215 | } |
| 216 | }) |
| 217 | } |
| 218 |