222 lines
8.2 KiB
Solidity
222 lines
8.2 KiB
Solidity
// SPDX-License-Identifier: UNLICENSED
|
|
pragma solidity ^0.8.26;
|
|
|
|
import "../../src/executors/UniswapV4Executor.sol";
|
|
import "./UniswapV4Utils.sol";
|
|
import "@src/executors/UniswapV4Executor.sol";
|
|
import {Constants} from "../Constants.sol";
|
|
import {Test} from "../../lib/forge-std/src/Test.sol";
|
|
import {SafeCallback} from "@uniswap/v4-periphery/src/base/SafeCallback.sol";
|
|
|
|
contract UniswapV4ExecutorExposed is UniswapV4Executor {
|
|
constructor(IPoolManager _poolManager) UniswapV4Executor(_poolManager) {}
|
|
|
|
function decodeData(bytes calldata data)
|
|
external
|
|
pure
|
|
returns (
|
|
address tokenIn,
|
|
address tokenOut,
|
|
uint256 amountOutMin,
|
|
bool zeroForOne,
|
|
address callbackExecutor,
|
|
bytes4 callbackSelector,
|
|
UniswapV4Pool[] memory pools
|
|
)
|
|
{
|
|
return _decodeData(data);
|
|
}
|
|
}
|
|
|
|
contract UniswapV4ExecutorTest is Test, Constants {
|
|
using SafeERC20 for IERC20;
|
|
|
|
UniswapV4ExecutorExposed uniswapV4Exposed;
|
|
IERC20 USDE = IERC20(USDE_ADDR);
|
|
IERC20 USDT = IERC20(USDT_ADDR);
|
|
address poolManager = 0x000000000004444c5dc75cB358380D2e3dE08A90;
|
|
|
|
function setUp() public {
|
|
uint256 forkBlock = 21817316;
|
|
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
|
uniswapV4Exposed =
|
|
new UniswapV4ExecutorExposed(IPoolManager(poolManager));
|
|
}
|
|
|
|
function testDecodeParams() public view {
|
|
uint256 minAmountOut = 100;
|
|
bool zeroForOne = true;
|
|
uint24 pool1Fee = 500;
|
|
int24 tickSpacing1 = 60;
|
|
uint24 pool2Fee = 1000;
|
|
int24 tickSpacing2 = -10;
|
|
|
|
UniswapV4Executor.UniswapV4Pool[] memory pools =
|
|
new UniswapV4Executor.UniswapV4Pool[](2);
|
|
pools[0] = UniswapV4Executor.UniswapV4Pool({
|
|
intermediaryToken: USDT_ADDR,
|
|
fee: pool1Fee,
|
|
tickSpacing: tickSpacing1
|
|
});
|
|
pools[1] = UniswapV4Executor.UniswapV4Pool({
|
|
intermediaryToken: USDE_ADDR,
|
|
fee: pool2Fee,
|
|
tickSpacing: tickSpacing2
|
|
});
|
|
|
|
bytes memory data = UniswapV4Utils.encodeExactInput(
|
|
USDE_ADDR,
|
|
USDT_ADDR,
|
|
minAmountOut,
|
|
zeroForOne,
|
|
address(uniswapV4Exposed),
|
|
SafeCallback.unlockCallback.selector,
|
|
pools
|
|
);
|
|
|
|
(
|
|
address tokenIn,
|
|
address tokenOut,
|
|
uint256 amountOutMin,
|
|
bool zeroForOneDecoded,
|
|
address callbackExecutor,
|
|
bytes4 callbackSelector,
|
|
UniswapV4Executor.UniswapV4Pool[] memory decodedPools
|
|
) = uniswapV4Exposed.decodeData(data);
|
|
|
|
assertEq(tokenIn, USDE_ADDR);
|
|
assertEq(tokenOut, USDT_ADDR);
|
|
assertEq(amountOutMin, minAmountOut);
|
|
assertEq(zeroForOneDecoded, zeroForOne);
|
|
assertEq(callbackExecutor, address(uniswapV4Exposed));
|
|
assertEq(callbackSelector, SafeCallback.unlockCallback.selector);
|
|
assertEq(decodedPools.length, 2);
|
|
assertEq(decodedPools[0].intermediaryToken, USDT_ADDR);
|
|
assertEq(decodedPools[0].fee, pool1Fee);
|
|
assertEq(decodedPools[0].tickSpacing, tickSpacing1);
|
|
assertEq(decodedPools[1].intermediaryToken, USDE_ADDR);
|
|
assertEq(decodedPools[1].fee, pool2Fee);
|
|
assertEq(decodedPools[1].tickSpacing, tickSpacing2);
|
|
}
|
|
|
|
function testSingleSwap() public {
|
|
uint256 amountIn = 100 ether;
|
|
deal(USDE_ADDR, address(uniswapV4Exposed), amountIn);
|
|
uint256 usdeBalanceBeforePool = USDE.balanceOf(poolManager);
|
|
uint256 usdeBalanceBeforeSwapExecutor =
|
|
USDE.balanceOf(address(uniswapV4Exposed));
|
|
|
|
UniswapV4Executor.UniswapV4Pool[] memory pools =
|
|
new UniswapV4Executor.UniswapV4Pool[](1);
|
|
pools[0] = UniswapV4Executor.UniswapV4Pool({
|
|
intermediaryToken: USDT_ADDR,
|
|
fee: uint24(100),
|
|
tickSpacing: int24(1)
|
|
});
|
|
|
|
bytes memory data = UniswapV4Utils.encodeExactInput(
|
|
USDE_ADDR,
|
|
USDT_ADDR,
|
|
uint256(1),
|
|
true,
|
|
address(uniswapV4Exposed),
|
|
SafeCallback.unlockCallback.selector,
|
|
pools
|
|
);
|
|
|
|
uint256 amountOut = uniswapV4Exposed.swap(amountIn, data);
|
|
assertEq(USDE.balanceOf(poolManager), usdeBalanceBeforePool + amountIn);
|
|
assertEq(
|
|
USDE.balanceOf(address(uniswapV4Exposed)),
|
|
usdeBalanceBeforeSwapExecutor - amountIn
|
|
);
|
|
assertTrue(USDT.balanceOf(address(uniswapV4Exposed)) == amountOut);
|
|
}
|
|
|
|
function testSingleSwapIntegration() public {
|
|
// USDE -> USDT
|
|
// Generated by the Tycho swap encoder - test_encode_uniswap_v4_simple_swap
|
|
bytes memory protocolData =
|
|
hex"4c9edd5852cd905f086c759e8383e09bff1e68b3dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000000000000000000000000000000000000000000001015615deb798bb3e4dfa0139dfa1b3d433cc23b72f91dd7346dac17f958d2ee523a2206206994597c13d831ec7000064000001";
|
|
|
|
uint256 amountIn = 100 ether;
|
|
deal(USDE_ADDR, address(uniswapV4Exposed), amountIn);
|
|
uint256 usdeBalanceBeforePool = USDE.balanceOf(poolManager);
|
|
uint256 usdeBalanceBeforeSwapExecutor =
|
|
USDE.balanceOf(address(uniswapV4Exposed));
|
|
|
|
uint256 amountOut = uniswapV4Exposed.swap(amountIn, protocolData);
|
|
assertEq(USDE.balanceOf(poolManager), usdeBalanceBeforePool + amountIn);
|
|
assertEq(
|
|
USDE.balanceOf(address(uniswapV4Exposed)),
|
|
usdeBalanceBeforeSwapExecutor - amountIn
|
|
);
|
|
assertTrue(USDT.balanceOf(address(uniswapV4Exposed)) == amountOut);
|
|
}
|
|
|
|
function testMultipleSwap() public {
|
|
// USDE -> USDT -> WBTC
|
|
uint256 amountIn = 100 ether;
|
|
deal(USDE_ADDR, address(uniswapV4Exposed), amountIn);
|
|
uint256 usdeBalanceBeforePool = USDE.balanceOf(poolManager);
|
|
uint256 usdeBalanceBeforeSwapExecutor =
|
|
USDE.balanceOf(address(uniswapV4Exposed));
|
|
|
|
UniswapV4Executor.UniswapV4Pool[] memory pools =
|
|
new UniswapV4Executor.UniswapV4Pool[](2);
|
|
pools[0] = UniswapV4Executor.UniswapV4Pool({
|
|
intermediaryToken: USDT_ADDR,
|
|
fee: uint24(100),
|
|
tickSpacing: int24(1)
|
|
});
|
|
pools[1] = UniswapV4Executor.UniswapV4Pool({
|
|
intermediaryToken: WBTC_ADDR,
|
|
fee: uint24(3000),
|
|
tickSpacing: int24(60)
|
|
});
|
|
|
|
bytes memory data = UniswapV4Utils.encodeExactInput(
|
|
USDE_ADDR,
|
|
WBTC_ADDR,
|
|
uint256(1),
|
|
true,
|
|
address(uniswapV4Exposed),
|
|
SafeCallback.unlockCallback.selector,
|
|
pools
|
|
);
|
|
|
|
uint256 amountOut = uniswapV4Exposed.swap(amountIn, data);
|
|
assertEq(USDE.balanceOf(poolManager), usdeBalanceBeforePool + amountIn);
|
|
assertEq(
|
|
USDE.balanceOf(address(uniswapV4Exposed)),
|
|
usdeBalanceBeforeSwapExecutor - amountIn
|
|
);
|
|
assertTrue(
|
|
IERC20(WBTC_ADDR).balanceOf(address(uniswapV4Exposed)) == amountOut
|
|
);
|
|
}
|
|
|
|
function testMultipleSwapIntegration() public {
|
|
// USDE -> USDT -> WBTC
|
|
// Generated by the Tycho swap encoder - test_encode_uniswap_v4_combined
|
|
bytes memory protocolData =
|
|
hex"4c9edd5852cd905f086c759e8383e09bff1e68b32260fac5e5542a773aa44fbcfedf7c193bc2c5990000000000000000000000000000000000000000000000000000000000000001015615deb798bb3e4dfa0139dfa1b3d433cc23b72f91dd7346dac17f958d2ee523a2206206994597c13d831ec70000640000012260fac5e5542a773aa44fbcfedf7c193bc2c599000bb800003c";
|
|
|
|
uint256 amountIn = 100 ether;
|
|
deal(USDE_ADDR, address(uniswapV4Exposed), amountIn);
|
|
uint256 usdeBalanceBeforePool = USDE.balanceOf(poolManager);
|
|
uint256 usdeBalanceBeforeSwapExecutor =
|
|
USDE.balanceOf(address(uniswapV4Exposed));
|
|
|
|
uint256 amountOut = uniswapV4Exposed.swap(amountIn, protocolData);
|
|
assertEq(USDE.balanceOf(poolManager), usdeBalanceBeforePool + amountIn);
|
|
assertEq(
|
|
USDE.balanceOf(address(uniswapV4Exposed)),
|
|
usdeBalanceBeforeSwapExecutor - amountIn
|
|
);
|
|
assertTrue(
|
|
IERC20(WBTC_ADDR).balanceOf(address(uniswapV4Exposed)) == amountOut
|
|
);
|
|
}
|
|
}
|