createContractEventFilter
Creates a filter to retrieve contract event logs with ABI decoding
Creates a filter to retrieve contract event logs with ABI decoding
Creates a filter to retrieve contract event logs that can be used with GetFilterChanges or GetFilterLogs.
Import the public actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/public"An example showing how to create an ABI-aware contract event filter and use it to fetch logs.
import ( "context" "log"
"github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public" "github.com/ChefBingbong/viem-go/abi" "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")erc20ABI, _ := abi.ParseABI(`[{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]`)
filter, err := public.CreateContractEventFilter(ctx, publicClient, public.CreateContractEventFilterParameters{ Address: contractAddr, ABI: erc20ABI, EventName: "Transfer",})if err != nil { log.Fatal(err)}
// Use filter.ID with GetFilterChanges or GetFilterLogslogs, err := public.GetFilterLogs(ctx, publicClient, public.GetFilterLogsParameters{ Filter: filter,})*CreateContractEventFilterReturnType
A filter object containing:
ID FilterID - The filter identifierType string - Always "event" for event filtersABI *abi.ABI - The parsed ABI for decoding logs laterEventName string - The filtered event name (empty if filtering all events)Args []any - The indexed args used for filteringStrict bool - Whether strict mode is enabledConfiguration options accepted by this action.
any (accepts common.Address, *common.Address, []common.Address, string, or []string)The contract address(es) to filter logs from.
import ( "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public")
contractAddr := common.HexToAddress("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48")filter, err := public.CreateContractEventFilter(ctx, publicClient, public.CreateContractEventFilterParameters{ Address: contractAddr, ABI: erc20ABI, EventName: "Transfer",})any (accepts *abi.ABI, JSON bytes, or string)The contract ABI for decoding event logs.
import "github.com/ChefBingbong/viem-go/abi"
erc20ABI, _ := abi.ParseABI(`[{"anonymous":false,"inputs":[...],"name":"Transfer",...}]`)filter, err := public.CreateContractEventFilter(ctx, publicClient, public.CreateContractEventFilterParameters{ Address: contractAddr, ABI: erc20ABI, EventName: "Transfer",})stringFilters logs to a specific event. If omitted, all events from the ABI are matched.
filter, err := public.CreateContractEventFilter(ctx, publicClient, public.CreateContractEventFilterParameters{ Address: contractAddr, ABI: erc20ABI, EventName: "Transfer",})[]anyFilters logs by indexed event parameters. Provide indexed args in order (use nil for "any" match).
senderAddr := common.HexToAddress("0xd8da6bf26964af9d7eed9e03e53415d37aa96045")filter, err := public.CreateContractEventFilter(ctx, publicClient, public.CreateContractEventFilterParameters{ Address: contractAddr, ABI: erc20ABI, EventName: "Transfer", Args: []any{senderAddr, nil}, // from=specific, to=any})*uint64The block number to start filtering from. Mutually exclusive with FromBlockTag.
fromBlock := uint64(16330000)filter, err := public.CreateContractEventFilter(ctx, publicClient, public.CreateContractEventFilterParameters{ Address: contractAddr, ABI: erc20ABI, EventName: "Transfer", FromBlock: &fromBlock,})BlockTagThe block tag to start filtering from. Mutually exclusive with FromBlock.
filter, err := public.CreateContractEventFilter(ctx, publicClient, public.CreateContractEventFilterParameters{ Address: contractAddr, ABI: erc20ABI, EventName: "Transfer", FromBlockTag: public.BlockTagSafe,})*uint64The block number to stop filtering at. Mutually exclusive with ToBlockTag.
toBlock := uint64(16330050)filter, err := public.CreateContractEventFilter(ctx, publicClient, public.CreateContractEventFilterParameters{ Address: contractAddr, ABI: erc20ABI, EventName: "Transfer", ToBlock: &toBlock,})BlockTagThe block tag to stop filtering at. Mutually exclusive with ToBlock.
filter, err := public.CreateContractEventFilter(ctx, publicClient, public.CreateContractEventFilterParameters{ Address: contractAddr, ABI: erc20ABI, EventName: "Transfer", ToBlockTag: public.BlockTagLatest,})boolfalseFilters out logs that don't match the indexed/non-indexed arguments in the event ABI.
filter, err := public.CreateContractEventFilter(ctx, publicClient, public.CreateContractEventFilterParameters{ Address: contractAddr, ABI: erc20ABI, EventName: "Transfer", Strict: true,})Underlying JSON-RPC method used by this action.