获取Web3py中具有数据的事件列表



我有一个工作代码,但定期接收数据后,终端给出一个错误:UserWarning:事务哈希值:HexBytes('0xa5dadecd01e175d70caaa7fb8e872bb68b45601fd714294b2c1368b8cff077d8')和logIndex: 73的日志在处理过程中遇到以下错误:MismatchedABI(事件签名与提供的ABI不匹配)。它已经被丢弃了。warnings.warn (接下来,出现数据,然后是(),在再次接收一系列正常数据之后。你怎样才能摆脱这个?我正在使用最新的代理合约ABI 0x84db6eE82b7Cf3b47E8F19270abdE5718B936670。

我还想从每个事件中提取数据:"以太"中的利害关系者,时间和数量,并尝试解决限制为10000块的过滤问题,可能需要一个循环,因为我想从部署合同的块中获得所有事件的列表

# import the following dependencies
import json
from web3 import Web3
from web3.auto import w3
import time

# add your blockchain connection information
#connect to infura eth chain http end point
infura_url='https://mainnet.infura.io/v3/e212e98e02a74730a1c6382e7ae6f95d'
w3 = Web3(Web3.HTTPProvider(infura_url))
#connection status
w3.isConnected()
# ankr pool address and abi
ankr_pool = '0x84db6eE82b7Cf3b47E8F19270abdE5718B936670'
ankr_pool_abi = json.loads('[ABI this contract 0xD201a7dF1D0F7E066EFdD448CBc8433F0b88C3e9]')
contract = w3.eth.contract(address=ankr_pool, abi=ankr_pool_abi)
greeting_Event = contract.events.StakeConfirmed() # Modification
def handle_event(event):
receipt = w3.eth.waitForTransactionReceipt(event['transactionHash'])
result = greeting_Event.processReceipt(receipt) # Modification
print(result)
def log_loop(event_filter, poll_interval):
while True:
for event in event_filter.get_all_entries():
handle_event(event)
time.sleep(poll_interval)
block_filter = w3.eth.filter({'fromBlock': 16128812 , 'address': ankr_pool})
log_loop(block_filter, 2)

我尝试使用日志日志,但我遇到了数据解码问题,我相信使用事件过滤器仍然更容易

出现MismatchedABI警告可能是因为您在部署合约后对函数进行了一些更改,并且在不知情的情况下使用了合约的ABI。最好仔细检查一下。尽管这样的警告是无害的,并且可以通过使用warnings库来禁用:

import warnings
def my_function():
# silence harmless warnings
warnings.filterwarnings("ignore") 

要获取事件日志,您可以尝试以下操作:

import Web3
w3 = Web3(Web3.HTTPProvider(RPC_URL))
while from_block < target_block:
contract = w3.eth.contract(address=Web3.toChecksumAddress(CONTRACT_ADDRESS), abi=ABI)
functionEvents = contract.events.MyEvent.getLogs(fromBlock=from_block, toBlock=toBlock)
# < Your desired logic >
from_block = from_block + 10000 + 1

然后可以在while循环中使用以下代码来解码事件日志:

txHash = myEvent.transactionHash.hex()
txInputReceipt = w3.eth.get_transaction_receipt(txHash)
logs = contract.events.MyEvent().processReceipt(txInputReceipt)

或者直接用:txInputReceipt.logs

这只是伪代码,希望你能明白。查看Web3Py文档获取更多有用的函数:https://web3py.readthedocs.io/en/v5/contracts.html?highlight=processreceipt#web3.contract.ContractEvents

最新更新