Developers
Tutorials
Events
5 min
events on caduceus are a way to raise awareness of, and track all historical transactions as they are persisted in blocks there are three main ways to subscribe to events in web3 js, the preferred javascript library for interacting with blockchain events the eth subscribe method this method of subscribing to events is like a “catch all” method it captures all event logs and therefore is all inclusive and exhaustive event logs are stored in a special area on evm let options = { fromblock 0, address \['address 1', 'address 2'], //only get events from specific addresses topics \[] //what topics to subscribe to }; let subscription = web3 eth subscribe('logs', options,(err,event) => { if (!err) console log(event) }); subscription on('data', event => console log(event)) subscription on('changed', changed => console log(changed)) subscription on('error', err => { throw err }) subscription on('connected', nr => console log(nr)) the subscribe method will return a subscription instance the subscription instance has these properties and methods id – this is the subscription id unsubscribe (callback) – this method can be used to unsubscribe from the subscription subscribe (callback) – this method can be used to re subscribe with the same parameters as the initial subscription had arguments – the subscription arguments that are used when re subscribing getpastevents the first method is called getpastevents and is attached to a contract’s instance it gets full historical data based on a filter criteria as an example, if we have an erc20 token we want to get all past transfer events where the value transferred was 1000 or 1337, we will do the following in our user facing javascript let options = { filter { value \['1000', '1337'] //only get events where transfer value was 1000 or 1337 }, fromblock 0, //number || "earliest" || "pending" || "latest" toblock 'latest' }; mycontract getpastevents('transfer', options) then(results => console log(results)) catch(err => throw err); you can filter what data you want to be returned since the transfer event returns from , to , and value we can filter on these properties blockchain data return is not instantenous, and can take time to return results contract instance event method the second way to subscribe to events from a contract is to use the event methods that have been automatically generated by web3 js on your contract instance this requires an abi this method is also temporary in nature, and will delete events when the user session ends let options = { filter { value \[], }, fromblock 0 }; mycontract events transfer(options) on('data', event => console log(event)) on('changed', changed => console log(changed)) on('error', err => throw err) on('connected', str => console log(str)) each events method will return an eventemitter the events that you can listen to are the ones we are listening to in the example code above where data – will fire each time an event of the type you are listening for has been emitted changed – will fire for each event of the type you are listening for that has been removed from the blockchain error – will fire if an error in the event subscription occurs connected – will fire when the subscription has successfully established a connection it will return a subscription id this event only fires once