Zapper.sol
53 lines · sha256 4f2632707e7fefe8e4053ea6b1de0cc30c5e0f475f62aac219968ed0a7fb2f05 · 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 {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 7 | import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 8 | import {KibbleToken} from "./KibbleToken.sol"; |
| 9 | import {Oracle} from "./Oracle.sol"; |
| 10 | import {LpLocker} from "./LpLocker.sol"; |
| 11 | import {PoolSwapper} from "./libraries/PoolSwapper.sol"; |
| 12 | |
| 13 | /// @title Zapper |
| 14 | /// @notice Rig purchases after block 0: half of the HYPE buys KIBBLE in the pool, the other half is paired with it |
| 15 | /// and the liquidity is locked in `LpLocker` forever (docs §3, §9, §11). |
| 16 | /// @dev Dust: a zap works on the whole HYPE + KIBBLE balance of this contract. Whatever the LP add cannot use |
| 17 | /// (rounding, price impact of the swap, or HYPE left unswapped because the price is outside the TWAP band) |
| 18 | /// stays here and is folded into the next zap, so every rig HYPE ends up in locked LP. The swap size accounts |
| 19 | /// for KIBBLE already held, so with no leftover it is exactly half of the HYPE. |
| 20 | contract Zapper is PoolSwapper, ReentrancyGuard { |
| 21 | using SafeERC20 for IERC20; |
| 22 | |
| 23 | Oracle public immutable oracle; |
| 24 | LpLocker public immutable lpLocker; |
| 25 | |
| 26 | constructor(IPoolManager poolManager_, KibbleToken kibble_, Oracle oracle_, LpLocker lpLocker_) |
| 27 | PoolSwapper(poolManager_, address(kibble_)) |
| 28 | { |
| 29 | oracle = oracle_; |
| 30 | lpLocker = lpLocker_; |
| 31 | } |
| 32 | |
| 33 | receive() external payable {} |
| 34 | |
| 35 | /// @notice Zaps `msg.value` plus any HYPE / KIBBLE left from previous zaps into locked LP. Called by |
| 36 | /// `Miner.buyRigs` after block 0; anyone can call (extra HYPE is a donation to locked LP). |
| 37 | function zap() external payable nonReentrant returns (uint128 liquidity) { |
| 38 | if (!oracle.initialized()) return 0; |
| 39 | oracle.update(); |
| 40 | IERC20 k = IERC20(kibbleToken); |
| 41 | uint256 hype = address(this).balance; |
| 42 | uint256 kib = k.balanceOf(address(this)); |
| 43 | uint256 kibValue = kib * oracle.spotPrice() / 1e18; |
| 44 | uint256 swapIn = kibValue >= hype ? 0 : (hype - kibValue) / 2; |
| 45 | if (swapIn > 0) _swapExactIn(true, swapIn, _buyLimit(oracle.twap())); |
| 46 | |
| 47 | uint256 kibAll = k.balanceOf(address(this)); |
| 48 | k.forceApprove(address(lpLocker), kibAll); |
| 49 | (liquidity,,) = lpLocker.addLiquidity{value: address(this).balance}(kibAll); |
| 50 | k.forceApprove(address(lpLocker), 0); |
| 51 | } |
| 52 | } |
| 53 |