// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; import {KibbleToken} from "./KibbleToken.sol"; import {Genesis} from "./Genesis.sol"; import {EmissionMath} from "./libraries/EmissionMath.sol"; import {ChainBlock} from "./libraries/ChainBlock.sol"; import {KibbleConstants as K} from "./libraries/KibbleConstants.sol"; /// @title Emission /// @notice Era schedule, reward per block, halving logic (docs §5, §11). Sole minter of mining rewards, called by /// `Miner` on `claim()`. Every mint is capped by the schedule up to the current block. contract Emission { error NotMiner(); error ScheduleExceeded(); /// @notice docs §11. event EraStarted(uint256 era, uint256 rewardPerBlock, uint256 startBlock); KibbleToken public immutable kibble; Genesis public immutable genesis; address public immutable miner; /// @notice Total KIBBLE minted as mining rewards. uint256 public totalMinted; /// @notice Last era for which `EraStarted` was emitted. uint256 public lastAnnouncedEra; constructor(KibbleToken kibble_, Genesis genesis_, address miner_) { kibble = kibble_; genesis = genesis_; miner = miner_; } // ------------------------------------------------------------------ constants (docs §2) function ERA_LENGTH() external pure returns (uint256) { return K.ERA_LENGTH; } function ERA1_EMISSION() external pure returns (uint256) { return K.ERA1_EMISSION; } // ------------------------------------------------------------------ mint /// @notice Mints claimed rewards. Only `Miner`. Reverts if total minted would exceed the schedule up to now. function mint(address to, uint256 amount) external { if (msg.sender != miner) revert NotMiner(); uint256 minted = totalMinted + amount; if (minted > scheduledEmission()) revert ScheduleExceeded(); totalMinted = minted; kibble.mint(to, amount); } /// @notice Emits `EraStarted` for every era reached and not yet announced. Anyone can call. function sync() public { uint256 b0 = genesis.block0(); if (b0 == 0) return; uint256 e = era(); uint256 a = lastAnnouncedEra; while (a < e) { ++a; emit EraStarted(a, EmissionMath.rewardPerBlockOf(a), b0 + (a - 1) * K.ERA_LENGTH); } lastAnnouncedEra = a; } // ------------------------------------------------------------------ views (docs §14) /// @notice Blocks since block 0 (0 before mining starts). function miningTime() public view returns (uint256) { uint256 b0 = genesis.block0(); if (b0 == 0) return 0; return ChainBlock.number() - b0; } /// @notice Current era, 1-based. 0 before block 0 (docs §5). function era() public view returns (uint256) { if (genesis.block0() == 0) return 0; return EmissionMath.eraAt(miningTime()); } /// @notice Current reward per block in KIBBLE wei (display value, 20.8333e18 in era 1). 0 before block 0. /// Accounting uses the exact ratio ERA1_EMISSION / ERA_LENGTH >> (era - 1). function rewardPerBlock() external view returns (uint256) { return EmissionMath.rewardPerBlockOf(era()); } /// @notice Reward per block of any era (era 1 = 20.8333e18). function rewardPerBlockOf(uint256 e) external pure returns (uint256) { return EmissionMath.rewardPerBlockOf(e); } /// @notice Emission of era e (4,500,000e18 for era 1). function eraEmission(uint256 e) external pure returns (uint256) { return EmissionMath.eraEmission(e); } /// @notice Cumulative emission at the end of era e (docs §5 "Total mined"). function cumulativeEmission(uint256 e) external pure returns (uint256) { return EmissionMath.cumulativeThroughEra(e); } /// @notice Blocks left in the current era. 0 before block 0. function blocksToNextHalving() external view returns (uint256) { if (genesis.block0() == 0) return 0; return K.ERA_LENGTH - (miningTime() % K.ERA_LENGTH); } /// @notice Block at which the current era started (0 before block 0). function eraStartBlock() external view returns (uint256) { uint256 b0 = genesis.block0(); if (b0 == 0) return 0; return b0 + (era() - 1) * K.ERA_LENGTH; } /// @notice Max emission the schedule allows from block 0 to now (includes blocks that emitted nothing). function scheduledEmission() public view returns (uint256) { if (genesis.block0() == 0) return 0; return EmissionMath.scheduledAt(miningTime()); } }