-
Notifications
You must be signed in to change notification settings - Fork 1
Sim evm stablecoins #30
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
Open
ivpusic
wants to merge
1
commit into
sim/evm-balance
Choose a base branch
from
sim/evm-stablecoins
base: sim/evm-balance
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package evm | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/url" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/duneanalytics/cli/output" | ||
| ) | ||
|
|
||
| // NewStablecoinsCmd returns the `sim evm stablecoins` command. | ||
| func NewStablecoinsCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "stablecoins <address>", | ||
| Short: "Get stablecoin balances for a wallet address", | ||
| Long: "Return stablecoin balances for the given wallet address across supported\n" + | ||
| "EVM chains, including USD valuations.\n\n" + | ||
| "Examples:\n" + | ||
| " dune sim evm stablecoins 0xd8da6bf26964af9d7eed9e03e53415d37aa96045\n" + | ||
| " dune sim evm stablecoins 0xd8da... --chain-ids 1,8453\n" + | ||
| " dune sim evm stablecoins 0xd8da... -o json", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: runStablecoins, | ||
| } | ||
|
|
||
| cmd.Flags().String("chain-ids", "", "Comma-separated chain IDs or tags (default: all default chains)") | ||
| cmd.Flags().String("filters", "", "Token filter: erc20 or native") | ||
| cmd.Flags().String("metadata", "", "Extra metadata fields: logo,url,pools") | ||
| cmd.Flags().Bool("exclude-spam", false, "Exclude tokens with <100 USD liquidity") | ||
| cmd.Flags().String("historical-prices", "", "Hour offsets for historical prices (e.g. 720,168,24)") | ||
| cmd.Flags().Int("limit", 0, "Max results (1-1000)") | ||
| cmd.Flags().String("offset", "", "Pagination cursor from previous response") | ||
| output.AddFormatFlag(cmd, "text") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runStablecoins(cmd *cobra.Command, args []string) error { | ||
| client, err := requireSimClient(cmd) | ||
|
Check failure on line 41 in cmd/sim/evm/stablecoins.go
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| address := args[0] | ||
| params := url.Values{} | ||
|
|
||
| if v, _ := cmd.Flags().GetString("chain-ids"); v != "" { | ||
| params.Set("chain_ids", v) | ||
| } | ||
| if v, _ := cmd.Flags().GetString("filters"); v != "" { | ||
| params.Set("filters", v) | ||
| } | ||
| if v, _ := cmd.Flags().GetString("metadata"); v != "" { | ||
| params.Set("metadata", v) | ||
| } | ||
| if v, _ := cmd.Flags().GetBool("exclude-spam"); v { | ||
| params.Set("exclude_spam_tokens", "true") | ||
| } | ||
| if v, _ := cmd.Flags().GetString("historical-prices"); v != "" { | ||
| params.Set("historical_prices", 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(), "/v1/evm/balances/"+address+"/stablecoins", 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 balancesResponse | ||
| if err := json.Unmarshal(data, &resp); err != nil { | ||
| return fmt.Errorf("parsing response: %w", err) | ||
| } | ||
|
|
||
| printWarnings(cmd, resp.Warnings) | ||
|
|
||
| columns := []string{"CHAIN", "SYMBOL", "AMOUNT", "PRICE_USD", "VALUE_USD"} | ||
| rows := make([][]string, len(resp.Balances)) | ||
| for i, b := range resp.Balances { | ||
| rows[i] = []string{ | ||
| b.Chain, | ||
| b.Symbol, | ||
| formatAmount(b.Amount, b.Decimals), | ||
| formatUSD(b.PriceUSD), | ||
| 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,42 @@ | ||
| package evm_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestEvmStablecoins_Text(t *testing.T) { | ||
| key := simAPIKey(t) | ||
|
|
||
| root := newSimTestRoot() | ||
| var buf bytes.Buffer | ||
| root.SetOut(&buf) | ||
| root.SetArgs([]string{"sim", "--sim-api-key", key, "evm", "stablecoins", evmTestAddress, "--chain-ids", "1"}) | ||
|
|
||
| require.NoError(t, root.Execute()) | ||
|
|
||
| out := buf.String() | ||
| assert.Contains(t, out, "CHAIN") | ||
| assert.Contains(t, out, "SYMBOL") | ||
| assert.Contains(t, out, "VALUE_USD") | ||
| } | ||
|
|
||
| func TestEvmStablecoins_JSON(t *testing.T) { | ||
| key := simAPIKey(t) | ||
|
|
||
| root := newSimTestRoot() | ||
| var buf bytes.Buffer | ||
| root.SetOut(&buf) | ||
| root.SetArgs([]string{"sim", "--sim-api-key", key, "evm", "stablecoins", evmTestAddress, "--chain-ids", "1", "-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") | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated run logic between stablecoins and balances commands
Low Severity
runStablecoinsis a near-verbatim copy ofrunBalances— the only differences are the API endpoint path and the omission of theasset-classflag. The flag definitions inNewStablecoinsCmdare similarly duplicated. A shared helper accepting the endpoint path (and optionally registering extra flags) would eliminate ~60 lines of duplication and ensure future bug fixes (e.g., to table rendering or param building) are applied consistently.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, can we dedupe some of it ?