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