I was creating a crowdsale smart contract that should interact with the Main token (deployed), where 0.01 ETH will give back 1000 tokens to the sender address.
The problem now is that is showing fail each time I send 0.01 eth the Crowsale(deployed) smart contract
Here's my Crowdsale Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IMainToken {
function mint(address account, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}
contract ICO {
address public owner;
IMainToken public mainToken;
uint256 public totalSaleLimit = 4000000 * 10**18; // total sale limit of 4,000,000 tokens
uint256 public exchangeRate = 1000; // exchange rate of 1 ETH to 1,000 tokens
uint256 public totalSale = 0; // initial total sale amount is 0
constructor(IMainToken _mainToken) {
owner = msg.sender;
mainToken = _mainToken;
}
function buy() public payable {
uint256 ethAmount = msg.value;
uint256 tokenAmount = ethAmount * exchangeRate;
require(tokenAmount >= 10 * 10**18, "Minimum buy is 0.01 ETH worth of tokens");
require(tokenAmount <= mainToken.balanceOf(address(this)), "Insufficient tokens available for sale");
require(totalSale + tokenAmount <= totalSaleLimit, "Exceeded total sale limit");
require(mainToken.mint(msg.sender, tokenAmount), "Failed to mint tokens");
totalSale += tokenAmount;
}
function withdraw() public {
require(msg.sender == owner, "Only owner can withdraw ETH");
payable(owner).transfer(address(this).balance);
}
}