Web3.swift icon indicating copy to clipboard operation
Web3.swift copied to clipboard

Getting contract Events

Open dsemenovsky opened this issue 6 years ago • 7 comments

Hi! I checked source code and documentation and it seems there is no way to list events of the contract or subscribe to event listener.

Am I right and it is to be implemented or I just didn't know where to look?

dsemenovsky avatar Jul 10 '18 18:07 dsemenovsky

@pixelmatrix Do we have this yet? I haven't used it so I can' really answer it...

koraykoska avatar Jul 10 '18 18:07 koraykoska

So as of right now there's partial support for events. It depends on what sort of events are you looking to get.

First, it's possible to look at what types of events can be returned from a contract by looking at JSONContract.events. However, these are not instances of the event, but the spec of an event.

As far as instances of events on the blockchain go, currently this library does not support much. You can decode one from the logs on an EthereumTransactionReceipt using ABI.decodeLog(event:from:), but it doesn't happen automatically like it does in web3.js. See this test for an example:

https://github.com/Boilertalk/Web3.swift/blob/master/Example/Tests/ContractTests/ContractTests.swift#L236

We've taken things a little bit further in Bitski SDK, we have a TransactionWatcher class that watches for confirmations and events. This could potentially be brought into the main Web3.swift library at some point. You can instantiate with a transaction hash, and pass in some events to look for:

https://github.com/BitskiCo/bitski-ios/blob/master/Example/Tests/Tests.swift#L333

There's also a feature in web3.js to get all past events from a given contract instance at an address. Currently neither Web3.swift or Bitski support that feature.

Hopefully that gives you an idea of the current support for events. I think that may be a good area to focus on next for this library, since they are pretty essential for interactive dapps.

pixelmatrix avatar Jul 10 '18 18:07 pixelmatrix

@pixelmatrix Thank you for the explanation! I will have to wait until getting past events are supported as this is needed for my application to work.

dsemenovsky avatar Jul 10 '18 19:07 dsemenovsky

No problem. Can you give me more details about what exact functionality you're looking for? Are you looking for all past events, or filtering them? I think we can at least scope this out and see how much work it would be.

pixelmatrix avatar Jul 10 '18 19:07 pixelmatrix

Well, ideally all functions described here: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#events However, contract.events.MyEvent() with filtering by block number will be enough.

I need the capability of getting logged events by event name, e.g. Register, and polling them every N seconds (with the same function, different block numbers) unless subscriptions are available.

dsemenovsky avatar Jul 10 '18 19:07 dsemenovsky

Got it. Thanks for the clarification!

pixelmatrix avatar Jul 11 '18 00:07 pixelmatrix

Hello guys, are there any updates regarding events support?

haritowa avatar Aug 27 '20 22:08 haritowa