To get event logs of the past, you can instantiate the event with a block range, and use the myEvent.get
function to retrieve events.
In your example, we could do something minimal like this:
let transferEvent = product.Transferred({}, {fromBlock: 0, toBlock: 'latest'})
transferEvent.get((error, logs) => {
// we have the logs, now print them
logs.forEach(log => console.log(log.args))
})
Note: The first argument {}
can be used like a filter, to only catch certain events.
Above code logs this to console:
{ from: '0xc7d748654199d3594239a244d806e51331ca14b5',
to: '0x3c11b23b4d5adb2d534d262cf83cee773c9b1c0a',
quantity: { [String: '1'] s: 1, e: 0, c: [ 1 ] } }
{ from: '0xc7d748654199d3594239a244d806e51331ca14b5',
to: '0x3c11b23b4d5adb2d534d262cf83cee773c9b1c0a',
quantity: { [String: '3'] s: 1, e: 0, c: [ 3 ] } }
What about for all events of a contract?
Here the event instantiation would just look like this (notice we only use one parameter here):
let events = product.allEvents({fromBlock: 0, toBlock: 'latest'})
And then continue as before, with events.get()
.
event.get() vs event.watch()
event.watch()
is used for dealing with new events that fit a filter, as they stream in ("watching" the blockchain).
event.get()
is for grabbing existing events that fit some filter.
Sources:
From the experience, the issue of using the filter is quite complicated, since the use of callback and synchronization with the blockchain make this type of utilities difficult.
To begin with, I tell you that the ideal would be to filter through the exact block that is the only one that gives me resultant feedback and does not give me failures. The fastest would be to use an external log to save the transactions and addresses.
I also recommend that you see this API, so you know a little how to work with these topics.
Tea I leave a pretty useful link with a case similar to what you try. Link
Best Answer
myContractWebsocket.events.MyLogName
returns aSubscription
response, which has an unsubscribe method. See here for a bit more detail.