0

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.

DappTokenSale.sol

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
  admin.transfer(address(this).balance);

DapptokenSale.js

(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)!!! – goodvibration Dec 23 '19 at 7:15
0

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:

web3.eth.getBalance(address)
  .then(balance => {
    // You can use balance here
    console.log(balance);
  })
  .catch(console.error);

// or

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

  // You can use balance here
  console.log(balance);
}
  • 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. – goodvibration 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.