Is it possible to have mappings inside mappings?

by mrAkax   Last Updated November 15, 2018 20:28 PM - source

Is this possible?

mapping(address => mapping(uint => customStruct[])) someName

Because I cannot debug and find my mistake... is there not enough gas, or it crashes on this code, while trying to fill someName.

For example:

uint length = someName[msg.sender][1].length;
someName[msg.sender][1][length].timestamp = block.timestamp;

Answers 1

I suppose customStruct is a struct with timestamp property. Then your code should work. However here my test scenario:

import "dapple/test.sol";

contract MyTest is Test {

  struct Struct {
    uint timestamp;

  // Mapping test
  mapping(uint => mapping(uint => uint)) mymap;

  mapping(address => mapping(uint => Struct[])) someName;

  function testNestedMappings() {
    //@log test nested mappings
    mymap[1][2] = 42;
    //@log mymap[1][2] = `uint mymap[1][2]`
    //@log test struct array:
    //@log someName[msg.sender][1].length = `uint someName[msg.sender][1].length`
    //@log incrementing length
    //@log saving timestamp to last entry
    someName[msg.sender][1][someName[msg.sender][1].length - 1].timestamp = block.timestamp;
    //@log `uint someName[msg.sender][1][someName[msg.sender][1].length-1].timestamp`

Which run with dapple test --report outputs the following:

  test nested mappings
  LOG:  test nested mappings
  LOG:  mymap[1][2] = 42
  LOG:  test struct array:
  LOG:  someName[msg.sender][1].length = 0
  LOG:  incrementing length
  LOG:  saving timestamp to last entry
  LOG:  1460035092

Cuz i can not debug

...but this is very important

April 07, 2016 13:22 PM

Related Questions

How to delete an element from a mapping?

Updated August 30, 2017 17:28 PM

Default value of a struct

Updated October 12, 2017 07:28 AM

Searching data on a smart contract?

Updated February 15, 2018 22:28 PM

Can't change a structs member value in a mapping?

Updated April 27, 2018 03:28 AM