pragma solidity ^0.4.24;
contract assignment2{
event ageread(address,int);
function getage(address peridentifier) public returns(uint){
human = mystruct("adam",10); //using struct mystruct
ageread(peridentifier,stateintvariable); //stateintvariable is age
}
}
- 30,570
- 21
- 53
- 96
- 181
- 2
- 6
-
Events are used to communicate the outside world what happens during contract execution. – Nulik Aug 19 '18 at 16:35
4 Answers
Events are to publish actions to listeners outside the blockchain. Smart contracts themselves cannot listen to any events.
All information in the blockchain is public and any actions can be found by looking into the transactions close enough but events are a shortcut to ease development of outside systems in cooperation with smart contracts. They are also indexable, meaning you can search for them. So whenever something happens within your smart contract which some system outside the blockchain should know about you should emit an event and the outside system can listen for such events.
Events are secure in a way that no other smart contract can fake events from your contract. So if you are listening to events from your contracts address and you receive such event you can be sure it was your contract which emitted it.
- 29,391
- 3
- 20
- 57
-
return is also going to do the same thing as when we emit an event? – Faheel Sattar Aug 19 '18 at 16:49
-
1No, return is used to return a value from a function. They are not so easily available as events and you can emit multiple events from one function – Lauri Peltonen Aug 19 '18 at 17:31
From the docs:
Events allow the convenient usage of the EVM logging facilities, which in turn can be used to “call” JavaScript callbacks in the user interface of a dapp, which listen for these events.
Events allow clients, (dapps) to monitor and react to what's happening to a smart contract.
Events are included in the transaction logs of the same block that fired the event.
There are 3 main use cases for events and logs:
Smart contract return values for the user interface
Asynchronous triggers with data
A cheaper form of storage
Read this article for more details.
- 2,567
- 14
- 38
-
I have no idea why that article claims function return values to be events. They are not the same as events emitted from functions with the
emitkeyword. – Lauri Peltonen Aug 20 '18 at 05:44 -
In a recent release (v0.4.21 at 8th March 2018),
emitkeyword has been introduced to emit the event. This will help to differentiate the functions from the event. – Soham Lawar Aug 20 '18 at 05:48 -
Read the following question to understand importance of events in solidity https://ethereum.stackexchange.com/questions/51039/why-to-write-return-statements-in-solidity-function – Soham Lawar Aug 20 '18 at 05:50
Besides, event will be recored in ethereum transaction's receipts; then explorer page like http://etherscan.io can read the receipt and display the internal transaction in smart contracts.
- 2,151
- 2
- 9
- 16