waitForTransactionReceipt
Waits for the transaction to be included on a block and returns the transaction receipt
Waits for the transaction to be included on a block and returns the transaction receipt
Waits for the transaction to be included on a block (one confirmation), and then returns the transaction receipt.
The waitForTransactionReceipt action additionally supports replacement detection (e.g., sped up transactions). Transactions can be replaced when a user modifies their transaction in their wallet (to speed up or cancel). Transactions are replaced when they are sent from the same nonce.
There are 3 types of transaction replacement reasons:
Import the public actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/public"An example showing how to wait for a transaction to be mined and handle timeouts.
import ( "context" "log" "time"
"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, cancel := context.WithTimeout(context.Background(), 180*time.Second)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() }()
txHash := common.HexToHash("0x4ca7ee652d57678f26e887c149ab0735f41de37bcad58c9f6d3ed5824f15b74d")receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{ Hash: txHash,})if err != nil { log.Fatal(err)}log.Printf("Transaction mined in block %d", receipt.BlockNumber)*types.Receipt
The transaction receipt.
Configuration options accepted by this action.
common.HashThe hash of the transaction to wait for.
import ( "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public")
txHash := common.HexToHash("0x4ca7ee652d57678f26e887c149ab0735f41de37bcad58c9f6d3ed5824f15b74d")receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{ Hash: txHash,})uint641The number of confirmations (blocks that have passed) to wait before resolving.
receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{ Hash: txHash, Confirmations: 5,})*booltrueWhether to check for transaction replacements.
checkReplacement := falsereceipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{ Hash: txHash, CheckReplacement: &checkReplacement,})func(ReplacementInfo)Optional callback to emit if the transaction has been replaced.
receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{ Hash: txHash, OnReplaced: func(info public.ReplacementInfo) { log.Printf("Transaction replaced: %s", info.Reason) },})time.Duration4 secondsPolling frequency.
import "time"
receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{ Hash: txHash, PollingInterval: 12 * time.Second,})int6Number of times to retry if the transaction or block is not found.
receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{ Hash: txHash, RetryCount: 3,})func(int) time.Duration(1 << count) * 200msTime to wait between retries.
import "time"
receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{ Hash: txHash, RetryDelay: func(count int) time.Duration { return 10 * time.Second },})time.Duration180 secondsOptional timeout to wait before stopping polling.
import "time"
receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{ Hash: txHash, Timeout: 60 * time.Second,})Underlying JSON-RPC methods used by this action.
Polls eth_getTransactionReceipt on each block until it has been processed. If a transaction has been replaced, calls eth_getBlockByNumber to find the replacement.