I have a mapping variable in my solidity code which maps address to name. I want to maintain the history/log of every change that happens in this mapping. What is the best possible solution to it?
Asked
Active
Viewed 512 times
2 Answers
5
Define a change event like
event MyMapChange(address _addr, string _newName);
and then, on each change, emit one like this:
emit MyMapChange(myAddr, myNewName);
Events get stored on the blockchain and can be queried, i.e. you can request request history, and also you can subscribe to new events, and even filter them by field values. To enable field filtering, you need to add indexed keyword to no more than 3 of the fields defined in your event, like this:
event MyMapChange(address indexed _addr, string _newName);
Check this related question and answers: How can I view event logs for an ethereum contract?
Utgarda
- 791
- 5
- 20
-
Although this is probably the most commonly used approach, there are some indications that events might be pruned, check this question https://ethereum.stackexchange.com/questions/6519/are-events-permanently-stored-and-reliably-available-to-contracts-in-the-future – KwahuNashoba Sep 24 '18 at 21:00
-
@KwahuNashoba, Events are permanent, the logs are part of the tx receipts. Even in the link you provide is described that events are permanent. – Jaime Dec 16 '18 at 10:28
-
Yes, I stand corrected! – KwahuNashoba Dec 27 '18 at 17:52
0
@Gagan, I think you need a combination of Mapping,Struct and Array. Please refer the below URL may be it is relevant to your problem:
Amit Modi
- 321
- 1
- 15