Summary
Including proper error handling ensures that unexpected issues during the execution of the exactInputSingle
call are appropriately handled.
Vulnerability Details
function swap(uint256 amountIn_, uint256 amountOutMinimum_) external onlyOwner returns (uint256) {
SwapParams memory params_ = params;
ISwapRouter.ExactInputSingleParams memory swapParams_ = ISwapRouter.ExactInputSingleParams({
tokenIn: params_.tokenIn,
tokenOut: params_.tokenOut,
fee: params_.fee,
recipient: address(this),
deadline: block.timestamp,
amountIn: amountIn_,
amountOutMinimum: amountOutMinimum_,
sqrtPriceLimitX96: params_.sqrtPriceLimitX96
});
uint256 amountOut_ = ISwapRouter(router).exactInputSingle(swapParams_);
emit TokensSwapped(params_.tokenIn, params_.tokenOut, amountIn_, amountOut_, amountOutMinimum_);
return amountOut_;
}
Impact
Discrepancy in the swap
function
Tools Used
Manual Review
Recommendations
Here's an example of how you might enhance error handling in the swap function:
function swap(uint256 amountIn_, uint256 amountOutMinimum_) external onlyOwner returns (uint256) {
SwapParams memory params_ = params;
ISwapRouter.ExactInputSingleParams memory swapParams_ = ISwapRouter.ExactInputSingleParams({
tokenIn: params_.tokenIn,
tokenOut: params_.tokenOut,
fee: params_.fee,
recipient: address(this),
deadline: block.timestamp,
amountIn: amountIn_,
amountOutMinimum: amountOutMinimum_,
sqrtPriceLimitX96: params_.sqrtPriceLimitX96
});
try ISwapRouter(router).exactInputSingle(swapParams_) returns (uint256 amountOut_) {
emit TokensSwapped(params_.tokenIn, params_.tokenOut, amountIn_, amountOut_, amountOutMinimum_);
return amountOut_;
} catch (bytes memory revertReason) {
revert("Swap failed");
}
}
In this example, the try block attempts to execute the exactInputSingle call, and if it succeeds, it emits an event and returns the amountOut_. If an exception occurs during the call (for example, if the Uniswap function reverts), the catch block is executed, and you can handle the error as needed. You can choose to log the error, revert with a custom error message, or take other appropriate actions based on your contract's logic.