Events are a log infrastructure provided by Ethereum Virtual Machine (EVM). They record operations and are saved as logs. They can also be used to notify the UI, provide the results of function calls, and other interactions. Logs and events bridge the platform and end users.
Contract log example
You can use logs and events to obtain the execution status of the contract code. An example contract is provided as follows:
pragma solidity ^0.4.0;
contract ClientReceipt {
event Deposit(
identity indexed _from,
uint _id,
uint _value
);
function deposit(uint _id) {
Deposit(msg.sender, _id, msg.value);
}
}
After the contract is deployed, you can call the deposit(uint256)
method to obtain events in a log
. You can use the event
keyword to define an event. The parameter list of the event includes the names and types of parameters you want provide in a log. Outputs of the contract log are as follows:
from: 1122334455667788991011121314151347181920929293949596979812345678
to: 10237462546362184738205736165f7391947195472655869214456471048395
log_data:00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000032
topics: [90890809c654f11d6e72a28fa60149770a0d11ec6c92319d6ceb2bb0a4ea1a15
1122334455667788991011121314151347181920929293949596979812345678]
The outputs include the following properties: from
, to
, log_data
, and topics
. For information about the detailed log structure, see Contract log structure.
Contract log structure
The log outputs generated by calling the deposit(uint256)
method show the contract log structure supported by the platform.
Property | Type | Length (bit) | Description |
from | identity | 256 | The ID of the contract caller. |
to | identity | 256 | The ID of the called contract. |
topics | string | Not defined | The contract log topics, including user-defined topics. To define a topic, you need to add the indexed keyword in the input parameter declaration of an event. For more information, see Contract log example. |
log_data | bytes | Not defined | The contract log data. You can obtain the outputs of the example contract log after calling a contract. |
For more information about logs and events, see Solidity official documentation.