|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity =0.7.6; |
| 3 | + |
| 4 | +import "../interfaces/IERC20Minimal.sol"; |
| 5 | + |
| 6 | +import "../libraries/SafeCast.sol"; |
| 7 | +import "../libraries/TickMath.sol"; |
| 8 | + |
| 9 | +import "../interfaces/callback/IKatanaV3MintCallback.sol"; |
| 10 | +import "../interfaces/callback/IKatanaV3SwapCallback.sol"; |
| 11 | +import "../interfaces/callback/IKatanaV3FlashCallback.sol"; |
| 12 | + |
| 13 | +import "../interfaces/IKatanaV3Pool.sol"; |
| 14 | + |
| 15 | +contract TestKatanaV3Callee is IKatanaV3MintCallback, IKatanaV3SwapCallback, IKatanaV3FlashCallback { |
| 16 | + using SafeCast for uint256; |
| 17 | + |
| 18 | + function swapExact0For1(address pool, uint256 amount0In, address recipient, uint160 sqrtPriceLimitX96) external { |
| 19 | + IKatanaV3Pool(pool).swap(recipient, true, amount0In.toInt256(), sqrtPriceLimitX96, abi.encode(msg.sender)); |
| 20 | + } |
| 21 | + |
| 22 | + function swap0ForExact1(address pool, uint256 amount1Out, address recipient, uint160 sqrtPriceLimitX96) external { |
| 23 | + IKatanaV3Pool(pool).swap(recipient, true, -amount1Out.toInt256(), sqrtPriceLimitX96, abi.encode(msg.sender)); |
| 24 | + } |
| 25 | + |
| 26 | + function swapExact1For0(address pool, uint256 amount1In, address recipient, uint160 sqrtPriceLimitX96) external { |
| 27 | + IKatanaV3Pool(pool).swap(recipient, false, amount1In.toInt256(), sqrtPriceLimitX96, abi.encode(msg.sender)); |
| 28 | + } |
| 29 | + |
| 30 | + function swap1ForExact0(address pool, uint256 amount0Out, address recipient, uint160 sqrtPriceLimitX96) external { |
| 31 | + IKatanaV3Pool(pool).swap(recipient, false, -amount0Out.toInt256(), sqrtPriceLimitX96, abi.encode(msg.sender)); |
| 32 | + } |
| 33 | + |
| 34 | + function swapToLowerSqrtPrice(address pool, uint160 sqrtPriceX96, address recipient) external { |
| 35 | + IKatanaV3Pool(pool).swap(recipient, true, type(int256).max, sqrtPriceX96, abi.encode(msg.sender)); |
| 36 | + } |
| 37 | + |
| 38 | + function swapToHigherSqrtPrice(address pool, uint160 sqrtPriceX96, address recipient) external { |
| 39 | + IKatanaV3Pool(pool).swap(recipient, false, type(int256).max, sqrtPriceX96, abi.encode(msg.sender)); |
| 40 | + } |
| 41 | + |
| 42 | + event SwapCallback(int256 amount0Delta, int256 amount1Delta); |
| 43 | + |
| 44 | + function katanaV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata data) external override { |
| 45 | + address sender = abi.decode(data, (address)); |
| 46 | + |
| 47 | + emit SwapCallback(amount0Delta, amount1Delta); |
| 48 | + |
| 49 | + if (amount0Delta > 0) { |
| 50 | + IERC20Minimal(IKatanaV3Pool(msg.sender).token0()).transferFrom(sender, msg.sender, uint256(amount0Delta)); |
| 51 | + } else if (amount1Delta > 0) { |
| 52 | + IERC20Minimal(IKatanaV3Pool(msg.sender).token1()).transferFrom(sender, msg.sender, uint256(amount1Delta)); |
| 53 | + } else { |
| 54 | + // if both are not gt 0, both must be 0. |
| 55 | + assert(amount0Delta == 0 && amount1Delta == 0); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + function mint(address pool, address recipient, int24 tickLower, int24 tickUpper, uint128 amount) external { |
| 60 | + IKatanaV3Pool(pool).mint(recipient, tickLower, tickUpper, amount, abi.encode(msg.sender)); |
| 61 | + } |
| 62 | + |
| 63 | + event MintCallback(uint256 amount0Owed, uint256 amount1Owed); |
| 64 | + |
| 65 | + function katanaV3MintCallback(uint256 amount0Owed, uint256 amount1Owed, bytes calldata data) external override { |
| 66 | + address sender = abi.decode(data, (address)); |
| 67 | + |
| 68 | + emit MintCallback(amount0Owed, amount1Owed); |
| 69 | + if (amount0Owed > 0) { |
| 70 | + IERC20Minimal(IKatanaV3Pool(msg.sender).token0()).transferFrom(sender, msg.sender, amount0Owed); |
| 71 | + } |
| 72 | + if (amount1Owed > 0) { |
| 73 | + IERC20Minimal(IKatanaV3Pool(msg.sender).token1()).transferFrom(sender, msg.sender, amount1Owed); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + event FlashCallback(uint256 fee0, uint256 fee1); |
| 78 | + |
| 79 | + function flash(address pool, address recipient, uint256 amount0, uint256 amount1, uint256 pay0, uint256 pay1) |
| 80 | + external |
| 81 | + { |
| 82 | + IKatanaV3Pool(pool).flash(recipient, amount0, amount1, abi.encode(msg.sender, pay0, pay1)); |
| 83 | + } |
| 84 | + |
| 85 | + function katanaV3FlashCallback(uint256 fee0, uint256 fee1, bytes calldata data) external override { |
| 86 | + emit FlashCallback(fee0, fee1); |
| 87 | + |
| 88 | + (address sender, uint256 pay0, uint256 pay1) = abi.decode(data, (address, uint256, uint256)); |
| 89 | + |
| 90 | + if (pay0 > 0) IERC20Minimal(IKatanaV3Pool(msg.sender).token0()).transferFrom(sender, msg.sender, pay0); |
| 91 | + if (pay1 > 0) IERC20Minimal(IKatanaV3Pool(msg.sender).token1()).transferFrom(sender, msg.sender, pay1); |
| 92 | + } |
| 93 | +} |
0 commit comments