0

I am trying to write a function in my contract which swap ERC20 token to ETH. Tokens or ETH are in my contract, not my wallet. I am the one who triggers the swap action for my contract. Here is my function in my contract. It is swapping ERC20 to WETH, not to ETH. How should I design this functionality? Thanks..

    function uniswapErc20ToEth(
        address _tokenIn,
        address _tokenOut,
        uint256 _amountIn
    ) external {
        callApprove(_tokenIn, UNISWAP_ROUTER, _amountIn);

        uint24 poolFee = 3000;
        // 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: _tokenIn, //USDT_ADDR
                tokenOut: _tokenOut, //WETH_ADDR
                fee: poolFee,
                recipient: address(this),
                deadline: block.timestamp,
                amountIn: _amountIn,
                amountOutMinimum: 0,
                sqrtPriceLimitX96: 0
            });

        // The call to `exactInputSingle` executes the swap.
        UNISWAP_ROUTER_CONTRACT.exactInputSingle{value: 0}(params);
        // UNISWAP_ROUTER_CONTRACT.refundETH();

        // refund leftover ETH to user
        // (bool success, ) = msg.sender.call{value: address(this).balance}("");
        // require(success, "refund failed");
    }

Don't consider the comment lines please.

2
  • Well, if this works to swap an ERC20 to WETH, why not just keep that and unwrap the ETH once you're done?
    – Foxxxey
    Jan 18, 2023 at 14:54
  • Because I wonder if it is possible to take ETH directly by adjusting parameters of this function and the solution of it. Jan 19, 2023 at 6:38

1 Answer 1

1

My two cents:

Always a best price is to NOT keep ETH on the contract, you should use WETH for it, and then unwrap calling withdrawn() from wrapped eth contract.

At this point i would do something like this:

    function uniswapErc20ToEth(
        address _tokenIn,
        address _tokenOut,
        uint256 _amountIn
    ) external {
        callApprove(_tokenIn, UNISWAP_ROUTER, _amountIn);

        uint24 poolFee = 3000;
        // 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: _tokenIn, //USDT_ADDR
                tokenOut: _tokenOut, //WETH_ADDR
                fee: poolFee,
                recipient: address(this),
                deadline: block.timestamp,
                amountIn: _amountIn,
                amountOutMinimum: 0,
                sqrtPriceLimitX96: 0
            });

        // The call to `exactInputSingle` executes the swap.
        UNISWAP_ROUTER_CONTRACT.exactInputSingle{value: amountIn}(params);
        // UNISWAP_ROUTER_CONTRACT.refundETH();

        // refund leftover ETH to user
        // (bool success, ) = msg.sender.call{value: address(this).balance}("");
        // require(success, "refund failed");
    }

Not that you code doesn't have a value (it should):

UNISWAP_ROUTER_CONTRACT.exactInputSingle{value: 0}(params);

2
  • This is not for Erc20 to ETH. This is working for swapping ETH to ERC20. And I already have this function named uniswapEthToErc20(...). This function sends ETH existing on contract to swap to any token (_tokenOut). I need ERC20 -> ETH directly, in one transaction. Is there any way in uniswap? Jan 19, 2023 at 6:46
  • I think you need to swap to weth and than unwrap it immediately after.
    – Chev_603
    Feb 24, 2023 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.