1

I am currently building a dapp, it has a backend that should listen to the smart contract events to check if a transaction happened or not.

To my knowledge web3.js provides a way to subscribe to events with websocket providers like infura through such method:

myContract.events.MyEvent({ filter: {myIndexedParam: [20,23], myOtherIndexedParam: '0x123456789...'}, // Using an array means OR: e.g. 20 or 23 fromBlock: 0 }, function(error, event){ console.log(event); }) .on("connected", function(subscriptionId){ console.log(subscriptionId); }) .on('data', function(event){ console.log(event); // same results as the optional callback above }) .on('changed', function(event){ // remove event from local database }) .on('error', function(error, receipt) { // If the transaction was rejected by the network with a receipt, the second parameter will be the receipt. ... }); 

how can i achieve the same thing with web3.py since my backend is built with django?

currently I constantly polling the infura node every second like this:

def handle_event(event): print(event) # and whatever def log_loop(event_filter, poll_interval): while True: for event in event_filter.get_new_entries(): handle_event(event) time.sleep(poll_interval) def main(): block_filter = w3.eth.filter('latest') worker = Thread(target=log_loop, args=(block_filter, 5), daemon=True) worker.start() # .. do some other stuff if __name__ == '__main__': main() 
1

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.