1

I have a small smart contract that has 2 functions, one to set 3 numbers and another function returning these 3 numbers.

return(a,b,c)

samplecont.setnumbers(100,250,250)
samplecont.getnumbers()
                 [100,250,250]
samplecont.setnumbers(101,260,260)
                 [101,260,260]
samplecont.setnumbers(102,270,270)
                 [102,270,270]

I am trying to access the past values of these 3 variables using getStorageAt. Have written following code after reading other responses on this topic: mycontractaddress consists of the address if mined contract.


uint maxBlocks = 50;
uint startBlock = eth.blockNumber;
for (var i = 0; i < maxBlocks; i++) { 
        current = web3.eth.getStorageAt(mycontractaddress, 0, startBlock-i);

        blockDate = new Date(web3.eth.getBlock(startBlock-i).timestamp*1000);

        console.log("Block #" + (startBlock-i) + " Timestamp:" + blockDate.toString()
          +  " Value:" + web3.toDecimal(current));

    }

however, output only shows the first of the 3 numbers:

Block #7547 Timestamp:Wed, 07 Sep 2016 14:06:55 IST Value:102
Block #7546 Timestamp:Wed, 07 Sep 2016 14:06:50 IST Value:102
Block #7545 Timestamp:Wed, 07 Sep 2016 14:06:47 IST Value:102
Block #7544 Timestamp:Wed, 07 Sep 2016 14:06:45 IST Value:102
Block #7543 Timestamp:Wed, 07 Sep 2016 14:06:17 IST Value:102
Block #7542 Timestamp:Wed, 07 Sep 2016 14:06:14 IST Value:102

what am I doing wrong? appreciate any pointers.

0

I see 2 things:

  • to get the other values, you need to look at another index in memory. Like so web3.eth.getStorageAt(mycontractaddress, 1, startBlock-i); and web3.eth.getStorageAt(mycontractaddress, 2, startBlock-i);. If 1 and 2 don't work, the numbers are at another index.
  • your samplecont.setnumbers() transactions were probably all mined in the same block. Try spacing your transactions in consecutive blocks.
| improve this answer | |
  • :) Perhaps mark as solved? – Xavier Leprêtre B9lab Sep 7 '16 at 11:21
  • Just did Xavier, thanks! new to stack-exchange :p – eNVy Sep 15 '16 at 8:30

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.