viem-goviem-go

getLogs

Returns a list of event logs matching the provided parameters

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 construct a public client and fetch logs that match a simple filter.

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")
fromBlock := uint64(16330000)
toBlock := uint64(16330050)
logs, err := public.GetLogs(ctx, publicClient, public.GetLogsParameters{
Address: contractAddr,
FromBlock: &fromBlock,
ToBlock: &toBlock,
})
if err != nil {
log.Fatal(err)
}
log.Printf("Found %d logs", len(logs))

Returns

[]formatters.Log

A list of event logs matching the filter criteria.

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")
logs, err := public.GetLogs(ctx, publicClient, public.GetLogsParameters{
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")
logs, err := public.GetLogs(ctx, publicClient, public.GetLogsParameters{
Address: contractAddr,
Topics: []any{transferTopic},
})

FromBlock

  • Type: *uint64
  • Optional

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

fromBlock := uint64(16330000)
logs, err := public.GetLogs(ctx, publicClient, public.GetLogsParameters{
Address: contractAddr,
FromBlock: &fromBlock,
})

FromBlockTag

  • Type: BlockTag
  • Optional

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

logs, err := public.GetLogs(ctx, publicClient, public.GetLogsParameters{
Address: contractAddr,
FromBlockTag: public.BlockTagSafe,
})

ToBlock

  • Type: *uint64
  • Optional

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

toBlock := uint64(16330050)
logs, err := public.GetLogs(ctx, publicClient, public.GetLogsParameters{
Address: contractAddr,
ToBlock: &toBlock,
})

ToBlockTag

  • Type: BlockTag
  • Optional

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

logs, err := public.GetLogs(ctx, publicClient, public.GetLogsParameters{
Address: contractAddr,
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.GetLogs(ctx, publicClient, public.GetLogsParameters{
BlockHash: &blockHash,
})

JSON-RPC Method

Underlying JSON-RPC method used by this action.

eth_getLogs