From 406325a50cfc614e589ab714430e09ba670ac97c Mon Sep 17 00:00:00 2001 From: tr1sm0s1n Date: Wed, 5 Mar 2025 17:13:38 +0530 Subject: [PATCH] ethclient: add Version method to retrieve client version --- ethclient/ethclient.go | 9 +++++++++ ethclient/ethclient_test.go | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 3d6a28eabf..d9f07fded6 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -77,6 +77,15 @@ func (ec *Client) ChainID(ctx context.Context) (*big.Int, error) { return (*big.Int)(&result), err } +// Version retrieves the current client version. +func (ec *Client) Version(ctx context.Context) (string, error) { + var version string + if err := ec.c.CallContext(ctx, &version, "web3_clientVersion"); err != nil { + return "", err + } + return version, nil +} + // BlockByHash returns the given full block. // // Note that loading full blocks requires two requests. Use HeaderByHash diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 787cad3f96..e71ba4a404 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -22,7 +22,10 @@ import ( "errors" "fmt" "math/big" + "os" + "path/filepath" "reflect" + "runtime" "testing" "time" @@ -165,6 +168,9 @@ func TestEthClient(t *testing.T) { "ChainID": { func(t *testing.T) { testChainID(t, client) }, }, + "Version": { + func(t *testing.T) { testVersion(t, client) }, + }, "GetBlock": { func(t *testing.T) { testGetBlock(t, client) }, }, @@ -320,6 +326,17 @@ func testChainID(t *testing.T, client *rpc.Client) { } } +func testVersion(t *testing.T, client *rpc.Client) { + ec := ethclient.NewClient(client) + v, err := ec.Version(context.Background()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if v != fmt.Sprintf("%s/%s-%s/%s", filepath.Base(os.Args[0]), runtime.GOOS, runtime.GOARCH, runtime.Version()) { + t.Fatalf("Version returned wrong value: %s", v) + } +} + func testGetBlock(t *testing.T, client *rpc.Client) { ec := ethclient.NewClient(client)