0

I am trying to swap some tokens for ETH in my smart contract. The tokens is already contained by the contract. Here is my code.

function approve(address token, uint amount) external {
        require(IERC20(token).approve(address(uniswap), amount), 'Smart contract approval failed');
    }

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

        //require(IERC20(token).transferFrom(msg.sender, address(this), amountIn), 'Transfer from wallet to contract failed');
        
        
        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;

    }

I first run the approve function and then the swapTokenForEth function, but the latter transaction reverts.

Thanks for the help

5
  • What input are you passing to each function? Jan 4 at 13:20
  • @goodvibration The same token and the same amount in Jan 4 at 13:21
  • 1
    Why do you have function approve inside the contract??? Calling it does not allow the contract to transfer tokens from the caller. Jan 4 at 13:30
  • @goodvibration If it's a smart contract that trades on Uniswap on behalf of you it might make sense Jan 4 at 13:37
  • @goodvibration exactly as I stated in the question, the contract has the tokens. There is no need for contract to transfer the tokens from the caller. Jan 4 at 14:21
-1

To see why a transaction reverts here is a service for you:

https://tenderly.co/

We cannot debug your code for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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