# Event Subscription
In Vite, events are notifications triggered by virtual machine to indicate state change and will be pushed to front-end program such as dApp for further processing.
A vmlog
will be created in the smart contract's response block when event is triggered.
Taking the following contract as example, when function transferWithEvent
is called, the contract transfers the token received to specified address and then triggers a TransferEvent
event.
The TransferEvent
event contains the following content:
- The hash of contract response block in which the event was created. This information can be used to retrieve contract address, contract account block height, request transaction hash and so on.
- Event signature as the hash of
TransferEvent(address,uint256,tokenId)
. - Indexed parameters, which are marked as
indexed
in event definition. Up to 3 indexed parameters can be defined in an event. Event signature and indexed parameters are stored intopics
field ofvmlog
, wheretopics[0]
is occupied by event signature. - Non-indexed parameters having no
indexed
marked. Non-indexed parameters are stored indata
field ofvmlog
.
Below is an example of event logged on the blockchain when sending 1 VITE to vite_9990375e0eaf10426d1d1f9b528b6dee158fd3adb0e1b9de70
# Event Subscription
If a certain event is subscribed to, the subscriber will be notified when this certain event occurs.
For example, if you subscribe to TransferEvent
of TransferContract
, you will receive a notification when transferWithEvent
method is called.
Event subscription can be used to listen to contract state changes or new transactions on blockchain.
Note: It's only allowed to listen to events that have not occurred yet. Historical events cannot be monitored but can only be fetched through Ledger API.
The following parameters need to be specified when subscribing to an event:
- Contract address. Required.
- Contract account height range. Optional. By this parameter, it's possible to subscribe to events happening within a certain height range.
- Event signature or indexed parameter. Optional. For example, you can subscribe to events that transfer to certain accounts.
For more information, see Subscription API and newVmLog