DEV Community

Precious Chicken
Precious Chicken

Posted on • Originally published at preciouschicken.com on

Decomposing a BigNumber in Truffle Console

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
}
Enter fullscreen mode Exit fullscreen mode

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);
  }
}
Enter fullscreen mode Exit fullscreen mode

and deployed as migrations/2_deploy_contract.js:

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

module.exports = function(deployer) {
  deployer.deploy(PreciousChickenCoin);
};
Enter fullscreen mode Exit fullscreen mode

The ERC20 import is achieved using:

npm install @openzeppelin/contracts
Enter fullscreen mode Exit fullscreen mode

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)
    }
  }
Enter fullscreen mode Exit fullscreen mode

Aim

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})
Enter fullscreen mode Exit fullscreen mode

which should return undefined.

Discover the msg.sender address

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

web3.eth.getAccounts()
Enter fullscreen mode Exit fullscreen mode

this should return all the accounts present in Ganache:

[
  '0x48a8163fA169Fd9eA3C14409aF29FF22699cE53f',
  '0x524b29d0817412fFc470a50Efc1241B3999Dfce9',
  '0x91167E8DaDa250B4465Bf0D63b679E27DBe69De3',
  '0xE98135dB5a4CB6b6F65C3AFC9fa3868e0845BE68',
  '0x7137C7a475918040d0eDC2b35cCF4911a3B590A6',
  '0xe50fFEeCA1beE4D0a83dF11a97CFbf78b6eB8aCc',
  '0x2E11471f2290C248fDC9ee338B9e2a8Cc1EC1773',
  '0xC90A29e53F07A6C6faC7A412bdF39816478fA6F4',
  '0xb0e5E47c8936640C0479387a82545A05051Bf331',
  '0x4224E151127da145D76dFDc01Aa1b9023EF4ed34'
]
Enter fullscreen mode Exit fullscreen mode

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:

app.balanceOf("0x48a8163fA169Fd9eA3C14409aF29FF22699cE53f")
Enter fullscreen mode Exit fullscreen mode

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

BN {
  negative: 0,
  words: [ 10000, <1 empty item> ],
  length: 1,
  red: null
}
Enter fullscreen mode Exit fullscreen mode

Now at this point you might think:

app.balanceOf("0x48a8163fA169Fd9eA3C14409aF29FF22699cE53f").words[0]
Enter fullscreen mode Exit fullscreen mode

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})
Enter fullscreen mode Exit fullscreen mode

which returns undefined.

Finally get the variable

Now:

balanceInstance.words[0]
Enter fullscreen mode Exit fullscreen mode

returns the value we want: 10000.

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

References

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

Top comments (2)

Collapse
 
bluewater0506 profile image
ilya

Hello
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.
why?
my npm is 14.0.1
Please help me

Collapse
 
preciouschicken profile image
Precious Chicken

Sorry, don't check comments often. Not sure if you still have the problem - but if you do I'd suggest writing up your problem at ethereum.stackexchange.com/?