Getting the Error when trying to use the smartcontract. We were not able to estimate gas. There might be an error in the contract and this transaction may fail.

Here the solidity code for this:

// SPDX-License-Identifier: MIT pragma solidity ^0.8.24;

interface IST20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns   (bool);
function decimals() external view returns (uint8);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}

library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a + b;
    require(c >= a, "SafeMath: addition overflow");
    return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    require(b <= a, "SafeMath: subtraction overflow");
    uint256 c = a - b;
    return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
        return 0;
    }
    uint256 c = a * b;
    require(c / a == b, "SafeMath: multiplication overflow");
    return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
    require(b > 0, "SafeMath: division by zero");
    uint256 c = a / b;
    return c;
}
}

contract Ownable {
address public owner;

event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

constructor() {
    owner = msg.sender;
}

modifier onlyOwner() {
    require(msg.sender == owner, "Only the owner can call this function");
    _;
}

function transferOwnership(address newOwner) public onlyOwner {
    require(newOwner != address(0), "Invalid address");
    emit OwnershipTransferred(owner, newOwner);
    owner = newOwner;
}
}

contract Sale_Contract is Ownable {
using SafeMath for uint256;
IST20 public token;
uint256 public rate;
uint256 public weiRaised;
uint256 public maxTokensAvailableForSale;
address payable private admin;
mapping(address => uint256) public purchasedBnb;
event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value,          uint256 amount);

constructor(uint256 _rate, IST20 _token, uint256 _maxTokens) {
    require(_rate > 0);
    require(address(_token) != address(0));
    rate = _rate;
    token = _token;
    admin = payable(msg.sender);
    maxTokensAvailableForSale = _maxTokens;
}

function updateMaxTokensAvailableForSale(uint256 _newMaxTokens) external onlyOwner {
    maxTokensAvailableForSale = _newMaxTokens;
}

fallback () external payable {
    buyTokens(msg.sender);
}

receive () external payable {
    buyTokens(msg.sender);
}

function buyTokens(address _beneficiary) public payable {
    require(msg.value > 0, "Value must be greater than 0");
    uint256 tokens = _getTokenAmount(msg.value);
    require(tokens > 0, "Token amount must be greater than 0");
    require(tokens <= maxBnb(_beneficiary), "Exceeds maximum tokens available for sale");
    
    weiRaised = weiRaised.add(msg.value);
    purchasedBnb[_beneficiary] = purchasedBnb[_beneficiary].add(msg.value);
    emit TokenPurchase(msg.sender, _beneficiary, msg.value, tokens);
    
    require(token.transfer(_beneficiary, tokens), "Token transfer failed");
    
    uint256 refundAmount = msg.value.sub(tokens.mul(1e18).div(rate));
    if (refundAmount > 0) {
        payable(msg.sender).transfer(refundAmount);
    }
}

function maxBnb(address _beneficiary) public view returns (uint256) {
    return maxTokensAvailableForSale.sub(purchasedBnb[_beneficiary]).div(rate);
}

function withdrawCoins() external {
    require(admin == msg.sender, "Caller is not the owner");
    admin.transfer(address(this).balance);
}

function withdrawTokens(address _beneficiary, uint256 _tokens) external onlyOwner {
    require(address(token) != address(0), "Token address not set");
    require(token.transfer(_beneficiary, _tokens), "Token transfer failed");
}

function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) {
    return _weiAmount.mul(rate).div(1e18);
}

function setPresaleRate(uint256 _rate) external onlyOwner {
    rate = _rate;
}
}

the token contract has spender address of this contract as well. token contract address - 0x72bb47956D22E8a71C95D8D2A189968d47bfF0A2 sales contract address - 0xaE46E84f6C7B2D65c837372022164963F5289193

Please help to resolve this Screenshot of the metamask`

In Token address i have added the sales contract and in spender field and added amount also. Not what causing the issue. I tried simplifying the code with Chatgpt and removed some functions still its not working

0

There are 0 answers