ethclient

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2025 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package ethclient provides a client for the Ethereum RPC API.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RevertErrorData added in v1.0.4

func RevertErrorData(err error) ([]byte, bool)

RevertErrorData returns the 'revert reason' data of a contract call.

This can be used with CallContract and EstimateGas, and only when the server is Geth.

Example

Here we show how to get the error message of reverted contract call.

// First create an ethclient.Client instance.
ctx := context.Background()
ec, _ := ethclient.DialContext(ctx, exampleNode.HTTPEndpoint())

// Call the contract.
// Note we expect the call to return an error.
contract := common.HexToAddress("290f1b36649a61e369c6276f6d29463335b4400c")
call := ethereum.CallMsg{To: &contract, Gas: 30000}
result, err := ec.CallContract(ctx, call, nil)
if len(result) > 0 {
	panic("got result")
}
if err == nil {
	panic("call did not return error")
}

// Extract the low-level revert data from the error.
revertData, ok := ethclient.RevertErrorData(err)
if !ok {
	panic("unpacking revert failed")
}
fmt.Printf("revert: %x\n", revertData)

// Parse the revert data to obtain the error message.
message, err := abi.UnpackRevert(revertData)
if err != nil {
	panic("parsing ABI error failed: " + err.Error())
}
fmt.Println("message:", message)
Output:

revert: 08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a75736572206572726f72
message: user error

Types

type Client

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

Client defines typed wrappers for the Ethereum RPC API.

func Dial

func Dial(rawurl string) (*Client, error)

Dial connects a client to the given URL.

func DialContext

func DialContext(ctx context.Context, rawurl string) (*Client, error)

DialContext connects a client to the given URL with context.

func DialOptions added in v1.0.4

func DialOptions(ctx context.Context, rawurl string, opts ...rpc.ClientOption) (*Client, error)

DialOptions creates a new RPC client for the given URL. You can supply any of the pre-defined client options to configure the underlying transport.

func NewClient

func NewClient(c *rpc.Client) *Client

NewClient creates a client that uses the given RPC client.

func (*Client) BalanceAt

func (ec *Client) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)

BalanceAt returns the wei balance of the given account. The block number can be nil, in which case the balance is taken from the latest known block.

func (*Client) BalanceAtHash added in v1.0.4

func (ec *Client) BalanceAtHash(ctx context.Context, account common.Address, blockHash common.Hash) (*big.Int, error)

BalanceAtHash returns the wei balance of the given account.

func (*Client) BestBidGasFee added in v1.0.4

func (ec *Client) BestBidGasFee(ctx context.Context, parentHash common.Hash) (*big.Int, error)

BestBidGasFee returns the gas fee of the best bid for the given parent hash.

func (*Client) BlobBaseFee added in v1.0.4

func (ec *Client) BlobBaseFee(ctx context.Context) (*big.Int, error)

BlobBaseFee retrieves the current blob base fee.

func (*Client) BlobSidecarByTxHash added in v1.0.4

func (ec *Client) BlobSidecarByTxHash(ctx context.Context, hash common.Hash) (*types.BlobSidecar, error)

BlobSidecarByTxHash return a sidecar of a given blob transaction

func (*Client) BlobSidecars added in v1.0.4

func (ec *Client) BlobSidecars(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]*types.BlobSidecar, error)

BlobSidecars return the Sidecars of a given block number or hash.

func (*Client) BlockByHash

func (ec *Client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)

BlockByHash returns the given full block.

Note that loading full blocks requires two requests. Use HeaderByHash if you don't need all transactions or uncle headers.

func (*Client) BlockByNumber

func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)

BlockByNumber returns a block from the current canonical chain. If number is nil, the latest known block is returned.

Note that loading full blocks requires two requests. Use HeaderByNumber if you don't need all transactions or uncle headers.

func (*Client) BlockNumber

func (ec *Client) BlockNumber(ctx context.Context) (uint64, error)

BlockNumber returns the most recent block number

func (*Client) BlockReceipts added in v1.0.4

func (ec *Client) BlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]*types.Receipt, error)

BlockReceipts returns the receipts of a given block number or hash.

func (*Client) CallContract

func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)

CallContract executes a message call transaction, which is directly executed in the VM of the node, but never mined into the blockchain.

blockNumber selects the block height at which the call runs. It can be nil, in which case the code is taken from the latest known block. Note that state from very old blocks might not be available.

func (*Client) CallContractAtHash added in v1.0.4

func (ec *Client) CallContractAtHash(ctx context.Context, msg ethereum.CallMsg, blockHash common.Hash) ([]byte, error)

CallContractAtHash is almost the same as CallContract except that it selects the block by block hash instead of block height.

func (*Client) ChainID

func (ec *Client) ChainID(ctx context.Context) (*big.Int, error)

ChainID retrieves the current chain ID for transaction replay protection.

func (*Client) Client added in v1.0.4

func (ec *Client) Client() *rpc.Client

