Event Logging and Extracting Transaction Information in Solidity.
Transactions that happen on the Blockchain are stored as Blocks. Every one of these transactions has logs and these logs represent the solidity events that have occurred on the Blockchain. These events can be accessed using the address of the contract.
Events can be created in a contract and it is defined using the Event keyword. When an event is created, the argument in that event is passed in the transaction logs.
Events can be declared in solidity using the format. We declare an event using the event keyword.
event <event Name> (parameters)
Example event myEvent (uint date, string value);
Once the event is declared you can emit event from a function as follows:
emit myEvent (block.timestamp, "message");
When events are triggered in solidity, the parameters are saved as logs in the Etherum Blokchain. In the example below we will create a contract where we will simply send a transaction without specifying any function.
We create a function, you don't have to invoke it, or specify which function you want to invoke. All we simply want to do is to be able to simply send Ethereum, we just want to be able to send the amount you want to send and the Ethereum Address.
This function we will create acts as a callback function. The function is created as shown below:
Contract transaction{
function () payable {}
};
We create the callback function by stating the function keyword without a function name, followed by the Payable keyword. This simply means we can interact with the function without having to specify what is in the function. So anyone on the Blockchain that has the address of this contract, can simply send ether to the contract addres.
We can then extract information from this fallback funtion. But before we do that we would have to specify two event logging mechanism.
Contract transaction{
event sender.logger(address)
function () payable {}
};
Next is to specify the second event logging mechanism, which is the value that has been logged.
This is done as follow:
Contract transaction{
event sender.logger(address);
event value.logger(uint);
function () payable {}
};
The code above is simply specifying the two event logging mechanism to extract the address and the value of the sender in a transaction that is been sent to the contract via the callback function.
Extracting Event Logs From A Transaction: To extract the address of the sender from the event log mecahansim above, we would do that as follows *sender.logger(msg.sender);
*
Contract transaction{
event sender.logger(address);
event value.logger(uint);
function () payable {
Sender.logger(msg.sender);
}
};
To also extract the value which was sent to the contract from the sender. we do that this way:value.logger(msg.value);
Contract transaction{
event sender.logger(address);
event value.logger(uint);
function () payable {
Sender.logger(msg.sender);
value.logger(msg.value);
}
};
We can further specify the access of this contract by modifying the access of the function call back and restricting it to only the owner with a valid value.
To be able to do this we first specify the owner
address private owner;
Then we need a way of specifying it by creating a function and assigning the owner as the sender of the message in the transaction;
owner = msg.sender;
}```
Then we will create our first modifier, to make sure that the transaction is only being handled by the owner. we do this by creating a modifier(refer to my other article on modifiers if you don't understand what modifiers are) [Funtion Modifiers and how to use them in Solidity](https://theucheedeoga.hashnode.dev/function-modifiers-and-how-to-use-them-in-solidity-ckltg03m4081e10s1dfrg9wk0)
```modifier isOwner {
require(owner == msg.sender);
_;
};```
Next, we create another modifier, to check if the value that was sent by the sender is valid.
```modifier isValueValid {
assert(msg.value >= 1 ether);
_;
};```
This means that the value that is been sent by the sender in the transaction must be greater than or equal to 1 ether.
We input these modifiers in the callback function as shown below:
```function()payable isOwner isValueValid {
sender.logger(msg.sender);
value.logger(msg.value);
}```
So whenever this function is been called and the sender wants to make a transaction to our contract
the modifiers must be true(i.e the sender must be the owner of the address and the value must be greater or equal to 1 ether before the transaction is successful.
The full smart contract now looks like this:
Contract transaction{ event sender.logger(address); event value.logger(uint);
address private owner
modifier isOwner { require(owner == msg.sender); _; };
modifier isValueValid { assert(msg.value >= 1 ether); _; };
function()payable isOwner isValueValid { sender.logger(msg.sender); value.logger(msg.value); }
};```
Other resources on events logging in Solidity: (bitdegree.org/learn/solidity-events)
(programtheblockchain.com/posts/2018/01/24/l..)
(tutorialspoint.com/solidity/solidity_events..)
You can also read up about events from the official solidity documentation to get a deeper understanding of what events in solidity are all about: (docs.soliditylang.org/en/v0.4.21/contracts...)