-
Notifications
You must be signed in to change notification settings - Fork 3
Add svm balances command with SimClient wiring #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package svm | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/url" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/duneanalytics/cli/output" | ||
| ) | ||
|
|
||
| // NewBalancesCmd returns the `sim svm balances` command. | ||
| func NewBalancesCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "balances <address>", | ||
| Short: "Get SVM token balances for a wallet address", | ||
| Long: "Return token balances for the given SVM wallet address across\n" + | ||
| "Solana and Eclipse chains, including USD valuations.\n\n" + | ||
| "Examples:\n" + | ||
| " dune sim svm balances 86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY\n" + | ||
| " dune sim svm balances 86xCnPeV... --chains solana,eclipse\n" + | ||
| " dune sim svm balances 86xCnPeV... --limit 50 -o json", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: runBalances, | ||
| } | ||
|
|
||
| cmd.Flags().String("chains", "", "Comma-separated chains: solana, eclipse (default: solana)") | ||
| cmd.Flags().Int("limit", 0, "Max results (1-1000, default 1000)") | ||
| cmd.Flags().String("offset", "", "Pagination cursor from previous response") | ||
| output.AddFormatFlag(cmd, "text") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| // --- Response types --- | ||
|
|
||
| type svmBalancesResponse struct { | ||
| ProcessingTimeMs float64 `json:"processing_time_ms,omitempty"` | ||
| WalletAddress string `json:"wallet_address"` | ||
| NextOffset string `json:"next_offset,omitempty"` | ||
| BalancesCount float64 `json:"balances_count,omitempty"` | ||
| Balances []svmBalanceEntry `json:"balances"` | ||
| } | ||
|
|
||
| type svmBalanceEntry struct { | ||
| Chain string `json:"chain"` | ||
| Address string `json:"address"` | ||
| Amount string `json:"amount"` | ||
| Balance string `json:"balance,omitempty"` | ||
| RawBalance string `json:"raw_balance,omitempty"` | ||
| ValueUSD float64 `json:"value_usd,omitempty"` | ||
| ProgramID *string `json:"program_id,omitempty"` | ||
| Decimals float64 `json:"decimals,omitempty"` | ||
| TotalSupply string `json:"total_supply,omitempty"` | ||
| Name string `json:"name,omitempty"` | ||
| Symbol string `json:"symbol,omitempty"` | ||
| URI *string `json:"uri,omitempty"` | ||
| PriceUSD float64 `json:"price_usd,omitempty"` | ||
| LiquidityUSD float64 `json:"liquidity_usd,omitempty"` | ||
| PoolType *string `json:"pool_type,omitempty"` | ||
| PoolAddress *string `json:"pool_address,omitempty"` | ||
| MintAuthority *string `json:"mint_authority,omitempty"` | ||
| } | ||
|
|
||
| func runBalances(cmd *cobra.Command, args []string) error { | ||
| client, err := requireSimClient(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| address := args[0] | ||
| params := url.Values{} | ||
|
|
||
| if v, _ := cmd.Flags().GetString("chains"); v != "" { | ||
| params.Set("chains", v) | ||
| } | ||
| if v, _ := cmd.Flags().GetInt("limit"); v > 0 { | ||
| params.Set("limit", fmt.Sprintf("%d", v)) | ||
| } | ||
| if v, _ := cmd.Flags().GetString("offset"); v != "" { | ||
| params.Set("offset", v) | ||
| } | ||
|
|
||
| data, err := client.Get(cmd.Context(), "/beta/svm/balances/"+address, params) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| w := cmd.OutOrStdout() | ||
| switch output.FormatFromCmd(cmd) { | ||
| case output.FormatJSON: | ||
| var raw json.RawMessage = data | ||
| return output.PrintJSON(w, raw) | ||
| default: | ||
| var resp svmBalancesResponse | ||
| if err := json.Unmarshal(data, &resp); err != nil { | ||
| return fmt.Errorf("parsing response: %w", err) | ||
| } | ||
|
|
||
| if len(resp.Balances) == 0 { | ||
| fmt.Fprintln(w, "No balances found.") | ||
| return nil | ||
| } | ||
|
|
||
| columns := []string{"CHAIN", "SYMBOL", "BALANCE", "PRICE_USD", "VALUE_USD"} | ||
| rows := make([][]string, len(resp.Balances)) | ||
| for i, b := range resp.Balances { | ||
| bal := b.Balance | ||
| if bal == "" { | ||
| bal = b.Amount | ||
| } | ||
| rows[i] = []string{ | ||
| b.Chain, | ||
| b.Symbol, | ||
| bal, | ||
| output.FormatUSD(b.PriceUSD), | ||
| output.FormatUSD(b.ValueUSD), | ||
| } | ||
| } | ||
| output.PrintTable(w, columns, rows) | ||
|
|
||
| if resp.NextOffset != "" { | ||
| fmt.Fprintf(w, "\nNext offset: %s\n", resp.NextOffset) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package svm_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSvmBalances_Text(t *testing.T) { | ||
| key := simAPIKey(t) | ||
|
|
||
| root := newSimTestRoot() | ||
| var buf bytes.Buffer | ||
| root.SetOut(&buf) | ||
| root.SetArgs([]string{"sim", "--sim-api-key", key, "svm", "balances", svmTestAddress}) | ||
|
|
||
| require.NoError(t, root.Execute()) | ||
|
|
||
| out := buf.String() | ||
| assert.Contains(t, out, "CHAIN") | ||
| assert.Contains(t, out, "SYMBOL") | ||
| assert.Contains(t, out, "BALANCE") | ||
| assert.Contains(t, out, "PRICE_USD") | ||
| assert.Contains(t, out, "VALUE_USD") | ||
| } | ||
|
|
||
| func TestSvmBalances_JSON(t *testing.T) { | ||
| key := simAPIKey(t) | ||
|
|
||
| root := newSimTestRoot() | ||
| var buf bytes.Buffer | ||
| root.SetOut(&buf) | ||
| root.SetArgs([]string{"sim", "--sim-api-key", key, "svm", "balances", svmTestAddress, "-o", "json"}) | ||
|
|
||
| require.NoError(t, root.Execute()) | ||
|
|
||
| var resp map[string]interface{} | ||
| require.NoError(t, json.Unmarshal(buf.Bytes(), &resp)) | ||
| assert.Contains(t, resp, "wallet_address") | ||
| assert.Contains(t, resp, "balances") | ||
|
|
||
| balances, ok := resp["balances"].([]interface{}) | ||
| require.True(t, ok) | ||
| if len(balances) > 0 { | ||
| b, ok := balances[0].(map[string]interface{}) | ||
| require.True(t, ok) | ||
| assert.Contains(t, b, "chain") | ||
| assert.Contains(t, b, "address") | ||
| assert.Contains(t, b, "amount") | ||
| } | ||
| } | ||
|
|
||
| func TestSvmBalances_WithChains(t *testing.T) { | ||
| key := simAPIKey(t) | ||
|
|
||
| root := newSimTestRoot() | ||
| var buf bytes.Buffer | ||
| root.SetOut(&buf) | ||
| root.SetArgs([]string{"sim", "--sim-api-key", key, "svm", "balances", svmTestAddress, "--chains", "solana", "-o", "json"}) | ||
|
|
||
| require.NoError(t, root.Execute()) | ||
|
|
||
| var resp map[string]interface{} | ||
| require.NoError(t, json.Unmarshal(buf.Bytes(), &resp)) | ||
| assert.Contains(t, resp, "balances") | ||
|
|
||
| // All balances should be on solana chain. | ||
| balances, ok := resp["balances"].([]interface{}) | ||
| require.True(t, ok) | ||
| for _, bal := range balances { | ||
| b, ok := bal.(map[string]interface{}) | ||
| require.True(t, ok) | ||
| assert.Equal(t, "solana", b["chain"]) | ||
| } | ||
| } | ||
|
|
||
| func TestSvmBalances_Limit(t *testing.T) { | ||
| key := simAPIKey(t) | ||
|
|
||
| root := newSimTestRoot() | ||
| var buf bytes.Buffer | ||
| root.SetOut(&buf) | ||
| root.SetArgs([]string{"sim", "--sim-api-key", key, "svm", "balances", svmTestAddress, "--limit", "3", "-o", "json"}) | ||
|
|
||
| require.NoError(t, root.Execute()) | ||
|
|
||
| var resp map[string]interface{} | ||
| require.NoError(t, json.Unmarshal(buf.Bytes(), &resp)) | ||
|
|
||
| balances, ok := resp["balances"].([]interface{}) | ||
| require.True(t, ok) | ||
| assert.LessOrEqual(t, len(balances), 3) | ||
| } | ||
|
|
||
| func TestSvmBalances_Pagination(t *testing.T) { | ||
| key := simAPIKey(t) | ||
|
|
||
| root := newSimTestRoot() | ||
| var buf bytes.Buffer | ||
| root.SetOut(&buf) | ||
| root.SetArgs([]string{"sim", "--sim-api-key", key, "svm", "balances", svmTestAddress, "--limit", "2", "-o", "json"}) | ||
|
|
||
| require.NoError(t, root.Execute()) | ||
|
|
||
| var resp map[string]interface{} | ||
| require.NoError(t, json.Unmarshal(buf.Bytes(), &resp)) | ||
| assert.Contains(t, resp, "balances") | ||
|
|
||
| // If next_offset is present, fetch page 2. | ||
| if offset, ok := resp["next_offset"].(string); ok && offset != "" { | ||
| root2 := newSimTestRoot() | ||
| var buf2 bytes.Buffer | ||
| root2.SetOut(&buf2) | ||
| root2.SetArgs([]string{"sim", "--sim-api-key", key, "svm", "balances", svmTestAddress, "--limit", "2", "--offset", offset, "-o", "json"}) | ||
|
|
||
| require.NoError(t, root2.Execute()) | ||
|
|
||
| var resp2 map[string]interface{} | ||
| require.NoError(t, json.Unmarshal(buf2.Bytes(), &resp2)) | ||
| assert.Contains(t, resp2, "balances") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package svm_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/duneanalytics/cli/cmd/sim" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // simAPIKey returns the DUNE_SIM_API_KEY env var or skips the test. | ||
| func simAPIKey(t *testing.T) string { | ||
| t.Helper() | ||
| key := os.Getenv("DUNE_SIM_API_KEY") | ||
| if key == "" { | ||
| t.Skip("DUNE_SIM_API_KEY not set, skipping e2e test") | ||
| } | ||
| return key | ||
| } | ||
|
|
||
| const svmTestAddress = "86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY" | ||
|
|
||
| // newSimTestRoot builds the full command tree: dune -> sim -> svm -> <subcommands>. | ||
| // Used for authenticated E2E tests. Pass the API key via --sim-api-key in SetArgs. | ||
| func newSimTestRoot() *cobra.Command { | ||
| root := &cobra.Command{Use: "dune"} | ||
| root.SetContext(context.Background()) | ||
| root.AddCommand(sim.NewSimCmd()) | ||
| return root | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.