7

While I was working on smart contract using truffle, whenever request some number like account balance or address from the truffle console; I receive a BN object which looks like this:

BN {
  negative: 0,
  words: [ 37748736, 3305132, 2220446, <1 empty item> ],
  length: 3,
  red: null
}

This object is part of bn.js library. But I am not able to find any documentation on how to interpret this object.

How do I read this. I want to learn what each field in this object means and be able to manually convert it to a normal number.

jarvis1234d
  • 81
  • 1
  • 5
  • 2
    what is this BN object? That's not a javascript built in - does [this documentation](https://web3js.readthedocs.io/en/v1.2.0/web3-utils.html#bn) help – Jaromanda X Jun 12 '21 at 06:18
  • 2
    If you are receiving a BN object already, probably you just need to use needed methods to get the number value: bn.toString(base, length), bn.toNumber(), etc – Eddy Jun 12 '21 at 07:41
  • I would guess it is [this](https://github.com/indutny/bn.js/) library. – President James K. Polk Jun 12 '21 at 14:18
  • 1
    The number represented appears to 10000000000000000000000. But this is based on analyzing the *current* internals of the source code. This can change over time, thus you should always use the methods suggested by @Eddy – President James K. Polk Jun 12 '21 at 14:29

2 Answers2

4

I am a bit late, but you can convert your function outcome like the following:

outcome = await app.balanceOf(0x....)
outcome.toNumber()

or even better:

(await app.balanceOf(0x....)).toNumber()
Dharman
  • 30,962
  • 25
  • 85
  • 135
Logan
  • 93
  • 9
1

bn.js has a readme with examples.

You can convert the BN object to JS native Number using the .toNumber() function.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100