I have a simple contract Foo which is deployed to a Ganache network.

pragma solidity ^0.4.18;

contract Foo {
    uint storageData;

    function set(uint x) public {
        storageData = x;

    function get() constant public returns (uint) {
        return storageData;

Now when I want to interact with it using truffle console, first I set the value

truffle(development)> Foo.at("0x4d4337B075a442cF3351Fa11c425C350cE2984F5").set(123)

Question 1: Then I tried to retrieve that value but it is returning a BignNumber. Why is it not an integer?

truffle(development)> Foo.at("0x4d4337B075a442cF3351Fa11c425C350cE2984F5").get.call()
BigNumber { s: 1, e: 0, c: [ 123 ] }

Question 2: How can you convert it to an integer or string?

truffle(development)> Foo.at("0x4d4337B075a442cF3351Fa11c425C350cE2984F5").get.call().toNumber()
TypeError: Foo.at(...).get.call(...).toNumber is not a function

Tried to console.log it after a .toNumber, it prints out the right value 123, but why is it printing an undefined too?

truffle(development)> Foo.at("0x4d4337B075a442cF3351Fa11c425C350cE2984F5").get.call().then(a => console.log(a.toNumber()))
  • use toNumber() function: count = await app.getCount; count.toNumber() – Justin Apr 10 '19 at 9:30

Some tips:

  • have to do this asynchronously. Truffle makes the function thenable.
  • don't need "call" explicitly because the get() is marked constant.
  • once you have the BigNumber, then you can convert to something useful. Remember that JavaScript can't deal with 256-bit integers, so if you need to add/substract etc., always use the BigNumber library and the original value. Save this sort of conversion for making human-readable representations.


var n;
var foo = Foo.at("0x4d4337B075a442cF3351Fa11c425C350cE2984F5");

foo.get().then(function(response) { 
  n = response.toString(10);

I just sketched it out to illustrate, so hopefully didn't flub the syntax.

Hope it helps.

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