[zksync2==0.6.0] <use python sdk to listen contract event error> #80
-
EnvironmentTestnet Acknowledgement
Issue DescriptionValueError: {'code': -32602, 'message': 'Filter not found'} Expected Behaviorcontract event list Code Exampleaddress = '0x2E5f01AEB81924014d225F62ab611FD544E0c636' Repo Link (Optional)No response |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 9 replies
-
Hey @rnsKelvin Can you provide more details on your setup, ideally a minimal example that I can use to reproduce the issue? |
Beta Was this translation helpful? Give feedback.
-
I am using python to listen for contract events without any issues. But I'm not using the zkSync python SDK, I simply use web3.py. |
Beta Was this translation helpful? Give feedback.
-
Here is the code that I use, (simplified as much as possible but not tested) transfer_event_signature_hash = web3.keccak(
text="Transfer(address,address,uint256)"
).hex()
filter_dict = {
'address': [ CONTRACT_ADDRESS ],
'topics': [[
transfer_event_signature_hash,
]],
'fromBlock': 0,
'toBlock': 2048,
}
filter_obj = web3.eth.filter(filter_dict)
for event in web3.eth.get_filter_changes(filter_obj.filter_id):
print(f"Event: {event}")
# ...do something with the event obj... |
Beta Was this translation helpful? Give feedback.
-
I think you need to add
Here are the docs: I didn't include it in my initial code post, because I wasn't sure if it was required, but it seems that it is. |
Beta Was this translation helpful? Give feedback.
-
The solution that @equalogix proposed works on zkSync Era correctly because that part is compatible with zk_web3 = ZkSyncBuilder.build("https://zksync2-testnet.zksync.dev")
zk_web3.middleware_onion.inject(geth_poa_middleware, layer=0)
zk_web3.middleware_onion.add(local_filter_middleware)
filter_dict = {
"address": [zk_web3.to_checksum_address(L2_ETH_TOKEN_ADDRESS)],
"topics": [
zk_web3.keccak(text="Transfer(address,address,uint256)").hex(),
],
}
filter_obj = zk_web3.zksync.filter(filter_dict)
for i in range(5):
time.sleep(10) # wait for node to filter result
# every time you call get_filter_changes() with the same filter ID,
# it will give you events that occurred after the last event you
# fetched using that filter.
print(f"Events fetch in interation: {i}")
events = zk_web3.zksync.get_filter_changes(filter_obj.filter_id)
for event in events:
print(f"Event: {event}")
# When you're finished with the filtering, make sure to close the filter on the node
# in order to conserve computational resources.
zk_web3.zksync.uninstall_filter(filter_obj.filter_id) Results:
In some iterations, there is no result because the node did not have enough time to prepare them. |
Beta Was this translation helpful? Give feedback.
The solution that @equalogix proposed works on zkSync Era correctly because that part is compatible with
Web3.py
.zksync2
SDK can be used in the same way. Here is the example on how to filterTransfer
event onETH_TOKEN
smart contract on L2.