0

I get the error of transacting and gas estimation errored

  // SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import './token.sol';
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract Vendor {
    address payable public admin;
    address payable private ethFunds = payable(0xF0a0c00Ac9D04d2e0eEF281607836b4AEa130eFa);
    Token public token;
    uint256 public tokensSold;
    int public tokenPriceUSD;
    AggregatorV3Interface internal priceFeed;

    uint256 public transactionCount;

    event Sell(address _buyer, uint256 _amount);

    struct Transaction {
        address buyer;
        uint256 amount;
    }

    mapping(uint256 => Transaction) public transaction;

    constructor(Token _token) {
        priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
        tokenPriceUSD = 50;
        token = _token;
        admin = payable(msg.sender);
    }

    function getETHPrice() public view returns(int) {
        (, int price, , , ) = priceFeed.latestRoundData();
        return (price / 10**8);
    }

    function facuTokenPriceInETH() public view returns(int) {
        int ethPrice = getETHPrice();
        return tokenPriceUSD / ethPrice;
    }

    function buyToken(uint256 _amount) public payable {
        //int facuTokenPriceETH = facuTokenPriceInETH();
        // Check that the buyer sends the enough ETH
        require(int(msg.value) >= tokenPriceUSD * int(_amount));
        // Check that the sale contract provides the enough ETH to make this transaction.
        require(token.balanceOf(address(this)) >= _amount);
        // Make the transaction inside of the require
        // transfer returns a boolean value.
        require(token.transfer(msg.sender, _amount));
        // Transfer the ETH of the buyer to us
        ethFunds.transfer(msg.value);
        // Increase the amount of tokens sold
        tokensSold += _amount;
        // Increase the amount of transactions
        transaction[transactionCount] = Transaction(msg.sender, _amount);
        transactionCount++;
        // Emit the Sell event
        emit Sell(msg.sender, _amount);
    }

    function endSale() public {
        require(msg.sender == admin);
        // Return the tokens that were left inside of the sale contract
        uint256 amount = token.balanceOf(address(this));
        require(token.transfer(admin, amount));
        selfdestruct(payable(admin));
    }

}
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import '@openzeppelin/contracts/token/ERC20/ERC20.sol';

contract Token is ERC20 {
    constructor() ERC20("CursedToken", "CT") {
        _mint(msg.sender, 1000000000 * 10**18);
    }
}

enter image description here

enter image description here

When I try to call the function buytokens it creates and transaction but it fails

4
  • Can you share all your smart contract code? Commented Mar 21, 2022 at 16:28
  • Updated the question
    – Megaluk
    Commented Mar 21, 2022 at 16:29
  • Please include also 'token.sol' code. Thanks Commented Mar 21, 2022 at 16:30
  • Also included token.sol
    – Megaluk
    Commented Mar 21, 2022 at 16:33

1 Answer 1

1

You received that error because the smart contract haven't token. I tried your smart contract and seen that if you don't send an amount of your tokens (CursedToken) to the smart contract's address you cannot call the buy function. This line of code give you reverted error:

require(token.balanceOf(address(this)) >= _amount);

Before you call the buy() function try to send an amount of your tokens (CursedToken) to smart contract's address and then try to call the buy() function or other function of smart contract. To send an amount of your token to smart contract you can use Send function in metamask.

NOTE: Before send the token you must to import your your token with Import Tokens function in metamask.

2
  • I get the error here require(int(msg.value) >= tokenPriceUSD * int(_amount))
    – Megaluk
    Commented Mar 21, 2022 at 17:22
  • If you're having error there, I think because you don't put the value into textbox greater than the multiplication operation. Commented Mar 22, 2022 at 22:27

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.