docs icon indicating copy to clipboard operation
docs copied to clipboard

Add warning about non-zero sqrtPriceLimitX96

Open one-hundred-proof opened this issue 1 year ago • 2 comments

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 this doc, will be extremely useful to developers wishing to integrate with Uniswap V3.

e.g.

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 avatar Mar 04 '24 00:03 one-hundred-proof