viem-goviem-go

getContractEvents

Returns a list of event logs emitted by a contract with decoded event data

Returns a list of event logs emitted by a contract with decoded event data using the provided ABI.

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 fetch and decode contract events using an ABI.

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() }()
// Parse ERC20 ABI
erc20ABI, err := 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"}]`)
if err != nil {
log.Fatal(err)
}
contractAddr := common.HexToAddress("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48")
logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddr,
ABI: erc20ABI,
EventName: "Transfer",
})
if err != nil {
log.Fatal(err)
}
log.Printf("Found %d Transfer events", len(logs))

Returns

[]ContractEventLog

A list of decoded event logs, each containing:

  • Log formatters.Log - The raw log data
  • EventName string - The decoded event name
  • DecodedArgs map[string]any - The decoded event arguments

Parameters

Configuration options accepted by this action.

Address

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

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")
logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddr,
ABI: erc20ABI,
EventName: "Transfer",
})

ABI

  • Type: any (accepts *abi.ABI, JSON bytes, or string)
  • Required

The contract ABI for decoding event logs.

import "github.com/ChefBingbong/viem-go/abi"
erc20ABI, err := abi.ParseABI(`[{"anonymous":false,"inputs":[...],"name":"Transfer",...}]`)
logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddr,
ABI: erc20ABI,
EventName: "Transfer",
})

EventName

  • Type: string
  • Optional

Filters logs to a specific event. If omitted, all events from the ABI are matched.

logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddr,
ABI: erc20ABI,
EventName: "Transfer",
})

Args

  • Type: []any
  • Optional

Filters logs by indexed event parameters. Provide indexed args in order (use nil for "any" match).

senderAddr := common.HexToAddress("0xd8da6bf26964af9d7eed9e03e53415d37aa96045")
logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddr,
ABI: erc20ABI,
EventName: "Transfer",
Args: []any{senderAddr, nil}, // from=specific, to=any
})

FromBlock

  • Type: *uint64
  • Optional

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

fromBlock := uint64(16330000)
logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddr,
ABI: erc20ABI,
EventName: "Transfer",
FromBlock: &fromBlock,
})

FromBlockTag

  • Type: BlockTag
  • Optional

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

logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddr,
ABI: erc20ABI,
EventName: "Transfer",
FromBlockTag: public.BlockTagSafe,
})

ToBlock

  • Type: *uint64
  • Optional

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

toBlock := uint64(16330050)
logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddr,
ABI: erc20ABI,
EventName: "Transfer",
ToBlock: &toBlock,
})

ToBlockTag

  • Type: BlockTag
  • Optional

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

logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddr,
ABI: erc20ABI,
EventName: "Transfer",
ToBlockTag: public.BlockTagLatest,
})

BlockHash

  • Type: *common.Hash
  • Optional

Filters logs from a specific block by hash. Mutually exclusive with FromBlock/ToBlock.

blockHash := common.HexToHash("0x...")
logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddr,
ABI: erc20ABI,
EventName: "Transfer",
BlockHash: &blockHash,
})

Strict

  • Type: bool
  • Default: false
  • Optional

Filters out logs that don't match the indexed/non-indexed arguments in the event ABI.

logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddr,
ABI: erc20ABI,
EventName: "Transfer",
Strict: true,
})

JSON-RPC Method

Underlying JSON-RPC method used by this action.

Uses eth_getLogs with ABI-encoded topics and decoded event data.