3

i am trying to get the balance of a wallet, the method is working but the returned result is a json object - { c:(2) [484895,26495000000000],e:19, s: 1}.

In which the value - 484895 corresponds to 48.4895 eth in account 1, value 26495000000000 cooresponds to 26.495 eth in account, my questionis why there is a big difference for the value in the array. what is the semantics of e(19) and s(1).

the code is below

  getBalance (address) {
      return new Promise (function (resolve, reject) {
        web3.eth.getBalance(address, function (error, result) {
          if (error) {
            reject(error);
          } else {
            resolve(result);
            }
        })
      })
    }

   getBalance(account).then((result) => {
         console.log('getBalance',result);
         web3.utils.fromWei(result.c[0], 'ether')
    });
5

It is the BigNumber representation of the wallet balance that is output by your script.

Indeed, BigNumbers need to be transformed through toString(int) or toNumber() in order to become compatible with javascript int or string. You can read more about this here and here.

To understand the object output, I found on BigNumbers's git that:

The value of a BigNumber is stored in a decimal floating point format in terms of a coefficient, exponent and sign.

So x = new BigNumber(-123.456); console.log(x); would output : { c:(2) [123,456], e:2, s: -1}, with

  • c : coefficients / significand
  • e : exponent
  • s : sign

In your case the exponent equals 19 because the smallest unit of ether is the WEI = 10-18 eth.

To find the value, this is how you can process:

  • c(1) concatenated to c(2) form the significant digits, called C
  • 19 is the exponent of your number in its modified normalized form, regarding the base 10, meaning in the shape x.y*10^n , with x in the range 1.0 to 10
  • -1 is the sign

this means 48.489526... = 4.8489526... * 10 ^ 19 * -1

You should then divide the value obtained through BigNumber.toNumber() by 10^18 to get a correct ETH amount to show to the user.

| improve this answer | |

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.