feat: Add transfer out for Uniswap V4

Add transfer in Executor and pass receiver address in encoding
This is done by using the TAKE action instead of TAKE_ALL

--- don't change below this line ---
ENG-4315 Took 1 hour 36 minutes

Took 2 minutes
This commit is contained in:
Diana Carvalho
2025-04-15 16:21:23 +01:00
parent 328a281a44
commit ec87969aa6
7 changed files with 43 additions and 27 deletions

View File

@@ -46,6 +46,7 @@ contract UniswapV4Executor is IExecutor, V4Router, ICallback, TokenTransfer {
address tokenOut,
bool zeroForOne,
TransferType transferType,
address receiver,
UniswapV4Executor.UniswapV4Pool[] memory pools
) = _decodeData(data);
@@ -72,7 +73,7 @@ contract UniswapV4Executor is IExecutor, V4Router, ICallback, TokenTransfer {
bytes memory actions = abi.encodePacked(
uint8(Actions.SWAP_EXACT_IN_SINGLE),
uint8(Actions.SETTLE_ALL),
uint8(Actions.TAKE_ALL)
uint8(Actions.TAKE)
);
bytes[] memory params = new bytes[](3);
@@ -87,7 +88,7 @@ contract UniswapV4Executor is IExecutor, V4Router, ICallback, TokenTransfer {
})
);
params[1] = abi.encode(tokenIn, amountIn); // currency to settle
params[2] = abi.encode(tokenOut, uint256(0)); // currency to take
params[2] = abi.encode(tokenOut, receiver, uint256(0)); // currency to take. 0 means to take the full amount
swapData = abi.encode(actions, params);
} else {
PathKey[] memory path = new PathKey[](pools.length);
@@ -104,7 +105,7 @@ contract UniswapV4Executor is IExecutor, V4Router, ICallback, TokenTransfer {
bytes memory actions = abi.encodePacked(
uint8(Actions.SWAP_EXACT_IN),
uint8(Actions.SETTLE_ALL),
uint8(Actions.TAKE_ALL)
uint8(Actions.TAKE)
);
bytes[] memory params = new bytes[](3);
@@ -119,22 +120,22 @@ contract UniswapV4Executor is IExecutor, V4Router, ICallback, TokenTransfer {
})
);
params[1] = abi.encode(currencyIn, amountIn);
params[2] = abi.encode(Currency.wrap(tokenOut), uint256(0));
params[2] = abi.encode(Currency.wrap(tokenOut), receiver, uint256(0));
swapData = abi.encode(actions, params);
}
uint256 tokenOutBalanceBefore;
tokenOutBalanceBefore = tokenOut == address(0)
? address(this).balance
: IERC20(tokenOut).balanceOf(address(this));
? receiver.balance
: IERC20(tokenOut).balanceOf(receiver);
executeActions(swapData);
uint256 tokenOutBalanceAfter;
tokenOutBalanceAfter = tokenOut == address(0)
? address(this).balance
: IERC20(tokenOut).balanceOf(address(this));
? receiver.balance
: IERC20(tokenOut).balanceOf(receiver);
calculatedAmount = tokenOutBalanceAfter - tokenOutBalanceBefore;
@@ -155,10 +156,11 @@ contract UniswapV4Executor is IExecutor, V4Router, ICallback, TokenTransfer {
address tokenOut,
bool zeroForOne,
TransferType transferType,
address receiver,
UniswapV4Pool[] memory pools
)
{
if (data.length < 67) {
if (data.length < 88) {
revert UniswapV4Executor__InvalidDataLength();
}
@@ -166,10 +168,11 @@ contract UniswapV4Executor is IExecutor, V4Router, ICallback, TokenTransfer {
tokenOut = address(bytes20(data[20:40]));
zeroForOne = (data[40] != 0);
transferType = TransferType(uint8(data[41]));
receiver = address(bytes20(data[42:62]));
uint256 poolsLength = (data.length - 42) / 26; // 26 bytes per pool object
uint256 poolsLength = (data.length - 62) / 26; // 26 bytes per pool object
pools = new UniswapV4Pool[](poolsLength);
bytes memory poolsData = data[42:];
bytes memory poolsData = data[62:];
uint256 offset = 0;
for (uint256 i = 0; i < poolsLength; i++) {
address intermediaryToken;

View File

@@ -423,6 +423,7 @@ contract TychoRouterSplitSwapTest is TychoRouterTestSetup {
USDT_ADDR,
true,
TokenTransfer.TransferType.TRANSFER_PERMIT2_TO_ROUTER,
ALICE,
pools
);
@@ -471,7 +472,7 @@ contract TychoRouterSplitSwapTest is TychoRouterTestSetup {
});
bytes memory protocolData = UniswapV4Utils.encodeExactInput(
USDE_ADDR, WBTC_ADDR, true, TokenTransfer.TransferType.NONE, pools
USDE_ADDR, WBTC_ADDR, true, TokenTransfer.TransferType.NONE, ALICE, pools
);
bytes memory swap = encodeSplitSwap(
@@ -483,7 +484,7 @@ contract TychoRouterSplitSwapTest is TychoRouterTestSetup {
tychoRouter.exposedSplitSwap(amountIn, 2, pleEncode(swaps));
assertEq(IERC20(WBTC_ADDR).balanceOf(tychoRouterAddr), 102718);
assertEq(IERC20(WBTC_ADDR).balanceOf(ALICE), 102718);
}
function testSplitInputCyclicSwapInternalMethod() public {

View File

@@ -22,6 +22,7 @@ contract UniswapV4ExecutorExposed is UniswapV4Executor {
address tokenOut,
bool zeroForOne,
TokenTransfer.TransferType transferType,
address receiver,
UniswapV4Pool[] memory pools
)
{
@@ -68,7 +69,7 @@ contract UniswapV4ExecutorTest is Test, Constants {
});
bytes memory data = UniswapV4Utils.encodeExactInput(
USDE_ADDR, USDT_ADDR, zeroForOne, transferType, pools
USDE_ADDR, USDT_ADDR, zeroForOne, transferType, ALICE, pools
);
(
@@ -76,6 +77,7 @@ contract UniswapV4ExecutorTest is Test, Constants {
address tokenOut,
bool zeroForOneDecoded,
TokenTransfer.TransferType transferTypeDecoded,
address receiver,
UniswapV4Executor.UniswapV4Pool[] memory decodedPools
) = uniswapV4Exposed.decodeData(data);
@@ -83,6 +85,7 @@ contract UniswapV4ExecutorTest is Test, Constants {
assertEq(tokenOut, USDT_ADDR);
assertEq(zeroForOneDecoded, zeroForOne);
assertEq(uint8(transferTypeDecoded), uint8(transferType));
assertEq(receiver, ALICE);
assertEq(decodedPools.length, 2);
assertEq(decodedPools[0].intermediaryToken, USDT_ADDR);
assertEq(decodedPools[0].fee, pool1Fee);
@@ -108,7 +111,7 @@ contract UniswapV4ExecutorTest is Test, Constants {
});
bytes memory data = UniswapV4Utils.encodeExactInput(
USDE_ADDR, USDT_ADDR, true, TokenTransfer.TransferType.NONE, pools
USDE_ADDR, USDT_ADDR, true, TokenTransfer.TransferType.NONE, ALICE, pools
);
uint256 amountOut = uniswapV4Exposed.swap(amountIn, data);
@@ -117,7 +120,7 @@ contract UniswapV4ExecutorTest is Test, Constants {
USDE.balanceOf(address(uniswapV4Exposed)),
usdeBalanceBeforeSwapExecutor - amountIn
);
assertTrue(USDT.balanceOf(address(uniswapV4Exposed)) == amountOut);
assertTrue(USDT.balanceOf(ALICE) == amountOut);
}
function testSingleSwapIntegration() public {
@@ -134,10 +137,10 @@ contract UniswapV4ExecutorTest is Test, Constants {
uint256 amountOut = uniswapV4Exposed.swap(amountIn, protocolData);
assertEq(USDE.balanceOf(poolManager), usdeBalanceBeforePool + amountIn);
assertEq(
USDE.balanceOf(address(uniswapV4Exposed)),
USDE.balanceOf(ALICE),
usdeBalanceBeforeSwapExecutor - amountIn
);
assertTrue(USDT.balanceOf(address(uniswapV4Exposed)) == amountOut);
assertTrue(USDT.balanceOf(ALICE) == amountOut);
}
function testMultipleSwap() public {
@@ -162,7 +165,7 @@ contract UniswapV4ExecutorTest is Test, Constants {
});
bytes memory data = UniswapV4Utils.encodeExactInput(
USDE_ADDR, WBTC_ADDR, true, TokenTransfer.TransferType.NONE, pools
USDE_ADDR, WBTC_ADDR, true, TokenTransfer.TransferType.NONE, ALICE, pools
);
uint256 amountOut = uniswapV4Exposed.swap(amountIn, data);
@@ -172,7 +175,7 @@ contract UniswapV4ExecutorTest is Test, Constants {
usdeBalanceBeforeSwapExecutor - amountIn
);
assertTrue(
IERC20(WBTC_ADDR).balanceOf(address(uniswapV4Exposed)) == amountOut
IERC20(WBTC_ADDR).balanceOf(ALICE) == amountOut
);
}
@@ -196,7 +199,7 @@ contract UniswapV4ExecutorTest is Test, Constants {
usdeBalanceBeforeSwapExecutor - amountIn
);
assertTrue(
IERC20(WBTC_ADDR).balanceOf(address(uniswapV4Exposed)) == amountOut
IERC20(WBTC_ADDR).balanceOf(ALICE) == amountOut
);
}
}

View File

@@ -9,6 +9,7 @@ library UniswapV4Utils {
address tokenOut,
bool zeroForOne,
UniswapV4Executor.TransferType transferType,
address receiver,
UniswapV4Executor.UniswapV4Pool[] memory pools
) public pure returns (bytes memory) {
bytes memory encodedPools;
@@ -23,7 +24,7 @@ library UniswapV4Utils {
}
return abi.encodePacked(
tokenIn, tokenOut, zeroForOne, transferType, encodedPools
tokenIn, tokenOut, zeroForOne, transferType, receiver, encodedPools
);
}
}