0

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);

    }
}
3
  • There are a lot of possible reasons. Doesn't remix show you a revert reason/string?
    – 0xSanson
    Aug 13, 2022 at 17:59
  • No, except those two messages, I have nothing else.
    – dinamo
    Aug 14, 2022 at 8:18
  • Can you share a failed transaction hash? Do the tokens have a fee on transfer?
    – Ismael
    Aug 14, 2022 at 15:36

1 Answer 1

0

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

It's working well:)

1
  • Are you sure that it will work? By removing that line you are requiring the contract has enough of the input tokens.
    – Ismael
    Aug 16, 2022 at 1:33

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.