I have encountered a problem with the web3js 1.0 library.

I have a simple function that is getting a balance from a smart contract:

async balance() {
  return await this.Marketplace.balances(this.storeOwner);

I have another function inside my angular component where I display the balance to the user:

async getBalance() {
  const balance = await this.smartContract.balance();
  this.balance = this.smartContract.web3.utils.fromWei(balance, 'ether');

When calling the web3.utils.fromWei() function I get this error:

Error: Uncaught (in promise): Error: Please pass numbers as strings or BigNumber objects to avoid precision errors.

but the console log shows me, that the balance is a BigNumber object:

BigNumber {s: 1, e: 0, c: Array(1)}

I am using version 1.0.0-beta.36. Why do I get the error?

  • did you try to convert balance to string? Like this: web3.utils.fromWei(''+balance, 'ether') – abed Dec 30 '18 at 23:40
  • Check if you have a dependency on a different version of web3 like v0.20. – Ismael Dec 31 '18 at 5:36
  • 1
    web3.js v0.x relies on BigNumber. web3.js v1.x relies on BN. To be on the safe side, use balance.toFixed(), which will guarantee a correct type (string) and a correct format (explicit decimal). – goodvibration Dec 31 '18 at 8:10
  • balance.toString() works. – Daniel Gretzke Jan 1 at 1:24
  • @goodvibration you're right. I use truffle to create the smart contract instance, which uses 0.x and pass the result to my web3 1.0 method. that's why it doesn't work – Daniel Gretzke Jan 1 at 1:26

Your Answer

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

Browse other questions tagged or ask your own question.