viem-goviem-go

Contract Events

Create event filters, get contract events, and watch contract events

Contract events are logged by the EVM and can be queried or watched. viem-go provides CreateContractEventFilter (for use with GetFilterChanges/GetFilterLogs), GetContractEvents (one-shot eth_getLogs with ABI decoding), and WatchContractEvent (streaming with decoding).

Import

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ChefBingbong/viem-go/actions/public"
)

GetContractEvents

Fetch event logs for a contract (and optionally a specific event and indexed args) in one call. Uses eth_getLogs and decodes logs using the ABI.

logs, err := public.GetContractEvents(ctx, publicClient, public.GetContractEventsParameters{
Address: contractAddress,
ABI: erc20ABI,
EventName: "Transfer",
Args: []any{fromAddress, nil}, // from=specific, to=any
})
if err != nil {
log.Fatal(err)
}
for _, log := range logs {
fmt.Println(log.EventName, log.DecodedArgs)
}

GetContractEvents parameters

ParameterTypeRequiredDescription
Addresscommon.Address or []common.AddressYesContract address(es).
ABIany (JSON or *abi.ABI)YesContract ABI for decoding.
EventNamestringNoFilter to this event (e.g. "Transfer"). Omit for all events in ABI.
Args[]anyNoIndexed args in order; use nil for “any”.
FromBlock / FromBlockTag*uint64 / BlockTagNoStart of range.
ToBlock / ToBlockTag*uint64 / BlockTagNoEnd of range.
BlockHash*common.HashNoSingle block by hash (mutually exclusive with range).
StrictboolNoIf true, exclude logs that don’t match event definition. Default: false.

Return type: []ContractEventLog — each has EventName, DecodedArgs (map[string]any), plus standard log fields (address, topics, data, blockNumber, etc.).

CreateContractEventFilter

Create a filter that can be polled with GetFilterChanges or GetFilterLogs (eth_newFilter). Use when you want to poll for new logs yourself.

filter, err := public.CreateContractEventFilter(ctx, publicClient, public.CreateContractEventFilterParameters{
Address: contractAddress,
ABI: erc20ABI,
EventName: "Transfer",
Args: []any{nil, toAddress}, // from=any, to=specific
})
if err != nil {
log.Fatal(err)
}
// Use filter.ID with public.GetFilterChanges(ctx, client, filter.ID) or GetFilterLogs

CreateContractEventFilter parameters

ParameterTypeRequiredDescription
Addresscommon.Address or []common.AddressYesContract address(es).
ABIanyYesContract ABI (for topic encoding).
EventNamestringNoFilter to this event.
Args[]anyNoIndexed args; nil = any.
FromBlock / FromBlockTag*uint64 / BlockTagNoStart block.
ToBlock / ToBlockTag*uint64 / BlockTagNoEnd block.
StrictboolNoStrict matching. Default: false.

Return type: CreateContractEventFilterReturnTypeID (FilterID), Type ("event"), ABI, EventName, Args, Strict. Use ID with GetFilterChanges/GetFilterLogs.

WatchContractEvent

Stream contract event logs with ABI decoding. Uses eth_subscribe (logs) when available, or polling (eth_newFilter + eth_getFilterChanges or eth_getLogs). Cancelling the context stops the stream.

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
events := public.WatchContractEvent(ctx, publicClient, public.WatchContractEventParameters{
Address: contractAddress,
ABI: erc20ABI,
EventName: "Transfer",
Args: map[string]any{"to": toAddress},
Batch: true,
})
for event := range events {
if event.Error != nil {
log.Println(event.Error)
continue
}
for _, log := range event.Logs {
fmt.Println(log)
}
}

WatchContractEvent parameters

ParameterTypeRequiredDescription
Addresscommon.Address or []common.AddressYesContract address(es).
ABI*abi.ABIYesContract ABI for decoding.
EventNamestringYesEvent to watch.
Argsmap[string]anyNoFilter by indexed args (name → value).
FromBlock*uint64NoStart block (forces polling if set).
StrictboolNoSkip logs that don’t match. Default: false.
BatchboolNoEmit multiple logs per event. Default: true.
Poll*boolNoForce polling vs subscription.
PollingIntervaltime.DurationNoPoll interval when polling.
WorkerPoolSizeintNoDecoding workers. Default: 4.

Return type: channel of WatchContractEventEventLogs (slice of formatted logs), Error. Close context to stop.

See also