10

I'm receiving the result in BN format. How can I convert it into actual string or number? I'm using ethjs library to interact with Smart Contract.

token.totalSupply().then((totalSupply) => {
  // result <BN ...>  4500000
});

1 Answer 1

10

Once you have a BN object, you can use .toString() or .toNumber() on it.

Per the comments below, your function isn't actually receiving a BN. It's getting some sort of Results object that has a single key in it: 0. (Presumably if the function returned multiple values, there would be more keys.)

So first extract the BN from the Result:

token.totalSupply().then(result => {
  const supply = result[0];
  console.log(supply.toString());  // or .toNumber()
});
8
  • 1
    UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: totalSupply.toNumber is not a function Mar 30, 2018 at 10:16
  • It's an object. Mar 30, 2018 at 10:19
  • Is this in the browser? If so, console.log(totalSupply) and browse through it to see what functions are available? ethjs says it uses bn.js, and those BN objects have toNumber and toString both documented.
    – user19510
    Mar 30, 2018 at 10:20
  • See also github.com/ethjs/ethjs/blob/….
    – user19510
    Mar 30, 2018 at 10:21
  • It's on NodeJS server. The totalSupply logs Result { '0': <BN: 100590> } The total supply must be 1050000. Mar 30, 2018 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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