txmanager

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2026 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultGasFallback is the final fallback gas limit if all estimation
	// methods fail.
	DefaultGasFallback = 300_000
	// DefaultCancelGasFallback is the gas limit used for cancel transactions
	// if estimation fails.
	DefaultCancelGasFallback = 21_000
	// DefaultEstimateGasTimeout is the timeout for gas estimation calls.
	DefaultEstimateGasTimeout = 20 * time.Second
)

Variables

View Source
var DefaultGasEstimateOpts = &GasEstimateOpts{
	MinGas:    21_000,
	MaxGas:    5_000_000,
	SafetyBps: 1000,
	Timeout:   DefaultEstimateGasTimeout,
	Fallback:  DefaultGasFallback,
}

DefaultGasEstimateOpts provides a reasonable default configuration for gas estimation. It includes a safety margin (10%) and retries (5), and sets sensible min (21,000)/max (5,000,000) limits. It also defines a fallback gas limit (300,000) if all estimation methods fail.

Functions

func EstimateGas

func EstimateGas(
	ctx context.Context,
	cli *rpc.Client,
	tm *TxManager,
	msg ethereum.CallMsg,
	opts *GasEstimateOpts,
	floorGasLimit uint64,
) (uint64, error)

EstimateGas attempts to estimate the gas limit for a transaction using multiple strategies to improve reliability. It first tries the standard EstimateGas method, retrying on failure. If that fails, it falls back to a binary search using eth_call to find the minimum gas limit that does not revert. It applies a safety margin and clamps the result within configured limits. If a non-nil TxManager is passed, it also caches successful estimates based on the call message to optimize future calls.

Types

type Config

type Config struct {
	MaxPendingTime     time.Duration
	MaxRetries         int
	FeeIncreasePercent int
	MaxGasPriceGwei    *big.Int
	MonitorInterval    time.Duration
	ChainID            *big.Int
	SimpleTxTimeout    time.Duration
	GasEstimateOpts    *GasEstimateOpts
}

Config holds configuration for the transaction manager

func DefaultConfig

func DefaultConfig(chainID uint64) Config

DefaultConfig returns a default configuration

type GasEstimateOpts

type GasEstimateOpts struct {
	MinGas    uint64        // minimum possible gas limit (default 21,000)
	MaxGas    uint64        // maximum possible gas limit (default 5,000,000)
	SafetyBps int           // safety margin in basis points (default +10%)
	Timeout   time.Duration // timeout for each estimation call (default 20s)
	Fallback  uint64        // final fallback gas (default 300,000)
}

GasEstimateOpts allows tuning of estimator behavior

type PendingTransaction

type PendingTransaction struct {
	// ID is a unique identifier for the transaction. It does not change even
	// if the transaction is retried with a different gas price or nonce.
	ID []byte
	// Hash is the current transaction hash. It may change if the transaction
	// is retried or cancelled.
	Hash common.Hash
	// Nonce is the transaction nonce. It may change if some nonce issue arises.
	Nonce uint64
	// Time the transaction was first sent.
	Timestamp time.Time
	// RetryCount is the number of times the transaction has been retried.
	RetryCount int
	// IsBlob indicates if the transaction is a blob transaction.
	IsBlob bool
	// OriginalGasPrice, OriginalBlobFee and OriginalGasLimit store the
	// original parameters of the transaction for reference in case of retries.
	// They do not change.
	OriginalGasPrice *big.Int
	OriginalBlobFee  *big.Int
	OriginalGasLimit uint64
	// To, Data and Value store the basic parameters of the transaction.
	To    common.Address
	Data  []byte
	Value *big.Int
	// BlobHashes store the hashes of the blobs associated with the transaction.
	BlobHashes []common.Hash
	// BlobSidecar stores the sidecar for rebuilding blob transactions.
	BlobSidecar *types.BlobTxSidecar
	// LastError stores the last error encountered for this transaction.
	// Used to categorize failures as permanent or temporary.
	LastError error
}

PendingTransaction represents a transaction that has been sent but not yet confirmed.

type TxManager

type TxManager struct {
	// contains filtered or unexported fields
}

TxManager handles nonce management and stuck transaction recovery.

func New

func New(ctx context.Context, web3pool *rpc.Web3Pool, cli *rpc.Client, signer *ethSigner.Signer, config Config) (*TxManager, error)

New creates a new transaction manager and initializes it by fetching the current on-chain nonce.

func (*TxManager) BuildDynamicFeeTx

func (tm *TxManager) BuildDynamicFeeTx(
	ctx context.Context,
	to common.Address,
	data []byte,
	nonce uint64,
) (*gethtypes.Transaction, error)

BuildDynamicFeeTx builds a standard EIP-1559 transaction with the given nonce and parameters. It fetches the current gas price and tip cap, estimates gas, and signs the transaction. It returns the signed transaction or an error if any step fails.

func (*TxManager) CheckTxStatusByHash

func (tm *TxManager) CheckTxStatusByHash(hash common.Hash) (bool, error)

CheckTxStatusByHash checks the status of a transaction given its tx hash. Returns true if the transaction was confirmed and successful, false if it is still pending, or an error if the receipt query fails or the transaction was reverted on-chain.

func (*TxManager) CheckTxStatusByID

func (tm *TxManager) CheckTxStatusByID(id []byte) (bool, error)

CheckTxStatusByID checks the status of a transaction given its ID. Returns true if the transaction was successful, false otherwise.

func (*TxManager) PendingTx

func (tm *TxManager) PendingTx(id []byte) (PendingTransaction, bool)

PendingTx retrieves a pending transaction by its ID. Returns a copy of the transaction (for thread safety) and true if found, or nil and false if not found.

func (*TxManager) SendTx

func (tm *TxManager) SendTx(
	ctx context.Context,
	txBuilder func(nonce uint64) (*gethtypes.Transaction, error),
) (types.HexBytes, *common.Hash, error)

SendTx sends a transaction with automatic fallback and recovery mechanisms. It accepts a transaction builder function that takes a nonce and returns a signed transaction. If a nonce mismatch is detected, it attempts to recover by querying the actual on-chain nonce and resending the transaction. It returns the transaction ID or an error.

func (*TxManager) Start

func (tm *TxManager) Start(ctx context.Context)

Start starts the background monitoring of pending transactions.

func (*TxManager) Stop

func (tm *TxManager) Stop()

Stop stops the background monitoring

func (*TxManager) TrackBlobTxWithSidecar

func (tm *TxManager) TrackBlobTxWithSidecar(tx *gethtypes.Transaction) error

TrackBlobTxWithSidecar tracks a blob transaction with its sidecar for potential recovery. This should be called immediately after sending a blob transaction if recovery is desired.

func (*TxManager) WaitTxByHash

func (tm *TxManager) WaitTxByHash(hash common.Hash, timeOut time.Duration, cb ...func(error)) error

WaitTxByHash waits for a transaction to be mined identified by its hash. If a callback is provided, it runs the wait in the background and calls the callback with the result. If no callback is provided, it waits synchronously and returns the result.

func (*TxManager) WaitTxByID

func (tm *TxManager) WaitTxByID(id []byte, timeOut time.Duration, cb ...func(error)) error

WaitTxByID waits for a transaction to be mined identified by its ID. If a callback is provided, it runs the wait in the background and calls the callback with the result. If no callback is provided, it waits synchronously and returns the result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL