viem-goviem-go

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 (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:

  • repriced: The gas price has been modified (e.g., different maxFeePerGas)
  • canceled: The transaction has been canceled (e.g., value === 0, sent to self)
  • replaced: The transaction has been replaced (e.g., different value or data)

Import

Import the public actions package so you can call this action.

import "github.com/ChefBingbong/viem-go/actions/public"

Usage

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)

Returns

*types.Receipt

The transaction receipt.

Parameters

Configuration options accepted by this action.

Hash

  • Type: common.Hash
  • Required

The 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,
})

Confirmations

  • Type: uint64
  • Default: 1
  • Optional

The number of confirmations (blocks that have passed) to wait before resolving.

receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{
Hash: txHash,
Confirmations: 5,
})

CheckReplacement

  • Type: *bool
  • Default: true
  • Optional

Whether to check for transaction replacements.

checkReplacement := false
receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{
Hash: txHash,
CheckReplacement: &checkReplacement,
})

OnReplaced

  • Type: func(ReplacementInfo)
  • Optional

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)
},
})

PollingInterval

  • Type: time.Duration
  • Default: 4 seconds
  • Optional

Polling frequency.

import "time"
receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{
Hash: txHash,
PollingInterval: 12 * time.Second,
})

RetryCount

  • Type: int
  • Default: 6
  • Optional

Number of times to retry if the transaction or block is not found.

receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{
Hash: txHash,
RetryCount: 3,
})

RetryDelay

  • Type: func(int) time.Duration
  • Default: Exponential backoff: (1 << count) * 200ms
  • Optional

Time 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
},
})

Timeout

  • Type: time.Duration
  • Default: 180 seconds
  • Optional

Optional timeout to wait before stopping polling.

import "time"
receipt, err := public.WaitForTransactionReceipt(ctx, publicClient, public.WaitForTransactionReceiptParameters{
Hash: txHash,
Timeout: 60 * time.Second,
})

JSON-RPC Method

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.