KibbleToken.sol
65 lines · sha256 227ff8816f8804ef28a6c8322ad85f90c063da74b3a6a6c1dd75ff7aa6ae6b58 · Raw
| 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.26; |
| 3 | |
| 4 | import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; |
| 5 | import {KibbleConstants as K} from "./libraries/KibbleConstants.sol"; |
| 6 | |
| 7 | /// @title KibbleToken |
| 8 | /// @notice $KIBBLE, ERC-20, 18 decimals (docs §2, §11). Mint restricted to `Emission` (mining rewards) and to the |
| 9 | /// one-time genesis mint by `Genesis`. Anyone can burn their own tokens. No owner, no admin. |
| 10 | contract KibbleToken is ERC20 { |
| 11 | error NotEmission(); |
| 12 | error NotGenesis(); |
| 13 | error GenesisAlreadyMinted(); |
| 14 | error MaxSupplyExceeded(); |
| 15 | |
| 16 | /// @notice The only address allowed to mint mining rewards (docs §11). |
| 17 | address public immutable emission; |
| 18 | /// @notice The only address allowed to perform the 1,000,000 genesis mint (docs §6). |
| 19 | address public immutable genesis; |
| 20 | |
| 21 | /// @notice True once the genesis mint happened. |
| 22 | bool public genesisMinted; |
| 23 | /// @notice Total ever minted (genesis + emission). Never exceeds MAX_SUPPLY. |
| 24 | uint256 public totalMinted; |
| 25 | /// @notice Total ever burned through `burn` (protocol burns and user burns). |
| 26 | uint256 public totalBurned; |
| 27 | |
| 28 | constructor(address emission_, address genesis_) ERC20("KIBBLE", "KIBBLE") { |
| 29 | emission = emission_; |
| 30 | genesis = genesis_; |
| 31 | } |
| 32 | |
| 33 | /// @notice Max supply, docs §2: 10,000,000 KIBBLE. |
| 34 | function maxSupply() external pure returns (uint256) { |
| 35 | return K.MAX_SUPPLY; |
| 36 | } |
| 37 | |
| 38 | /// @notice Mints mining rewards. Only `Emission` (docs §11 "mint restricted to Emission"). |
| 39 | function mint(address to, uint256 amount) external { |
| 40 | if (msg.sender != emission) revert NotEmission(); |
| 41 | _mintCapped(to, amount); |
| 42 | } |
| 43 | |
| 44 | /// @notice One-time genesis mint of 1,000,000 KIBBLE to seed the pool (docs §6). Only `Genesis`. |
| 45 | function mintGenesis(address to) external { |
| 46 | if (msg.sender != genesis) revert NotGenesis(); |
| 47 | if (genesisMinted) revert GenesisAlreadyMinted(); |
| 48 | genesisMinted = true; |
| 49 | _mintCapped(to, K.GENESIS_SUPPLY); |
| 50 | } |
| 51 | |
| 52 | /// @notice Burns `amount` of the caller's KIBBLE (docs §11 "public burn"). |
| 53 | function burn(uint256 amount) external { |
| 54 | _burn(msg.sender, amount); |
| 55 | totalBurned += amount; |
| 56 | } |
| 57 | |
| 58 | function _mintCapped(address to, uint256 amount) private { |
| 59 | uint256 minted = totalMinted + amount; |
| 60 | if (minted > K.MAX_SUPPLY) revert MaxSupplyExceeded(); |
| 61 | totalMinted = minted; |
| 62 | _mint(to, amount); |
| 63 | } |
| 64 | } |
| 65 |