ERC20: transfer amount exceeds balance

Hi everyone !

I am trying to create 2 smart contracts : one for an ERC20 and the other for an ERC721. What I want to do now is to buy a token of ERC721 by sending ERC20 amount to the seller balance.

The problem is really simple : When I call the transfer() fonction from the ERC721 contract with token.transfer(), I have the error “ERC20: transfer amount exceeds balance”.

When I looked on the forum, people had the same issue, and the solution was to use transferFrom() instead of transfer().
But the error I got was “ERC20: transfer amount exceeds allowance”.
I tried giving allowance to the contract address (because I think the caller is the contract) and I still got this error.

I am using ERC20 and ERC721 OpenZeppelin contracts on Remix, with version 0.8.0 of Solidity.

contract TokenTransfer is ERC721 {
    function transfer() external {
        Token token = Token(0xd9145CCE52D386f254917e481eB44e9943F39138);
        token.transfer(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2, 150);

0xd9145CCE52D386f254917e481eB44e9943F39138 : address of the contract “Token”.
0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 : an address not owning RMN (not the caller).

contract Token is ERC20 {
    constructor(uint256 initialSupply) ERC20("RAMEN TOKEN", "RMN") {
        _mint(msg.sender, initialSupply);

I tried to only provide the relevant code for my issue.

Thanks for your help !

1 Like

Hi @Aymane,

Welcome to the community :wave:

If you use transfer then the contract needs to hold tokens.
If you use transferFrom then the token holder needs to have approved an allowance of tokens for the contract to transfer.

I suggest looking at: Example on how to use ERC20 token in another contract

Depending on your use case you may actually want three contracts, an ERC20, and ERC721 and a sale contract that accepts ERC20 and mints (or transfers) ERC721. I recommend only including functionality in a contract needed for the life of the contract, so sale functionality may not always be needed in the ERC721.

Thanks !
It finally worked :slight_smile:

1 Like

Hi @Aymane,

Glad it worked. When you get a chance you can Introduce yourself here!