Web3.py: How do I decode the transaction's log?

Created on 17 Sep 2018  路  10Comments  路  Source: ethereum/web3.py

  • Web3.py version: 4.4.1
  • Python: 3.7.0
  • OS: linux
  • geth version: 1.8.15-unstable

I can filter my events (please see), but instead of that if I know only the transaction hash and if its already deployed, is it possible to obtain and parse the transaction's log data using Web3.py? Please see the solution for web3.js. Is there any web3.eth.abi.decodeLog function under Web3.py?

For example, from receipt we can obtain the logs.data.

> tx = '0x5c7e74a21419a6ff825aca9b54df1a86599b4b1ee82e60e6410e8d54cbb58b2c'    
> web3.eth.getTransactionReceipt(tx).logs
    [{
        address: "0x611dc53934550684825f3477ecb68029b1b908f3",
        blockHash: "0x356829b068046b6d44147663f8bd6c187e9507c578d9ec9d69e65d7a248ff368",
        blockNumber: 1180103,
        data: "0x0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000002e516d52736142454763717851634a6242784369314c4e39697a3562444147445752364878375a76577167716d64520000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007536369656e636500000000000000000000000000000000000000000000000000",
        logIndex: 0,
        removed: false,
        topics: ["0x711007a4be830d68a6d2a7b6546227b676bf9e7d7a0e3c248552ed72cfec2441", "0x0000000000000000000000004e4a0750350796164d8defc442a712b7557bf282"],
        transactionHash: "0x5c7e74a21419a6ff825aca9b54df1a86599b4b1ee82e60e6410e8d54cbb58b2c",
        transactionIndex: 0
    }]

Example log I have is, where string has a dynamic size. If possible, I want to decode web3.eth.getTransactionReceipt(tx).logs[<index>]['data] and web3.eth.getTransactionReceipt(tx).logs[<index>]['topics].

event LogJob(address indexed clusterAddress,
             string indexed key,
             uint index,
             uint8 fileID,
             string desc,
             string hash
             );

Most helpful comment

I believe you can do it as follows.

>>> receipt = web3.eth.getTransactionReceipt(tx_hash)
>>> logs = my_contract.events.myEvent.processReceipt(receipt)
...

The logs output should be a tuple of the decoded log objects from that receipt. However, I would be ideal to have a processLog(log) method which did this for a single log entry.

All 10 comments

I believe you can do it as follows.

>>> receipt = web3.eth.getTransactionReceipt(tx_hash)
>>> logs = my_contract.events.myEvent.processReceipt(receipt)
...

The logs output should be a tuple of the decoded log objects from that receipt. However, I would be ideal to have a processLog(log) method which did this for a single log entry.

Works like charm. There is a small typo, () should come after myEvent:

my_contract.events.myEvent().processReceipt(receipt)

The function name is always myEvent()

Or it changes as per the functions of your contract because in both the cases I'm getting error that MismatchedABI

I believe you can do it as follows.

>>> receipt = web3.eth.getTransactionReceipt(tx_hash)
>>> logs = my_contract.events.myEvent.processReceipt(receipt)
...

The logs output should be a tuple of the decoded log objects from that receipt. However, I would be ideal to have a processLog(log) method which did this for a single log entry.

@pipermerriam
I am wondering what is my_contract, is this under any library or you define it yourself?

@ShreySatapara @luolixuan my_contract here is just a placeholder and should be replaced by the name of the smart contract deployed by you. The same holds true for myEvent. It is the name of the event in your smart contract that you're interested in.

I have did the same Bt it shows me MISMATCHED ABI

@ShreySatapara can you provide a sample please? It will be easier to see what exactly is going on

@ShreySatapara @luolixuan my_contract here is just a placeholder and should be replaced by the name of the smart contract deployed by you. The same holds true for myEvent. It is the name of the event in your smart contract that you're interested in.

Thanks vioth. If I want to decode the log of a existing block on the chain, what should I do? I know the block number, the transaction hash...

MISMATCHED ABI

Could it be possible that you're trying to decode logs of an event that was not emitted by your smart contract? I'm not sure how processReceipt(receipt) handles errors in such a case.

@luolixuan Do you have the ABI of the smart contract whose logs you're trying to decode?
Btw, You can join the gitter channel of web3.py or you can ask a question on ethereum.stackexchange. stackexchange has more ppl who'll be willing to help you.

@luolixuan take a look at this section of the documentation:

https://web3py.readthedocs.io/en/stable/contracts.html#web3.contract.ContractEvents.myEvent

Was this page helpful?
0 / 5 - 0 ratings