0

I am using ganache and truffle for building DApp,

In my smart contract, I have declared one mapping variable, now in function I am assigning some value to that mapping variable. Every time I am assigning different values to that mapping variable.

Now, I want that what are the different values which has been assigned to that mapping variable till now means fetching the history of that mapping variable. So how to that, is there any way to fetch all the details of state variables (mapping in my case) ?

  • Contract needs it for something, or client needs it for something? – Rob Hitchens - B9lab Apr 17 '18 at 0:23
1

Not possible

What you are asking for is not possible, as the keys are hashed before being placed in the trie.

Even if you had a way of knowing what entries belonged to your contract, there would be no way to know what the original value of the key was.

The single best (and recommended) way of doing this is to emit an event that contains the information you are interested in, this event will then be iterable outside of the contract.

If you are not able to do this (for example you have already deployed and have live code), there's a (slightly harder) novel way of manually stepping through the blockchain and processing transactions to your contract here:

Iterating over all public values of a mapping in web3

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.