Gas Estimation Errors on Transfer Token

So right now I’m working on two contracts, Contract A & B. Contract A is the one I’m actually using, Contract B is just to test the external transfer function, which I’ve been having issues with.

Contract A is simply supposed to take a token from an outside source and send it to another address (it’s basically a switcher).

Both contracts have functions that change the state variables so that I can switch which token is being used and where its ending up.

To simplify the end goal: EOA inputs an amount of a token and allows Contract A to send that amount to a set address.

I keep running up against gas estimation errors on remix. I’ve tried adjusting allowance, messing with the approve function. I’ve also checked the functions that modify the contract address, “set address” which is the final destination, etc.

Not sure if this is something super simple that I’m doing wrong with the transferFrom/allowance/aprove side of this. But I would appreciate any guidance!

pragma solidity ^0.7.6;

interface IERC20 {

    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

// Contract to enable and provide ownership features for the contract
contract Owned {
    address public owner;
    address public newOwner;

    event OwnershipTransferred(address indexed _from, address indexed _to);

    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    function transferOwnership(address _newOwner) public onlyOwner {
        newOwner = _newOwner;
    }
    function acceptOwnership() public {
        require(msg.sender == newOwner);
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
        newOwner = address(0);
    }
}

contract ContractA is Owned {

    uint256 public deposit;

    // Initializing tokenA, tokenB and exchangeToken(WEth) as ERC20 interfaces
    IERC20 public tokenA;
    //IERC20 public setAddress; //is this needed? May not be as its not a token
    
    // Storing contract addresses of tokenA, tokenB and exchangeToken
    address public addressOfTokenA;
    address public setAddress;


    event Log(string message);
    
    constructor (address _tokenA, address _setAddress) {

        // Setting owner, ERC20 contracts and storing the contract addresses
        owner = msg.sender;
        tokenA = IERC20(_tokenA);
        setAddress = _setAddress;
        
    }
    
    receive() external payable {}
    
    function transfer(uint amount) external payable {
        
        tokenA.allowance(msg.sender, address(this));
        //tokenA.transfer(SetAddress,token.balanceOf(address(this));
        tokenA.transferFrom(msg.sender, setAddress, tokenA.balanceOf(address(this)));
    }
    
    // Modify token being used as TokenA
    function modifyTokenA(address _newTokenA) public onlyOwner returns (bool) {
        require(addressOfTokenA != _newTokenA, "New token has the same contract address as the old one");
        tokenA = IERC20(_newTokenA);
        return true;
    }
    
    // Modify address tokens being sent
    function modifySetAddress(address _newSetAddress) public onlyOwner returns (bool) {
        require(setAddress != _newSetAddress, "New token has the same contract address as the old one");
        setAddress = _newSetAddress;
        return true;
    }
    
}

contract ContractB is Owned {
    
     uint256 public deposit;

    // Initializing tokenA, tokenB and exchangeToken(WEth) as ERC20 interfaces
    IERC20 public tokenA;
    //IERC20 public setAddress; //is this needed? May not be as its not a token
    
    // Storing contract addresses of tokenA, tokenB and exchangeToken
    address public addressOfTokenA;
    address public contractAddress;

    constructor (address _tokenA, address _contractAddress) {

        // Setting owner, ERC20 contracts and storing the contract addresses
        owner = msg.sender;
        tokenA = IERC20(_tokenA);
        contractAddress = _contractAddress;
        
    }
    
    function transfer(uint amount) public {
        //tokenA.transfer(SetAddress,token.balanceOf(address(this));
        tokenA.approve(contractAddress, amount);
        payable(contractAddress).transfer(amount);
    }
    
    // Modify token being used as TokenA
    function modifyTokenA(address _newTokenA) public onlyOwner returns (bool) {
        require(addressOfTokenA != _newTokenA, "New token has the same contract address as the old one");
        tokenA = IERC20(_newTokenA);
        return true;
    }
    
    // Modify address tokens being sent
    function modifyContractAddress(address _newContractAddress) public onlyOwner returns (bool) {
        require(contractAddress != _newContractAddress, "New token has the same contract address as the old one");
        contractAddress = _newContractAddress;
        return true;
    }
    
}

:computer: Environment

Right now I’m using remix for this with Metamask

Hi, welcome! :wave:

It seems like in the contractA, you are trying to transfer ERC20 token, so you are not expected to add keyword payable for the function. And it is useless to add this line: tokenA.allowance(msg.sender, address(this));