Events
使用 eth-abi 解碼契約日誌的“數據”欄位引發錯誤:“填充字節不為空:b'0x0000000000’”
我使用本指南創建了下面的程式碼。我懷疑這個錯誤與我對字元串所做的編碼過程有些相關,因為這是我唯一偏離指南的地方。這是我解碼合約日誌數據欄位的程式碼:
from sha3 import keccak_256 import json import urllib.request import ssl import eth_abi def format_raw_data(logs,event_name): abi = json.loads(json.loads(open("./abi.json").read())['result']) types = [] names = [] indexed_types = [] indexed_names = [] for elem in abi: if 'name' in elem and elem['name'] == event_name: for input in elem['inputs']: if input['indexed']: indexed_types.append(input["type"]) indexed_names.append(input["name"]) else: types.append(input["type"]) names.append(input["name"]) break for log in logs: encoded_topics = list(map(lambda x: str.encode(x), log['topics'][1:])) indexed_values = [eth_abi.decode_single(t, v) for t, v in zip(indexed_types, encoded_topics)] #error occurs here values = eth_abi.decode_abi(types,log['data'].encode()) print("Data: ", dict(zip(names,values)))
拋出的錯誤是“eth_abi.exceptions.NonEmptyPaddingBytes:填充字節不為空:b'0x0000000000’”一旦將 abi 下載到同一目錄中,該錯誤應該是可重現的。
非常感謝
您正在傳遞一個需要字節的十六進製字元串。該程式碼進行了一些小的更改:
from eth_utils import decode_hex ... for log in logs: encoded_topics = [decode_hex(topic) for topic in log['topics'][1:]] indexed_values = [eth_abi.decode_single(t, v) for t, v in zip(indexed_types, encoded_topics)] #error occurs here values = eth_abi.decode_abi(types, decode_hex(log['data'])) print("Data: ", dict(zip(names, values)))