feat: add router params
This commit is contained in:
@@ -12,6 +12,9 @@ import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
|
||||
import {IUnlockCallback} from "@uniswap/v4-core/src/interfaces/callback/IUnlockCallback.sol";
|
||||
import {TransientStateLibrary} from "@uniswap/v4-core/src/libraries/TransientStateLibrary.sol";
|
||||
import {V4Router} from "@uniswap/v4-periphery/src/V4Router.sol";
|
||||
import {Actions} from "@uniswap/v4-periphery/src/libraries/Actions.sol";
|
||||
import {IV4Router} from "@uniswap/v4-periphery/src/interfaces/IV4Router.sol";
|
||||
import {Permit2Payments} from "../../lib/Permit2Payments.sol";
|
||||
|
||||
error UniswapV4Executor__InvalidDataLength();
|
||||
error UniswapV4Executor__SwapFailed();
|
||||
@@ -29,14 +32,6 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
||||
uint256 private constant MAX_SQRT_RATIO =
|
||||
1461446703485210103287273052203988822378723970342;
|
||||
|
||||
struct SwapCallbackData {
|
||||
PoolKey key;
|
||||
IPoolManager.SwapParams params;
|
||||
address tokenIn;
|
||||
address tokenOut;
|
||||
address receiver;
|
||||
}
|
||||
|
||||
constructor(IPoolManager _poolManager) V4Router(_poolManager) {}
|
||||
|
||||
function swap(
|
||||
@@ -47,11 +42,12 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint24 fee,
|
||||
address receiver,
|
||||
address target,
|
||||
address receiver, // TODO: Investigate
|
||||
bool zeroForOne
|
||||
) = _decodeData(data);
|
||||
|
||||
uint128 amountIn128 = uint128(amountIn);
|
||||
uint128 amountOut128 = uint128(amountOut);
|
||||
PoolKey memory key = PoolKey({
|
||||
currency0: Currency.wrap(zeroForOne ? tokenIn : tokenOut),
|
||||
currency1: Currency.wrap(zeroForOne ? tokenOut : tokenIn),
|
||||
@@ -60,33 +56,42 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
||||
hooks: IHooks(address(0)) // No hooks needed for basic swaps
|
||||
});
|
||||
|
||||
IPoolManager.SwapParams memory params = IPoolManager.SwapParams({
|
||||
zeroForOne: zeroForOne,
|
||||
amountSpecified: int256(amountIn),
|
||||
sqrtPriceLimitX96: uint160(
|
||||
zeroForOne ? MIN_SQRT_RATIO + 1 : MAX_SQRT_RATIO - 1
|
||||
)
|
||||
});
|
||||
bytes memory actions = abi.encodePacked(
|
||||
uint8(Actions.SWAP_EXACT_IN_SINGLE),
|
||||
uint8(Actions.SETTLE_ALL),
|
||||
uint8(Actions.TAKE_ALL)
|
||||
);
|
||||
|
||||
SwapCallbackData memory callbackData = SwapCallbackData({
|
||||
key: key,
|
||||
params: params,
|
||||
tokenIn: tokenIn,
|
||||
tokenOut: tokenOut,
|
||||
receiver: receiver
|
||||
});
|
||||
bytes[] memory params = new bytes[](3);
|
||||
|
||||
IPoolManager poolManager = IPoolManager(target);
|
||||
params[0] = abi.encode(
|
||||
IV4Router.ExactInputSingleParams({
|
||||
poolKey: key,
|
||||
zeroForOne: zeroForOne,
|
||||
amountIn: amountIn128,
|
||||
amountOutMinimum: amountOut128,
|
||||
hookData: bytes("")
|
||||
})
|
||||
);
|
||||
|
||||
try poolManager.unlock(abi.encode(callbackData)) returns (
|
||||
bytes memory result
|
||||
) {
|
||||
amountOut = abi.decode(result, (uint256));
|
||||
params[1] = abi.encode(key.currency0, amountIn128);
|
||||
params[2] = abi.encode(key.currency1, amountOut128);
|
||||
|
||||
if (amountOut == 0) revert UniswapV4Executor__InsufficientOutput();
|
||||
} catch {
|
||||
// Convert the encoded parameters to calldata format
|
||||
bytes memory encodedActions = abi.encode(actions, params);
|
||||
(bool success, ) = address(this).call(
|
||||
abi.encodeWithSelector(this.executeActions.selector, encodedActions)
|
||||
);
|
||||
|
||||
if (!success) {
|
||||
revert UniswapV4Executor__SwapFailed();
|
||||
}
|
||||
|
||||
return amountOut;
|
||||
}
|
||||
|
||||
function executeActions(bytes calldata actions) external {
|
||||
_executeActions(actions);
|
||||
}
|
||||
|
||||
function _decodeData(
|
||||
@@ -99,27 +104,27 @@ contract UniswapV4Executor is IExecutor, V4Router {
|
||||
address tokenOut,
|
||||
uint24 fee,
|
||||
address receiver,
|
||||
address target,
|
||||
bool zeroForOne
|
||||
)
|
||||
{
|
||||
if (data.length != 84) {
|
||||
if (data.length != 64) {
|
||||
revert UniswapV4Executor__InvalidDataLength();
|
||||
}
|
||||
|
||||
tokenIn = address(bytes20(data[0:20]));
|
||||
tokenIn = address(bytes20(data[:20]));
|
||||
tokenOut = address(bytes20(data[20:40]));
|
||||
fee = uint24(bytes3(data[40:43]));
|
||||
receiver = address(bytes20(data[43:63]));
|
||||
target = address(bytes20(data[63:83]));
|
||||
zeroForOne = uint8(data[83]) > 0;
|
||||
zeroForOne = uint8(bytes1(data[63])) > 0;
|
||||
}
|
||||
|
||||
function _pay(
|
||||
Currency token,
|
||||
address payer,
|
||||
uint256 amount
|
||||
) internal override {}
|
||||
) internal override {
|
||||
// TODO: Implement
|
||||
}
|
||||
|
||||
function msgSender() public view override returns (address) {
|
||||
return msg.sender;
|
||||
|
||||
Reference in New Issue
Block a user