Documentation
¶
Index ¶
- Constants
- Variables
- func FeePriorityName(tier int) string
- func FindSingleEvent(abi *abi.ABI, receipt *types.Receipt, contractAddress common.Address, ...) error
- func IsSynced(ctx context.Context, backend Backend, maxDelay time.Duration) (bool, time.Time, error)
- func ParseEvent(a *abi.ABI, eventName string, c any, e types.Log) error
- func ParseFeePriority(s string) (int, error)
- func ValidateFeePriority(tier int) error
- func WaitBlockAfterTransaction(ctx context.Context, backend Backend, pollingInterval time.Duration, ...) (*types.Header, error)
- func WaitSynced(ctx context.Context, logger log.Logger, backend Backend, ...) error
- type Backend
- type Monitor
- type ReceiptTimeoutError
- type ResendResult
- type ResentTransaction
- type Service
- type StoredTransaction
- type TxRequest
Constants ¶
const ( FeePriorityLow = iota + 1 FeePriorityMarket FeePriorityAggressive )
Fee priority selects which priority-fee percentile from eth_feeHistory to use. The zero value means the priority is unset.
const ( DefaultGasLimit = 1_000_000 // Used for contract operations when setGasLimit flag is enabled MinReplacementBumpPercent = 15 DefaultTipBoostPercent = 25 MaxGasLimit = 10_000_000 // Maximum allowed gas limit to prevent excessive values MinGasLimit = 21_000 // Minimum gas for any transaction GasBufferPercent = 33 // Add 33% buffer to estimated gas FallbackGasLimit = 900_000 // Fallback when estimation fails and no minimum is set )
Variables ¶
var ( ErrEventNotFound = errors.New("event not found") ErrNoTopic = errors.New("no topic") )
var ( ErrTransactionCancelled = errors.New("transaction cancelled") ErrMonitorClosed = errors.New("monitor closed") )
var ( // ErrTransactionReverted denotes that the sent transaction has been // reverted. ErrTransactionReverted = errors.New("transaction reverted") ErrUnknownTransaction = errors.New("unknown transaction") ErrAlreadyImported = errors.New("already imported") // ErrReplacementUnderpriced denotes that the node kept the previous // transaction with the same nonce because the new fees are too low. ErrReplacementUnderpriced = errors.New("replacement transaction underpriced") // ErrNonceUsed denotes that the nonce of the transaction is already used on chain. ErrNonceUsed = errors.New("nonce already used") // ErrSuperseded denotes that a newer transaction has taken the nonce. // One hash of the old chain may still be mined, but the old chain can no longer be resent or cancelled. ErrSuperseded = errors.New("a newer transaction uses this nonce") // ErrCancellation denotes that the hash is a cancellation of its nonce. ErrCancellation = errors.New("transaction is a cancellation") // ErrNotInResendChain denotes that the hash is not the transaction this nonce is being resent as. ErrNotInResendChain = errors.New("transaction is not the current transaction for its nonce") // ErrMissingFees denotes that a stored transaction has no fees to bump. ErrMissingFees = errors.New("stored transaction has no fees to bump") // ErrReceiptTimeout means the transaction was broadcast, but no receipt // arrived before the wait timed out. The transaction may still be mined. ErrReceiptTimeout = errors.New("transaction was not mined before the wait timed out; it may still be pending") // ErrApprovalTimeout means the token approval before an operation was not // mined in time. The approval hash is omitted: resending it does not // complete the operation and only spends gas. ErrApprovalTimeout = errors.New("approval was not mined before the wait timed out") )
var ErrUnknownFeePriority = errors.New("unknown fee priority")
Functions ¶
func FeePriorityName ¶
FeePriorityName returns the priority name. The zero value returns an empty string.
func FindSingleEvent ¶
func FindSingleEvent(abi *abi.ABI, receipt *types.Receipt, contractAddress common.Address, event abi.Event, out any) error
FindSingleEvent will find the first event of the given kind.
func IsSynced ¶
func IsSynced(ctx context.Context, backend Backend, maxDelay time.Duration) (bool, time.Time, error)
IsSynced will check if we are synced with the given blockchain backend. This is true if the current wall clock is after the block time of last block with the given maxDelay as the maximum duration we can be behind the block time.
func ParseEvent ¶
ParseEvent will parse the specified abi event from the given log
func ParseFeePriority ¶
ParseFeePriority validates a fee priority name (for API headers and config). An empty name returns 0, which means the priority is unset.
func ValidateFeePriority ¶
ValidateFeePriority returns ErrUnknownFeePriority for any tier other than low, market, or aggressive.
Types ¶
type Backend ¶
type Backend interface {
backend.Geth
SuggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error)
SuggestFeeHistory(ctx context.Context, tier int) (gasFeeCap, gasTipCap *big.Int, err error)
}
Backend is the minimum of blockchain backend functions we need.
type Monitor ¶
type Monitor interface {
io.Closer
// WatchTransaction watches the transaction until either there is 1 confirmation or a competing transaction with cancellationDepth confirmations.
WatchTransaction(txHash common.Hash, nonce uint64) (<-chan types.Receipt, <-chan error, error)
}
Monitor is a nonce-based watcher for transaction confirmations. Instead of watching transactions individually, the senders nonce is monitored and transactions are checked based on this. The idea is that if the nonce is still lower than that of a pending transaction, there is no point in actually checking the transaction for a receipt. At the same time if the nonce was already used and this was a few blocks ago we can reasonably assume that it will never confirm.
type ReceiptTimeoutError ¶
ReceiptTimeoutError is returned by SendAndWait when the receipt wait times out. Hash is the transaction the client can resend. It is zero when resending would not complete the operation.
func NewApprovalTimeout ¶
func NewApprovalTimeout() *ReceiptTimeoutError
NewApprovalTimeout reports that the token approval was not mined in time. The approval hash is omitted.
func NewReceiptTimeout ¶
func NewReceiptTimeout(hash common.Hash) *ReceiptTimeoutError
NewReceiptTimeout reports that hash was broadcast but not mined in time.
func (*ReceiptTimeoutError) Error ¶
func (e *ReceiptTimeoutError) Error() string
func (*ReceiptTimeoutError) Message ¶
func (e *ReceiptTimeoutError) Message() string
Message is the client-facing text, without the transaction hash.
func (*ReceiptTimeoutError) TxHash ¶
func (e *ReceiptTimeoutError) TxHash() string
TxHash is the hex hash to show the client, or empty when there is nothing to resend.
func (*ReceiptTimeoutError) Unwrap ¶
func (e *ReceiptTimeoutError) Unwrap() error
type ResendResult ¶
type ResendResult struct {
Nonce uint64
CurrentHash common.Hash
PrevHashes []common.Hash // earlier hashes, oldest first
}
ResendResult is what a caller sees after a resend. Superseded chains stay in the stored record.
type ResentTransaction ¶
type ResentTransaction struct {
Nonce uint64
CurrentHash common.Hash // last broadcast hash
PrevHashes []common.Hash // earlier hashes, oldest first
// Superseded holds the chains of earlier owners of the nonce, each oldest first.
// They can no longer be resent, but one of them may still be mined.
Superseded [][]common.Hash
}
ResentTransaction is the resend chain of one nonce. Every hash in it is the same transaction with different fees. It is persisted under resendTxKey.
type Service ¶
type Service interface {
io.Closer
// Send creates a transaction based on the request (with gasprice increased by provided percentage) and sends it.
Send(ctx context.Context, request *TxRequest, tipCapBoostPercent int) (txHash common.Hash, err error)
// SendAndWait sends the request and waits until the transaction is mined.
// It returns ErrTransactionReverted when the transaction failed.
// timeout limits the wait for the receipt. Zero waits until the transaction
// is mined or ctx is cancelled. A deadline returns *ReceiptTimeoutError with
// the broadcast hash. Broadcasting the transaction is not limited by timeout.
SendAndWait(ctx context.Context, request *TxRequest, tipCapBoostPercent int, timeout time.Duration) (receipt *types.Receipt, err error)
// Call simulate a transaction based on the request.
Call(ctx context.Context, request *TxRequest) (result []byte, err error)
// WaitForReceipt waits until either the transaction with the given hash has been mined or the context is cancelled.
// This is only valid for transaction sent by this service.
WaitForReceipt(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error)
// WatchSentTransaction start watching the given transaction.
// This wraps the monitors watch function by loading the correct nonce from the store.
// This is only valid for transaction sent by this service.
WatchSentTransaction(txHash common.Hash) (<-chan types.Receipt, <-chan error, error)
// StoredTransaction retrieves the stored information for the transaction
StoredTransaction(txHash common.Hash) (*StoredTransaction, error)
// PendingTransactions retrieves the list of all pending transaction hashes
PendingTransactions() ([]common.Hash, error)
// ResendTransaction broadcasts a previously sent transaction again with the same nonce and content and new fees.
ResendTransaction(ctx context.Context, txHash common.Hash, bumpPercent int) (*ResendResult, error)
// KnownTransaction returns the newest hash of txHash's chain that the node has answered for.
KnownTransaction(ctx context.Context, txHash common.Hash) (common.Hash, bool, error)
// TransactionReceipt returns the receipt of the transaction.
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
// CancelTransaction cancels a previously sent transaction by double-spending its nonce with zero-transfer one.
CancelTransaction(ctx context.Context, originalTxHash common.Hash) (common.Hash, error)
// TransactionFee retrieves the transaction fee
TransactionFee(ctx context.Context, txHash common.Hash) (*big.Int, error)
// UnwrapABIError tries to unwrap the ABI error if the given error is not nil.
// The original error is wrapped together with the ABI error if it exists.
UnwrapABIError(ctx context.Context, req *TxRequest, err error, abiErrors map[string]abi.Error) error
}
Service is the service to send transactions. It takes care of gas price, gas limit and nonce management.
type StoredTransaction ¶
type StoredTransaction struct {
To *common.Address // recipient of the transaction
Data []byte // transaction data
GasPrice *big.Int // used gas price
GasLimit uint64 // used gas limit
GasTipBoost int // adds a tip for the miner for prioritizing transaction
GasTipCap *big.Int // adds a cap to the tip
GasFeeCap *big.Int // adds a cap to maximum fee user is willing to pay
FeePriority int // fee priority that priced the transaction
Value *big.Int // amount of wei to send
Nonce uint64 // used nonce
Created int64 // creation timestamp
Description string // description
}
type TxRequest ¶
type TxRequest struct {
To *common.Address // recipient of the transaction
Data []byte // transaction data
GasPrice *big.Int // gas price or nil if suggested gas price should be used
GasLimit uint64 // gas limit or 0 if it should be estimated
MinEstimatedGasLimit uint64 // minimum gas limit to use if the gas limit was estimated; it will not apply when this value is 0 or when GasLimit is not 0
GasFeeCap *big.Int // adds a cap to maximum fee user is willing to pay
FeePriority int // fee priority; when set, it takes precedence over GasPrice
Value *big.Int // amount of wei to send
Description string // optional description
}
TxRequest describes a request for a transaction that can be executed.