watchPendingTransactions
Watches and returns pending transaction hashes
Import
Import the public actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/public"Usage
Watch actions return a channel of events. Cancel the context to stop watching.
import ( "context" "fmt" "log"
"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, 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() }()
events := public.WatchPendingTransactions(ctx, publicClient, public.WatchPendingTransactionsParameters{ Batch: true,})
for event := range events { if event.Error != nil { log.Printf("error: %v", event.Error) continue } fmt.Printf("Pending transactions: %d", len(event.Hashes)) for _, hash := range event.Hashes { fmt.Printf(" - %s", hash.Hex()) }}Returns
<-chan WatchPendingTransactionsEvent
A channel that emits WatchPendingTransactionsEvent structs containing:
Hashes []common.Hash- The pending transaction hashes (may contain multiple whenBatchis true)Error error- Any error that occurred
Parameters
Configuration options accepted by this action.
Batch
- Type:
bool - Default:
true - Optional
Whether or not to batch the transaction hashes between polling intervals.
events := public.WatchPendingTransactions(ctx, publicClient, public.WatchPendingTransactionsParameters{ Batch: false,})Poll
- Type:
*bool - Default:
falsefor WebSocket Transports,truefor non-WebSocket Transports - Optional
Whether or not to use a polling mechanism instead of a WebSocket subscription.
poll := trueevents := public.WatchPendingTransactions(ctx, publicClient, public.WatchPendingTransactionsParameters{ Poll: &poll,})PollingInterval
- Type:
time.Duration - Default: Client's
PollingInterval - Optional
The interval between polls when using polling mode.
import "time"
events := public.WatchPendingTransactions(ctx, publicClient, public.WatchPendingTransactionsParameters{ PollingInterval: 5 * time.Second,})JSON-RPC Method
Underlying JSON-RPC methods used by this action.
- When polling:
- Calls
eth_newPendingTransactionFilterto initialize the filter - Calls
eth_getFilterChangeson a polling interval
- Calls
- When subscribing: uses
eth_subscribewith"newPendingTransactions"event