如何获取事件日志 Web3.py? [英] How to get event log Web3.py?

查看:56
本文介绍了如何获取事件日志 Web3.py?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用solidity 0.7.4、web3.py 5.12.2和python 3.7.

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

我正在使用 Windows 10.

I am working with Windows 10.

我的目标是在solidity函数内发出一个事件,以便在函数执行后检索日志.

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

这是我的事件 event logString(string arg);

这就是我发出事件的方式 emit logString("example string");

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

在 Remix 上它可以工作,并且我能够在事务日志中检索我发出的字符串.

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

当我在 Python 上尝试时,它不起作用.

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

这是我的 Python 代码:

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']

不幸的是,tx_receipt['logs'] 是空的,我得到一个异常.

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

你知道我如何检索事件日志吗?

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

推荐答案

我设法解决了这个问题.我不知道为什么,我必须在 truffle config.js 中准确指定 network_id.然后我再次迁移合约并在该特定 network_id 上运行 ganache-cli.

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.

这篇关于如何获取事件日志 Web3.py?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