watchEvent
Watches and returns emitted event logs
Watches and returns emitted event logs
Watch actions return a channel of events. Cancel the context to stop watching.
Import the public actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/public"An example showing how to watch for events and handle each batch of logs from the channel.
import ( "context" "fmt" "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, cancel := context.WithCancel(context.Background())defer cancel()
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")transferEvent, _ := abi.ParseEvent("Transfer(address indexed, address indexed, uint256)")
events := public.WatchEvent(ctx, publicClient, public.WatchEventParameters{ Address: contractAddr, Event: transferEvent, Batch: true,})
for event := range events { if event.Error != nil { log.Printf("error: %v", event.Error) continue } fmt.Printf("Received %d logs", len(event.Logs))}<-chan WatchEventEvent
A channel that emits WatchEventEvent structs containing:
Logs []formatters.Log - The event logs (may contain multiple when Batch is true)Error error - Any error that occurredConfiguration 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")events := public.WatchEvent(ctx, publicClient, public.WatchEventParameters{ Address: contractAddr, Event: transferEvent,})*abi.EventA single event definition to filter for. Mutually exclusive with Events.
import "github.com/ChefBingbong/viem-go/abi"
transferEvent, _ := abi.ParseEvent("Transfer(address indexed, address indexed, uint256)")events := public.WatchEvent(ctx, publicClient, public.WatchEventParameters{ Address: contractAddr, Event: transferEvent,})[]*abi.EventA list of event definitions to filter for. Mutually exclusive with Event.
transferEvent, _ := abi.ParseEvent("Transfer(...)")approvalEvent, _ := abi.ParseEvent("Approval(...)")events := public.WatchEvent(ctx, publicClient, public.WatchEventParameters{ Address: contractAddr, Events: []*abi.Event{transferEvent, approvalEvent},})map[string]anyIndexed event arguments to filter by. Keys are parameter names, values are the expected values.
senderAddr := common.HexToAddress("0xd8da6bf26964af9d7eed9e03e53415d37aa96045")events := public.WatchEvent(ctx, publicClient, public.WatchEventParameters{ Address: contractAddr, Event: transferEvent, Args: map[string]any{"from": senderAddr},})booltrueWhether to batch logs together. When true, multiple logs are collected and emitted together. When false, each log is emitted as a separate event.
events := public.WatchEvent(ctx, publicClient, public.WatchEventParameters{ Address: contractAddr, Event: transferEvent, Batch: false,})*uint64The block number to start watching from. If set, forces polling mode.
fromBlock := uint64(16330000)events := public.WatchEvent(ctx, publicClient, public.WatchEventParameters{ Address: contractAddr, Event: transferEvent, FromBlock: &fromBlock,})*boolfalse for WebSocket Transports, true for non-WebSocket TransportsWhether or not to use a polling mechanism instead of a WebSocket subscription.
poll := trueevents := public.WatchEvent(ctx, publicClient, public.WatchEventParameters{ Address: contractAddr, Event: transferEvent, Poll: &poll,})time.DurationPollingIntervalThe interval between polls when using polling mode.
import "time"
events := public.WatchEvent(ctx, publicClient, public.WatchEventParameters{ Address: contractAddr, Event: transferEvent, PollingInterval: 5 * time.Second,})boolfalseDetermines whether logs must match the event definition exactly. When true, logs with mismatched indexed/non-indexed arguments are skipped.
events := public.WatchEvent(ctx, publicClient, public.WatchEventParameters{ Address: contractAddr, Event: transferEvent, Strict: true,})int4The number of workers for parallel log processing. Only used when decoding event logs.
events := public.WatchEvent(ctx, publicClient, public.WatchEventParameters{ Address: contractAddr, Event: transferEvent, WorkerPoolSize: 8,})Underlying JSON-RPC methods used by this action.
eth_newFilter to create a filtereth_getFilterChanges on a polling intervaleth_getLogs for each block rangeeth_subscribe with "logs" event