ethclient: enhance the function description

This commit is contained in:
Gary Rong 2025-02-18 12:55:08 +08:00
parent 08a90ad171
commit 56c1e516d8

View file

@ -85,28 +85,27 @@ func (ec *Client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Blo
return ec.getBlock(ctx, "eth_getBlockByHash", hash, true) return ec.getBlock(ctx, "eth_getBlockByHash", hash, true)
} }
// BlockByNumber returns a block from the current canonical chain. If number is nil, the // BlockByNumber returns a block from the current canonical chain.
// latest known block is returned. // If `number` is nil, the latest known block is returned.
// //
// Note that loading full blocks requires two requests. Use HeaderByNumber // Use `HeaderByNumber` if you don't need full transaction data or uncle headers.
// if you don't need all transactions or uncle headers. //
// if you want to use tag like earliest, latest, finalized, safe, or finalized, you can use the // Supported special block number tags:
// UnmarshalJSON fuction to get the corresponding number // - `earliest` : The genesis (earliest) block
// eg: // - `latest` : The most recently included block
// var bn rpc.BlockNumber // - `safe` : The latest safe head block
// err := json.Unmarshal([]byte(`"finalized"`), &bn) // - `finalized` : The latest finalized block
// you will get -3 and you can use for the number param // - `pending` : The pending block
//
// Example usage:
//
// ```go
// BlockByNumber(context.Background(), big.NewInt(int64(rpc.LatestBlockNumber)))
// ```
func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) { func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
return ec.getBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), true) return ec.getBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), true)
} }
// BlockByNumber returns a block from the current canonical chain. If tag is nil, the
// latest known block is returned.
// you can use the special tag earliest, latest, finalized, safe, or finalized.
func (ec *Client) BlockBySpecialTag(ctx context.Context, tag string) (*types.Block, error) {
return ec.getBlock(ctx, "eth_getBlockByNumber", tag, true)
}
// BlockNumber returns the most recent block number // BlockNumber returns the most recent block number
func (ec *Client) BlockNumber(ctx context.Context) (uint64, error) { func (ec *Client) BlockNumber(ctx context.Context) (uint64, error) {
var result hexutil.Uint64 var result hexutil.Uint64
@ -223,8 +222,21 @@ func (ec *Client) HeaderByHash(ctx context.Context, hash common.Hash) (*types.He
return head, err return head, err
} }
// HeaderByNumber returns a block header from the current canonical chain. If number is // HeaderByNumber returns a block header from the current canonical chain.
// nil, the latest known header is returned. // If `number` is nil, the latest known block header is returned.
//
// Supported special block number tags:
// - `earliest` : The genesis (earliest) block
// - `latest` : The most recently included block
// - `safe` : The latest safe head block
// - `finalized` : The latest finalized block
// - `pending` : The pending block
//
// Example usage:
//
// ```go
// HeaderByNumber(context.Background(), big.NewInt(int64(rpc.LatestBlockNumber)))
// ```
func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) { func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
var head *types.Header var head *types.Header
err := ec.c.CallContext(ctx, &head, "eth_getBlockByNumber", toBlockNumArg(number), false) err := ec.c.CallContext(ctx, &head, "eth_getBlockByNumber", toBlockNumArg(number), false)