Skip to content

Commit

Permalink
add eth_getBalance test (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother authored Mar 30, 2023
1 parent 24a953f commit 519051f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
25 changes: 25 additions & 0 deletions node/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,28 @@ func (c *client) GetAccounts(ctx context.Context) ([]eth.Address, error) {

return accountList, nil
}

func (c *client) GetBalance(ctx context.Context, addr eth.Address, numberOrTag eth.BlockNumberOrTag) (uint64, error) {
request := jsonrpc.Request{
ID: jsonrpc.ID{Num: 1},
Method: "eth_getBalance",
Params: jsonrpc.MustParams(addr, &numberOrTag),
}

applyContext(ctx, &request)
response, err := c.Request(ctx, &request)
if err != nil {
return 0, errors.Wrap(err, "could not make request")
}
if response.Error != nil {
return 0, errors.New(string(*response.Error))
}

q := eth.Quantity{}
err = json.Unmarshal(response.Result, &q)
if err != nil {
return 0, errors.Wrap(err, "could not decode result")
}

return q.UInt64(), nil
}
5 changes: 4 additions & 1 deletion node/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ type Client interface {
// is used to call read-only functions of a smart contract
Call(ctx context.Context, msg eth.Transaction, numberOrTag eth.BlockNumberOrTag) (string, error)

// executes get_accounts and retrieves address array
// executes get_accounts and retrieves address array
GetAccounts(ctx context.Context) ([]eth.Address, error)

// GetBalance returns available balance
GetBalance(ctx context.Context, addr eth.Address, numberOrTag eth.BlockNumberOrTag) (uint64, error)

// ChainId returns the chain id
ChainId(ctx context.Context) (string, error)

Expand Down
19 changes: 17 additions & 2 deletions node/mocks/node.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion node/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func getClient(t *testing.T, ctx context.Context) node.Client {
base_url := os.Getenv("ETHLIBS_TEST_URL")
if base_url == "" {
t.Skip("ETHLIBS_TEST_URL not set, skipping test. Set to a valid websocket URL to execute this test.")
t.Skip("ETHLIBS_TEST_URL not set, skipping test. Set to a valid http/ws URL to execute this test.")
}
auth_id := os.Getenv("AUTH_ID")
if auth_id == "" {
Expand Down Expand Up @@ -66,6 +66,15 @@ func TestConnection_Get_Accounts(t *testing.T) {
require.Empty(t, accountList)
}

func TestConnection_Get_Balance(t *testing.T) {
ctx := context.Background()
conn := getClient(t, ctx)

bal, err := conn.GetBalance(ctx, *eth.MustAddress("0x148772F29058DcC772613260b078dCa8C14afF6c"), *eth.MustBlockNumberOrTag("latest"))
require.NoError(t, err)
require.NotNil(t, bal)
}

func TestConnection_GetTransactionCount(t *testing.T) {
ctx := context.Background()
conn := getClient(t, ctx)
Expand Down

0 comments on commit 519051f

Please sign in to comment.