1

On etherscan.io, when looking at the Events tab, we can see the logs, and also the method name. Using web3js, I can get the logs without problem (with getPastEvents). I cannot figure out how to get the method name (or even method id) from the logs.

I red the documentation, searched online, and can't figure it out. Any help appreciated

5
  • Can you post an example log? Will explain its anatomy in detail
    – pbsh
    Jan 26, 2022 at 18:41
  • Sure ! Thank you @pbsh For example, here, we can see on the logs that it's a type Transfer and the method used is Mint. For a log of type Transfer it could be the method mint, devMint or freeMint. I thought I could get that info from the topics but apparently not (or I misunderstood something). To formulate my question above in different words : How to listen to a method call and not for an event (as transfer event can come from many methods for example)
    – Sequoya
    Jan 26, 2022 at 22:37
  • and here is an example log I got from getPastEvents
    – Sequoya
    Jan 26, 2022 at 22:42
  • 1
    i am looking to understand this as well, did you find an answer @sequoya?
    – SimonStern
    Apr 16, 2022 at 22:13
  • @SimonStern no, unfortunately, haven't had the chance to look more into it
    – Sequoya
    May 29, 2022 at 16:33

2 Answers 2

1

There is no need to search in transactions. You can read the method directly from response. Just use the first 8 characters in first topics element.

Sample resposne event:

{
'address': '0x167384319b41f7094e62f7506409eb38079abff8', 
'topics': ['0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67', '0x000000000000000000000000e592427a0aece92de3edee1f18e0157c05861564', '0x000000000000000000000000016ba20db4b681f83e5fbbe0c433c96268ab5815'], 
'data': '0xfffffffffffffffffffffffffffffffffffffffffffffde69146e9fcc68000000000000000000000000000000000000000000000000000005f932d12f7a8cabb000000000000000000000000000000000000000006bce171a76b01c29a6a63cf0000000000000000000000000000000000000000000120c1bbfbedc19289aa54fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffee3cf', 
'blockNumber': '0x24ff6f0', 
'transactionHash': '0x5ff2de0e94d650752931e006036da98ab954676fc2463d65befddd26f0c41f47',
'transactionIndex': '0x2', 
'blockHash': '0xb49da04ce353b3e683e4be3540c6a786ccdc6379f0643aa5cbc4049c50acc8f8',
'logIndex': '0x26', 
'removed': False
}

method id = 0xc42079f9 which you can search on ethereum signature database. Now you can see, this event is swap method.

You can subscribe for specific method. Just use topics attribute:

'[
  "logs",  
  {
    "address":  "0x167384319b41f7094e62f7506409eb38079abff8",   
    "topics": ["0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67"]
  }
]'
2

As per your comment, you are looking to listen for a particular function call. I don't think it's possible to listen to a method call like how you would listen for events. But there is a workaround. You could listen to an event using getPastLog, and in the response, you can find the transaction hash. With that, you can look up the transaction details, specifically input data. By looking at the function signature in the beginning of the input data (first 4 bytes) you can decide the function. Hope this helps :)

2
  • Thanks! Got me on the right track, yet i'm still confused. From the transaction input, I do see it begins with 0x375a069a (as shown on etherscan.io). I tried to use web3.utils.hexToString, which tells me it's not valid utf-8. I'm confused on how to get the method name from the input (ie, how to "decide" the function from it?)
    – Sequoya
    Jan 26, 2022 at 23:27
  • I see there is this npmjs.com/package/ethereum-input-data-decoder, I'd really like to understand and do it with web3 directly. It seems I need to map it to the ABI, haven't found how in the web3 documentation (checked web3.eth.abi and did not find what I was supposed to use?)
    – Sequoya
    Jan 26, 2022 at 23:57

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.