feat: add test for UniswapV4Executor

This commit is contained in:
royvardhan
2025-02-10 23:08:29 +05:30
parent e62c332451
commit 4599f07df0
3 changed files with 112 additions and 31 deletions

View File

@@ -6,31 +6,17 @@ import {IERC20, SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeE
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
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();
error UniswapV4Executor__InsufficientOutput();
error UniswapV4Executor__ManagerMismatch();
contract UniswapV4Executor is IExecutor, V4Router {
using SafeERC20 for IERC20;
using CurrencyLibrary for Currency;
using SafeCast for int128;
using SafeCast for int256;
using TransientStateLibrary for IPoolManager;
uint256 private constant MIN_SQRT_RATIO = 4295128739;
uint256 private constant MAX_SQRT_RATIO =
1461446703485210103287273052203988822378723970342;
constructor(IPoolManager _poolManager) V4Router(_poolManager) {}
@@ -42,8 +28,9 @@ contract UniswapV4Executor is IExecutor, V4Router {
address tokenIn,
address tokenOut,
uint24 fee,
address receiver, // TODO: Investigate
bool zeroForOne
address receiver, // TODO: This is not used right now
bool zeroForOne,
uint24 tickSpacing
) = _decodeData(data);
uint128 amountIn128 = uint128(amountIn);
@@ -52,8 +39,8 @@ contract UniswapV4Executor is IExecutor, V4Router {
currency0: Currency.wrap(zeroForOne ? tokenIn : tokenOut),
currency1: Currency.wrap(zeroForOne ? tokenOut : tokenIn),
fee: fee,
tickSpacing: 60, // Standard tick spacing
hooks: IHooks(address(0)) // No hooks needed for basic swaps
tickSpacing: int24(tickSpacing),
hooks: IHooks(address(0))
});
bytes memory actions = abi.encodePacked(
@@ -77,20 +64,13 @@ contract UniswapV4Executor is IExecutor, V4Router {
params[1] = abi.encode(key.currency0, amountIn128);
params[2] = abi.encode(key.currency1, amountOut128);
// 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();
}
this.executeActions(abi.encode(actions, params));
// TODO: This is still hardcode to zero, find a way to return the actual amount out
return amountOut;
}
function executeActions(bytes calldata actions) external {
function executeActions(bytes calldata actions) public {
_executeActions(actions);
}
@@ -104,10 +84,11 @@ contract UniswapV4Executor is IExecutor, V4Router {
address tokenOut,
uint24 fee,
address receiver,
bool zeroForOne
bool zeroForOne,
uint24 tickSpacing
)
{
if (data.length != 64) {
if (data.length != 67) {
revert UniswapV4Executor__InvalidDataLength();
}
@@ -116,6 +97,7 @@ contract UniswapV4Executor is IExecutor, V4Router {
fee = uint24(bytes3(data[40:43]));
receiver = address(bytes20(data[43:63]));
zeroForOne = uint8(bytes1(data[63])) > 0;
tickSpacing = uint24(bytes3(data[64:67]));
}
function _pay(
@@ -123,7 +105,7 @@ contract UniswapV4Executor is IExecutor, V4Router {
address payer,
uint256 amount
) internal override {
// TODO: Implement
token.transfer(payer, amount);
}
function msgSender() public view override returns (address) {