Skip to content

Commit c592a1d

Browse files
Add balance cmd to CLI (#1691)
1 parent 7fac7fa commit c592a1d

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

cmd/hypersdk-cli/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ For interactive input:
126126
hypersdk-cli tx Transfer
127127
```
128128

129+
### balance
130+
131+
Query the balance of an address
132+
133+
```bash
134+
hypersdk-cli balance --sender 0x00c4cb545f748a28770042f893784ce85b107389004d6a0e0d6d7518eeae1292d9
135+
```
136+
137+
If `--sender` isn't provided, the address associated with the private key in
138+
`~/.hypersdk-cli/config.yaml` is queried.
139+
140+
129141
## Notes
130142

131143
- Only flat actions are supported. Arrays, slices, embedded structs, maps, and struct fields are not supported.
@@ -134,7 +146,6 @@ hypersdk-cli tx Transfer
134146

135147
## Known Issues
136148

137-
- The `balance` command is not currently implemented due to the lack of a standardized balance RPC method at the HyperSDK level.
138149
- The `maxFee` for transactions is currently hardcoded to 1,000,000.
139150
- The `key set` and `endpoint set` commands use a nested command structure which adds unnecessary complexity for a small CLI tool. A flatter command structure would be more appropriate.
140151
- Currency values are represented as uint64 without decimal point support in the ABI. The CLI cannot automatically parse decimal inputs (e.g. "12.0") since there is no currency type annotation. Users must enter the raw uint64 value including all decimal places (e.g. "12000000000" for 12 coins with 9 decimal places).

cmd/hypersdk-cli/balance.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package main
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"strings"
10+
11+
"github.com/spf13/cobra"
12+
13+
"github.com/ava-labs/hypersdk/api/jsonrpc"
14+
"github.com/ava-labs/hypersdk/auth"
15+
"github.com/ava-labs/hypersdk/codec"
16+
)
17+
18+
var balanceCmd = &cobra.Command{
19+
Use: "balance [address]",
20+
Short: "Get the balance of an address",
21+
RunE: func(cmd *cobra.Command, _ []string) error {
22+
// 1. figure out sender address
23+
addressStr, err := cmd.Flags().GetString("sender")
24+
if err != nil {
25+
return fmt.Errorf("failed to get sender: %w", err)
26+
}
27+
28+
var address codec.Address
29+
30+
if addressStr != "" {
31+
address, err = codec.StringToAddress(addressStr)
32+
if err != nil {
33+
return fmt.Errorf("failed to convert sender to address: %w", err)
34+
}
35+
} else {
36+
// ok, infer user's address from the private key
37+
keyString, err := getConfigValue(cmd, "key", true)
38+
if err != nil {
39+
return fmt.Errorf("failed to get key from config: %w", err)
40+
}
41+
key, err := privateKeyFromString(keyString)
42+
if err != nil {
43+
return fmt.Errorf("failed to decode key: %w", err)
44+
}
45+
address = auth.NewED25519Address(key.PublicKey())
46+
}
47+
48+
// 2. create client
49+
endpoint, err := getConfigValue(cmd, "endpoint", true)
50+
if err != nil {
51+
return fmt.Errorf("failed to get endpoint: %w", err)
52+
}
53+
client := jsonrpc.NewJSONRPCClient(endpoint)
54+
55+
// 3. get balance
56+
balance, err := client.GetBalance(context.Background(), address)
57+
errorString := ""
58+
if err != nil {
59+
errorString = err.Error()
60+
}
61+
62+
return printValue(cmd, balanceResponse{
63+
Balance: balance,
64+
BalanceError: errorString,
65+
})
66+
},
67+
}
68+
69+
type balanceResponse struct {
70+
Balance uint64 `json:"balance"`
71+
BalanceError string `json:"error"`
72+
}
73+
74+
func (b balanceResponse) String() string {
75+
var result strings.Builder
76+
if b.BalanceError != "" {
77+
result.WriteString(fmt.Sprintf("❌ Error: %s\n", b.BalanceError))
78+
} else {
79+
result.WriteString(fmt.Sprintf("✅ Balance: %d\n", b.Balance))
80+
}
81+
82+
return result.String()
83+
}
84+
85+
func init() {
86+
balanceCmd.Flags().String("sender", "", "Address being queried in hex")
87+
rootCmd.AddCommand(balanceCmd)
88+
}

0 commit comments

Comments
 (0)