1

I have a balance as follows such that after changing its value, I want to keep the history of previous transactions, for example, for the first time, value of Energy_Balance is 5 and for the second time 2 units will be added to Energy_Balance such that the total value will be 7. Now I want to keep the history of all transactions such that we would be able to know what was the first value (i.e. 5) and what was the second transaction value (i.e. 2). If I have to define an event for this, how to define it and how to invoke event ?

mapping(address => uint256) Energy_Balance

In case of using an array as proposed by user Eli Drion, as follows:

mapping (address => uint[]) Energy_Balance

How can I set the new value of Energy_Balance ? For example,

First Transaction: balances[owner][0] = balances[owner][0] + value; 
Second Transaction: balances[owner][1] = balances[owner][1] + value;

is correct ?

2

Now I want to keep the history of all transactions

You will need to use an array then, because the order matters here. If you don't need more informations about the user, you don't need to use a struct then, you can directly do like that.

mapping (address => uint[]) txs_history;

If I have to define an event for this, how to define it and how to invoke event ?

Defining an event

event MyEvent(address _sender, uint _amount);

Invoking an event

function myFunction(uint _amount) {
    //we do alot of things here
    MyEvent(msg.sender, _amount);
}
  • Thank you. Do you mean one of your proposed approaches is enough or i have to use both "array" and "event" ? thanks – Questioner Apr 27 '18 at 11:37
  • If you use a UI (website) interacting with the contract, you want to use events, otherwise the website doesn't know what happens. – Eli Drion Apr 27 '18 at 11:40
  • So it means one of approaches is enough ? ex. array or event ? Thanks – Questioner Apr 27 '18 at 11:47
  • Depends on what you want to do. If you have a website, you will have to use events too eventually. – Eli Drion Apr 27 '18 at 11:49
  • Thanks again. And do you agree with what I edited in my question ? Is it correct ? – Questioner Apr 27 '18 at 12:00

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.