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?
Best Answer
There are a variety of ways to keep your runtime alive after your script completes. A couple options are:
python -i my_script.py
while True: time.sleep(10)
to your script and just Ctrl + C to exit