CONTEXT OF THE QUESTION:
The references at the end of this post gave me the following understanding of events:
Whenever a transaction takes place that is meant to alter information in the blockchain, an event is the tool used to do the logging of that information.
Since I am coding my first ERC-20 Standard compatible Token, here is an example of the usage of events:
mapping (address => uint256) public balances;
event Transfer(address indexed from, address indexed to, uint tokens);
function transfer(address to, uint tokens) public returns (bool success)
{
balances[msg.sender] = balances[msg.sender].sub(tokens);
balances[to] = balances[to].add(tokens);
Transfer(msg.sender, to, tokens);
return true;
}
The second line declares an event that has 3 input variables, who is sending (from), who is receiving (to), and the amount being sent (tokens). And when the function transfer
is involved we see the usage of the event. It makes sense to me that this is where it should be involved, but what I don't understand is how Solidity ties this code:
event Transfer(address indexed from, address indexed to, uint tokens);
with making sure that the balances
of users are not faked. The event in my mind must be linking the 3 parameters together into the blockchain, but how does that linking happen? and how does the blockchain then prevent balances
from being faked.
DETAILED QUESTIONS:
What happens behind the scenes that links the
Transfer
event with the verification of what the balances are? Please feel free to go into depthsIf I had used another word, like
Foobar
and replaced the wordTransfer
with it, would the code still work? The reason I ask is because it seems like I am defining whatTransfer
is doing by declaring it (so by default it not being a predefined event of solidity), but I don't seem to be coding in much else.
References: