getContractEvents
Returns a list of contract event logs matching the provided parameters.
Usage
By default, getContractEvents returns all matched events on the ABI. In practice, you must use scoping to filter for specific events.
import { } from "./client";
import { } from "./abi";
// Fetch event logs for every event on every ERC-20 contract.
const = await .({
: ,
});
// [{ ... }, { ... }, { ... }]export const erc20Abi = [
{
type: "event",
name: "Transfer",
inputs: [
{
indexed: true,
name: "from",
type: "address",
},
{
indexed: true,
name: "to",
type: "address",
},
{
indexed: false,
name: "value",
type: "uint256",
},
],
},
] as const;import { createPublicClient, http } from "cive";
import { mainnet } from "cive/chains";
export const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
});Scoping
You can also scope to a set of given attributes.
import { parseAbiItem } from "cive";
import { publicClient } from "./client";
import { erc20Abi } from "./abi";
const logs = await publicClient.getContractEvents({
address: "cfx:...",
abi: erc20Abi,
eventName: "Transfer",
args: {
from: "cfx:...",
to: "cfx:...",
},
fromEpoch: 1n,
toEpoch: 100n,
});export const erc20Abi = [
{
type: "event",
name: "Transfer",
inputs: [
{
indexed: true,
name: "from",
type: "address",
},
{
indexed: true,
name: "to",
type: "address",
},
{
indexed: false,
name: "value",
type: "uint256",
},
],
},
] as const;import { createPublicClient, http } from "cive";
import { mainnet } from "cive/chains";
export const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
});Address
Logs can be scoped to an address:
const logs = await publicClient.getContractEvents({
abi: erc20Abi,
address: "cfx:...",
});Event
Logs can be scoped to an event.
const logs = await publicClient.getContractEvents({
abi: erc20Abi,
address: "cfx:...",
eventName: "Transfer",
});Arguments
Logs can be scoped to given indexed arguments:
const logs = await publicClient.getContractEvents({
abi: erc20Abi,
address: "cfx:...",
eventName: "Transfer",
args: {
from: "cfx:...",
to: "cfx:...",
},
});Only indexed arguments in event are candidates for args.
An argument can also be an array to indicate that other values can exist in the position:
const logs = await publicClient.getContractEvents({
abi: erc20Abi,
address: "cfx:...",
eventName: "Transfer",
args: {
// '0xd8da...' OR '0xa5cc...' OR '0xa152...'
from: ["cfx:...", "cfx:...", "cfx:..."],
},
});Block Range
Logs can be scoped to a block range:
const logs = await publicClient.getContractEvents({
abi: erc20Abi,
address: "cfx:...",
eventName: "Transfer",
fromBlock: 16330000n,
toBlock: 16330050n,
});Epoch Range
Logs can be scoped to a epoch range:
const logs = await publicClient.getContractEvents({
abi: erc20Abi,
address: "cfx:...",
eventName: "Transfer",
fromEpoch: 16330000n,
toEpoch: 16330050n,
});Epoch Tag
- Type: "latest_state" | "latest_confirmed" | "latest_checkpoint" | "earliest"
- Defaut: fromEpoch "latest_checkpoint" toEpoch 'latest_state'
const logs = await publicClient.getContractEvents({
abi: erc20Abi,
address: "cfx:...",
eventName: "Transfer",
fromEpoch: "earliest",
toEpoch: "latest_state",
});Strict Mode
By default, getContractEvents will include logs that do not conform to the indexed & non-indexed arguments on the event.
cive will not return a value for arguments that do not conform to the ABI, thus, some arguments on args may be undefined.
import { } from "./client";
import { } from "./abi";
const = await .({
: ,
: "Transfer",
});
[0].args;
You can turn on strict mode to only return logs that conform to the indexed & non-indexed arguments on the event, meaning that args will always be defined. The trade-off is that non-conforming logs will be filtered out.
import { } from "./client";
import { } from "./abi";
const = await .({
: ,
: "Transfer",
: true,
});
[0].args
Returns
A list of event logs.
Parameters
abi
- Type:
Abi
The contract's ABI.
const logs = await publicClient.getContractEvents({
abi: erc20Abi,
})address
- Type:
Address | Address[]
A contract address or a list of contract addresses. Only logs originating from the contract(s) will be included in the result.
const logs = await publicClient.getContractEvents({
abi: erc20Abi,
address: 'cfx:...',
})eventName
- Type:
string
An event name on the abi.
const logs = await publicClient.getContractEvents({
abi: erc20Abi,
eventName: 'Transfer'
})args
- Type: Inferred.
A list of indexed event arguments.
const logs = await publicClient.getContractEvents({
abi: erc20Abi,
eventName: 'Transfer',
args: {
from: 'cfx:...',
to: 'cfx:...'
},
})fromEpoch(number)
- Type:
bigint
const filter = await publicClient.getContractEvents({
abi: erc20Abi,
fromEpoch: 69420n
})toEpoch(number)
- Type:
bigint
const filter = await publicClient.getContractEvents({
abi: erc20Abi,
toEpoch: 69420n
})fromEpoch(tag)
- Type:
latest_state|latest_confirmed|latest_checkpoint|earliest - Defaut:
latest_checkpoint
const filter = await publicClient.getContractEvents({
abi: erc20Abi,
fromEpoch: 'latest_checkpoint'
})toEpoch(tag)
- Type:
latest_state|latest_confirmed|latest_checkpoint|earliest - Defaut:
latest_state
const filter = await publicClient.getContractEvents({
abi: erc20Abi,
toEpoch: 'latest_state'
})blockHashes
- Type:
hash[]
const filter = await publicClient.getContractEvents({
abi: erc20Abi,
blockHashes: ["0x..."],
});