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 "cive";Usage
import { } from "cive";
import { } from "cive/chains";
import { } from "./abi.js";
const = ({
: ,
: [
"0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0",
],
: .,
});
/**
* {
* eventName: "Transfer",
* args: undefined,
* }
*/export const Abi = [
{
inputs: [],
stateMutability: "nonpayable",
type: "constructor",
},
{
inputs: [],
name: "Transfer",
type: "event",
},
{
inputs: [],
name: "Approve",
type: "event",
},
] as const;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",
],
networkId: mainnet.id,
});
// [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",
],
networkId: mainnet.id,
strict: false, // [!code ++]
});
/**
* {
* eventName: 'Transfer',
* args: ['cfx:aak39z1fdm02v71y33znvaxwthh99skcp2evrfgefg']
* }
*/Return Value
{
eventName: string;
args: Inferred;
}Decoded ABI event topics.
Parameters
networkId
- Type:
Number
The network ID will be used for decoding the base32 address.
const topics = decodeEventLog({
abi: Abi,
networkId: mainnet.id,
data: "0x0000000000000000000000000000000000000000000000000000000000000001",
topics: [
"0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0",
],
});abi
- Type:
Abi
The contract's ABI.
const topics = decodeEventLog({
abi: Abi,
networkId: mainnet.id,
data: "0x0000000000000000000000000000000000000000000000000000000000000001",
topics: [
"0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0",
],
});topics
- Type:
[Hex, ...(Hex | Hex[] | null)[]]
A set of topics (encoded indexed args) from the Event Log.
const topics = decodeEventLog({
abi: Abi,
networkId: mainnet.id,
data: "0x0000000000000000000000000000000000000000000000000000000000000001",
topics: [
"0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0",
],
});data (optional)
- Type:
string
The data (encoded non-indexed args) from the Event Log.
const topics = decodeEventLog({
abi: Abi,
networkId: mainnet.id,
data: "0x0000000000000000000000000000000000000000000000000000000000000001",
topics: [
"0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0",
],
});eventName (optional)
- Type:
string
An event name from the ABI. Provide an eventName to infer the return type of decodeEventLog.
const topics = decodeEventLog({
abi: Abi,
networkId: mainnet.id,
eventName: "Transfer",
data: "0x0000000000000000000000000000000000000000000000000000000000000001",
topics: [
"0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0",
],
});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.
decodeEventLog({
abi: parseAbi(["event Transfer(address indexed, address, uint256)"]),
data: "0x0000000000000000000000000000000000000000000000000000000000000001",
topics: [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266",
],
networkId: mainnet.id,
strict: false,
});