How would you guys call a mapping from a test and get the values?

This is the mapping that I have on my contract:

mapping(address => uint[29]) balanceOf;

It´s a mapping to an array of integers which reflects the different slots in the balance of the user. So, I´m trying to call directly from the myTest.js (truffle) but return me an error:

 TypeError: Cannot read property 'call' of undefined
  at Context.it (test/ovxet2.js:13:50)
  at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

And I´m doing this action like below, using async/await:

    var balanceOf = await instance.balanceOf.call({from: [accounts[0]][0]});

And I´ve also tried call in another way to receive the whole array of integers:

var balanceOf = await instance.balanceOf.call({from: accounts[0]});

However, I have another tests written using promise chaining...and I´ve implemented the same ways in order to get those values but return me the same errors.

So, can truffle returns mappings? Or I have to create my own getter function in the contract (for testing purposes) and then call that function from the test?

2 Answers 2


You cannot return a mapping type. Among the several reasons: keys are never stored and cannot be retrieved. A mapping doesn't known if a key has data or not, and it should return the whole structure which is 2^256 addresses containing 32 bytes each.

If your contract has declared

mapping(address => uint[29]) public balanceOf;

The solidity compiler will create a getter with a single parameter that will accept an address.

var balance = await instance.balanceOf.call(account);
  • That step in the test doesn´t work....returns : TypeError: Cannot read property 'call' of undefined...I´ve tried call balanceOf using accounts[0] and also {from: accounts[0]}....
    – Jorge
    Dec 13, 2017 at 2:38
  • @Jorge Note that I've added the public qualifier to balanceOf declaration.
    – Ismael
    Dec 13, 2017 at 4:02
  • Ah ok,but I also tried that way. Return Invalid number of arguments to Solidity function. I´ve tried calling in different ways: call(accounts[0]),call(acounts[0][0]), call({from: accounts[0]}) and call({from: accounts[0][0]}). And also, I´ve kept away accounts[0] in a var and tried all ways, but the same result...
    – Jorge
    Dec 13, 2017 at 19:02
  • @Jorge It does work for me, perhaps my example is different. Without more code I cannot test what is wrong with yours.
    – Ismael
    Dec 13, 2017 at 20:06
  • But I think it doesn´t depend on the code. If the declaration is the same...and the way how you call the variable across the test is the same...I don´t understand why you are doing the process OK.
    – Jorge
    Dec 20, 2017 at 1:23

In addition, the getter can be also called directly without the call prefix:

var balance = await instance.balanceOf(account);

Your Answer

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

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