event Transfer:
sender: indexed(address)
receiver: indexed(address)
value: uint256
log Transfer(msg.sender, _to, _amount)
abi = # 编译器生成的abi
address = # 需要监听的合约地址
my_contract = web3.eth.contract(address=address, abi=abi)
event_filter = my_contract.events.Transfer.createFilter(fromBlock='latest')
event = event_filter.get_new_entries():
# 获取到的事件输出类似这样
[
AttributeDict(
{'args': AttributeDict(
{
'from': '0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf',
'to': '0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf',
'value': 10}),
'event': 'Transfer',
'logIndex': 0,
'transactionIndex': 0,
'transactionHash': HexBytes('...'),
'address': '0xF2E246BB76DF876Cef8b38ae84130F4F55De395b',
'blockHash': HexBytes('...'),
'blockNumber': 3
}
)
]