Tbh, it was hard to explain the reason on Title, btw I have a presale test contract that let user to buy tokens from and once hardcap reached stop sell more tokens

here is contract

contract TESTPresale is ReentrancyGuard {
    using SafeMath for uint256;
    using SafeBEP20 for IBEP20;
    // Maps user to the number of tokens owned
    mapping(address => uint256) public tokensOwned;
    // The number of unclaimed tokens the user has
    mapping(address => uint256) public tokensUnclaimed;

    // TEST token
    IBEP20 public TEST;
    // USDC token
    IBEP20 public USDC;

    // Sale active
    bool isSaleActive;
    // Claim active
    bool isClaimActive;
    // Total TEST sold
    uint256 totalTokensSold = 0;
    // Price of presale TEST, 1 USDC
    uint256 public testPerUsdc = 1e18/uint256(950000);
    // Amount of USDC received in presale
    uint256 usdcReceived = 0;

    uint256 HardCap = 85000 * 10 ** 18; // 85,000

    event TokenBuy(address user, uint256 tokens);
    event TokenClaim(address user, uint256 tokens);

        address _TEST,
        address _USDC
    ) public {
        TEST = IBEP20(_TEST);
        USDC = IBEP20(_USDC);

    function setTestPerUsdc(uint256 _testPerUsdc) public onlyOwner{
        testPerUsdc = _testPerUsdc;

    function buy(uint256 _amount) public nonReentrant {
        require(isSaleActive, "Presale has not started");
        address _buyer = msg.sender;
        uint256 tokens = _amount.mul(testPerUsdc);
        require ( totalTokensSold + tokens <= HardCap, "presale hardcap reached");

        USDC.safeTransferFrom(msg.sender, address(this), _amount);

        tokensOwned[_buyer] = tokensOwned[_buyer].add(tokens);
        tokensUnclaimed[_buyer] = tokensUnclaimed[_buyer].add(tokens);
        totalTokensSold = totalTokensSold.add(tokens);

        usdcReceived = usdcReceived.add(_amount);
        emit TokenBuy(msg.sender, tokens);

    function getTotalTokensSold() public view returns (uint256) {
        return totalTokensSold;

    function getTESTTokensLeft() external view returns (uint256) {
        return TEST.balanceOf(address(this));


in Constructor I have TEST Token and USDC, I'm using a test tokens for these both inputs

but whenever I send for example 50k of tokens to presale contract and then try to test buy function with Atleast 1000000000000000000 WEI = 1 TEST token I get this error

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error.
"code": 3,
"message": "execution reverted: presale hardcap reached",
"data": "0x0.......}

but when I try to buy something like 30000000000 WEI = 0.00000003 it works perfectly.

can someone help where is the problem please?

2 Answers 2


In your code you have following require condition:

require ( totalTokensSold + tokens <= HardCap, "presale hardcap reached");

As we can see in your code

totalTokensSold = 0
tokens = _amount.mul(testPerUsdc)-->
tokens = 50k * 1= 50000*10**18 * 1e18/uint256(950000)--> 
(to make easier change 95000 to 100000) -->
tokens = 50k * 1= 50000*10**18 * 1e13 
and this is greater than your HardcCap of 85*10**1e18

The error should appear roughly around 2e13, in your first case the value is above that's why it fails, in the second is below and not fails!

Hope it helps!

  • But in my case if i send only 100 tokens to presale contract for sale and buy back from that amount i get same error
    – BlackRain
    Feb 25, 2023 at 8:29
  • 100 token equal 100*10**18 which is greater than 2e13
    – donoso.eth
    Feb 25, 2023 at 10:34

You have to define testPerUsdc in per wei USDC basis.


testPerUsdc = No of token in 1 wei usdc * 10**18/10**decimalUSDC
            = No of token in 1 wei usdc * 10**18 / 10**9
            = No of otken in 1 wei usdc * 10**9

if No of token in 1 usdc = 1 then in 1 wei USDC with decimals 9 would be = 1 * 10**9 wei of your token

  • Yeah,my bad thank you
    – BlackRain
    Mar 4, 2023 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.