Skip to content

Reading Elysium

Mine

Emission.sol

124 lines · sha256 9eaa2242c6823e6a7940f47bb89c2f9ff96f08100277124be61fc6b8df17527d · Raw

1// SPDX-License-Identifier: MIT
2pragma solidity ^0.8.26;
3
4import {KibbleToken} from "./KibbleToken.sol";
5import {Genesis} from "./Genesis.sol";
6import {EmissionMath} from "./libraries/EmissionMath.sol";
7import {ChainBlock} from "./libraries/ChainBlock.sol";
8import {KibbleConstants as K} from "./libraries/KibbleConstants.sol";
9
10/// @title Emission
11/// @notice Era schedule, reward per block, halving logic (docs §5, §11). Sole minter of mining rewards, called by
12/// `Miner` on `claim()`. Every mint is capped by the schedule up to the current block.
13contract Emission {
14 error NotMiner();
15 error ScheduleExceeded();
16
17 /// @notice docs §11.
18 event EraStarted(uint256 era, uint256 rewardPerBlock, uint256 startBlock);
19
20 KibbleToken public immutable kibble;
21 Genesis public immutable genesis;
22 address public immutable miner;
23
24 /// @notice Total KIBBLE minted as mining rewards.
25 uint256 public totalMinted;
26 /// @notice Last era for which `EraStarted` was emitted.
27 uint256 public lastAnnouncedEra;
28
29 constructor(KibbleToken kibble_, Genesis genesis_, address miner_) {
30 kibble = kibble_;
31 genesis = genesis_;
32 miner = miner_;
33 }
34
35 // ------------------------------------------------------------------ constants (docs §2)
36
37 function ERA_LENGTH() external pure returns (uint256) {
38 return K.ERA_LENGTH;
39 }
40
41 function ERA1_EMISSION() external pure returns (uint256) {
42 return K.ERA1_EMISSION;
43 }
44
45 // ------------------------------------------------------------------ mint
46
47 /// @notice Mints claimed rewards. Only `Miner`. Reverts if total minted would exceed the schedule up to now.
48 function mint(address to, uint256 amount) external {
49 if (msg.sender != miner) revert NotMiner();
50 uint256 minted = totalMinted + amount;
51 if (minted > scheduledEmission()) revert ScheduleExceeded();
52 totalMinted = minted;
53 kibble.mint(to, amount);
54 }
55
56 /// @notice Emits `EraStarted` for every era reached and not yet announced. Anyone can call.
57 function sync() public {
58 uint256 b0 = genesis.block0();
59 if (b0 == 0) return;
60 uint256 e = era();
61 uint256 a = lastAnnouncedEra;
62 while (a < e) {
63 ++a;
64 emit EraStarted(a, EmissionMath.rewardPerBlockOf(a), b0 + (a - 1) * K.ERA_LENGTH);
65 }
66 lastAnnouncedEra = a;
67 }
68
69 // ------------------------------------------------------------------ views (docs §14)
70
71 /// @notice Blocks since block 0 (0 before mining starts).
72 function miningTime() public view returns (uint256) {
73 uint256 b0 = genesis.block0();
74 if (b0 == 0) return 0;
75 return ChainBlock.number() - b0;
76 }
77
78 /// @notice Current era, 1-based. 0 before block 0 (docs §5).
79 function era() public view returns (uint256) {
80 if (genesis.block0() == 0) return 0;
81 return EmissionMath.eraAt(miningTime());
82 }
83
84 /// @notice Current reward per block in KIBBLE wei (display value, 20.8333e18 in era 1). 0 before block 0.
85 /// Accounting uses the exact ratio ERA1_EMISSION / ERA_LENGTH >> (era - 1).
86 function rewardPerBlock() external view returns (uint256) {
87 return EmissionMath.rewardPerBlockOf(era());
88 }
89
90 /// @notice Reward per block of any era (era 1 = 20.8333e18).
91 function rewardPerBlockOf(uint256 e) external pure returns (uint256) {
92 return EmissionMath.rewardPerBlockOf(e);
93 }
94
95 /// @notice Emission of era e (4,500,000e18 for era 1).
96 function eraEmission(uint256 e) external pure returns (uint256) {
97 return EmissionMath.eraEmission(e);
98 }
99
100 /// @notice Cumulative emission at the end of era e (docs §5 "Total mined").
101 function cumulativeEmission(uint256 e) external pure returns (uint256) {
102 return EmissionMath.cumulativeThroughEra(e);
103 }
104
105 /// @notice Blocks left in the current era. 0 before block 0.
106 function blocksToNextHalving() external view returns (uint256) {
107 if (genesis.block0() == 0) return 0;
108 return K.ERA_LENGTH - (miningTime() % K.ERA_LENGTH);
109 }
110
111 /// @notice Block at which the current era started (0 before block 0).
112 function eraStartBlock() external view returns (uint256) {
113 uint256 b0 = genesis.block0();
114 if (b0 == 0) return 0;
115 return b0 + (era() - 1) * K.ERA_LENGTH;
116 }
117
118 /// @notice Max emission the schedule allows from block 0 to now (includes blocks that emitted nothing).
119 function scheduledEmission() public view returns (uint256) {
120 if (genesis.block0() == 0) return 0;
121 return EmissionMath.scheduledAt(miningTime());
122 }
123}
124