watchPendingTransactions
Watches and returns pending transaction hashes
Loading...
Watches and returns pending transaction hashes
Import the public actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/public"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()) }}<-chan WatchPendingTransactionsEvent
A channel that emits WatchPendingTransactionsEvent structs containing:
Hashes []common.Hash - The pending transaction hashes (may contain multiple when Batch is true)Error error - Any error that occurredConfiguration options accepted by this action.
booltrueWhether or not to batch the transaction hashes between polling intervals.
events := public.WatchPendingTransactions(ctx, publicClient, public.WatchPendingTransactionsParameters{ Batch: false,})*boolfalse for WebSocket Transports, true for non-WebSocket TransportsWhether or not to use a polling mechanism instead of a WebSocket subscription.
poll := trueevents := public.WatchPendingTransactions(ctx, publicClient, public.WatchPendingTransactionsParameters{ Poll: &poll,})time.DurationPollingIntervalThe interval between polls when using polling mode.
import "time"
events := public.WatchPendingTransactions(ctx, publicClient, public.WatchPendingTransactionsParameters{ PollingInterval: 5 * time.Second,})Underlying JSON-RPC methods used by this action.
eth_newPendingTransactionFilter to initialize the filtereth_getFilterChanges on a polling intervaleth_subscribe with "newPendingTransactions" event