How to get event log Web3.py?

元气小坏坏 提交于 2020-12-13 03:37:50

问题


I am using solidity 0.7.4, web3.py 5.12.2, and python 3.7.

I am working with Windows 10.

My goal is to emit an event inside a solidity function, in order to retrieve the log after the function is executed.

This is my event event logString(string arg);

And this is how I emit the event emit logString("example string");

On Remix it works, and I am able to retrieve the string I emit, in the log of the transaction.

When I try it on Python, it doesn't work.

This is my Python code:

web3_instance = Web3(HTTPProvider("http://"+host+":"+port, request_kwargs={'timeout': timeout}))
Platform_contract= web3_instance.eth.contract(address=contract_address, abi=abi, bytecode=bytecode)
coinbase= web3_instance.eth.coinbase
functions= Platform_contract.functions

tx_hash =functions.market_clearing(n_clearings, t_clearing_first,supplier_bids,uniform_pricing,discriminative_pricing).transact({'from': coinbase})
tx_receipt = web3_instance.eth.getTransactionReceipt(tx_hash)

log_to_process = tx_receipt['logs'][0]
processed_log = Platform_contract.events.logString().processLog(log_to_process)
log = processed_log['args']['arg']

Unfortunately tx_receipt['logs'] is empty and I get an exception.

Do you know how can I retrieve the log of the event?


回答1:


I managed to solve the problem. I don't know why, I had to exactly specify the network_id in the truffle config.js. Then I migrated the contracts again and ran ganache-cli on that specific network_id.



来源:https://stackoverflow.com/questions/64916187/how-to-get-event-log-web3-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!