2

I am trying to figure out how to use Web3 filters, as I need to sync a backend with my blockchain.

Ideally, I want a function to trigger every time a transaction takes place.

I have it working fine based on the examples here, however this only works in the interactive shell. If run in a Python script, it just executes synchronously and terminates immediately. Here is the code:

import sys

from web3 import Web3, IPCProvider

from web3.utils.filters import TransactionFilter

w3 = Web3(IPCProvider('/x/x/x/ethereum/geth.ipc'))

def new_transaction_callback(transaction_hash):
    sys.stdout.write("New Block: {0}".format(transaction_hash))

new_transaction_filter = w3.eth.filter('pending')
new_transaction_filter.watch(new_transaction_callback)

What is the correct way to wait indefinitely, and execute the callback whenever a transaction takes place?

  • FYI, anything in web3.utils.* is not meant as a public API, and could change at any time. – carver Mar 21 '18 at 17:28
1

If run in a Python script, it just executes synchronously and terminates immediately.

There are a variety of ways to keep your runtime alive after your script completes. A couple options are:

  • Invoke your script with python -i my_script.py
  • Append while True: time.sleep(10) to your script and just Ctrl + C to exit
  • many more...

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.