It happen when use truffle test,

enter image description here

here my contract file:

pragma solidity ^0.5.0;

import "./DappToken.sol";

contract DappTokenSale {

    address payable admin;
    DappToken public tokenContract;
    uint256 public tokenPrice;
    uint256 public tokensSold;

    event Sell(address _buyer, uint256 _amount);

    constructor (DappToken _tokenContract, uint256 _tokenPrice) public {
        admin = msg.sender;
        tokenContract = _tokenContract;
        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(address(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))));


and here my DappTokenSale js file:

var DappToken = artifacts.require('./DappToken.sol');
var DappTokenSale = artifacts.require('./DappTokenSale.sol');

contract('DappTokenSale', function(accounts) {
    var tokenInstance;
    var tokenSaleInstance;
    var admin = accounts[0];
    var buyer = accounts[1]; 
    var tokenPrice = 1000000000000000;
    var tokensAvailable = 750000;
    var numberOfTokens;

    //Some code i turn off for simplicity

    it('end token sale', function() {
        return DappTokenSale.deployed().then(function(instance){
            tokenInstance = instance;
            return DappTokenSale.deployed();
            tokenSaleInstance = instance;
            return tokenSaleInstance.endSale({from:buyer });
            assert(error.message.indexOf('revert' >= 0, 'must be admin to end sale'));
            return tokenSaleInstance.endSale({from: admin});
            return tokenInstance.balanceOf(admin); //<-- I think prolem is here but don't know how to fix it.
            assert.equal(balance.toNumber(), 999990, 'return all unsold dapp tokens to admin')
            return tokenSaleInstance.tokenPrice();
        }).then(function(price) {
            assert.equal(price.toNumber(), 0,'token price was reset')

How I can fix this ? i don't know which mistake i made :

  • I got a down vote from this question before, i know why someone do that ? was it too intolerant for this kind question ? – johnho.91 Apr 24 at 7:58
  • Not the one who downvoted you, but you're not supposed to just dump your code here and ask people to sort things out for you. Let alone, when parts of it (namely, the actual contract) are missing!!! Please post all the relevant details and ONLY the relevant details. Please also care to include your debug efforts and any intermediate conclusions that you've made along the way. Please refrain from screenshots, as well as other types of images. – goodvibration Apr 24 at 8:08
  • Thanks for reply, I posted my contract on. I tried to find solution and I think after search 1hour with no result then you should ask for help, so i did. And what problem with screenshot, I think screenshots is good to help everyone figure problem clearer. – johnho.91 Apr 24 at 8:16
  • 1
    One cannot copy paste from a screenshot in order to explain the problem. And with regards to the other issues - you just dumped your code here, expecting us to dig into whatever the problem is. You're the one who should be digging. And when you're done, you should post ONLY the information relevant to your problem, and point us to the exact point in your code where this problem takes place. You haven't done any of that, and if you want others to make an effort for you then I believe that you should first prove that you've done some effort on your own. – goodvibration Apr 24 at 8:52
  • Sorry, my bad, I would take that a lesson for myself – johnho.91 Apr 24 at 9:03

tokenInstance is an instance of DappTokenSale, which indeed does not have a method called balanceOf. So the issue is exactly what the error message says.

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.