EmissionMath.sol
70 lines · sha256 125fcc5bb9c4cb00de3ba89f5ef8822902428bbcb2c74dc722d76867d682e9d0 · Raw
| 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.26; |
| 3 | |
| 4 | import {KibbleConstants as K} from "./KibbleConstants.sol"; |
| 5 | |
| 6 | /// @title EmissionMath |
| 7 | /// @notice Pure era schedule (docs §5). Time is "mining time" t = blocks since block 0. |
| 8 | /// @dev Reward per block is never stored as a rounded number: the reward over b blocks of era e is |
| 9 | /// b × eraEmission(e) / ERA_LENGTH, with eraEmission(e) = ERA1_EMISSION >> (e - 1). |
| 10 | /// A full era 1 is therefore exactly 4,500,000e18. Emission stops once eraEmission(e) == 0. |
| 11 | library EmissionMath { |
| 12 | /// @notice Era containing mining-time t (era 1 = t in [0, ERA_LENGTH)). |
| 13 | function eraAt(uint256 t) internal pure returns (uint256) { |
| 14 | return t / K.ERA_LENGTH + 1; |
| 15 | } |
| 16 | |
| 17 | /// @notice Total emission of era e (0 for e == 0 or once halved to zero). |
| 18 | function eraEmission(uint256 e) internal pure returns (uint256) { |
| 19 | if (e == 0 || e > 256) return 0; |
| 20 | return K.ERA1_EMISSION >> (e - 1); |
| 21 | } |
| 22 | |
| 23 | /// @notice Display value of the reward per block in era e (truncated to wei). Accounting never uses it. |
| 24 | function rewardPerBlockOf(uint256 e) internal pure returns (uint256) { |
| 25 | return eraEmission(e) / K.ERA_LENGTH; |
| 26 | } |
| 27 | |
| 28 | /// @notice Cumulative emission of all full eras 1..n (docs §5 "Total mined" column). |
| 29 | function cumulativeThroughEra(uint256 n) internal pure returns (uint256 total) { |
| 30 | for (uint256 e = 1; e <= n; ++e) { |
| 31 | uint256 em = eraEmission(e); |
| 32 | if (em == 0) break; |
| 33 | total += em; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /// @notice Maximum emission that the schedule allows over mining-time [0, t) (blocks with zero hashrate included). |
| 38 | function scheduledAt(uint256 t) internal pure returns (uint256) { |
| 39 | uint256 e = eraAt(t); |
| 40 | uint256 full = cumulativeThroughEra(e - 1); |
| 41 | uint256 into = t - (e - 1) * K.ERA_LENGTH; |
| 42 | return full + into * eraEmission(e) / K.ERA_LENGTH; |
| 43 | } |
| 44 | |
| 45 | /// @notice Reward-per-hash increment and allocated emission for [from, to) at constant hashrate h. |
| 46 | /// Splits the range per era segment (docs §12 "Era boundaries"). |
| 47 | /// @return dAcc increment of the accumulator, scaled by `precision` |
| 48 | /// @return allocatedScaled emission distributed in the range, scaled by `precision` (sum of dAcc × h per segment, |
| 49 | /// an exact upper bound of what wallets can claim for the range) |
| 50 | function accrue(uint256 from, uint256 to, uint256 h, uint256 precision) |
| 51 | internal |
| 52 | pure |
| 53 | returns (uint256 dAcc, uint256 allocatedScaled) |
| 54 | { |
| 55 | if (h == 0 || to <= from) return (0, 0); |
| 56 | while (from < to) { |
| 57 | uint256 e = eraAt(from); |
| 58 | uint256 em = eraEmission(e); |
| 59 | if (em == 0) break; |
| 60 | uint256 segEnd = e * K.ERA_LENGTH; |
| 61 | if (segEnd > to) segEnd = to; |
| 62 | uint256 b = segEnd - from; |
| 63 | uint256 d = (b * em * precision) / (K.ERA_LENGTH * h); |
| 64 | dAcc += d; |
| 65 | allocatedScaled += d * h; |
| 66 | from = segEnd; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 |