3

I would like to return a 2D array from solidity code and look at it with web3.js

this is the solidity function:

uint8 [10][10][100][100] public blocks;
function getBlock(uint x, uint y) returns (uint8[10][10]) {
    if (x < blockStoreSide && y < blockStoreSide) {
        return blocks[x][y];
    }
}

but what I get back from getBlock in web3.js is this number:

0xf5027db1aeac4c95acf2c3c32651cfd3f5561906399e19bd14818a6c7cc2d9cb

My assumption is this is an address?

I tried web3.eth.getStorageAt for that address but I just get zeros, not what I actually set the value to.

the ABI looks okay to me:

{ constant: false,
  inputs: 
   [ { name: 'x', type: 'uint256' },
     { name: 'y', type: 'uint256' } ],
  name: 'getBlock',
  outputs: [ { name: '', type: 'uint8[10][10]' } ],
  type: 'function' }

So how do I actually read the storage? More general question, how can I efficiently read large amounts of storage?

3

The hex number you got back was a transaction hash. If you want to read from storage, you should add the constant keyword to your function:

uint8 [10][10][100][100] public blocks;     
function getBlock(uint x, uint y) constant returns (uint8[10][10]) {
    if (x < blockStoreSide && y < blockStoreSide) {
        return blocks[x][y]; 
    } 
}

Alternatively, you can use the call method.

myContract.getBlock.call(3,4)
  • how embarrassing. thanks. Not sure how I missed that. – Paul S Mar 4 '16 at 21:44
  • it was a 2 part question: If I want to read a large array it seems most efficient to read getStorageAt, but how do I find the address of the 4d array? – Paul S Mar 4 '16 at 21:45
  • 1
    if I return the 4d array I quietly run out of gas (i.e. nothing programmatically, just something in the miner logfile) – Paul S Mar 5 '16 at 4:14

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.