Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Utility] Contract event listener #112

Closed
barrasso opened this issue Dec 17, 2018 · 7 comments
Closed

[Utility] Contract event listener #112

barrasso opened this issue Dec 17, 2018 · 7 comments

Comments

@barrasso
Copy link
Contributor

barrasso commented Dec 17, 2018

Background

Events allow the convenient usage of the EVM logging facilities, which in turn can be used to “call” callback functions in the user interface of an app, which listens for these events. You can find more info on events here.

It would be great to have an easy way to subscribe to events that occur within smart contracts in web3swift's Utils/.

This functionality spans many use cases since developers consistently want to reflect UI changes based on smart contract event updates in real-time.

Acceptance Criteria

[] Clear and concise code that makes it easy to listen to smart contract events and handles subscriptions to those events
[] Document sample usage of how to use your code

@barrasso barrasso changed the title [Utility] Event listener for contract events [Utility] Listener for contract events Dec 17, 2018
@barrasso barrasso changed the title [Utility] Listener for contract events [Utility] Contract event listener Dec 17, 2018
@gitcoinbot
Copy link

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


This issue now has a funding of 2.962 ETH (249.97 USD @ $84.39/ETH) attached to it as part of the ethsociety fund.

@gitcoinbot
Copy link

gitcoinbot commented Dec 17, 2018

Issue Status: 1. Open 2. Cancelled


Workers have applied to start work.

These users each claimed they can complete the work by 3 years, 2 months ago.
Please review their action plans below:

1) baldyash has applied to start work (Funders only: approve worker | reject worker).

  1. see what have already done in lib
  2. define event
  3. setup provider
  4. setup listener
  5. profit

Learn more on the Gitcoin Issue Details page.

@BaldyAsh
Copy link
Collaborator

@barrasso Hi. I looked at what is done in lib and @shamatar had already made built-in Event listener.
Look at class Eventloop in Web3/Classes/Web3+Instance.
You can find tests for it in web3swift_Eventloop_Tests.

Also look at Utils/Classes/NonceMiddleware and Contract/Classes/EthereumContract.

And you can listen events in current block in that way:

  • create provider: let web3 = Web3.InfuraMainnetWeb3() //or other net
  • create contract obj: let contract = web3.contract(<contract json string>, at: nil, abiVersion: 2)!
  • create event filter: var filter = EventFilter() and configure it if needed
  • create event parser for chosen event: let eventParser = contract.createEventParser(<event name>, filter: filter)!
  • get current block number: let blockNumber = try web3.eth.getBlockNumber()
  • here your event parser result: let event = try eventParser.parseBlockByNumber(blockNumber)

Please, tell me if you did not find what you were looking for

@barrasso
Copy link
Contributor Author

@BaldyAsh Thanks! This is pretty much exactly what I was looking for. Very glad that this functionality already exists in the library. :)

I'm trying to use these event listeners from within the context of the ENS Registrar contract.

Specifically, I want to get a list of "Recently opened auctions" using an event filter. Let's say I want to get a list of all the auctions that were opened in the past 5000 blocks. Do I simply just filter the event using a previous blockNumber? (i.e. currentBlockNumber - 5000?)

@shamatar
Copy link
Contributor

Filter structure is quire advanced, you can specify block limits. Please refer to this test file, function called testGetIndexedEventsPromise

You can also specify some manual listener function like in an example here

@barrasso
Copy link
Contributor Author

Thank you @shamatar! That test function is exactly what I was looking for 👍🏼

@gitcoinbot
Copy link

Issue Status: 1. Open 2. Cancelled


The funding of 2.962 ETH (249.97 USD @ $84.39/ETH) attached to this issue has been cancelled by the bounty submitter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants