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();
console.log(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?
balance
to string? Like this:web3.utils.fromWei(''+balance, 'ether')
– abed Dec 30 '18 at 23:40BigNumber
. web3.js v1.x relies onBN
. To be on the safe side, usebalance.toFixed()
, which will guarantee a correct type (string) and a correct format (explicit decimal). – goodvibration Dec 31 '18 at 8:10balance.toString()
works. – Daniel Gretzke Jan 1 '19 at 1:24