|
| 1 | +package evm |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/duneanalytics/cli/output" |
| 11 | +) |
| 12 | + |
| 13 | +// NewSupportedChainsCmd returns the `sim evm supported-chains` command. |
| 14 | +func NewSupportedChainsCmd() *cobra.Command { |
| 15 | + cmd := &cobra.Command{ |
| 16 | + Use: "supported-chains", |
| 17 | + Short: "List supported EVM chains and their endpoint availability", |
| 18 | + Long: "Display all EVM chains supported by the Sim API and which endpoints\n" + |
| 19 | + "(balances, activity, transactions, etc.) are available for each chain.\n\n" + |
| 20 | + "This endpoint is public and does not require a Sim API key.\n\n" + |
| 21 | + "Examples:\n" + |
| 22 | + " dune sim evm supported-chains\n" + |
| 23 | + " dune sim evm supported-chains -o json", |
| 24 | + Annotations: map[string]string{"skipSimAuth": "true"}, |
| 25 | + RunE: runSupportedChains, |
| 26 | + } |
| 27 | + |
| 28 | + output.AddFormatFlag(cmd, "text") |
| 29 | + |
| 30 | + return cmd |
| 31 | +} |
| 32 | + |
| 33 | +type supportedChainsResponse struct { |
| 34 | + Chains []chainEntry `json:"chains"` |
| 35 | +} |
| 36 | + |
| 37 | +type chainEntry struct { |
| 38 | + Name string `json:"name"` |
| 39 | + ChainID json.Number `json:"chain_id"` |
| 40 | + Tags []string `json:"tags"` |
| 41 | + Balances endpointSupport `json:"balances"` |
| 42 | + Activity endpointSupport `json:"activity"` |
| 43 | + Transactions endpointSupport `json:"transactions"` |
| 44 | + TokenInfo endpointSupport `json:"token_info"` |
| 45 | + TokenHolders endpointSupport `json:"token_holders"` |
| 46 | + Collectibles endpointSupport `json:"collectibles"` |
| 47 | + DefiPositions endpointSupport `json:"defi_positions"` |
| 48 | +} |
| 49 | + |
| 50 | +type endpointSupport struct { |
| 51 | + Supported bool `json:"supported"` |
| 52 | +} |
| 53 | + |
| 54 | +func runSupportedChains(cmd *cobra.Command, _ []string) error { |
| 55 | + client := SimClientFromCmd(cmd) |
| 56 | + if client == nil { |
| 57 | + return fmt.Errorf("sim client not initialized") |
| 58 | + } |
| 59 | + |
| 60 | + data, err := client.Get(cmd.Context(), "/v1/evm/supported-chains", nil) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + w := cmd.OutOrStdout() |
| 66 | + switch output.FormatFromCmd(cmd) { |
| 67 | + case output.FormatJSON: |
| 68 | + var raw json.RawMessage = data |
| 69 | + return output.PrintJSON(w, raw) |
| 70 | + default: |
| 71 | + var resp supportedChainsResponse |
| 72 | + if err := json.Unmarshal(data, &resp); err != nil { |
| 73 | + return fmt.Errorf("parsing response: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + columns := []string{ |
| 77 | + "NAME", "CHAIN_ID", "TAGS", |
| 78 | + "BALANCES", "ACTIVITY", "TXS", |
| 79 | + "TOKEN_INFO", "HOLDERS", "COLLECTIBLES", "DEFI", |
| 80 | + } |
| 81 | + rows := make([][]string, len(resp.Chains)) |
| 82 | + for i, c := range resp.Chains { |
| 83 | + rows[i] = []string{ |
| 84 | + c.Name, |
| 85 | + c.ChainID.String(), |
| 86 | + strings.Join(c.Tags, ","), |
| 87 | + boolYN(c.Balances.Supported), |
| 88 | + boolYN(c.Activity.Supported), |
| 89 | + boolYN(c.Transactions.Supported), |
| 90 | + boolYN(c.TokenInfo.Supported), |
| 91 | + boolYN(c.TokenHolders.Supported), |
| 92 | + boolYN(c.Collectibles.Supported), |
| 93 | + boolYN(c.DefiPositions.Supported), |
| 94 | + } |
| 95 | + } |
| 96 | + output.PrintTable(w, columns, rows) |
| 97 | + return nil |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func boolYN(b bool) string { |
| 102 | + if b { |
| 103 | + return "Y" |
| 104 | + } |
| 105 | + return "N" |
| 106 | +} |
0 commit comments