I am using truffle and ganache to make a token demo for myself. I'm following this guide then making my own changes according to my project.

When I try running tests to verify that my contract is running properly I am presented with an interesting return of data that I was not anticipating.

When I mint a token using this test:

describe("mint", () => {
    it("Created token with specified attributes", async () => {
        let instance = await Token.deployed();
        let owner = await instance.owner();

        let tokenId = await instance.mint(5, 2, 5, 10, 5);

        describe("Expected output: ", tokenId);

        let attributes = await instance.getToken(0);
        assert.equal(attributes, [5, 2, 5, 10, 5]);

I was expecting this function to return [5, 2, 5, 10, 5]:

function getToken( uint _TokenId ) public view returns(
    uint256 value1,
    uint256 value2,
    uint256 value3,
    uint256 value4,
    uint16 value5
    Token memory _token = tokens[_TokenId];
    value1 = _token.value1;
    value2 = _token.value2;
    value3 = _token.value3;
    value4 = _token.value4;
    value5 = _token.value5;

But instead the variable attributes reportedly returns this weird data structure which appears to be an array of 5 with 3 mappings of the characters "c", "e", "s" to a variable. "c" contains the value I was hoping for:

{"c" : [5]  "e" : 0 "s" : 1}
{"c" : [2]  "e" : 0 "s" : 1}
{"c" : [5]  "e" : 0 "s" : 1}
{"c" : [10] "e" : 1 "s" : 1}
{"c" : [5]  "e" : 0 "s" : 1}

My question is why I am I getting this data structure? Shouldn't I be receiving my expected results? I believe the "e"'s mapping is the number of digits - 1, but I don't know what "s" represents. I could go through and make an array with each of the "c" mappings, but I don't know if this is the best way of handling this.


TLDR; use tokenId.toString()

The array you are receiving is a representation of the BigNumber which is returned from the blockchain. Or actually the blockchain returns an uint256 (or however many bits you use) and it is then converted into a BigNumber.

The "problem" with BigNumber is that you can't display its value like that. I'm not exactly sure what the array values represent but the library itself is https://github.com/MikeMcl/bignumber.js/ I believe.

Anyway, to display a human-friendly version of the number, you can just use value.toString().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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