Skip to content

decodeEventLog

Decodes ABI encoded event topics & data (from an Event Log) into an event name and structured arguments (both indexed & non-indexed).

Install

import { decodeEventLog } from 'viem'

Usage

example.ts
import { decodeEventLog } from 'viem'
 
const topics = decodeEventLog({
  abi: wagmiAbi,
  data: '0x0000000000000000000000000000000000000000000000000000000000000001',
  topics: [
    '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0', 
    '0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
    '0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'
  ]
})
/**
 *  {
 *    eventName: 'Transfer',
 *    args: {
 *      from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
 *      to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
 *      value: 1n
 *    }
 *  }
 */

Partial Decode

By default, if the topics and data does not conform to the ABI (a mismatch between the number of indexed/non-indexed arguments), decodeEventLog will throw an error.

For example, the following will throw an error as there is a mismatch in non-indexed arguments & data length.

decodeEventLog({
  abi: parseAbi(['event Transfer(address indexed, address, uint256)']), 
  // `data` should be 64 bytes, but is only 32 bytes.
  data: '0x0000000000000000000000000000000000000000000000000000000000000001', 
  topics: [
    '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
    '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  ]
})
// [DecodeLogDataMismatch]: Data size of 32 bytes is too small for non-indexed event parameters.

It is possible for decodeEventLog to try and partially decode the Log, this can be done by setting the strict argument to false:

decodeEventLog({ 
  abi: parseAbi(['event Transfer(address indexed, address, uint256)']), 
  data: '0x0000000000000000000000000000000000000000000000000000000000000001', 
  topics: [
    '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
    '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  ],
  strict: false
})
/**
 * {
 *   eventName: 'Transfer',
 *   args: ['0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266']
 * }
 */

Return Value

{
  eventName: string;
  args: Inferred;
}

Decoded ABI event topics.

Parameters

abi

The contract's ABI.

const topics = decodeEventLog({
  abi: wagmiAbi, 
  data: '0x0000000000000000000000000000000000000000000000000000000000000001',
  topics: [
    '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0', 
    '0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
    '0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'
  ]
})

topics

  • Type: [Hex, ...(Hex | Hex[] | null)[]]

A set of topics (encoded indexed args) from the Event Log.

const topics = decodeEventLog({
  abi: wagmiAbi,
  data: '0x0000000000000000000000000000000000000000000000000000000000000001',
  topics: [ 
    '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0', 
    '0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
    '0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'
  ]
})

data (optional)

  • Type: string

The data (encoded non-indexed args) from the Event Log.

const topics = decodeEventLog({
  abi: wagmiAbi,
  data: '0x0000000000000000000000000000000000000000000000000000000000000001', 
  topics: [
    '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0', 
    '0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
    '0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'
  ]
})

eventName (optional)

  • Type: string

An event name from the ABI. Provide an eventName to infer the return type of decodeEventLog.

const topics = decodeEventLog({
  abi: wagmiAbi,
  eventName: 'Transfer', 
  topics: [
    '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0', 
    '0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
    '0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'
  ]
})

strict (optional)

  • Type: boolean
  • Default: true

If true, decodeEventLog will throw an error if the data & topics lengths to not conform to the event on the ABI. If false, decodeEventLog will try and partially decode.

const topics = decodeEventLog({
  abi: wagmiAbi,
  strict: false, 
  topics: [
    '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0', 
    '0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
    '0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'
  ]
})