如何使用 web3 py 从 BSC 交易收据的特定部分解码十六进制?

发布于 2025-01-12 19:55:03 字数 789 浏览 3 评论 0原文

我正在使用 web3 包编写 python 脚本。

流程说明:

  1. 我有一笔交易,我阅读了交易收据

    txn_receipt = w3.eth.getTransactionReceipt('0x8ddd5ab8f53df7365a2feb8ee249ca2d317edcdcb6f40faae728a3cb946b4eb1')

  2. 仅在这个示例中,我读取了日志的特定部分。这将返回一个十六进制值。

    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:

  1. I have a transaction, which I read the transaction receipt for

    txn_receipt = w3.eth.getTransactionReceipt('0x8ddd5ab8f53df7365a2feb8ee249ca2d317edcdcb6f40faae728a3cb946b4eb1')

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燃情 2025-01-19 19:55:03

假设您不需要键名称,您可以使用基本 python 来完成此操作(不需要 web3 库)。

BSC 日志中的数据字段是一串十六进制编码值,它只是十进制值的基数 16 表示。在您的示例中:

0x00000000000000000000000000000000000000000000008076b6fbd0ebb5bd3c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000047b025e26b62ed08f

只需修剪开头,将其拆分,然后使用 python 的 int() 函数转换每个字符串:

hexdata = '0x00000000000000000000000000000000000000000000008076b6fbd0ebb5bd3c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000047b025e26b62ed08f'

# Trim '0x' from beginning of string
hexdataTrimed = hexdata[2:]

# Split trimmed string every 64 characters
n = 64
dataSplit = [hexdataTrimed[i:i+n] for i in range(0, len(hexdataTrimed), n)]

# Fill new list with converted decimal values
data = []
for val in range(len(dataSplit)):
    toDec = int(dataSplit[val], 16)
    data.append(toDec)

print(data) 
# returns [2369737542851785768252, 0, 0, 82650726831815053455]

来源:

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:

0x00000000000000000000000000000000000000000000008076b6fbd0ebb5bd3c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000047b025e26b62ed08f

Just trim the beginning, split it up, and convert each string with python's int() function:

hexdata = '0x00000000000000000000000000000000000000000000008076b6fbd0ebb5bd3c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000047b025e26b62ed08f'

# Trim '0x' from beginning of string
hexdataTrimed = hexdata[2:]

# Split trimmed string every 64 characters
n = 64
dataSplit = [hexdataTrimed[i:i+n] for i in range(0, len(hexdataTrimed), n)]

# Fill new list with converted decimal values
data = []
for val in range(len(dataSplit)):
    toDec = int(dataSplit[val], 16)
    data.append(toDec)

print(data) 
# returns [2369737542851785768252, 0, 0, 82650726831815053455]

Sources:

https://www.binaryhexconverter.com/hex-to-decimal-converter
https://www.w3schools.com/python/ref_func_int.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文