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