2

I found this repo, which retrieves recent gas prices using web3:

https://github.com/ethgasstation/gasstation-express-oracle/blob/master/gasExpress.py

I'd like to write a similar script that would stay running and get a callback every time a new block occurred with all the transactions in the block, and keep a running average of gas prices rather than invoke all these calls every time we need to check.

Seems like there is a websockets server option in the web3 library. I'm struggling with how to test this. Does testrpc allow me to observe block changes automatically? If so, how?

  • 1
    Because blocks are created relatively infrequently (about every 15 seconds), simple polling is a good way to go. web3.eth.getBlockNumber() tells you the latest block number, and if it's one you haven't seen before, you can then process the block. I recommend this just because websocket support isn't great yet. For example, neither ganache-cli (formerly testrpc) nor Infura support it so far. – user19510 Jan 9 '18 at 1:14
2

Polling for changes with a filter is the only standard universally supported across nodes. In this case, you would create a new block filter, and poll your node from time to time. Whenever new block(s) come in, you would get the list of new blocks in response. In web3.py, that might look like:

block_filter = web3.eth.filter('latest')

# get list of new blocks:
latest_blocks = web3.eth.getFilterChanges(block_filter.filter_id)

# wait a few seconds, and then again:
latest_blocks = web3.eth.getFilterChanges(block_filter.filter_id)

There are node-specific implementations that have a pub/sub style model, like parity's parity_subscription. Using that, you can "subscribe" to changes in the latest block, and have new data pushed to you when it appears. But it's non-standard, so support in the web3* flavors is weak/non-existent. You would have to manage low-level interaction with the json-rpc interface.

IMO, it's not worth customizing, since polling doesn't lose you much time against 15s block times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.