I have written a buy function. I am using Remix ide when I am trying greater than 100 Wei , it's failed. Showing transfer amount exceeds balance. I have 5 ether on my test net.
Here is the code :




uint256 public tokensPerEth = 100000;

address constant public deployerAddress = 0xB65f5Ac5eAD9598f7Ec61c8C89033B970dA7B77D;

event BuyTokens(address buyer, uint256 amountOfETH, uint256 amountOfTokens);

   

    function buyTokens() public payable returns (uint256 tokenAmount) {

    require(msg.value > 0, "Send ETH to buy some tokens");


    uint256 amountToBuy = msg.value * tokensPerEth;


    // check if the Vendor Contract has enough amount of tokens for the transaction

    uint256 vendorBalance = balanceOf(deployerAddress);

    require(vendorBalance >= amountToBuy, "Vendor contract has not enough tokens in its balance");


    // Transfer token to the msg.sender

    (bool sent) = transfer(msg.sender, amountToBuy);

    require(sent, "Failed to transfer token to user");


    // emit the event

    emit BuyTokens(msg.sender, msg.value, amountToBuy);


    return amountToBuy;

  }

Aug 30, 2022, 3:37 AM

© 2024 Draquery.com All rights reserved.