Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify impact of non-zero sqrtPriceLimitX96 on exactInputSingle #671

Open
one-hundred-proof opened this issue Mar 4, 2024 · 1 comment

Comments

@one-hundred-proof
Copy link

one-hundred-proof commented Mar 4, 2024

A common integration error is to pass in a non-zero sqrtPriceLimitX96 value and then not refund the user the unswapped tokens.

Highlighting this in the documentation, or even providing a more complex example in the documentation, will be extremely useful to developers wishing to integrate with Uniswap V3.

contract SomeContract {

    // ...

    function swapExactInputSingle(uint256 amountIn, uint160 sqrtPriceLimitX96) external returns (uint256 amountOut) {
        // msg.sender must approve this contract
        // Transfer the specified amount of DAI to this contract.
        TransferHelper.safeTransferFrom(DAI, msg.sender, address(this), amountIn);
        // Approve the router to spend DAI.
        TransferHelper.safeApprove(DAI, address(swapRouter), amountIn);
        // Naively set amountOutMinimum to 0. In production, use an oracle or other data source to choose a safer value for amountOutMinimum.
        // We also set the sqrtPriceLimitx96 to be 0 to ensure we swap our exact input amount.
        ISwapRouter.ExactInputSingleParams memory params =
            ISwapRouter.ExactInputSingleParams({
                tokenIn: DAI,
                tokenOut: WETH9,
                fee: poolFee,
                recipient: msg.sender,
                deadline: block.timestamp,
                amountIn: amountIn,
                amountOutMinimum: 0,
                sqrtPriceLimitX96: sqrtPriceLimitX96
            });
        // The call to `exactInputSingle` executes the swap.
        amountOut = swapRouter.exactInputSingle(params);
        
        // ***ERROR***: Less than amountIn token may have been swapped. They need to be refunded to the user! 
    }

    // ....

}
@one-hundred-proof
Copy link
Author

Here is a PR with suggested changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant