Rakesh Pradhan
Rakesh Pradhan

Reputation: 11

How to swap Erc20 to Eth with uniswap v3 router?

Uniswap v3 doesn't have a function to perform ERC20 to ETH swap.

I am currently working on a project involving token swapping. We initially used Uniswap V2, but as Uniswap V3 is better, we decided to migrate to it. However, we have been unable to find a function in Uniswap V3 that facilitates ERC20 to ETH swaps, similar to the **swapExactTokensForETHSupportingFeeOnTransferTokens **function in V2.

How can I perform ERC20 to ETH swaps in Uniswap V3?

Upvotes: 0

Views: 2273

Answers (3)

Mikhail Babushkin
Mikhail Babushkin

Reputation: 39

On 1Inch you can use placeholder 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE as ETH token address. Probably, the same for the Uniswap. Answered here

Upvotes: 0

user22490254
user22490254

Reputation: 1

To swap ERC20 for ERC20 or ETH you have to transfer money from the contract of your sell token and approve for selling them to uniswap router contract.

Upvotes: 0

Cédric Ngakam
Cédric Ngakam

Reputation: 21

Yes you can swap ETH for ERC20 tokens with uniswap V3 (router contract) The functions for a swap on the router contract are all payable, it means that from a smart contract that wants to perform a swap, you will need to act like u want to swap WETH (put the WETH address on the tokenIn input) and send the ETH amountIn to the router contract

example:

contract Swap {

receive() external payable {}

address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
ISwapRouter public constant swapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);

function swapExactInputSingleETH(
    address tokenOut,
    uint256 amountOutMinimum
) external payable returns (uint256 amountOut) {
    address tokenIn = WETH;
    uint256 amountIn = msg.value;

    ISwapRouter.ExactInputSingleParams memory params =
                        ISwapRouter.ExactInputSingleParams({
            tokenIn: tokenIn,
            tokenOut: tokenOut,
            fee: 3000,
            recipient: msg.sender,
            deadline: block.timestamp,
            amountIn: amountIn,
            amountOutMinimum: amountOutMinimum,
            sqrtPriceLimitX96: 0
        });

    // The call to `exactInputSingle` executes the swap.
    amountOut = swapRouter.exactInputSingle{value: amountIn}(params);
    }
}

It defo works for ETH as TokenIn but I don't know how to sxap erc20 tokens for ETH.

If you use exactOutput with that method, you wont receive the extra ETH back so be aware

Upvotes: 2

Related Questions