// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {KibbleToken} from "./KibbleToken.sol"; import {Oracle} from "./Oracle.sol"; import {LpLocker} from "./LpLocker.sol"; import {PoolSwapper} from "./libraries/PoolSwapper.sol"; /// @title Zapper /// @notice Rig purchases after block 0: half of the HYPE buys KIBBLE in the pool, the other half is paired with it /// and the liquidity is locked in `LpLocker` forever (docs §3, §9, §11). /// @dev Dust: a zap works on the whole HYPE + KIBBLE balance of this contract. Whatever the LP add cannot use /// (rounding, price impact of the swap, or HYPE left unswapped because the price is outside the TWAP band) /// stays here and is folded into the next zap, so every rig HYPE ends up in locked LP. The swap size accounts /// for KIBBLE already held, so with no leftover it is exactly half of the HYPE. contract Zapper is PoolSwapper, ReentrancyGuard { using SafeERC20 for IERC20; Oracle public immutable oracle; LpLocker public immutable lpLocker; constructor(IPoolManager poolManager_, KibbleToken kibble_, Oracle oracle_, LpLocker lpLocker_) PoolSwapper(poolManager_, address(kibble_)) { oracle = oracle_; lpLocker = lpLocker_; } receive() external payable {} /// @notice Zaps `msg.value` plus any HYPE / KIBBLE left from previous zaps into locked LP. Called by /// `Miner.buyRigs` after block 0; anyone can call (extra HYPE is a donation to locked LP). function zap() external payable nonReentrant returns (uint128 liquidity) { if (!oracle.initialized()) return 0; oracle.update(); IERC20 k = IERC20(kibbleToken); uint256 hype = address(this).balance; uint256 kib = k.balanceOf(address(this)); uint256 kibValue = kib * oracle.spotPrice() / 1e18; uint256 swapIn = kibValue >= hype ? 0 : (hype - kibValue) / 2; if (swapIn > 0) _swapExactIn(true, swapIn, _buyLimit(oracle.twap())); uint256 kibAll = k.balanceOf(address(this)); k.forceApprove(address(lpLocker), kibAll); (liquidity,,) = lpLocker.addLiquidity{value: address(this).balance}(kibAll); k.forceApprove(address(lpLocker), 0); } }