Skip to content

AbiEvent

Utilities & types for working with Events on ABIs.

AbiEvent is a sub-type of AbiItem.

Examples

Below are some examples demonstrating common usages of the AbiEvent module:

Instantiating via JSON ABI

An AbiEvent can be instantiated from a JSON ABI by using AbiEvent.fromAbi:

import { Abi, AbiEvent } from 'ox'
 
const abi = Abi.from([
  'function foo()',
  'event Transfer(address owner, address to, uint256 tokenId)',
  'function bar(string a) returns (uint256 x)',
])
 
const 
const item: { readonly name: "Transfer"; readonly type: "event"; readonly inputs: readonly [...]; }
item
= AbiEvent.fromAbi(abi, 'Transfer')

Instantiating via Human-Readable ABI Item

An AbiEvent can be instantiated from a human-readable ABI by using AbiEvent.from:

import { AbiEvent } from 'ox'
 
const transfer = AbiEvent.from(
  'event Transfer(address indexed from, address indexed to, uint256 value)'
)
 
const transfer: { readonly name: "Transfer"; readonly type: "event"; readonly inputs: readonly [{ readonly type: "address"; readonly name: "from"; readonly indexed: true; }, { readonly type: "address"; readonly name: "to"; readonly indexed: true; }, { ...; }]; }
transfer

Encoding to Event Topics

Encode an AbiEvent into topics using AbiEvent.encode:

import { AbiEvent } from 'ox'
 
const transfer = AbiEvent.from(
  'event Transfer(address indexed from, address indexed to, uint256 value)'
)
 
const { topics } = AbiEvent.encode(transfer, {
  from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
})
[
'0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0',
'0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
'0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'
]

Decoding Event Topics and Data

Event topics and data can be decoded using AbiEvent.decode:

import { AbiEvent } from 'ox'
 
const transfer = AbiEvent.from(
  'event Transfer(address indexed from, address indexed to, uint256 value)'
)
 
const log = {
  // ...
  data: '0x0000000000000000000000000000000000000000000000000000000000000001',
  topics: [
    '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
    '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac',
    '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac',
  ],
} as const
 
const decoded = AbiEvent.decode(transfer, log)
{
from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac',
to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac',
value: 1n
}

Functions

NameDescription
AbiEvent.assertArgsAsserts that the provided arguments match the decoded log arguments.
AbiEvent.decodeABI-Decodes the provided Log Topics and Data according to the ABI Event's parameter types (input).
AbiEvent.encodeABI-encodes the provided event input (inputs) into an array of Event Topics.
AbiEvent.formatFormats an AbiEvent.AbiEvent into a Human Readable ABI Error.
AbiEvent.fromParses an arbitrary JSON ABI Event or Human Readable ABI Event into a typed AbiEvent.AbiEvent.
AbiEvent.fromAbiExtracts an AbiEvent.AbiEvent from an Abi.Abi given a name and optional arguments.
AbiEvent.getSelectorComputes the event selector (hash of event signature) for an AbiEvent.AbiEvent.

Errors

NameDescription
AbiEvent.ArgsMismatchErrorThrown when the provided arguments do not match the expected arguments.
AbiEvent.DataMismatchErrorThrown when the provided data size does not match the expected size from the non-indexed parameters.
AbiEvent.FilterTypeNotSupportedErrorThrown when the provided filter type is not supported.
AbiEvent.InputNotFoundErrorThrown when no argument was found on the event signature.
AbiEvent.SelectorTopicMismatchErrorThrown when the provided selector does not match the expected selector.
AbiEvent.TopicsMismatchErrorThrown when the provided topics do not match the expected number of topics.

Types

NameDescription
AbiEvent.AbiEventRoot type for an AbiItem.AbiItem with an event type.
AbiEvent.ExtractNames
AbiEvent.FromAbiExtracts an AbiEvent.AbiEvent item from an Abi.Abi, given a name.
AbiEvent.NameExtracts the names of all AbiError.AbiError items in an Abi.Abi.