DEV Community


Decomposing a BigNumber in Truffle Console

preciouschicken profile image Precious Chicken Originally published at on ・3 min read

The BigNumber problem

The Truffle Console when queried tends to return BigNumbers, which look a bit like this:

BN {
  negative: 0,
  words: [ 10000, <1 empty item> ],
  length: 1,
  red: null

Converting this into individual values, i.e. integers, on the console is not obvious; so an aide memoire follows. I'm using Truffle v5.1.30, node v14.4.0 and npm package @openzeppelin/contracts v3.0.2; my OS of choice is Ubuntu 20.04 LTS.

The smart contract

Just so we are on the same page, here is the very simple smart contract I'm using as an example which is saved at contracts/PreciousChickenCoin.sol:

pragma solidity ^0.6.2;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract PreciousChickenCoin is ERC20 {
  uint public initialSupply = 10000;
  constructor() public ERC20("PreciousChickenCoin", "PCC") {
      _mint(msg.sender, initialSupply);

and deployed as migrations/2_deploy_contract.js:

var PreciousChickenCoin = artifacts.require("PreciousChickenCoin");

module.exports = function(deployer) {

The ERC20 import is achieved using:

npm install @openzeppelin/contracts

Additionally with the version of Truffle I'm using, v5.1.30, you have to explicitly tell it to use a version of Solidity that works with OpenZeppelin 3.0.2. You do that by modifying the following lines of the truffle-config.js file:

  compilers: {
    solc: {
      version: "^0.6.0",    // Fetch exact version from solc-bin (default: truffle's version)


The objective here is to use OpenZeppelin's ERC20 balanceOf() method to find out how many PreciousChicken coins the msg.sender has received on instantiating the smart contract.

This isn't a Ethereum tutorial, so I'm going to assume that you have compiled, migrated, are running something like Ganache, etc, etc and have started the truffle console with truffle console at the command line.

All the commands below will be at the truffle(ganache)> prompt.

Assign the deployed smart contract to a variable with a Promise

First we need to assign the deployed instance to a variable using the JavaScript Promise API (i.e. .then()).

PreciousChickenCoin.deployed().then(function(instance) {app = instance})

which should return undefined.

Discover the msg.sender address

Next find the msg.sender address, which we can do at the terminal:


this should return all the accounts present in Ganache:


Copy the first address (assuming it has been used - you can check this using Ganache).

Use the ERC20 balanceOf() method

Using the address you copied above:


should return the BigNumber (BN) we are interested in:

BN {
  negative: 0,
  words: [ 10000, <1 empty item> ],
  length: 1,
  red: null

Now at this point you might think:


would provide you with the balance, but in actual fact we get a string of errors.

Assign balance to a variable with a Promise

Due to the asynchronous nature of JavaScript we have to assign the balance first:

app.balanceOf("0x48a8163fA169Fd9eA3C14409aF29FF22699cE53f").then(function(balance) { balanceInstance = balance})

which returns undefined.

Finally get the variable



returns the value we want: 10000.

If you found this useful, or have feedback, please comment below.


The How to Build Ethereum Dapp (Decentralized Application Development Tutorial) by DApp University was particularly useful, especially the console demonstration section.

Discussion (1)

Editor guide
zimenkoliyailya profile image

Good post!
I am one problem.
I am going to use truffle for ethereum. So, i installed global truffle but truffle command is not recognized.
my npm is 14.0.1
Please help me