viem-goviem-go

createEventFilter

Creates a filter to listen for new events

Creates a filter object to receive logs matching the specified criteria.

Import

Import the public actions package so you can call this action.

import "github.com/ChefBingbong/viem-go/actions/public"

Usage

An example showing how to create an event filter for a contract and use it to fetch changes.

import (
"context"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/ChefBingbong/viem-go/actions/public"
"github.com/ChefBingbong/viem-go/client"
"github.com/ChefBingbong/viem-go/client/transport"
"github.com/ChefBingbong/viem-go/chain/definitions"
)
ctx := context.Background()
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{
Chain: definitions.Mainnet,
Transport: transport.HTTP("https://eth.llamarpc.com"),
})
if err != nil {
log.Fatal(err)
}
defer func() { _ = publicClient.Close() }()
contractAddr := common.HexToAddress("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48")
transferTopic := common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")
filter, err := public.CreateEventFilter(ctx, publicClient, public.CreateEventFilterParameters{
Address: contractAddr,
Topics: []any{transferTopic},
})
if err != nil {
log.Fatal(err)
}
// Use filter.ID with GetFilterChanges to get new logs
changes, err := public.GetFilterChangesLogs(ctx, publicClient, filter.ID)

Returns

*CreateEventFilterReturnType

A filter object containing:

  • ID FilterID - The filter identifier
  • Type string - Always "event" for event filters

Parameters

Configuration options accepted by this action.

Address

  • Type: any (accepts common.Address, *common.Address, []common.Address, string, or []string)
  • Optional

The contract address(es) to filter logs from. Can be a single address or a slice of addresses.

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ChefBingbong/viem-go/actions/public"
)
contractAddr := common.HexToAddress("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48")
filter, err := public.CreateEventFilter(ctx, publicClient, public.CreateEventFilterParameters{
Address: contractAddr,
})

Topics

  • Type: []any
  • Optional

The indexed event topics to filter. Each topic can be a single value or an array of values (OR condition).

transferTopic := common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")
filter, err := public.CreateEventFilter(ctx, publicClient, public.CreateEventFilterParameters{
Address: contractAddr,
Topics: []any{transferTopic},
})

FromBlock

  • Type: *uint64
  • Optional

The block number to start filtering from. Mutually exclusive with FromBlockTag.

fromBlock := uint64(16330000)
filter, err := public.CreateEventFilter(ctx, publicClient, public.CreateEventFilterParameters{
Address: contractAddr,
FromBlock: &fromBlock,
})

FromBlockTag

  • Type: BlockTag
  • Optional

The block tag to start filtering from. Mutually exclusive with FromBlock.

filter, err := public.CreateEventFilter(ctx, publicClient, public.CreateEventFilterParameters{
Address: contractAddr,
FromBlockTag: public.BlockTagSafe,
})

ToBlock

  • Type: *uint64
  • Optional

The block number to stop filtering at. Mutually exclusive with ToBlockTag.

toBlock := uint64(16330050)
filter, err := public.CreateEventFilter(ctx, publicClient, public.CreateEventFilterParameters{
Address: contractAddr,
ToBlock: &toBlock,
})

ToBlockTag

  • Type: BlockTag
  • Optional

The block tag to stop filtering at. Mutually exclusive with ToBlock.

filter, err := public.CreateEventFilter(ctx, publicClient, public.CreateEventFilterParameters{
Address: contractAddr,
ToBlockTag: public.BlockTagLatest,
})

JSON-RPC Method

Underlying JSON-RPC method used by this action.

eth_newFilter