2

Etherscan is able to decode event data from unverified contracts, even without an exposed ABI. Below is an example screenshot. No signature available for this event. I've been able to replicate the filtering by topic0, but I'm unable to separate and decode the values in the returned data, which looks like this for the same kind of event:

0x00000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000000000000000000000000000046000000000000000000000000000000000000000000eaef915630f8a56831c0000000000000000000000000000000000000000000000000000000000065ee513b00000000000000000000000000000000000000000000000000000000664a9a3b00000000000000000000000000000000000000000000000000000000012f0b15000000000000000000000000000000000000000000000000000000000097b74a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016345785d8a00000000000000000000000000000000000000000000000000000000000000000000

Any ideas how to decode that like Etherscan does? The code I used to filter and log anon events is the following (Ethers 5.7):

import { ethers } from 'ethers'
const CONTRACT_ADDR = '0x-----insert-your-contract-event-emitting-address----'

function App() {
  const provider = ethers.providers.getDefaultProvider();
  
  async function yo() {
    const blockNumber = await provider.getBlockNumber()
    const logs = await provider.getLogs({ 
      topics: ["0x----------insert-your-topic0-signature------"],
      address: CONTRACT_ADDR,
      fromBlock: Number(blockNumber)-50,
      toBlock: blockNumber,
    })
    console.log(logs[0].data)
    console.log(logs)
  }

  yo();

enter image description here

1
  • I’m voting to close this question because Etherescan is a proprietary product and how they decode unverified events is a support question for Etherscan team. Public does not have insight into Etherscan's internal product development. Mar 11 at 8:14

3 Answers 3

3

Divide the resulting log by 32 bytes:

0x0000000000000000000000000000000000000000000000000000000000000032 //hex to dec = 50
0x0000000000000000000000000000000000000000000000000000000000000046 // 70
0x000000000000000000000000000000000000000000eaef915630f8a56831c000 // 284019968367857035417600000
0x0000000000000000000000000000000000000000000000000000000065ee513b // 1710117179 (timestamp) => GMT: Monday, 11 March 2024 г., 0:32:59
0x00000000000000000000000000000000000000000000000000000000664a9a3b // 1716165179 => GMT: Monday, 20 May 2024 г., 0:32:59
0x00000000000000000000000000000000000000000000000000000000012f0b15 // 19860245 YYYYMMDD ???
0x000000000000000000000000000000000000000000000000000000000097b74a // 9942858
0x0000000000000000000000000000000000000000000000000000000000000000
0x000000000000000000000000000000000000000000000000016345785d8a0000 // 100000000000000000 (wei?)
0x0000000000000000000000000000000000000000000000000000000000000000
1
  • 1
    That's exactly what I'm trying to achieve. I was looking for a method to do that. ^_^'
    – user130657
    Mar 11 at 9:57
2

Found it! I managed to use hexDataSlice() from ethers.js https://docs.ethers.org/v5/api/utils/bytes/#utils-hexDataSlice and Javascript parseInt():

const result = (logs[0].data)
const size = result.length
const decodedValues = new Array
console.log(size)
for(let i=0; i<size; i+=32) {
  let slice = ethers.utils.hexDataSlice(result,i,(i+32))
  let decoded = parseInt(slice,16)
  decodedValues.push(decoded)
}
console.log(decodedValues)
0

Events in the EVM are encoded using the Contract ABI specification.

https://docs.soliditylang.org/en/v0.8.24/abi-spec.html

Given an event name and series of event parameters, we split them into two sub-series: those which are indexed and those which are not. Those which are indexed, which may number up to 3 (for non-anonymous events) or 4 (for anonymous ones), are used alongside the Keccak hash of the event signature to form the topics of the log entry. Those which are not indexed form the byte array of the event.

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.