fix: remove executeActions wrapper, strict equality checks and rename swap return

This commit is contained in:
royvardhan
2025-02-12 20:32:46 +05:30
parent b47cff3fc9
commit 2371ab2a1f
2 changed files with 40 additions and 66 deletions

View File

@@ -2,9 +2,14 @@
pragma solidity ^0.8.26; pragma solidity ^0.8.26;
import "@interfaces/IExecutor.sol"; import "@interfaces/IExecutor.sol";
import {IERC20, SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {
IERC20,
SafeERC20
} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol"; import {
Currency, CurrencyLibrary
} from "@uniswap/v4-core/src/types/Currency.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol"; import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {V4Router} from "@uniswap/v4-periphery/src/V4Router.sol"; import {V4Router} from "@uniswap/v4-periphery/src/V4Router.sol";
@@ -21,10 +26,11 @@ contract UniswapV4Executor is IExecutor, V4Router {
constructor(IPoolManager _poolManager) V4Router(_poolManager) {} constructor(IPoolManager _poolManager) V4Router(_poolManager) {}
function swap( function swap(uint256, bytes calldata data)
uint256, external
bytes calldata data payable
) external payable returns (uint256 amountInOrOut) { returns (uint256 calculatedAmount)
{
( (
address tokenIn, address tokenIn,
address tokenOut, address tokenOut,
@@ -36,31 +42,21 @@ contract UniswapV4Executor is IExecutor, V4Router {
uint256 tokenOutBefore = IERC20(tokenOut).balanceOf(receiver); uint256 tokenOutBefore = IERC20(tokenOut).balanceOf(receiver);
uint256 tokenInBefore = IERC20(tokenIn).balanceOf(address(this)); uint256 tokenInBefore = IERC20(tokenIn).balanceOf(address(this));
this.executeActions(data); _executeActions(data);
uint256 tokenOutAfter = IERC20(tokenOut).balanceOf(receiver); uint256 tokenOutAfter = IERC20(tokenOut).balanceOf(receiver);
uint256 tokenInAfter = IERC20(tokenIn).balanceOf(address(this)); uint256 tokenInAfter = IERC20(tokenIn).balanceOf(address(this));
if (isExactInput) { if (isExactInput) {
amountInOrOut = tokenOutAfter - tokenOutBefore; calculatedAmount = tokenOutAfter - tokenOutBefore;
} else { } else {
amountInOrOut = tokenInBefore - tokenInAfter; calculatedAmount = tokenInBefore - tokenInAfter;
} }
// slither-disable-next-line incorrect-equality return calculatedAmount;
if (amountInOrOut == 0) {
revert UniswapV4Executor__SwapFailed();
}
return amountInOrOut;
} }
function executeActions(bytes calldata actions) public { function _decodeData(bytes calldata data)
_executeActions(actions);
}
function _decodeData(
bytes calldata data
)
internal internal
pure pure
returns ( returns (
@@ -71,10 +67,8 @@ contract UniswapV4Executor is IExecutor, V4Router {
uint256 amount uint256 amount
) )
{ {
(bytes memory actions, bytes[] memory params) = abi.decode( (bytes memory actions, bytes[] memory params) =
data, abi.decode(data, (bytes, bytes[]));
(bytes, bytes[])
);
// 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]));
@@ -83,10 +77,8 @@ contract UniswapV4Executor is IExecutor, V4Router {
(, receiver,) = abi.decode(params[2], (Currency, address, uint256)); (, 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 = abi.decode( IV4Router.ExactInputSingleParams memory swapParams =
params[0], abi.decode(params[0], (IV4Router.ExactInputSingleParams));
(IV4Router.ExactInputSingleParams)
);
tokenIn = swapParams.zeroForOne tokenIn = swapParams.zeroForOne
? address(uint160(swapParams.poolKey.currency0.toId())) ? address(uint160(swapParams.poolKey.currency0.toId()))
@@ -97,10 +89,8 @@ contract UniswapV4Executor is IExecutor, V4Router {
isExactInput = true; isExactInput = true;
amount = swapParams.amountIn; amount = swapParams.amountIn;
} else if (action == uint8(Actions.SWAP_EXACT_OUT_SINGLE)) { } else if (action == uint8(Actions.SWAP_EXACT_OUT_SINGLE)) {
IV4Router.ExactOutputSingleParams memory swapParams = abi.decode( IV4Router.ExactOutputSingleParams memory swapParams =
params[0], abi.decode(params[0], (IV4Router.ExactOutputSingleParams));
(IV4Router.ExactOutputSingleParams)
);
tokenIn = swapParams.zeroForOne tokenIn = swapParams.zeroForOne
? address(uint160(swapParams.poolKey.currency0.toId())) ? address(uint160(swapParams.poolKey.currency0.toId()))
@@ -111,23 +101,18 @@ contract UniswapV4Executor is IExecutor, V4Router {
isExactInput = false; isExactInput = false;
amount = swapParams.amountOut; amount = swapParams.amountOut;
} else if (action == uint8(Actions.SWAP_EXACT_IN)) { } else if (action == uint8(Actions.SWAP_EXACT_IN)) {
IV4Router.ExactInputParams memory swapParams = abi.decode( IV4Router.ExactInputParams memory swapParams =
params[0], abi.decode(params[0], (IV4Router.ExactInputParams));
(IV4Router.ExactInputParams)
);
tokenIn = address(uint160(swapParams.currencyIn.toId())); tokenIn = address(uint160(swapParams.currencyIn.toId()));
PathKey memory lastPath = swapParams.path[ PathKey memory lastPath =
swapParams.path.length - 1 swapParams.path[swapParams.path.length - 1];
];
tokenOut = address(uint160(lastPath.intermediateCurrency.toId())); tokenOut = address(uint160(lastPath.intermediateCurrency.toId()));
isExactInput = true; isExactInput = true;
amount = swapParams.amountIn; amount = swapParams.amountIn;
} else if (action == uint8(Actions.SWAP_EXACT_OUT)) { } else if (action == uint8(Actions.SWAP_EXACT_OUT)) {
IV4Router.ExactOutputParams memory swapParams = abi.decode( IV4Router.ExactOutputParams memory swapParams =
params[0], abi.decode(params[0], (IV4Router.ExactOutputParams));
(IV4Router.ExactOutputParams)
);
PathKey memory firstPath = swapParams.path[0]; PathKey memory firstPath = swapParams.path[0];
tokenIn = address(uint160(firstPath.intermediateCurrency.toId())); tokenIn = address(uint160(firstPath.intermediateCurrency.toId()));
@@ -137,11 +122,10 @@ contract UniswapV4Executor is IExecutor, V4Router {
} }
} }
function _pay( function _pay(Currency token, address payer, uint256 amount)
Currency token, internal
address payer, override
uint256 amount {
) internal override {
token.transfer(payer, amount); token.transfer(payer, amount);
} }

View File

@@ -9,9 +9,7 @@ import {console} from "forge-std/console.sol";
contract UniswapV4ExecutorExposed is UniswapV4Executor { contract UniswapV4ExecutorExposed is UniswapV4Executor {
constructor(IPoolManager _poolManager) UniswapV4Executor(_poolManager) {} constructor(IPoolManager _poolManager) UniswapV4Executor(_poolManager) {}
function decodeData( function decodeData(bytes calldata data)
bytes calldata data
)
external external
pure pure
returns ( returns (
@@ -37,9 +35,8 @@ contract UniswapV4ExecutorTest is Test, Constants {
function setUp() public { function setUp() public {
uint256 forkBlock = 21817316; uint256 forkBlock = 21817316;
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock); vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
uniswapV4Exposed = new UniswapV4ExecutorExposed( uniswapV4Exposed =
IPoolManager(poolManager) new UniswapV4ExecutorExposed(IPoolManager(poolManager));
);
} }
function testDecodeParams() public view { function testDecodeParams() public view {
@@ -76,18 +73,11 @@ contract UniswapV4ExecutorTest is Test, Constants {
uint256 amountIn = 100 ether; uint256 amountIn = 100 ether;
deal(USDE_ADDR, address(uniswapV4Exposed), amountIn); deal(USDE_ADDR, address(uniswapV4Exposed), amountIn);
uint256 usdeBalanceBeforePool = USDE.balanceOf(poolManager); uint256 usdeBalanceBeforePool = USDE.balanceOf(poolManager);
uint256 usdeBalanceBeforeSwapExecutor = USDE.balanceOf( uint256 usdeBalanceBeforeSwapExecutor =
address(uniswapV4Exposed) USDE.balanceOf(address(uniswapV4Exposed));
);
bytes memory data = _encodeExactInputSingle( bytes memory data = _encodeExactInputSingle(
USDE_ADDR, USDE_ADDR, USDT_ADDR, 100, BOB, true, 1, uint128(amountIn)
USDT_ADDR,
100,
BOB,
true,
1,
uint128(amountIn)
); );
uint256 amountOut = uniswapV4Exposed.swap(amountIn, data); uint256 amountOut = uniswapV4Exposed.swap(amountIn, data);