CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-web3-eth-abi

Web3 module encode and decode EVM in/output.

Pending
Overview
Eval results
Files

event-processing.mddocs/

Event Processing

Event signature encoding and log data decoding with support for indexed parameters and anonymous events. Essential for processing Ethereum event logs and building event filters.

Capabilities

Encode Event Signature

Encodes the event name to its ABI signature, which is the sha3 hash of the event name including input types.

/**
 * Encodes the event name to its ABI signature (sha3 hash)
 * @param functionName - The event name to encode or the AbiEventFragment object
 * @returns The ABI signature of the event (32 bytes)
 */
function encodeEventSignature(functionName: string | AbiEventFragment): string;

Usage Examples:

import { encodeEventSignature } from "web3-eth-abi";

// Using event name string
const signature1 = encodeEventSignature('myEvent(uint256,bytes32)');
console.log(signature1);
// 0xf2eeb729e636a8cb783be044acf6b7b1e2c5863735b60d6daae84c366ee87d97

// Using JSON interface object
const signature2 = encodeEventSignature({
  name: "myEvent",
  type: "event",
  inputs: [
    { type: "uint256", name: "myNumber" },
    { type: "bytes32", name: "myBytes" }
  ]
});
console.log(signature2);
// 0xf2eeb729e636a8cb783be044acf6b7b1e2c5863735b60d6daae84c366ee87d97

// ERC-20 Transfer event
const transferSignature = encodeEventSignature({
  inputs: [
    { indexed: true, name: "from", type: "address" },
    { indexed: true, name: "to", type: "address" },
    { indexed: false, name: "value", type: "uint256" }
  ],
  name: "Transfer",
  type: "event"
});
console.log(transferSignature);
// 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef

Decode Event Log

Decodes ABI-encoded log data and indexed topic data from Ethereum event logs. Handles both indexed and non-indexed parameters correctly.

/**
 * Decodes ABI-encoded log data and indexed topic data
 * @param inputs - Array of ABI parameter inputs defining the event structure
 * @param data - The ABI byte code in the data field of a log
 * @param topics - Array with indexed parameter topics (without topic[0] for non-anonymous events)
 * @returns Decoded parameters object with numeric and named keys
 */
function decodeLog<ReturnType extends DecodedParams>(
  inputs: Array<AbiParameter> | ReadonlyArray<AbiParameter>,
  data: HexString,
  topics: string | string[]
): ReturnType;

Usage Examples:

import { decodeLog } from "web3-eth-abi";

// Decode event log with mixed indexed/non-indexed parameters
const decodedLog = decodeLog(
  [
    { type: "string", name: "myString" },
    { type: "uint256", name: "myNumber", indexed: true },
    { type: "uint8", name: "mySmallNumber", indexed: true }
  ],
  "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000748656c6c6f252100000000000000000000000000000000000000000000000000",
  [
    "0x000000000000000000000000000000000000000000000000000000000000f310",
    "0x0000000000000000000000000000000000000000000000000000000000000010"
  ]
);

console.log(decodedLog);
// {
//   '0': 'Hello%!',
//   '1': 62224n,
//   '2': 16n,
//   __length__: 3,
//   myString: 'Hello%!',
//   myNumber: 62224n,
//   mySmallNumber: 16n
// }

// Decode ERC-20 Transfer event
const transferLog = decodeLog(
  [
    { type: "address", name: "from", indexed: true },
    { type: "address", name: "to", indexed: true },
    { type: "uint256", name: "value", indexed: false }
  ],
  "0x000000000000000000000000000000000000000000000000016345785d8a0000",
  [
    "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", // event signature
    "0x000000000000000000000000a0b86a33e6288db8e74b93f60e5c5e7b56b7b8f5", // from
    "0x000000000000000000000000742d35cc6638c2532c1ccf6344d4039be6498c2f"  // to
  ]
);

console.log(transferLog);
// {
//   '0': '0xa0b86a33e6288db8e74b93f60e5c5e7b56b7b8f5',
//   '1': '0x742d35cc6638c2532c1ccf6344d4039be6498c2f',
//   '2': 100000000000000000n,
//   __length__: 3,
//   from: '0xa0b86a33e6288db8e74b93f60e5c5e7b56b7b8f5',
//   to: '0x742d35cc6638c2532c1ccf6344d4039be6498c2f',
//   value: 100000000000000000n
// }

// Decode anonymous event (no event signature in topics)
const anonymousLog = decodeLog(
  [
    { type: "uint256", name: "value1", indexed: true },
    { type: "uint256", name: "value2", indexed: true },
    { type: "string", name: "message" }
  ],
  "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d48656c6c6f20576f726c64210000000000000000000000000000000000000000",
  [
    "0x0000000000000000000000000000000000000000000000000000000000000001",
    "0x0000000000000000000000000000000000000000000000000000000000000002"
  ]
);

console.log(anonymousLog);
// {
//   '0': 1n,
//   '1': 2n,
//   '2': 'Hello World!',
//   __length__: 3,
//   value1: 1n,
//   value2: 2n,
//   message: 'Hello World!'
// }

Event Processing Concepts

Indexed Parameters

Indexed parameters are stored in the topics array of the event log and are limited to 32 bytes. Complex types (like strings and bytes) are hashed when indexed. Up to 3 parameters can be indexed per event.

Non-Indexed Parameters

Non-indexed parameters are stored in the data field of the event log and can be of any size. They are ABI-encoded together.

Anonymous Events

Anonymous events don't include the event signature hash as the first topic, so all topics correspond directly to indexed parameters.

Topic Handling

  • For non-anonymous events: topics[0] is the event signature, topics[1...] are indexed parameters
  • For anonymous events: topics[0...] are indexed parameters directly
  • The decodeLog function automatically handles the offset based on the number of topics vs indexed parameters

Types

interface AbiEventFragment {
  type: "event";
  name?: string;
  inputs?: AbiParameter[];
  anonymous?: boolean;
}

interface AbiParameter {
  name: string;
  type: string;
  indexed?: boolean;
  components?: AbiParameter[];
}

interface DecodedParams {
  [key: string]: unknown;
  __length__: number;
}

type HexString = string;

Install with Tessl CLI

npx tessl i tessl/npm-web3-eth-abi

docs

eip-712.md

error-handling.md

event-processing.md

function-operations.md

index.md

parameter-processing.md

tile.json