0

Here's a link to the docs for reference:

I have the following code:

txhash = "{hash}"
tx_receipt = w3.eth.get_transaction_receipt(txhash)
processed_logs = w3.eth.contract(address="{address}", abi=contract_abi).events[event_name]().process_receipt(tx_receipt)

If I just look at the log, the encoded data looks like:

AttributeDict({'address': '0x6f5dfB0eaF832fF0D4204EC85184b634C7946457', 'blockHash': HexBytes('0x2657856d1ce53ab9ff04e08d13029082b3e8f403e1076222cefee8ed5287593d'), 'blockNumber': 4133628, 'data': HexBytes('0x000000000000000000000000471bb2d2f650f84e138852de8d9dd9dbc69bf0f300000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000960000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000007233641343545430000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008b53c673e3c706174682066696c6c3d27236666662720643d274d39342e353238203434362e383538682d362e39303576322e39313268322e3635387631352e313431682d332e34323476322e3931316831302e313939762d322e393131682d322e353238762d31382e3035335a4d3130372e343736203434362e383538682d362e39303476322e39313268322e3635377631352e313431682d332e34323376322e3931316831302e313939762d322e393131682d322e353239762d31382e3035335a4d3131372e303136203435372e343432632e3134342d312e3520312e31372d322e3320332e3031392d322e3320312e383439203020322e3737342e3820322e37373420322e353333762e3432326c2d352e3038352e363236632d332e3031392e3337392d352e33383820312e3730332d352e33383820342e363838203020322e393720322e32333920342e36373320352e36333320342e36373320322e383436203020342e3130332d312e30393220342e3639352d322e303234682e32383976312e37363268332e393538762d392e353563302d342e3136342d322e3635382d362e3034322d362e3738392d362e3034322d342e31363120302d362e37303320312e3837382d372e30303620342e373735762e34333768332e395a6d2d2e35303620352e373863302d312e3135312e3836372d312e37303420322e3132342d312e3837396c342e3137352d2e353533762e363536633020322e34362d312e36313820332e3639372d332e38343320332e3639372d312e353620302d322e3435362d2e3735372d322e3435362d312e3932315a4d3133302e313236203436372e38323268332e39762d392e38313263302d312e3837382e3938322d322e36393420322e3335352d322e36393420312e323238203020312e3833342e37313420312e38333420312e3932327631302e35383468332e393031762d392e38313263302d312e3837382e3938322d322e36393420322e33342d322e36393420312e323432203020312e3833352e37313420312e38333520312e3932327631302e35383468332e393134762d31312e31323363302d322e3736362d312e3733332d342e3431312d342e3337372d342e3431312d322e33363920302d332e34363720312e3033342d342e31303220322e313639682d2e323839632d2e3433342d2e39392d312e3530332d322e3136392d332e3639382d322e3136392d322e31353320302d332e3039312e3930332d332e36313220312e3832682d2e323839762d312e353239682d332e3731327631352e3234335a4d3135372e313035203435372e343432632e3134352d312e3520312e31372d322e3320332e3031392d322e3320312e383439203020322e3737342e3820322e37373420322e353333762e3432326c2d352e3038352e363236632d332e30322e3337392d352e33383820312e3730332d352e33383820342e363838203020322e393720322e32333920342e36373320352e36333320342e36373320322e383436203020342e3130332d312e30393220342e3639352d322e303234682e32383976312e37363248313637762d392e353563302d342e3136342d322e3635382d362e3034322d362e3738392d362e3034322d342e31363120302d362e37303320312e3837382d372e30303620342e373735762e34333768332e395a6d2d2e35303620352e373863302d312e3135312e3836372d312e37303420322e3132342d312e3837396c342e3137352d2e353533762e363536633020322e34362d312e36313820332e3639372d332e38343320332e3639372d312e353620302d322e3435362d2e3735372d322e3435362d312e3932315a272f3e3c672066696c6c3d2723364134354543273e3c7061746820643d274d33362e393536203435382e32343961312e333720312e3337203020302031202e33392d2e393432632e3234352d2e3235312e3537372d2e3339372e3932372d2e3430364835352e3861362e32363220362e32363220302030203020342e3435382d312e38353520362e33353220362e33353220302030203020312e3336392d322e303620362e34303620362e343036203020302030202e3438322d322e34333356343238682d342e39367632322e35353361312e33373120312e3337312030203020312d2e3338392e39343220312e33353120312e3335312030203020312d2e3932372e3430374833382e323961362e32363320362e3236332030203020302d342e34343520312e38363141362e33383320362e333833203020302030203332203435382e3234397631352e35363868342e393536762d31352e3536385a4d36342e333034203433322e323938682d2e32326c2e32312e32313376342e363568342e3536316c322e313420322e31373961362e32383620362e3238362030203020302d332e36313720322e32303520362e33393420362e3339342030203020302d312e34323220342e3031367632382e32363268342e393336762d32382e323639632e3030352d2e3335362e3134382d2e3639362e3339372d2e3934382e32352d2e3235322e3538372d2e3339352e39342d2e34682e373663322e393132203020332e392d312e36333820342e3139322d322e3332352e3239332d2e3638382e3734342d322e3536352d312e3331362d342e3635316c2d342e3833312d342e3931354836392e35334d35312e313933203437312e33363263312e303833203020322e3135342e32313620332e3135332e36333861382e31353320382e31353320302030203120322e36363520312e38313768366131332e3232342031332e3232342030203020302d342e38352d352e3433382031332e3035392031332e3035392030203020302d362e3936382d322e303137632d322e34363420302d342e3837392e3639392d362e39363820322e3031376131332e3232352031332e3232352030203020302d342e383520352e343338683641382e31353420382e3135342030203020312034382e3033382034373261382e31303120382e31303120302030203120332e3135342d2e3633385a272f3e3c2f673e3c2f673e0000000000000000000000'), 'logIndex': 36, 'removed': False, 'topics': [HexBytes('0xd85d4fdd7876f7e7ce4ebfe722811abb3513522a11add221d40de3baba0f3e2d'), HexBytes('0x000000000000000000000000f2c61e275d48efa8a6556529f60ce1e376510e0f')], 'transactionHash': HexBytes('0x25a57f565a2a8bb999d3344d9dc5ad305cc38263d253ae2a530d49542f5edb0b'), 'transactionIndex': 2})

