1

I saw this question, but there are contracts like the pcs's router which have no event defined in the contract, see this transaction for example.

How can I decode the logs in such transactions?

alper
  • 8,395
  • 11
  • 63
  • 152
iraj jelodari
  • 113
  • 1
  • 6

2 Answers2

1

You can fetch the logs related to transaction using Eth.get_transaction_receipt(tx_hash) . web3 should be connected into the network you are using, where the transaction is deployed.

>>> web3.eth.get_transaction_receipt('0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060')
AttributeDict({
    'blockHash': '0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd',
    'blockNumber': 46147,
    'contractAddress': None,
    'cumulativeGasUsed': 21000,
    'from': '0xA1E4380A3B1f749673E270229993eE55F35663b4',
    'gasUsed': 21000,
    'logs': [],
    'logsBloom': '0x000000000000000000000000000000000000000000000000...0000',
    'status': 1, # 0 or 1
    'to': '0x5DF9B87991262F6BA471F09758CDE1c0FC1De734',
    'transactionHash': '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',
    'transactionIndex': 0, }) ```

From here you can fetch the logs, which should return transaction Receipt Event Logs corresponding to here.

>>> tx = web3.eth.get_transaction_receipt('0x4e07578a43f627e3a9fb2757cde7f796040868882229a35a68006a0b2aa5e21e')
>>> logs = tx["logs"]
alper
  • 8,395
  • 11
  • 63
  • 152
  • I don't have a problem until here. I'm connected to network. I send a transaction and it will be approved by bsc and I'll get back a reciept. I've got "tx" and "logs". I have no event name in events. – iraj jelodari Nov 18 '21 at 10:29
  • check the contract of pancakeswap router above in the question. there is no event defined in the source code. also in the documents on the pcs website, there is no information about events in the router's contract but functions – iraj jelodari Nov 18 '21 at 10:34
  • 1
    I got it but the pancakeswap function may call other contract's function, I believe they migh have the events that are trigger – alper Nov 18 '21 at 11:24
  • 1
    I do see events in contracts\interfaces\IPancakePair.sol in the source code (https://github.com/pancakeswap/pancake-swap-core/blob/master/contracts/interfaces/IPancakePair.sol) – alper Nov 18 '21 at 11:27
  • thanks man, so I've to figure out why contract.events is not populated with events – iraj jelodari Nov 18 '21 at 20:05
  • @irajjelodari did you finally find a way to parse the logs? I'm looking for the same thing – Jul Feb 01 '22 at 13:54
0

This is solution how to decode data from log, without any adition network requests.

w3 = Web3(provider=Web3.HTTPProvider("your_RPC_provider"))
contract = w3.eth.contract(address=address, abi=contract_abi)
eld = EventLogDecoder(contract)

result = { 'address': '0x45dda9cb7c25131df268515131f647d726f50608', 'topics': ['0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67', '0x0000000000000000000000004c60051384bd2d3c01bfc845cf5f4b44bcbe9de5', '0x000000000000000000000000a1bc0292191192b6f03fc0045a26592ce4719bba'], 'data': '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffff4af79b90000000000000000000000000000000000000000000000000199430ac4f396b900000000000000000000000000000000000060344446a57b653f75b746ecd6e80000000000000000000000000000000000000000000000001db24a4d833c90130000000000000000000000000000000000000000000000000000000000031603', 'blockNumber': '0x25235e9', 'transactionHash': '0x03c147dd2c1e1e65b01628b1adc1e268db7188b7c4de2d38e1964a96f3395073', 'transactionIndex': '0x4e', 'blockHash': '0x57ebe7b3dd2a3c0bf9cc7191bbaeb49f74043f2da81bb98220874909b040b6a0', 'logIndex': '0x142', 'removed': False }

out = eld.decode_log(result)

EventLogDecoder definition is in solution.

Peter Trcka
  • 176
  • 1
  • 4