Solidity – How to Debug ‘Swap Exact Tokens For Tokens: False Transaction Mined but Execution Failed’ in Remix and Foundry

contract-debuggingfoundryremixsolidity

I'm using Remix with a Foundry Provider.

I'm trying to create a smart contract to swap between 2 or several tokens.
I have no issue when I compile and deploy the contract, but when I call the swap function, I have this message :

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
execution reverted

And when I send the transaction execution, I have this status :

false Transaction mined but execution failed

Do you any ideas why ? I tried to increase the gas limit, but it's not working.

This is my code :

pragma solidity >=0.8.7;

interface IERC20 {
    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);

    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);
    function totalSupply() external view returns (uint);
    function balanceOf(address owner) external view returns (uint);
    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint value) external returns (bool);
    function transfer(address to, uint value) external returns (bool);
    function transferFrom(address from, address to, uint value) external returns (bool);
    function deposit() external payable;
}

interface IQuickSwapRouter
{
    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
        ) external returns (uint[] memory amounts);
}

contract swapToken {

    function swap(address contractRouter, address tokenAddr, address[] memory path, uint256 nbtoken, address to) public {
        

        IERC20(tokenAddr).transferFrom(msg.sender, address(this), nbtoken);

        IERC20(tokenAddr).approve(contractRouter, nbtoken);

        IQuickSwapRouter(contractRouter).swapExactTokensForTokens(
        nbtoken, 
        0, 
        path, 
        to, 
        block.timestamp);

    }
}

Best Answer

I find the solution by commenting the line : IERC20(tokenAddr).transferFrom(msg.sender, address(this), nbtoken);.

It's working well:)

Related Topic