From ace888b9cfda79ad535f05bddaeacc6573a41935 Mon Sep 17 00:00:00 2001 From: EdisonSR <61781882@qq.com> Date: Sat, 4 Jan 2025 16:30:49 +0800 Subject: [PATCH 1/2] add_get_block_by_tag_function --- ethclient/ethclient.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index f10626c01f..0a08d1e326 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -94,6 +94,13 @@ func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Bl 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 func (ec *Client) BlockNumber(ctx context.Context) (uint64, error) { var result hexutil.Uint64 From 08a90ad171432ce4af1810088478b509a148c8be Mon Sep 17 00:00:00 2001 From: EdisonSR <61781882@qq.com> Date: Thu, 9 Jan 2025 09:25:29 +0800 Subject: [PATCH 2/2] Detail how to use the BlockByNumber method when special labels are used --- ethclient/ethclient.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 0a08d1e326..eeb67f9bb4 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -90,6 +90,12 @@ func (ec *Client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Blo // // Note that loading full blocks requires two requests. Use HeaderByNumber // 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 +// UnmarshalJSON fuction to get the corresponding number +// eg: +// var bn rpc.BlockNumber +// err := json.Unmarshal([]byte(`"finalized"`), &bn) +// you will get -3 and you can use for the number param func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) { return ec.getBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), true) }