Where data is HexBytes and it looks to be encoded, however, when I run process_receipt, it is doing some decoding and I end up with the following log:

(AttributeDict({'args': AttributeDict({'policyMetadataLogic': '0xf2C61E275d48efA8a6556529F60cE1E376510e0F', 'policyMetadata': '0x471BB2d2f650F84E138852De8D9DD9dbC69bF0F3', 'initializationData': b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07#6A45EC\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\xb5<g><path fill='#fff' d='M94.528 446.858h-6.905v2.912h2.658v15.141h-3.424v2.911h10.199v-2.911h-2.528v-18.053ZM107.476 446.858h-6.904v2.912h2.657v15.141h-3.423v2.911h10.199v-2.911h-2.529v-18.053ZM117.016 457.442c.144-1.5 1.17-2.3 3.019-2.3 1.849 0 2.774.8 2.774 2.533v.422l-5.085.626c-3.019.379-5.388 1.703-5.388 4.688 0 2.97 2.239 4.673 5.633 4.673 2.846 0 4.103-1.092 4.695-2.024h.289v1.762h3.958v-9.55c0-4.164-2.658-6.042-6.789-6.042-4.161 0-6.703 1.878-7.006 4.775v.437h3.9Zm-.506 5.78c0-1.151.867-1.704 2.124-1.879l4.175-.553v.656c0 2.46-1.618 3.697-3.843 3.697-1.56 0-2.456-.757-2.456-1.921ZM130.126 467.822h3.9v-9.812c0-1.878.982-2.694 2.355-2.694 1.228 0 1.834.714 1.834 1.922v10.584h3.901v-9.812c0-1.878.982-2.694 2.34-2.694 1.242 0 1.835.714 1.835 1.922v10.584h3.914v-11.123c0-2.766-1.733-4.411-4.377-4.411-2.369 0-3.467 1.034-4.102 2.169h-.289c-.434-.99-1.503-2.169-3.698-2.169-2.153 0-3.091.903-3.612 1.82h-.289v-1.529h-3.712v15.243ZM157.105 457.442c.145-1.5 1.17-2.3 3.019-2.3 1.849 0 2.774.8 2.774 2.533v.422l-5.085.626c-3.02.379-5.388 1.703-5.388 4.688 0 2.97 2.239 4.673 5.633 4.673 2.846 0 4.103-1.092 4.695-2.024h.289v1.762H167v-9.55c0-4.164-2.658-6.042-6.789-6.042-4.161 0-6.703 1.878-7.006 4.775v.437h3.9Zm-.506 5.78c0-1.151.867-1.704 2.124-1.879l4.175-.553v.656c0 2.46-1.618 3.697-3.843 3.697-1.56 0-2.456-.757-2.456-1.921Z'/><g fill='#6A45EC'><path d='M36.956 458.249a1.37 1.37 0 0 1 .39-.942c.245-.251.577-.397.927-.406H55.8a6.262 6.262 0 0 0 4.458-1.855 6.352 6.352 0 0 0 1.369-2.06 6.406 6.406 0 0 0 .482-2.433V428h-4.96v22.553a1.371 1.371 0 0 1-.389.942 1.351 1.351 0 0 1-.927.407H38.29a6.263 6.263 0 0 0-4.445 1.861A6.383 6.383 0 0 0 32 458.249v15.568h4.956v-15.568ZM64.304 432.298h-.22l.21.213v4.65h4.561l2.14 2.179a6.286 6.286 0 0 0-3.617 2.205 6.394 6.394 0 0 0-1.422 4.016v28.262h4.936v-28.269c.005-.356.148-.696.397-.948.25-.252.587-.395.94-.4h.76c2.912 0 3.9-1.638 4.192-2.325.293-.688.744-2.565-1.316-4.651l-4.831-4.915H69.53M51.193 471.362c1.083 0 2.154.216 3.153.638a8.153 8.153 0 0 1 2.665 1.817h6a13.224 13.224 0 0 0-4.85-5.438 13.059 13.059 0 0 0-6.968-2.017c-2.464 0-4.879.699-6.968 2.017a13.225 13.225 0 0 0-4.85 5.438h6A8.154 8.154 0 0 1 48.038 472a8.101 8.101 0 0 1 3.154-.638Z'/></g></g>\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"}), 'event': 'PolicyMetadataSet', 'logIndex': 36, 'transactionIndex': 2, 'transactionHash': HexBytes('0x25a57f565a2a8bb999d3344d9dc5ad305cc38263d253ae2a530d49542f5edb0b'), 'address': '0x6f5dfB0eaF832fF0D4204EC85184b634C7946457', 'blockHash': HexBytes('0x2657856d1ce53ab9ff04e08d13029082b3e8f403e1076222cefee8ed5287593d'), 'blockNumber': 4133628}),)

Here, initializationData seems to have been decoded rather than just giving me the full bytecode like I would expect.

For example: I see

x00\x00\x00\x00\x07#6A45EC\x00\x00\x00\x00...

With the "color" property decoded. I think that's how it's supposed to work but it gives me the wrong number of bytes.

This works for other events in my code so I am not sure what's happening with this one, could it be a bug with web3.py or something I'm missing?

Here's the event for reference:

https://sepolia.etherscan.io/address/0x6f5dfB0eaF832fF0D4204EC85184b634C7946457#events

Thank you

EDIT:

Adding more context on what I'm trying to accomplish:

This is troublesome because I am using web3.eth_abi and later in my code I am trying to decode:

decode('(string,string)' ,logo["initializationData"]), and then this fails with the error:

InsufficientDataBytes: Tried to read 514314436127049056 bytes, only got 2297 bytes

I tried the answer by Ake below which is to do some modifications:

for log in logs:
        if 'initializationData' in log[0]['args']:
            sanitized_log = dict(log[0])
            sanitized_log['args'] = dict(sanitized_log['args'])
            sanitized_log['args']['initializationData'] = w3.to_hex(sanitized_log['args']['initializationData'])
            sanitized_logs.append((sanitized_log,))
        else:
            sanitized_logs.append((log,))

However, if I do that, web3.eth_abi then complains

TypeError: The datavalue must be of bytes type. Got <class 'str'>

If I do bytes.fromhex() then I run into the old problem again of it being decoded. Could it be that SVG vectors (or maybe XML tags?) get decoded automatically by Python? Is there a workaround?

Here's an example of an event that I'm able to decode correctly because of how it turns up after process_receipt:

AttributeDict({'args': AttributeDict({'accountLogic': '0x915Af6753f03D2687Fa923b2987625e21e2991aE', 'account': '0xf147B323B00bE213FBB4F5cC148FD611a02D8845', 'initializationData': b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0eLlama Treasury\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'}), 'event': 'AccountCreated', 'logIndex': 38, 'transactionIndex': 2, 'transactionHash': HexBytes('0xacff465726f30a96f007287ceebe5d4eb7c170dc8c24579aa685ec4161cbedac'), 'address': '0x688576dC6b1AbaEe1d7Ee2B7c41Ee5D81BFD293c', 'blockHash': HexBytes('0x8a2a6f5248aa047d7e9fd20061fe3ff6c247555bde04916692bfbc063636d77d'), 'blockNumber': 4121274})

I also tried (decode(...., strict=False) but that didn't seem to be picked up.

EDIT 2:

After working on this for a while, I'm convinced, decode() with strict=False should be what does the trick but doesn't seem to be working for some reason. The code is still going to check and I get InsufficientDataBytes error when it should be skipped:

https://github.com/ethereum/eth-abi/blob/master/eth_abi/decoding.py#L517

1 Answer 1

0

I wouldn't call it a bug in web3.py, this is more of the default Python behavior to output bytes.

Checking your code, initializationData is bytes, so that's how Python prints it using the \x notation.

One way to fix this would be to use the Web3.to_hex helper and just loop through the log.

Example:

from web3 import Web3
import json

node_endpoint = "NODE_ENDPOINT"
w3 = Web3(Web3.HTTPProvider(node_endpoint))

txhash = "0x25a57f565a2a8bb999d3344d9dc5ad305cc38263d253ae2a530d49542f5edb0b"

tx_receipt = w3.eth.get_transaction_receipt(txhash)

with open('CONTRACT_ABI.json', 'r') as abi_file:
    contract_abi = json.load(abi_file)

contract_address = "0x6f5dfB0eaF832fF0D4204EC85184b634C7946457"
event_name = "PolicyMetadataSet"

contract = w3.eth.contract(address=contract_address, abi=contract_abi)
processed_logs = contract.events[event_name]().process_receipt(tx_receipt)

# convert initializationData to hex
modified_logs = []
for log in processed_logs:
    if 'initializationData' in log['args']:
        modified_log = dict(log)
        modified_log['args'] = dict(modified_log['args'])
        modified_log['args']['initializationData'] = w3.to_hex(modified_log['args']['initializationData'])
        modified_logs.append(modified_log)
    else:
        modified_logs.append(log)

print(modified_logs)
1
  • hey @Ake thank you for the prompt response. I've edited my question to add more context. I tried this but I run into other problems when I convert it to a hex string before I send to web3.eth_abi.decode() as it expects bytes rather than a string. Sep 29, 2023 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.