// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; import {IUnlockCallback} from "@uniswap/v4-core/src/interfaces/callback/IUnlockCallback.sol"; import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol"; import {Currency} from "@uniswap/v4-core/src/types/Currency.sol"; import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol"; import {SwapParams} from "@uniswap/v4-core/src/types/PoolOperation.sol"; import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {KibblePool} from "./KibblePool.sol"; import {PriceMath} from "./PriceMath.sol"; import {KibbleConstants as K} from "./KibbleConstants.sol"; /// @title PoolSwapper /// @notice Exact-input swaps on the HYPE / KIBBLE pool through PoolManager.unlock (no router). abstract contract PoolSwapper is IUnlockCallback { using SafeERC20 for IERC20; using PoolIdLibrary for PoolKey; using StateLibrary for IPoolManager; error NotPoolManager(); IPoolManager public immutable poolManager; address public immutable kibbleToken; constructor(IPoolManager poolManager_, address kibble_) { poolManager = poolManager_; kibbleToken = kibble_; } function _poolKey() internal view returns (PoolKey memory) { return KibblePool.key(kibbleToken); } function _sqrtPrice() internal view returns (uint160 sqrtPriceX96) { (sqrtPriceX96,,,) = poolManager.getSlot0(_poolKey().toId()); } /// @notice sqrt price limit for buying KIBBLE with HYPE: stop once HYPE-per-KIBBLE exceeds /// TWAP × (1 + MAX_TWAP_DEVIATION_BPS). function _buyLimit(uint256 twapPrice) internal pure returns (uint160) { return PriceMath.sqrtFromPrice(twapPrice * (10_000 + K.MAX_TWAP_DEVIATION_BPS) / 10_000); } /// @notice Exact-input swap that stops at `sqrtLimit`. Returns (0, 0) when the price is already past the limit. function _swapExactIn(bool zeroForOne, uint256 amountIn, uint160 sqrtLimit) internal returns (uint256 spent, uint256 received) { if (amountIn == 0 || amountIn > uint256(type(int256).max)) return (0, 0); uint160 current = _sqrtPrice(); if (current == 0) return (0, 0); if (zeroForOne) { if (sqrtLimit < TickMath.MIN_SQRT_PRICE + 1) sqrtLimit = TickMath.MIN_SQRT_PRICE + 1; if (sqrtLimit >= current) return (0, 0); } else { if (sqrtLimit > TickMath.MAX_SQRT_PRICE - 1) sqrtLimit = TickMath.MAX_SQRT_PRICE - 1; if (sqrtLimit <= current) return (0, 0); } bytes memory res = poolManager.unlock(abi.encode(zeroForOne, amountIn, sqrtLimit)); (spent, received) = abi.decode(res, (uint256, uint256)); } /// @inheritdoc IUnlockCallback function unlockCallback(bytes calldata data) external virtual returns (bytes memory) { if (msg.sender != address(poolManager)) revert NotPoolManager(); (bool zeroForOne, uint256 amountIn, uint160 sqrtLimit) = abi.decode(data, (bool, uint256, uint160)); PoolKey memory key = _poolKey(); BalanceDelta d = poolManager.swap( key, SwapParams({zeroForOne: zeroForOne, amountSpecified: -int256(amountIn), sqrtPriceLimitX96: sqrtLimit}), "" ); int128 a0 = d.amount0(); int128 a1 = d.amount1(); uint256 spent; uint256 received; if (zeroForOne) { spent = a0 < 0 ? uint256(uint128(-a0)) : 0; received = a1 > 0 ? uint256(uint128(a1)) : 0; if (spent > 0) poolManager.settle{value: spent}(); if (received > 0) poolManager.take(key.currency1, address(this), received); } else { spent = a1 < 0 ? uint256(uint128(-a1)) : 0; received = a0 > 0 ? uint256(uint128(a0)) : 0; if (spent > 0) { poolManager.sync(key.currency1); IERC20(kibbleToken).safeTransfer(address(poolManager), spent); poolManager.settle(); } if (received > 0) poolManager.take(key.currency0, address(this), received); } return abi.encode(spent, received); } }