ERC20 transferFrom fails "ERC20: transfer amount exceeds balance"

I've minted a token and given one of my dummy accounts all of the balance. It shows in Metamask as received.

I then created a contract that I want to work as a delegate contract to use TransferFrom - called Market Exchange for example.

I approve the Market Exchange address and that goes through.

When I try to call TransferFrom - from the Owner contract I get the error that I don't have enough tokens to transfer.

I've taken inspiration from this solution by abcoathup

and still can't get Remix to work. Where am I being Dumb here :smiley: thank you.

:1234: Code to reproduce

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";

contract Token is ERC20 {
    address public admin;
    
    constructor() ERC20('Cally Token', 'POP') {
        admin = 0x492980696aBD3129a22770C1f14FFB3E2E7299bf;
        _mint(admin, 10000 * 10 ** 18);
    }
}
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import './Token.sol';

contract MarketEx {
    address public tokenAddress = 0x552b4b432BcEA755dE362031c1f2D781c057C75E;

    function delegateTransfer(address recipient, uint256 amount) external {
        Token token = Token(tokenAddress);
        token.transferFrom(msg.sender, recipient, amount);
    }
}
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import './Token.sol';
import './MarketEx.sol';

contract Owner {
    function approveOtherContract(ERC20 token, address marketExchange, uint256 amount) external {
        token.approve(marketExchange, amount);
    }
    
    function sendTokens(MarketEx marketExchange, address recipient, uint256 amount) external {
        marketExchange.delegateTransfer(recipient, amount);
    }
}

:computer: Environment

Remix.

Gas estimation errored with the following message (see below).
The transaction execution will likely fail. Do you want to force sending?
execution reverted: ERC20: transfer amount exceeds balance { "originalError": { "code": 3, "data": "0x08c3.....", "message": "execution reverted: ERC20: transfer amount exceeds balance" } }

Hi Cally,

You are trying to transfer tokens from the Owner contract address, however you mint the tokens to a different account. The error is because the Owner contract has a 0 token balance.

Replace the admin address on the Token contract with the address of the Owner contract instead of your dummy account address.

Hope that helps!

1 Like

I suggest to save the contracts of the addresses that you need to set in a variable and then set it initially in the constructor of the Contract. Then set the owner of the contract in the constructor with owner = msg.sender.
In remix is not easy to use the functions if you set the addresses in the way you did. Try to do like I said so that you can use the addresses that remix gives you

1 Like

Thank you @Allennick @thelasthash

Literally, 20 minutes after writing this, I thought that the Owner contract could be the owner.

After figuring that out and adding an allowance to the 3rd party contract I was able to transfer tokens using TransferFrom().

Many thanks.