1

I built a token based on ERC20, when I try to buy tokens I get this error

In the truffle console

Quinn.deployed().then(function(ins){token=ins}) QuinnSale.deployed().then(function(ins){tokenSale = ins})

const acc = await web3.eth.getAccounts()

tokenSale.buyTokens(1,{from:acc[1],value:1000000000000000,gas:500000})

Note: token price is 0.001 eth, this acc[1] has enough eth, token sale address has allowance

here is the code of the contracts

quinn.sol

pragma solidity ^0.8.19;

contract Quinn {
    string public name = "Quinn";
    string public symbol = "QNN";
    string public standard = "QNN v1.0";

    uint256 public totalSupply;

    // transfer event
    event Transfer(address indexed _from, address indexed _to, uint256 _value);

    // approve event
    event Approval(
        address indexed _owner,
        address indexed _spender,
        uint256 _value
    );

    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;

    constructor(uint256 _initialSupply) {
        totalSupply = _initialSupply; // 1 million
        balanceOf[msg.sender] = _initialSupply;
    }

    //transfer
    //Exception if account does not have enough
    //return a boolen
    //transfer event
    function transfer(
        address _to,
        uint256 _value
    ) public returns (bool success) {
        require(balanceOf[msg.sender] >= _value);

        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;

        // trigger the transfer event
        emit Transfer(msg.sender, _to, _value);

        return true;
    }

    //delegated transfer

    // approve transfer
    function approve(
        address _spender,
        uint256 _value
    ) public returns (bool success) {
        // allowance
        allowance[msg.sender][_spender] = _value;
        // approve event
        emit Approval(msg.sender, _spender, _value);

        return true;
    }

    // transfer from
    function transferFrom(
        address _from,
        address _to,
        uint256 _value
    ) public returns (bool success) {
        // require _from has enough tokens
        require(_value <= balanceOf[_from]);

        // require  allowance is big enough
        require(_value <= allowance[_from][msg.sender]);

        // transfer event
        emit Transfer(_from, _to, _value);

        // change the balanceOf address
        balanceOf[_from] -= _value;

        // update the balanceOf address
        balanceOf[_to] += _value;

        // update the allowance of address
        allowance[_from][msg.sender] -= _value;

        return true;
    }
}

quinnsale.sol

pragma solidity ^0.8.19;

import "./Quinn.sol";

contract QuinnSale {
    // DO NOT expose the address of admin
    address admin;
    Quinn public tokenContract;
    uint256 public tokenPrice;
    uint256 public tokensSold;

    event Sell(address _buyer, uint256 _amount);

    constructor(Quinn _tokenContract, uint256 _tokenPrice) {
        // Assign an admin
        admin = msg.sender;
        tokenContract = _tokenContract;
        tokenPrice = _tokenPrice;
    }

    // mulitply
    //pure means not create transactions, not read or write data to blockchain
    function multiply(uint256 x, uint256 y) internal pure returns (uint256 z) {
        // ensuring that after multiplication, returned value is not getting overflowed.
        require(y == 0 || (z = x * y) / y == x);
    }

    // Buy tokens
    function buyTokens(uint256 _numberOfTokens) public payable {
        // require that value is equal to tokens
        require(msg.value == multiply(_numberOfTokens, tokenPrice));
        // require that the contract has enough tokens
        require(tokenContract.balanceOf(admin) >= _numberOfTokens);
        // msg.sender here is the buyer
        // in transfer function, msg.sender is QuinnSale contract
        require(tokenContract.transfer(msg.sender, _numberOfTokens));

        tokensSold += _numberOfTokens;

        emit Sell(msg.sender, _numberOfTokens);
    }
}

I have no idea why i am facing this error please help :(

1
  • 1
    Try putting messages on your require statements to see if one of them is causing the revert.
    – Bruce
    Sep 5, 2023 at 19:05

1 Answer 1

0

The possible problem could be that you are checking the balance of the admin account, but transfering tokens from the sale contract.

    // require that the contract has enough tokens
    require(tokenContract.balanceOf(admin) >= _numberOfTokens);
    // msg.sender here is the buyer
    // in transfer function, msg.sender is QuinnSale contract
    require(tokenContract.transfer(msg.sender, _numberOfTokens));

Either transfer tokens availables for sell to the sale contract, or the admin has to approve the sale contract and use transferFrom on the sale contract.

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.