fix: _pay and msgSender

This commit is contained in:
royvardhan
2025-02-13 00:20:14 +05:30
parent 0c40e9e979
commit d79068282a
2 changed files with 17 additions and 39 deletions

View File

@@ -31,20 +31,15 @@ contract UniswapV4Executor is IExecutor, V4Router {
payable payable
returns (uint256 calculatedAmount) returns (uint256 calculatedAmount)
{ {
( (address tokenIn, address tokenOut, bool isExactInput, uint256 amount) =
address tokenIn, _decodeData(data);
address tokenOut,
address receiver,
bool isExactInput,
uint256 amount
) = _decodeData(data);
uint256 tokenOutBalanceBefore; uint256 tokenOutBalanceBefore;
uint256 tokenInBalanceBefore; uint256 tokenInBalanceBefore;
tokenOutBalanceBefore = tokenOut == address(0) tokenOutBalanceBefore = tokenOut == address(0)
? receiver.balance ? address(this).balance
: IERC20(tokenOut).balanceOf(receiver); : IERC20(tokenOut).balanceOf(address(this));
tokenInBalanceBefore = tokenIn == address(0) tokenInBalanceBefore = tokenIn == address(0)
? address(this).balance ? address(this).balance
@@ -56,8 +51,8 @@ contract UniswapV4Executor is IExecutor, V4Router {
uint256 tokenInBalanceAfter; uint256 tokenInBalanceAfter;
tokenOutBalanceAfter = tokenOut == address(0) tokenOutBalanceAfter = tokenOut == address(0)
? receiver.balance ? address(this).balance
: IERC20(tokenOut).balanceOf(receiver); : IERC20(tokenOut).balanceOf(address(this));
tokenInBalanceAfter = tokenIn == address(0) tokenInBalanceAfter = tokenIn == address(0)
? address(this).balance ? address(this).balance
@@ -78,7 +73,6 @@ contract UniswapV4Executor is IExecutor, V4Router {
returns ( returns (
address tokenIn, address tokenIn,
address tokenOut, address tokenOut,
address receiver,
bool isExactInput, bool isExactInput,
uint256 amount uint256 amount
) )
@@ -89,9 +83,6 @@ contract UniswapV4Executor is IExecutor, V4Router {
// First byte of actions determines the swap type // First byte of actions determines the swap type
uint8 action = uint8(bytes1(actions[0])); uint8 action = uint8(bytes1(actions[0]));
// Get receiver from params[2] for all cases
(, receiver,) = abi.decode(params[2], (Currency, address, uint256));
if (action == uint8(Actions.SWAP_EXACT_IN_SINGLE)) { if (action == uint8(Actions.SWAP_EXACT_IN_SINGLE)) {
IV4Router.ExactInputSingleParams memory swapParams = IV4Router.ExactInputSingleParams memory swapParams =
abi.decode(params[0], (IV4Router.ExactInputSingleParams)); abi.decode(params[0], (IV4Router.ExactInputSingleParams));
@@ -142,10 +133,12 @@ contract UniswapV4Executor is IExecutor, V4Router {
internal internal
override override
{ {
token.transfer(payer, amount); IERC20(Currency.unwrap(token)).safeTransfer(
address(poolManager), amount
);
} }
function msgSender() public view override returns (address) { function msgSender() public view override returns (address) {
return msg.sender; return address(this);
} }
} }

View File

@@ -15,7 +15,6 @@ contract UniswapV4ExecutorExposed is UniswapV4Executor {
returns ( returns (
address tokenIn, address tokenIn,
address tokenOut, address tokenOut,
address receiver,
bool isExactInput, bool isExactInput,
uint256 amount uint256 amount
) )
@@ -41,30 +40,17 @@ contract UniswapV4ExecutorTest is Test, Constants {
function testDecodeParams() public view { function testDecodeParams() public view {
uint24 expectedPoolFee = 500; uint24 expectedPoolFee = 500;
address expectedReceiver = address(2);
uint128 expectedAmount = 100; uint128 expectedAmount = 100;
bytes memory data = _encodeExactInputSingle( bytes memory data = _encodeExactInputSingle(
USDE_ADDR, USDE_ADDR, USDT_ADDR, expectedPoolFee, false, 1, expectedAmount
USDT_ADDR,
expectedPoolFee,
expectedReceiver,
false,
1,
expectedAmount
); );
( (address tokenIn, address tokenOut, bool isExactInput, uint256 amount) =
address tokenIn, uniswapV4Exposed.decodeData(data);
address tokenOut,
address receiver,
bool isExactInput,
uint256 amount
) = uniswapV4Exposed.decodeData(data);
assertEq(tokenIn, USDE_ADDR); assertEq(tokenIn, USDE_ADDR);
assertEq(tokenOut, USDT_ADDR); assertEq(tokenOut, USDT_ADDR);
assertEq(receiver, expectedReceiver);
assertTrue(isExactInput); assertTrue(isExactInput);
assertEq(amount, expectedAmount); assertEq(amount, expectedAmount);
} }
@@ -77,7 +63,7 @@ contract UniswapV4ExecutorTest is Test, Constants {
USDE.balanceOf(address(uniswapV4Exposed)); USDE.balanceOf(address(uniswapV4Exposed));
bytes memory data = _encodeExactInputSingle( bytes memory data = _encodeExactInputSingle(
USDE_ADDR, USDT_ADDR, 100, BOB, true, 1, uint128(amountIn) USDE_ADDR, USDT_ADDR, 100, true, 1, uint128(amountIn)
); );
uint256 amountOut = uniswapV4Exposed.swap(amountIn, data); uint256 amountOut = uniswapV4Exposed.swap(amountIn, data);
@@ -86,14 +72,13 @@ contract UniswapV4ExecutorTest is Test, Constants {
USDE.balanceOf(address(uniswapV4Exposed)), USDE.balanceOf(address(uniswapV4Exposed)),
usdeBalanceBeforeSwapExecutor - amountIn usdeBalanceBeforeSwapExecutor - amountIn
); );
assertTrue(USDT.balanceOf(BOB) == amountOut); assertTrue(USDT.balanceOf(address(uniswapV4Exposed)) == amountOut);
} }
function _encodeExactInputSingle( function _encodeExactInputSingle(
address tokenIn, address tokenIn,
address tokenOut, address tokenOut,
uint24 fee, uint24 fee,
address receiver,
bool zeroForOne, bool zeroForOne,
uint24 tickSpacing, uint24 tickSpacing,
uint128 amountIn uint128 amountIn
@@ -109,7 +94,7 @@ contract UniswapV4ExecutorTest is Test, Constants {
bytes memory actions = abi.encodePacked( bytes memory actions = abi.encodePacked(
uint8(Actions.SWAP_EXACT_IN_SINGLE), uint8(Actions.SWAP_EXACT_IN_SINGLE),
uint8(Actions.SETTLE_ALL), uint8(Actions.SETTLE_ALL),
uint8(Actions.TAKE) uint8(Actions.TAKE_ALL)
); );
bytes[] memory params = new bytes[](3); bytes[] memory params = new bytes[](3);
@@ -125,7 +110,7 @@ contract UniswapV4ExecutorTest is Test, Constants {
); );
params[1] = abi.encode(key.currency0, amountIn); params[1] = abi.encode(key.currency0, amountIn);
params[2] = abi.encode(key.currency1, receiver, 0); params[2] = abi.encode(key.currency1, 0);
return abi.encode(actions, params); return abi.encode(actions, params);
} }