AbiEvent.decode
ABI-Decodes the provided Log Topics and Data according to the ABI Event's parameter types (input
).
Imports
Named
import { AbiEvent } from 'ox'
Examples
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 }
End-to-end
Below is an end-to-end example of using AbiEvent.decode
to decode the topics of a Transfer
event on the Wagmi Mint Example contract.
import 'ox/window'
import { AbiEvent, Hex } from 'ox'
// 1. Instantiate the `Transfer` ABI Event.
const transfer = AbiEvent.from(
'event Transfer(address indexed from, address indexed to, uint256 value)',
)
// 2. Encode the ABI Event into Event Topics.
const { topics } = AbiEvent.encode(transfer)
// 3. Query for events matching the encoded Topics.
const logs = await window.ethereum!.request({
method: 'eth_getLogs',
params: [
{
address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2',
fromBlock: Hex.fromNumber(19760235n),
toBlock: Hex.fromNumber(19760240n),
topics,
},
],
})
// 4. Decode the Log.
const decoded = AbiEvent.decode(transfer, logs[0]!)
{ from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', value: 603n }
Definition
function decode<abiEvent>(
abiEvent: abiEvent | AbiEvent,
log: decode.Log,
): decode.ReturnType<abiEvent>
Source: src/AbiEvent.ts
Parameters
abiEvent
- Type:
abiEvent | AbiEvent
The ABI Event to decode.
log
- Type:
decode.Log
topics
& data
to decode.
log.data
- Type:
0x${string}
- Optional
log.topics
- Type:
readonly 0x${string}[]
Return Type
The decoded event.
decode.ReturnType<abiEvent>
Error Type
AbiEvent.decode.ErrorType