如何使用 web3 py 从 BSC 交易收据的特定部分解码十六进制?
我正在使用 web3 包编写 python 脚本。
流程说明:
我有一笔交易,我阅读了交易收据
txn_receipt = w3.eth.getTransactionReceipt('0x8ddd5ab8f53df7365a2feb8ee249ca2d317edcdcb6f40faae728a3cb946b4eb1')
仅在这个示例中,我读取了日志的特定部分。这将返回一个十六进制值。
x = txn_receipt['日志'][4]['数据']
问题: 我如何解码这个十六进制?如果您进入 BSC SCAN,您将在块 453 处看到我期望的解码值。
预期值:
amount0In :
2369737542851785768252
amount1In :
0
amount0Out :
0
amount1Out :
82650726831815053455
请参见此处: https://bscscan.com/tx/0x8ddd5ab8f53df7365a2feb8ee249ca2d317edcdcb6f40faae728a3cb946b4eb1#eventlog
I am writing a python script using web3 package.
The process explained:
I have a transaction, which I read the transaction receipt for
txn_receipt = w3.eth.getTransactionReceipt('0x8ddd5ab8f53df7365a2feb8ee249ca2d317edcdcb6f40faae728a3cb946b4eb1')
Just for this example, I read a specific section of the log. This returns a hex.
x = txn_receipt['logs'][4]['data']
PROBLEM:
How do I decode this hex? If you go to BSC SCAN, you will see the decoded value I am expecting at block 453.
Expected value:
amount0In :
2369737542851785768252
amount1In :
0
amount0Out :
0
amount1Out :
82650726831815053455
See here:
https://bscscan.com/tx/0x8ddd5ab8f53df7365a2feb8ee249ca2d317edcdcb6f40faae728a3cb946b4eb1#eventlog
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您不需要键名称,您可以使用基本 python 来完成此操作(不需要 web3 库)。
BSC 日志中的数据字段是一串十六进制编码值,它只是十进制值的基数 16 表示。在您的示例中:
只需修剪开头,将其拆分,然后使用 python 的
int()
函数转换每个字符串:来源:
https://www.binaryhexconverter.com/hex-to-decimal-converter
https://www.w3schools.com/python/ref_func_int.asp
Assuming you don't need the key names, you could do this with basic python (no need for web3 library).
The data field in BSC logs is a string of hex encoded values, which is just a base 16 representation of the decimal value. In your example:
Just trim the beginning, split it up, and convert each string with python's
int()
function:Sources:
https://www.binaryhexconverter.com/hex-to-decimal-converter
https://www.w3schools.com/python/ref_func_int.asp