PoolSwapper.sol
101 lines · sha256 4b35c1a42ee752c49746042306aaf98378a825b0b283b9f2f795c56f83d4c2e4 · 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 {IUnlockCallback} from "@uniswap/v4-core/src/interfaces/callback/IUnlockCallback.sol"; |
| 6 | import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; |
| 7 | import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol"; |
| 8 | import {Currency} from "@uniswap/v4-core/src/types/Currency.sol"; |
| 9 | import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol"; |
| 10 | import {SwapParams} from "@uniswap/v4-core/src/types/PoolOperation.sol"; |
| 11 | import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; |
| 12 | import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; |
| 13 | import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 14 | import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 15 | import {KibblePool} from "./KibblePool.sol"; |
| 16 | import {PriceMath} from "./PriceMath.sol"; |
| 17 | import {KibbleConstants as K} from "./KibbleConstants.sol"; |
| 18 | |
| 19 | /// @title PoolSwapper |
| 20 | /// @notice Exact-input swaps on the HYPE / KIBBLE pool through PoolManager.unlock (no router). |
| 21 | abstract contract PoolSwapper is IUnlockCallback { |
| 22 | using SafeERC20 for IERC20; |
| 23 | using PoolIdLibrary for PoolKey; |
| 24 | using StateLibrary for IPoolManager; |
| 25 | |
| 26 | error NotPoolManager(); |
| 27 | |
| 28 | IPoolManager public immutable poolManager; |
| 29 | address public immutable kibbleToken; |
| 30 | |
| 31 | constructor(IPoolManager poolManager_, address kibble_) { |
| 32 | poolManager = poolManager_; |
| 33 | kibbleToken = kibble_; |
| 34 | } |
| 35 | |
| 36 | function _poolKey() internal view returns (PoolKey memory) { |
| 37 | return KibblePool.key(kibbleToken); |
| 38 | } |
| 39 | |
| 40 | function _sqrtPrice() internal view returns (uint160 sqrtPriceX96) { |
| 41 | (sqrtPriceX96,,,) = poolManager.getSlot0(_poolKey().toId()); |
| 42 | } |
| 43 | |
| 44 | /// @notice sqrt price limit for buying KIBBLE with HYPE: stop once HYPE-per-KIBBLE exceeds |
| 45 | /// TWAP × (1 + MAX_TWAP_DEVIATION_BPS). |
| 46 | function _buyLimit(uint256 twapPrice) internal pure returns (uint160) { |
| 47 | return PriceMath.sqrtFromPrice(twapPrice * (10_000 + K.MAX_TWAP_DEVIATION_BPS) / 10_000); |
| 48 | } |
| 49 | |
| 50 | /// @notice Exact-input swap that stops at `sqrtLimit`. Returns (0, 0) when the price is already past the limit. |
| 51 | function _swapExactIn(bool zeroForOne, uint256 amountIn, uint160 sqrtLimit) |
| 52 | internal |
| 53 | returns (uint256 spent, uint256 received) |
| 54 | { |
| 55 | if (amountIn == 0 || amountIn > uint256(type(int256).max)) return (0, 0); |
| 56 | uint160 current = _sqrtPrice(); |
| 57 | if (current == 0) return (0, 0); |
| 58 | if (zeroForOne) { |
| 59 | if (sqrtLimit < TickMath.MIN_SQRT_PRICE + 1) sqrtLimit = TickMath.MIN_SQRT_PRICE + 1; |
| 60 | if (sqrtLimit >= current) return (0, 0); |
| 61 | } else { |
| 62 | if (sqrtLimit > TickMath.MAX_SQRT_PRICE - 1) sqrtLimit = TickMath.MAX_SQRT_PRICE - 1; |
| 63 | if (sqrtLimit <= current) return (0, 0); |
| 64 | } |
| 65 | bytes memory res = poolManager.unlock(abi.encode(zeroForOne, amountIn, sqrtLimit)); |
| 66 | (spent, received) = abi.decode(res, (uint256, uint256)); |
| 67 | } |
| 68 | |
| 69 | /// @inheritdoc IUnlockCallback |
| 70 | function unlockCallback(bytes calldata data) external virtual returns (bytes memory) { |
| 71 | if (msg.sender != address(poolManager)) revert NotPoolManager(); |
| 72 | (bool zeroForOne, uint256 amountIn, uint160 sqrtLimit) = abi.decode(data, (bool, uint256, uint160)); |
| 73 | PoolKey memory key = _poolKey(); |
| 74 | BalanceDelta d = poolManager.swap( |
| 75 | key, |
| 76 | SwapParams({zeroForOne: zeroForOne, amountSpecified: -int256(amountIn), sqrtPriceLimitX96: sqrtLimit}), |
| 77 | "" |
| 78 | ); |
| 79 | int128 a0 = d.amount0(); |
| 80 | int128 a1 = d.amount1(); |
| 81 | uint256 spent; |
| 82 | uint256 received; |
| 83 | if (zeroForOne) { |
| 84 | spent = a0 < 0 ? uint256(uint128(-a0)) : 0; |
| 85 | received = a1 > 0 ? uint256(uint128(a1)) : 0; |
| 86 | if (spent > 0) poolManager.settle{value: spent}(); |
| 87 | if (received > 0) poolManager.take(key.currency1, address(this), received); |
| 88 | } else { |
| 89 | spent = a1 < 0 ? uint256(uint128(-a1)) : 0; |
| 90 | received = a0 > 0 ? uint256(uint128(a0)) : 0; |
| 91 | if (spent > 0) { |
| 92 | poolManager.sync(key.currency1); |
| 93 | IERC20(kibbleToken).safeTransfer(address(poolManager), spent); |
| 94 | poolManager.settle(); |
| 95 | } |
| 96 | if (received > 0) poolManager.take(key.currency0, address(this), received); |
| 97 | } |
| 98 | return abi.encode(spent, received); |
| 99 | } |
| 100 | } |
| 101 |