I am running this code on NodeJS application:

  let destinationBalanceWei = web3.eth.getBalance(process.env.DESTINATION_WALLET_ADDRESS).then(result => result.toString())
  let destinationBalance = web3.utils.fromWei(destinationBalanceWei, 'ether')

but I get following error in the console:

(node:79279) UnhandledPromiseRejectionWarning: Error: Please pass numbers as strings or BigNumber objects to avoid precision errors.
    at Object.fromWei (/Users/sahelanthropus/progetti/nodejs/node_modules/web3-utils/dist/web3-utils.cjs.js:440:11)
    at proveTransaction (/Users/sahelanthropus/progetti/nodejs/routes/index.js:49:39)
    at /Users/sahelanthropus/progetti/nodejs/routes/index.js:105:7
    at IncomingForm.<anonymous> (/Users/sahelanthropus/progetti/nodejs/node_modules/formidable/lib/incoming_form.js:107:9)
    at IncomingForm.emit (events.js:197:13)
    at IncomingForm._maybeEnd (/Users/sahelanthropus/progetti/nodejs/node_modules/formidable/lib/incoming_form.js:557:8)
    at /Users/sahelanthropus/progetti/nodejs/node_modules/formidable/lib/incoming_form.js:238:12
    at WriteStream.<anonymous> (/Users/sahelanthropus/progetti/nodejs/node_modules/formidable/lib/file.js:79:5)
    at Object.onceWrapper (events.js:285:13)
    at WriteStream.emit (events.js:197:13)
    at finishMaybe (_stream_writable.js:646:14)
    at stream._final (_stream_writable.js:624:5)
    at WriteStream._final (internal/fs/streams.js:270:3)
    at callFinal (_stream_writable.js:617:10)
    at processTicksAndRejections (internal/process/next_tick.js:76:17)
(node:79279) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:79279) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

What I can do to solve it?


This is incorrect handling of asynchronous function call:

let destinationBalanceWei = web3.eth.getBalance(...).then(result => result.toString());
let destinationBalance = web3.utils.fromWei(destinationBalanceWei, 'ether');

The value of destinationBalanceWei is probably undefined when you pass it to fromWei.

You can do it like this:

let destinationBalance;
web3.eth.getBalance(...).then(function(destinationBalanceWei) {
    destinationBalance = web3.utils.fromWei(destinationBalanceWei, 'ether');

Where you will have to find a way to be sure that destinationBalance has been set.

Or you can do it like this:

async function func() {
    let destinationBalanceWei = await web3.eth.getBalance(...);
    let destinationBalance = web3.utils.fromWei(destinationBalanceWei, 'ether');
    return destinationBalance;

I personally find the latter more convenient, unless of course you have a good reason for which you must run getBalance without waiting for it complete (performance-related reason).

  • Can you update your solution to work in order to print destinationBalanceWei with console.log(...) please? – kitsune Feb 14 at 10:41
  • @kitsune: Done. – goodvibration Feb 14 at 10:53

The issue is that web3.eth.getBalance returns a Promise and you have to "wait" until it gets executed.

You can use ES2018 and encapsulate your code in an async function:

async function myFunc() {
  let destinationBalanceWei = await web3.eth.getBalance(process.env.DESTINATION_WALLET_ADDRESS);
  let destinationBalance = web3.utils.fromWei(destinationBalanceWei, 'ether');
  console.log("destinationBalance", destinationBalance);

Also, note that the web3.utils.fromWei method expects a string or a BigNumber object passed as the first parameter. destinationBalanceWei is already a string so we don't have to mind that in the scenario above.

  • No no no. The problem is that the promise is handled asynchronously, while the (meaningless) return value destinationBalanceWei is passed to web3.utils.fromWei. – goodvibration Feb 14 at 9:49
  • Whoops, I jumped straight to the error message. I updated my solution, thanks @goodvibration! – Paul Berg Feb 14 at 9:54
  • Not working... cannot see the console.log(...) output. – kitsune Feb 14 at 10:40
  • 1
    is already a string - true in web3.js v.1.x, not true in web3.js v.0.x (where getBalance returns a BigNumber instance). – goodvibration Feb 14 at 11:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.