Client gets the underlying RPC client.

func (*Client) Close

func (ec *Client) Close()

Close closes the underlying RPC connection.

func (*Client) CodeAt

func (ec *Client) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)

CodeAt returns the contract code of the given account. The block number can be nil, in which case the code is taken from the latest known block.

func (*Client) CodeAtHash added in v1.0.4

func (ec *Client) CodeAtHash(ctx context.Context, account common.Address, blockHash common.Hash) ([]byte, error)

CodeAtHash returns the contract code of the given account.

func (*Client) EstimateGas

func (ec *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error)

EstimateGas tries to estimate the gas needed to execute a specific transaction based on the current state of the backend blockchain. There is no guarantee that this is the true gas limit requirement as other transactions may be added or removed by miners, but it should provide a basis for setting a reasonable default.

Note that the state used by this method is implementation-defined by the remote RPC server, but it's reasonable to assume that it will either be the pending or latest state.

func (*Client) EstimateGasAtBlock added in v1.0.4

func (ec *Client) EstimateGasAtBlock(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (uint64, error)

EstimateGasAtBlock is almost the same as EstimateGas except that it selects the block height instead of using the remote RPC's default state for gas estimation.

func (*Client) EstimateGasAtBlockHash added in v1.0.4

func (ec *Client) EstimateGasAtBlockHash(ctx context.Context, msg ethereum.CallMsg, blockHash common.Hash) (uint64, error)

EstimateGasAtBlockHash is almost the same as EstimateGas except that it selects the block hash instead of using the remote RPC's default state for gas estimation.

func (*Client) FeeHistory added in v1.0.4

func (ec *Client) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error)

FeeHistory retrieves the fee market history.

func (*Client) FilterLogs

func (ec *Client) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)

FilterLogs executes a filter query.

func (*Client) FinalizedBlock added in v1.0.4

func (ec *Client) FinalizedBlock(ctx context.Context, verifiedValidatorNum int64, fullTx bool) (*types.Block, error)

GetFinalizedBlock returns the requested finalized block.

func (*Client) FinalizedHeader added in v1.0.4

func (ec *Client) FinalizedHeader(ctx context.Context, verifiedValidatorNum int64) (*types.Header, error)

GetFinalizedHeader returns the requested finalized block header.

func (*Client) GetRootByDiffHash added in v1.0.4

func (ec *Client) GetRootByDiffHash(ctx context.Context, blockNr *big.Int, blockHash common.Hash, diffHash common.Hash) (*core.VerifyResult, error)

func (*Client) HasBuilder added in v1.0.4

func (ec *Client) HasBuilder(ctx context.Context, address common.Address) (bool, error)

HasBuilder returns whether the builder is registered

func (*Client) HeaderByHash

func (ec *Client) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)

HeaderByHash returns the block header with the given hash.

func (*Client) HeaderByNumber

func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)

HeaderByNumber returns a block header from the current canonical chain. If number is nil, the latest known header is returned.

func (*Client) LockBalanceAt

func (ec *Client) LockBalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)

LockBalanceAt returns the wei lock balance of the given account. The block number can be nil, in which case the balance is taken from the latest known block.

func (*Client) LockBalanceAtHash added in v1.0.4

func (ec *Client) LockBalanceAtHash(ctx context.Context, account common.Address, blockHash common.Hash) (*big.Int, error)

LockBalanceAt returns the wei lock balance of the given account. The block number can be nil, in which case the balance is taken from the latest known block.

func (*Client) MevParams added in v1.0.4

func (ec *Client) MevParams(ctx context.Context) (*types.MevParams, error)

MevParams returns the static params of mev

func (*Client) MevRunning added in v1.0.4

func (ec *Client) MevRunning(ctx context.Context) (bool, error)

MevRunning returns whether MEV is running

func (*Client) NetworkID

func (ec *Client) NetworkID(ctx context.Context) (*big.Int, error)

NetworkID returns the network ID for this client.

func (*Client) NonceAt

func (ec *Client) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)

NonceAt returns the account nonce of the given account. The block number can be nil, in which case the nonce is taken from the latest known block.

func (*Client) NonceAtHash added in v1.0.4

func (ec *Client) NonceAtHash(ctx context.Context, account common.Address, blockHash common.Hash) (uint64, error)

NonceAtHash returns the account nonce of the given account.

func (*Client) PeerCount added in v1.0.4

func (ec *Client) PeerCount(ctx context.Context) (uint64, error)

PeerCount returns the number of p2p peers as reported by the net_peerCount method.

func (*Client) PendingBalanceAt

func (ec *Client) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error)

PendingBalanceAt returns the wei balance of the given account in the pending state.

func (*Client) PendingCallContract

func (ec *Client) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error)

PendingCallContract executes a message call transaction using the EVM. The state seen by the contract call is the pending state.

func (*Client) PendingCodeAt

func (ec *Client) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)

