How to interpret javascript BN object?
Asked Answered
U

2

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.

Unhorse answered 12/6, 2021 at 6:16 Comment(4)
what is this BN object? That's not a javascript built in - does this documentation helpHypoxanthine
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(), etcPisci
I would guess it is this library.Canara
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 @PisciCanara
N
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()
Neoptolemus answered 5/12, 2021 at 7:11 Comment(0)
H
1

bn.js has a readme with examples.

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

Halitosis answered 13/6, 2021 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.