|
| 1 | +package evm |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/duneanalytics/cli/output" |
| 11 | +) |
| 12 | + |
| 13 | +// NewBalanceCmd returns the `sim evm balance` command (single token). |
| 14 | +func NewBalanceCmd() *cobra.Command { |
| 15 | + cmd := &cobra.Command{ |
| 16 | + Use: "balance <wallet_address>", |
| 17 | + Short: "Get balance for a single token", |
| 18 | + Long: "Return the balance of a single token for the given wallet address on one chain.\n" + |
| 19 | + "Use \"native\" as the --token value to query the native asset (e.g. ETH).\n\n" + |
| 20 | + "Examples:\n" + |
| 21 | + " dune sim evm balance 0xd8da... --token native --chain-ids 1\n" + |
| 22 | + " dune sim evm balance 0xd8da... --token 0xa0b8...eb48 --chain-ids 8453\n" + |
| 23 | + " dune sim evm balance 0xd8da... --token native --chain-ids 1 -o json", |
| 24 | + Args: cobra.ExactArgs(1), |
| 25 | + RunE: runBalance, |
| 26 | + } |
| 27 | + |
| 28 | + cmd.Flags().String("token", "", "Token contract address or \"native\" (required)") |
| 29 | + cmd.Flags().String("chain-ids", "", "Chain ID (required)") |
| 30 | + _ = cmd.MarkFlagRequired("token") |
| 31 | + _ = cmd.MarkFlagRequired("chain-ids") |
| 32 | + output.AddFormatFlag(cmd, "text") |
| 33 | + |
| 34 | + return cmd |
| 35 | +} |
| 36 | + |
| 37 | +func runBalance(cmd *cobra.Command, args []string) error { |
| 38 | + client := SimClientFromCmd(cmd) |
| 39 | + if client == nil { |
| 40 | + return fmt.Errorf("sim client not initialized") |
| 41 | + } |
| 42 | + |
| 43 | + address := args[0] |
| 44 | + tokenAddress, _ := cmd.Flags().GetString("token") |
| 45 | + |
| 46 | + params := url.Values{} |
| 47 | + if v, _ := cmd.Flags().GetString("chain-ids"); v != "" { |
| 48 | + params.Set("chain_ids", v) |
| 49 | + } |
| 50 | + |
| 51 | + path := fmt.Sprintf("/v1/evm/balances/%s/token/%s", address, tokenAddress) |
| 52 | + data, err := client.Get(cmd.Context(), path, params) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + w := cmd.OutOrStdout() |
| 58 | + switch output.FormatFromCmd(cmd) { |
| 59 | + case output.FormatJSON: |
| 60 | + var raw json.RawMessage = data |
| 61 | + return output.PrintJSON(w, raw) |
| 62 | + default: |
| 63 | + var resp balancesResponse |
| 64 | + if err := json.Unmarshal(data, &resp); err != nil { |
| 65 | + return fmt.Errorf("parsing response: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + if len(resp.Balances) == 0 { |
| 69 | + fmt.Fprintln(w, "No balance found.") |
| 70 | + return nil |
| 71 | + } |
| 72 | + |
| 73 | + b := resp.Balances[0] |
| 74 | + fmt.Fprintf(w, "Chain: %s (ID: %d)\n", b.Chain, b.ChainID) |
| 75 | + fmt.Fprintf(w, "Token: %s\n", b.Address) |
| 76 | + fmt.Fprintf(w, "Symbol: %s\n", b.Symbol) |
| 77 | + if b.Name != "" { |
| 78 | + fmt.Fprintf(w, "Name: %s\n", b.Name) |
| 79 | + } |
| 80 | + fmt.Fprintf(w, "Decimals: %d\n", b.Decimals) |
| 81 | + fmt.Fprintf(w, "Amount: %s\n", formatAmount(b.Amount, b.Decimals)) |
| 82 | + fmt.Fprintf(w, "Price USD: %s\n", formatUSD(b.PriceUSD)) |
| 83 | + fmt.Fprintf(w, "Value USD: %s\n", formatUSD(b.ValueUSD)) |
| 84 | + |
| 85 | + return nil |
| 86 | + } |
| 87 | +} |
0 commit comments