0

On line 22 my code is -- require(tokenContract.balanceOf(this) >= _numberOfTokens);

This gives the error message

TypeError: Member "balanceOf" not found or not visible after argument-dependent lookup in string storage ref.require(tokenContract.balanceOf(this) >= numberOfTokens); ^---------------------^

My entire code is:

pragma solidity ^0.5.1;

contract ChampTokenSale {
    address admin;
    string public tokenContract;
    uint256 public tokenPrice;
    uint256 public tokensSold;

    event Sell(address _buyer, uint256 _amount);

    constructor (ChampTokenSale _tokenContract, uint256 _tokenPrice) public {
      admin = msg.sender;
      address expected = address(this);
      tokenPrice = _tokenPrice;
    }

    function multiply(uint x, uint y) internal pure returns (uint z) {
      require(y == 0 || (z = x * y) / y == x);
    }

    function buyTokens(uint256 _numberOfTokens) public payable {
      require(msg.value == multiply(_numberOfTokens, tokenPrice));
      require(tokenContract.balanceOf(this)>= _numberOfTokens);
      require(tokenContract.transfer(msg.sender, _numberOfTokens));

      tokensSold += _numberOfTokens;

      emit Sell(msg.sender, _numberOfTokens);
    }

    function endSale() public {
     require(msg.sender == admin);
     require(tokenContract.transfer(admin,  tokenContract.balanceOf(address(this))));


     admin.transfer(address(this).balance);
   }
}
0

In your code, tokenContract is a string, as defined here:

string public tokenContract;

There is no method balanceOf that can be called on a string.

0

There is a lot going wrong here.

The short answer is tokenContract is a string because of this:

string public tokenContract;

There is no reason to think a string should have a method balanceOf(). It's not possible.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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