#TIL 28 - Decode receipt logs with ethers | ProductsWay

#TIL 28 - Decode receipt logs with ethers

Jul 15, 2022 · Dung Huynh

What

Decode hex-encoded event logs from an Ethereum transaction receipt using your contract ABI.

Why

Receipt logs store topics and data as raw bytes — you can't read event names or arguments without the ABI. Interface.parseLog matches topic0 to the event signature and decodes the remaining fields into typed JavaScript values.

How

import { ethers } from "ethers";

const iface = new ethers.utils.Interface(abi);

function decodeLogs(logs: Log[], contractAddress: string) {
  return logs
    .filter(
      (log) => log.address.toLowerCase() === contractAddress.toLowerCase(),
    )
    .map((log) => iface.parseLog({ topics: log.topics, data: log.data }))
    .filter(Boolean);
}