PendingCodeAt returns the contract code of the given account in the pending state.

func (*Client) PendingNonceAt

func (ec *Client) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)

PendingNonceAt returns the account nonce of the given account in the pending state. This is the nonce that should be used for the next transaction.

func (*Client) PendingStorageAt

func (ec *Client) PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error)

PendingStorageAt returns the value of key in the contract storage of the given account in the pending state.

func (*Client) PendingTransactionCount

func (ec *Client) PendingTransactionCount(ctx context.Context) (uint, error)

PendingTransactionCount returns the total number of transactions in the pending state.

func (*Client) SendBid added in v1.0.4

func (ec *Client) SendBid(ctx context.Context, args types.BidArgs) (common.Hash, error)

SendBid sends a bid

func (*Client) SendTransaction

func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) error

SendTransaction injects a signed transaction into the pending pool for execution.

If the transaction was a contract creation use the TransactionReceipt method to get the contract address after the transaction has been mined.

func (*Client) SendTransactionConditional added in v1.0.4

func (ec *Client) SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts types.TransactionOpts) error

SendTransactionConditional injects a signed transaction into the pending pool for execution.

If the transaction was a contract creation use the TransactionReceipt method to get the contract address after the transaction has been mined.

func (*Client) StorageAt

func (ec *Client) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error)

StorageAt returns the value of key in the contract storage of the given account. The block number can be nil, in which case the value is taken from the latest known block.

func (*Client) StorageAtHash added in v1.0.4

func (ec *Client) StorageAtHash(ctx context.Context, account common.Address, key common.Hash, blockHash common.Hash) ([]byte, error)

StorageAtHash returns the value of key in the contract storage of the given account.

func (*Client) SubscribeFilterLogs

func (ec *Client) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)

SubscribeFilterLogs subscribes to the results of a streaming filter query.

func (*Client) SubscribeNewFinalizedHeader added in v1.0.4

func (ec *Client) SubscribeNewFinalizedHeader(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error)

SubscribeNewFinalizedHeader subscribes to notifications about the current blockchain finalized header on the given channel.

func (*Client) SubscribeNewHead

func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error)

SubscribeNewHead subscribes to notifications about the current blockchain head on the given channel.

func (*Client) SubscribeNewVotes added in v1.0.4

func (ec *Client) SubscribeNewVotes(ctx context.Context, ch chan<- *types.VoteEnvelope) (ethereum.Subscription, error)

SubscribeNewVotes subscribes to notifications about the new votes into vote pool

func (*Client) SuggestGasPrice

func (ec *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error)

SuggestGasPrice retrieves the currently suggested gas price to allow a timely execution of a transaction.

func (*Client) SuggestGasTipCap added in v1.0.4

func (ec *Client) SuggestGasTipCap(ctx context.Context) (*big.Int, error)

SuggestGasTipCap retrieves the currently suggested gas tip cap after 1559 to allow a timely execution of a transaction.

func (*Client) SyncProgress

func (ec *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error)

SyncProgress retrieves the current progress of the sync algorithm. If there's no sync currently running, it returns nil.

func (*Client) TransactionByHash

func (ec *Client) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error)

TransactionByHash returns the transaction with the given hash.

func (*Client) TransactionCount

func (ec *Client) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error)

TransactionCount returns the total number of transactions in the given block.

func (*Client) TransactionDataAndReceipt

func (ec *Client) TransactionDataAndReceipt(ctx context.Context, txHash common.Hash) (*types.OriginalDataAndReceipt, error)

TransactionDataAndReceipt returns the original data and receipt of a transaction by transaction hash. Note that the receipt is not available for pending transactions.

func (*Client) TransactionInBlock

func (ec *Client) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error)

TransactionInBlock returns a single transaction at index in the given block.

func (*Client) TransactionReceipt

func (ec *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)

TransactionReceipt returns the receipt of a transaction by transaction hash. Note that the receipt is not available for pending transactions.

func (*Client) TransactionRecipientsInBlock

func (ec *Client) TransactionRecipientsInBlock(ctx context.Context, number *big.Int) ([]*types.Receipt, error)

TransactionRecipientsInBlock returns a single transaction at index in the given block.

func (*Client) TransactionSender

func (ec *Client) TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error)

TransactionSender returns the sender address of the given transaction. The transaction must be known to the remote node and included in the blockchain at the given block and index. The sender is the one derived by the protocol at the time of inclusion.

There is a fast-path for transactions retrieved by TransactionByHash and TransactionInBlock. Getting their sender address can be done without an RPC interaction.

func (*Client) TransactionsInBlock

func (ec *Client) TransactionsInBlock(ctx context.Context, number *big.Int) ([]*types.Transaction, error)

TransactionsInBlock returns a single transaction at index in the given block.

Directories

Path Synopsis
Package gethclient provides an RPC client for geth-specific APIs.
Package gethclient provides an RPC client for geth-specific APIs.

Jump to

Keyboard shortcuts

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