Burner.sol
68 lines · sha256 43695be85cd074cb24dd7965164538e1df310c541dd2dfcc5214344d63fff5eb · Raw
| 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.26; |
| 3 | |
| 4 | import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; |
| 5 | import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; |
| 6 | import {KibbleToken} from "./KibbleToken.sol"; |
| 7 | import {Oracle} from "./Oracle.sol"; |
| 8 | import {PoolSwapper} from "./libraries/PoolSwapper.sol"; |
| 9 | |
| 10 | /// @title Burner |
| 11 | /// @notice Receives consumed HYPE power, swaps it for KIBBLE in the pool and burns it; also burns the KIBBLE |
| 12 | /// consumed as power directly (docs §4, §9, §11). |
| 13 | /// @dev `burn()` is public. Buybacks stop at TWAP × (1 + 10%) (MAX_TWAP_DEVIATION_BPS) so a public burn cannot be |
| 14 | /// sandwiched at an arbitrary price; HYPE not spent stays here for the next call. |
| 15 | contract Burner is PoolSwapper, ReentrancyGuard { |
| 16 | /// @notice docs §11. `hypeSpent` = 0 for KIBBLE burned directly (power paid in KIBBLE, dust). |
| 17 | event Burned(uint256 kibbleAmount, uint256 hypeSpent); |
| 18 | |
| 19 | Oracle public immutable oracle; |
| 20 | |
| 21 | /// @notice Total KIBBLE burned by the protocol (direct burns + buybacks). |
| 22 | uint256 public totalBurned; |
| 23 | /// @notice KIBBLE burned directly (power paid in KIBBLE, genesis / zap dust). |
| 24 | uint256 public totalBurnedDirect; |
| 25 | /// @notice KIBBLE bought back with HYPE and burned. |
| 26 | uint256 public totalBurnedBuyback; |
| 27 | /// @notice HYPE spent on buybacks. |
| 28 | uint256 public totalHypeSpent; |
| 29 | |
| 30 | constructor(IPoolManager poolManager_, KibbleToken kibble_, Oracle oracle_) |
| 31 | PoolSwapper(poolManager_, address(kibble_)) |
| 32 | { |
| 33 | oracle = oracle_; |
| 34 | } |
| 35 | |
| 36 | receive() external payable {} |
| 37 | |
| 38 | /// @notice Burns all KIBBLE held, then buys back KIBBLE with all HYPE held (within the TWAP band) and burns it. |
| 39 | /// Anyone can call (docs §4 "100% of the HYPE consumed is used for KIBBLE buyback and burn"). |
| 40 | function burn() external nonReentrant returns (uint256 burnedDirect, uint256 bought, uint256 hypeSpent) { |
| 41 | KibbleToken k = KibbleToken(kibbleToken); |
| 42 | burnedDirect = k.balanceOf(address(this)); |
| 43 | if (burnedDirect > 0) { |
| 44 | k.burn(burnedDirect); |
| 45 | totalBurned += burnedDirect; |
| 46 | totalBurnedDirect += burnedDirect; |
| 47 | emit Burned(burnedDirect, 0); |
| 48 | } |
| 49 | if (!oracle.initialized()) return (burnedDirect, 0, 0); |
| 50 | oracle.update(); |
| 51 | uint256 hype = address(this).balance; |
| 52 | if (hype == 0) return (burnedDirect, 0, 0); |
| 53 | (hypeSpent, bought) = _swapExactIn(true, hype, _buyLimit(oracle.twap())); |
| 54 | if (bought > 0) { |
| 55 | k.burn(bought); |
| 56 | totalBurned += bought; |
| 57 | totalBurnedBuyback += bought; |
| 58 | totalHypeSpent += hypeSpent; |
| 59 | emit Burned(bought, hypeSpent); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// @notice HYPE waiting to be spent on buybacks. |
| 64 | function pendingHype() external view returns (uint256) { |
| 65 | return address(this).balance; |
| 66 | } |
| 67 | } |
| 68 |