I need check that token has no balance when selfDestruct was called, but shows an error that balance.toNumber is not a function in the test file.


function endSale() public {
  //Require Admin (Only Admin can end the Sale)
  require(msg.sender == admin);

  //Transfer remaining dapp tokens to admin
  require(tokenContract.transfer(admin, tokenContract.balanceOf((address(this)))));

  // Destroy contract at the end - transfer remaining token to the admin


(This test code is only for admin.transfer(address(this).balance); )

balance = web3.eth.getBalance(tokenSaleInstance.address);
assert.equal(balance.toNumber(), 0);
  • Debug your code: console.log(balance)!!! Dec 23 '19 at 7:15

web3.eth.getBalance returns a Promise, with the actual balance in Wei a a string. To get the balance, you can use Promise.then() or async/await:

  .then(balance => {
    // You can use balance here

// or

const getBalance = async () => {
  const balance = await web3.eth.getBalance(address);

  // You can use balance here
  • web3.eth.getBalance returns a Promise object in web3.js v1.x. In web3.js v0.x, it returns the actual balance. Since we don't know what version this dude is using, I suggested (in a comment) that he prints the returned value, in order to see what it is. Dec 23 '19 at 9:53

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.