跳转到主要内容

Ethereum事件解码器和主题生成器

项目描述

eth-event

Pypi Status Build Status Coverage Status

用于Ethereum事件解码和主题生成的工具。

安装

您可以通过pip安装最新版本

pip install eth-event

或者克隆存储库并使用setuptools获取最新版本

git clone https://github.com/iamdefinitelyahuman/eth-event.git
cd eth-event
python3 setup.py install

用法

公共API在docstrings中有很好的文档记录。以下示例可能也有帮助

>>> from eth_event import get_topic_map

# generating a topic map
>>> abi = open('abi.json').read()
>>> topic_map = get_topic_map(abi)
>>> topic_map
{
    '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef': {
        'name': 'Transfer',
        'inputs': [
            {'name': 'from', 'type': 'address', 'indexed': True},
            {'name': 'to', 'type': 'address', 'indexed': True},
            {'name': 'value', 'type': 'uint256', 'indexed': False}
        ]
    }
}

# decoding event logs from a transaction receipt
>>> tx = token.transfer(account[1], 100, {'from': account[0]})
<Transaction object '0x615a157e84715d5f960a38fe2a3ddb566c8393cfc71f15b06170a0eff74dfdde'>
>>> eth_event.decode_logs(tx.logs, topic_map)
[{
    'name': 'Transfer',
    'address': "0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87",
    'data': [
        {'name': 'from', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a', 'decoded': True},
        {'name': 'to', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a', 'decoded': True},
        {'name': 'tokens', 'type': 'uint256', 'value': 100, 'decoded': True}
    ],
    'logIndex': 0,
    'blockNumber': 0,
    'transactionIndex': 0
}]

# decoding a structLog from Geth's debug_traceTransaction endpoint
>>> trace = web3.provider.make_request(
    "debug_traceTransaction",
    ['0x615a157e84715d5f960a38fe2a3ddb566c8393cfc71f15b06170a0eff74dfdde', {}]
)
>>> struct_log = trace['result']['structLogs']

>>> eth_event.decode_trace(struct_log, topic_map, initial_address="0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87")
[{
    'name': 'Transfer',
    'address': "0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87",
    'data': [
        {'name': 'from', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a', 'decoded': True},
        {'name': 'to', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a', 'decoded': True},
        {'name': 'tokens', 'type': 'uint256', 'value': 100, 'decoded': True}
    ],
}]

限制

  • 如果数组在事件中被索引,则主题将生成一个sha3哈希,因此无法解码。在这种情况下,将返回未解码的主题,并将decoded设置为False

  • 匿名事件无法解码。当调用decode_logsdecode_trace时,使用allow_undecoded关键字参数接收未解码的日志,而不会引发异常。

  • 在解码跟踪时,无法确定调用初始地址。要包括已解码事件发出地址,必须使用initial_address关键字参数提供初始地址。

测试

要运行测试套件

$ tox

开发

此项目仍在开发中。欢迎评论、问题、批评和拉取请求。

许可证

此项目根据MIT许可证授权。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

eth-event-1.2.5.tar.gz (10.1 kB 查看哈希值)

上传时间

构建分布

eth_event-1.2.5-py3-none-any.whl (7.5 kB 查看哈希值)

上传时间 Python 3

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面