Skip to content

Commit 70c87a1

Browse files
committed
cleanup
1 parent ab5061a commit 70c87a1

4 files changed

Lines changed: 100 additions & 9 deletions

File tree

cmd/sim/evm/balances.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,8 @@ func NewBalancesCmd() *cobra.Command {
2626
RunE: runBalances,
2727
}
2828

29-
cmd.Flags().String("chain-ids", "", "Comma-separated chain IDs or tags (default: all default chains)")
30-
cmd.Flags().String("filters", "", "Token filter: erc20 or native")
29+
addBalanceFlags(cmd)
3130
cmd.Flags().String("asset-class", "", "Asset class filter: stablecoin")
32-
cmd.Flags().String("metadata", "", "Extra metadata fields: logo,url,pools")
33-
cmd.Flags().Bool("exclude-spam", false, "Exclude tokens with <100 USD liquidity")
34-
cmd.Flags().String("historical-prices", "", "Hour offsets for historical prices (e.g. 720,168,24)")
35-
cmd.Flags().Int("limit", 0, "Max results (1-1000)")
36-
cmd.Flags().String("offset", "", "Pagination cursor from previous response")
37-
output.AddFormatFlag(cmd, "text")
3831

3932
return cmd
4033
}
@@ -70,6 +63,30 @@ type warningEntry struct {
7063
}
7164

7265
func runBalances(cmd *cobra.Command, args []string) error {
66+
return runBalancesEndpoint(cmd, args, "/v1/evm/balances/", "")
67+
}
68+
69+
// addBalanceFlags registers the common flags shared by the balances and
70+
// stablecoins commands.
71+
func addBalanceFlags(cmd *cobra.Command) {
72+
cmd.Flags().String("chain-ids", "", "Comma-separated chain IDs or tags (default: all default chains)")
73+
cmd.Flags().String("filters", "", "Token filter: erc20 or native")
74+
cmd.Flags().String("metadata", "", "Extra metadata fields: logo,url,pools")
75+
cmd.Flags().Bool("exclude-spam", false, "Exclude tokens with <100 USD liquidity")
76+
cmd.Flags().String("historical-prices", "", "Hour offsets for historical prices (e.g. 720,168,24)")
77+
cmd.Flags().Int("limit", 0, "Max results (1-1000)")
78+
cmd.Flags().String("offset", "", "Pagination cursor from previous response")
79+
output.AddFormatFlag(cmd, "text")
80+
}
81+
82+
// runBalancesEndpoint is the shared run implementation for the balances and
83+
// stablecoins commands. The final API path is built as:
84+
//
85+
// pathPrefix + address + pathSuffix
86+
//
87+
// For example "/v1/evm/balances/" + addr + "" for balances,
88+
// or "/v1/evm/balances/" + addr + "/stablecoins" for stablecoins.
89+
func runBalancesEndpoint(cmd *cobra.Command, args []string, pathPrefix, pathSuffix string) error {
7390
client := SimClientFromCmd(cmd)
7491
if client == nil {
7592
return fmt.Errorf("sim client not initialized")
@@ -84,6 +101,8 @@ func runBalances(cmd *cobra.Command, args []string) error {
84101
if v, _ := cmd.Flags().GetString("filters"); v != "" {
85102
params.Set("filters", v)
86103
}
104+
// asset-class is only registered on the balances command; silently ignored
105+
// when the flag is absent.
87106
if v, _ := cmd.Flags().GetString("asset-class"); v != "" {
88107
params.Set("asset_class", v)
89108
}
@@ -103,7 +122,7 @@ func runBalances(cmd *cobra.Command, args []string) error {
103122
params.Set("offset", v)
104123
}
105124

106-
data, err := client.Get(cmd.Context(), "/v1/evm/balances/"+address, params)
125+
data, err := client.Get(cmd.Context(), pathPrefix+address+pathSuffix, params)
107126
if err != nil {
108127
return err
109128
}

cmd/sim/evm/evm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func NewEvmCmd() *cobra.Command {
4141
cmd.AddCommand(NewSupportedChainsCmd())
4242
cmd.AddCommand(NewBalancesCmd())
4343
cmd.AddCommand(NewBalanceCmd())
44+
cmd.AddCommand(NewStablecoinsCmd())
4445

4546
return cmd
4647
}

cmd/sim/evm/stablecoins.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package evm
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// NewStablecoinsCmd returns the `sim evm stablecoins` command.
8+
func NewStablecoinsCmd() *cobra.Command {
9+
cmd := &cobra.Command{
10+
Use: "stablecoins <address>",
11+
Short: "Get stablecoin balances for a wallet address",
12+
Long: "Return stablecoin balances for the given wallet address across supported\n" +
13+
"EVM chains, including USD valuations.\n\n" +
14+
"Examples:\n" +
15+
" dune sim evm stablecoins 0xd8da6bf26964af9d7eed9e03e53415d37aa96045\n" +
16+
" dune sim evm stablecoins 0xd8da... --chain-ids 1,8453\n" +
17+
" dune sim evm stablecoins 0xd8da... -o json",
18+
Args: cobra.ExactArgs(1),
19+
RunE: runStablecoins,
20+
}
21+
22+
addBalanceFlags(cmd)
23+
24+
return cmd
25+
}
26+
27+
func runStablecoins(cmd *cobra.Command, args []string) error {
28+
return runBalancesEndpoint(cmd, args, "/v1/evm/balances/", "/stablecoins")
29+
}

cmd/sim/evm/stablecoins_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package evm_test
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestEvmStablecoins_Text(t *testing.T) {
13+
key := simAPIKey(t)
14+
15+
root := newSimTestRoot()
16+
var buf bytes.Buffer
17+
root.SetOut(&buf)
18+
root.SetArgs([]string{"sim", "--sim-api-key", key, "evm", "stablecoins", evmTestAddress, "--chain-ids", "1"})
19+
20+
require.NoError(t, root.Execute())
21+
22+
out := buf.String()
23+
assert.Contains(t, out, "CHAIN")
24+
assert.Contains(t, out, "SYMBOL")
25+
assert.Contains(t, out, "VALUE_USD")
26+
}
27+
28+
func TestEvmStablecoins_JSON(t *testing.T) {
29+
key := simAPIKey(t)
30+
31+
root := newSimTestRoot()
32+
var buf bytes.Buffer
33+
root.SetOut(&buf)
34+
root.SetArgs([]string{"sim", "--sim-api-key", key, "evm", "stablecoins", evmTestAddress, "--chain-ids", "1", "-o", "json"})
35+
36+
require.NoError(t, root.Execute())
37+
38+
var resp map[string]interface{}
39+
require.NoError(t, json.Unmarshal(buf.Bytes(), &resp))
40+
assert.Contains(t, resp, "wallet_address")
41+
assert.Contains(t, resp, "balances")
42+
}

0 commit comments

Comments
 (0)