Swap Token to Ether on Uni-V2 – Resolving ETH_TRANSFER_FAILED Error

solidityuniswap

I have two interfaces:

interface IUniswap {
    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable /*DELETE MAYBE*/ returns (uint[] memory amounts);
    function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts);   
}

interface IERC20 {
     function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
     function approve(address spender, uint256 amount) external returns (bool);
     function allowance(address owner, address spender) external view returns (uint256);
}

I would like to swap some tokens (that the contract already has) into ETH, using the following function:

function swapTokenForEth(address token, uint amountIn) external returns (bool) {
    

    address[] memory path = new address[](2);
    uint deadline = block.timestamp + 600;

    path[0] = token;
    path[1] = WETH;

    require(IERC20(token).approve(address(uniswap), (amountIn + 10000)), 'Uniswap approval failed');
    uniswap.swapExactTokensForETH(
        amountIn,
        1, //MIN FIXED TO 1 FOR TESTING
        path,
        address(this),
        deadline
    );

    return true;

}

The transaction reverts giving the following error: Fail with error 'TransferHelper: ETH_TRANSFER_FAILED'.

Thanks in advanced for the help.

Best Answer

The Router02 documentation says that

If the to address is a smart contract, it must have the ability to receive ETH.

This is exactly the case now, so you have to use the payable modifier on your function:

function swapTokenForEth(address token, uint amountIn) external payable returns (bool) {
    

    address[] memory path = new address[](2);
    uint deadline = block.timestamp + 600;

    path[0] = token;
    path[1] = WETH;

    require(IERC20(token).approve(address(uniswap), (amountIn + 10000)), 'Uniswap approval failed');
    uniswap.swapExactTokensForETH(
        amountIn,
        1, //MIN FIXED TO 1 FOR TESTING
        path,
        address(this),
        deadline
    );

    return true;

}

Related Topic