ethclient: add Version method to retrieve client version

This commit is contained in:
tr1sm0s1n 2025-03-05 17:13:38 +05:30
parent 7405dc5c4b
commit 406325a50c
No known key found for this signature in database
GPG key ID: BC64CB662BFBBFE4
2 changed files with 26 additions and 0 deletions

View file

@ -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

View file

@ -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)