|
| 1 | +package evm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | + |
| 15 | + "github.com/duneanalytics/cli/output" |
| 16 | +) |
| 17 | + |
| 18 | +// NewSupportedChainsCmd returns the `sim evm supported-chains` command. |
| 19 | +func NewSupportedChainsCmd() *cobra.Command { |
| 20 | + cmd := &cobra.Command{ |
| 21 | + Use: "supported-chains", |
| 22 | + Short: "List supported EVM chains and their endpoint availability", |
| 23 | + Long: "Display all EVM chains supported by the Sim API and which endpoints\n" + |
| 24 | + "(balances, activity, transactions, etc.) are available for each chain.\n\n" + |
| 25 | + "This endpoint is public and does not require a Sim API key.\n\n" + |
| 26 | + "Examples:\n" + |
| 27 | + " dune sim evm supported-chains\n" + |
| 28 | + " dune sim evm supported-chains -o json", |
| 29 | + Annotations: map[string]string{"skipSimAuth": "true"}, |
| 30 | + RunE: runSupportedChains, |
| 31 | + } |
| 32 | + |
| 33 | + output.AddFormatFlag(cmd, "text") |
| 34 | + |
| 35 | + return cmd |
| 36 | +} |
| 37 | + |
| 38 | +type supportedChainsResponse struct { |
| 39 | + Chains []chainEntry `json:"chains"` |
| 40 | +} |
| 41 | + |
| 42 | +type chainEntry struct { |
| 43 | + Name string `json:"name"` |
| 44 | + ChainID json.Number `json:"chain_id"` |
| 45 | + Tags []string `json:"tags"` |
| 46 | + Balances endpointSupport `json:"balances"` |
| 47 | + Activity endpointSupport `json:"activity"` |
| 48 | + Transactions endpointSupport `json:"transactions"` |
| 49 | + TokenInfo endpointSupport `json:"token_info"` |
| 50 | + TokenHolders endpointSupport `json:"token_holders"` |
| 51 | + Collectibles endpointSupport `json:"collectibles"` |
| 52 | + DefiPositions endpointSupport `json:"defi_positions"` |
| 53 | +} |
| 54 | + |
| 55 | +type endpointSupport struct { |
| 56 | + Supported bool `json:"supported"` |
| 57 | +} |
| 58 | + |
| 59 | +func runSupportedChains(cmd *cobra.Command, _ []string) error { |
| 60 | + // This endpoint is public (no auth required). Use the context client |
| 61 | + // if available, otherwise create a bare HTTP client. |
| 62 | + client := SimClientFromCmd(cmd) |
| 63 | + if client == nil { |
| 64 | + client = newBareSimClient() |
| 65 | + } |
| 66 | + |
| 67 | + data, err := client.Get(cmd.Context(), "/v1/evm/supported-chains", nil) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + w := cmd.OutOrStdout() |
| 73 | + switch output.FormatFromCmd(cmd) { |
| 74 | + case output.FormatJSON: |
| 75 | + var raw json.RawMessage = data |
| 76 | + return output.PrintJSON(w, raw) |
| 77 | + default: |
| 78 | + var resp supportedChainsResponse |
| 79 | + if err := json.Unmarshal(data, &resp); err != nil { |
| 80 | + return fmt.Errorf("parsing response: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + columns := []string{ |
| 84 | + "NAME", "CHAIN_ID", "TAGS", |
| 85 | + "BALANCES", "ACTIVITY", "TXS", |
| 86 | + "TOKEN_INFO", "HOLDERS", "COLLECTIBLES", "DEFI", |
| 87 | + } |
| 88 | + rows := make([][]string, len(resp.Chains)) |
| 89 | + for i, c := range resp.Chains { |
| 90 | + rows[i] = []string{ |
| 91 | + c.Name, |
| 92 | + c.ChainID.String(), |
| 93 | + strings.Join(c.Tags, ","), |
| 94 | + boolYN(c.Balances.Supported), |
| 95 | + boolYN(c.Activity.Supported), |
| 96 | + boolYN(c.Transactions.Supported), |
| 97 | + boolYN(c.TokenInfo.Supported), |
| 98 | + boolYN(c.TokenHolders.Supported), |
| 99 | + boolYN(c.Collectibles.Supported), |
| 100 | + boolYN(c.DefiPositions.Supported), |
| 101 | + } |
| 102 | + } |
| 103 | + output.PrintTable(w, columns, rows) |
| 104 | + return nil |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func boolYN(b bool) string { |
| 109 | + if b { |
| 110 | + return "Y" |
| 111 | + } |
| 112 | + return "N" |
| 113 | +} |
| 114 | + |
| 115 | +// bareSimClient is a minimal HTTP client for public Sim API endpoints |
| 116 | +// that don't require authentication. |
| 117 | +type bareSimClient struct { |
| 118 | + httpClient *http.Client |
| 119 | +} |
| 120 | + |
| 121 | +func newBareSimClient() *bareSimClient { |
| 122 | + return &bareSimClient{ |
| 123 | + httpClient: &http.Client{Timeout: 30 * time.Second}, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func (c *bareSimClient) Get(ctx context.Context, path string, params url.Values) ([]byte, error) { |
| 128 | + u, err := url.Parse("https://api.sim.dune.com" + path) |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("invalid URL: %w", err) |
| 131 | + } |
| 132 | + if params != nil { |
| 133 | + u.RawQuery = params.Encode() |
| 134 | + } |
| 135 | + |
| 136 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) |
| 137 | + if err != nil { |
| 138 | + return nil, fmt.Errorf("creating request: %w", err) |
| 139 | + } |
| 140 | + req.Header.Set("Accept", "application/json") |
| 141 | + |
| 142 | + resp, err := c.httpClient.Do(req) |
| 143 | + if err != nil { |
| 144 | + return nil, fmt.Errorf("request failed: %w", err) |
| 145 | + } |
| 146 | + defer resp.Body.Close() |
| 147 | + |
| 148 | + body, err := io.ReadAll(resp.Body) |
| 149 | + if err != nil { |
| 150 | + return nil, fmt.Errorf("reading response: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + if resp.StatusCode >= 400 { |
| 154 | + return nil, fmt.Errorf("Sim API error (HTTP %d)", resp.StatusCode) |
| 155 | + } |
| 156 | + |
| 157 | + return body, nil |
| 158 | +} |
0 commit comments