Execution reverted: ERC20: transfer amount exceeds balance - Goerli Testnet

I have my own token named "OK", but when I call
token.transfer(msg.sender,amount)

it returns with error:

Returned error: {"jsonrpc":"2.0","error":"execution reverted: ERC20: transfer amount exceeds balance","id":3350378333478486}

In my contract, I have a buyPlot function, which is as follows:

function buyPlot(uint256 coord) external payable isValidCoord(coord) {
    Plot storage plot = grid[coord];
    uint256 plotPrice = BASE_PRICE << plot.purchaseCount;

    require(msg.value >= plotPrice, "Not enough ETH sent to buy plot");

    if (msg.value > plotPrice) {
        payable(msg.sender).transfer(msg.value - plotPrice);
    }

    address currentOwner = plot.owner;

    // Ensure the sender is not the contract itself
    require(msg.sender != address(this), "Contract cannot buy plots");

    // protocol takes 25% of the purchase price
    // 75% goes to the previous owner
    // if there is no previous owner, 100% goes to the protocol
    // the protocol is the owner of all plots at the start
    if (currentOwner != address(0)) {
        uint256 transferAmount = plotPrice.mul(3) / 4;

        // Ensure the transfer to the current owner is successful
        require(payable(currentOwner).send(transferAmount), "Transfer to current owner failed");

        if (currentOwner != msg.sender) {
            plotsOwned[currentOwner] -= 1;
            plotsOwned[msg.sender] += 1;
            plot.owner = msg.sender;
        }
    } else {
        plot.owner = msg.sender;
        plotsOwned[msg.sender] += 1;
    }

    // Transfer the purchase price to the contract owner
    if (address(this).balance > 0) {

        require(payable(treasury).send(address(this).balance), "Transfer to treasury failed");
    }
    plot.purchaseCount += 2; // Double the purchase count instead of increasing by 1

    // mint tokens for the buyer
    // require( token.approve(msg.sender, mintPerPlot), "NOT approve");
    // require( token.transferFrom(msg.sender,itemOwner, mintPerPlot), "NOT transferFrom");
    // token.approve(msg.sender, mintPerPlot);
    token.transfer(msg.sender, mintPerPlot); // error exits here

    emit PlotPurchase(msg.sender, coord, plotPrice, plot.purchaseCount);
}

"OK" Token Contract
https://goerli.etherscan.io/token/0x7911211e2749fb37ad2FD9f3b2C3A37C8424F44A

1 Like

Your contract does not have a sufficient amount of tokens.

I other words, token.balanceOf(address(this)) < mintPerPlot when the line above is executed.

As a side-note, this line in your code is useless:

require(msg.sender != address(this), "Contract cannot buy plots");

Do you mean the current contract or the token contract, I have already minted in my token contract, if you can check, is there anything else I have to do?

Maybe, but you haven't transferred that to the contract which implements the buyPlot function.

1 Like

This line is meaning the token will be sent from the contract which has the buyPlot function to the msg.sender.
So you need to send the ok token to the contract which has the buyPlot function first.
I think this is your contract:

The token balance is 0. That's why you got the error.

1 Like

Yes, thanks for your time :smiley:

Thanks for your time sire :smiley: