Skip to content

Commit

Permalink
fix (pricefeed): query price cli (#500)
Browse files Browse the repository at this point in the history
* fix: CmdPrice() for querying a specific pair

* refactor: more CmdPrices to query.go

* add commands single command

* add error for pair

* fix linter

* fix imports

* simplify add command

Co-authored-by: Mat-Cosmos <97468149+matthiasmatt@users.noreply.github.com>
Co-authored-by: Agent Smith <agentsmith@matrixsystems.co>
  • Loading branch information
3 people authored May 27, 2022
1 parent 47e1ed8 commit 3281043
Showing 1 changed file with 53 additions and 56 deletions.
109 changes: 53 additions & 56 deletions x/pricefeed/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,59 @@ package cli
import (
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"

// "strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
// "github.com/cosmos/cosmos-sdk/client/flags"
// sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/pricefeed/types"
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
// Group pricefeed queries under a subcommand
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
// Group pricefeed queries as subcommands of query
queryCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf(
"Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdPrice())

cmd.AddCommand(CmdPrices())

cmd.AddCommand(CmdRawPrices())

cmd.AddCommand(CmdOracles())
queryCmd.AddCommand(
CmdQueryParams(),
CmdPrice(),
CmdPrices(),
CmdRawPrices(),
CmdOracles(),
CmdPairs(),
)

cmd.AddCommand(CmdPairs())

// this line is used by starport scaffolding # 1

return cmd
return queryCmd
}

func CmdPairs() *cobra.Command {
func CmdPrice() *cobra.Command {
cmd := &cobra.Command{
Use: "markets",
Short: "Query markets",
Args: cobra.ExactArgs(0),
Use: "price [pair-id]",
Short: "Display current price for the given pair",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryPairsRequest{}
pair, err := common.NewTokenPairFromStr(args[0])
if err != nil {
return fmt.Errorf("invalid pair: %w", err)
}

res, err := queryClient.Pairs(cmd.Context(), params)
params := &types.QueryPriceRequest{PairId: pair.String()}

res, err := queryClient.Price(cmd.Context(), params)
if err != nil {
return err
}
Expand All @@ -72,22 +69,22 @@ func CmdPairs() *cobra.Command {
return cmd
}

func CmdOracles() *cobra.Command {
func CmdPrices() *cobra.Command {
cmd := &cobra.Command{
Use: "oracles",
Short: "Query oracles",
Use: "prices",
Short: "Display current prices for all pairs",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryOraclesRequest{}
params := &types.QueryPricesRequest{}

res, err := queryClient.Oracles(cmd.Context(), params)
res, err := queryClient.Prices(cmd.Context(), params)
if err != nil {
return err
}
Expand All @@ -101,20 +98,22 @@ func CmdOracles() *cobra.Command {
return cmd
}

func CmdQueryParams() *cobra.Command {
func CmdPairs() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "shows the parameters of the module",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
Use: "markets",
Short: "Query markets",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{})
params := &types.QueryPairsRequest{}

res, err := queryClient.Pairs(cmd.Context(), params)
if err != nil {
return err
}
Expand All @@ -128,10 +127,10 @@ func CmdQueryParams() *cobra.Command {
return cmd
}

func CmdPrice() *cobra.Command {
func CmdOracles() *cobra.Command {
cmd := &cobra.Command{
Use: "price",
Short: "Query price",
Use: "oracles",
Short: "Query oracles",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
Expand All @@ -141,9 +140,9 @@ func CmdPrice() *cobra.Command {

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryPriceRequest{}
params := &types.QueryOraclesRequest{}

res, err := queryClient.Price(cmd.Context(), params)
res, err := queryClient.Oracles(cmd.Context(), params)
if err != nil {
return err
}
Expand All @@ -157,22 +156,20 @@ func CmdPrice() *cobra.Command {
return cmd
}

func CmdPrices() *cobra.Command {
func CmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Use: "prices",
Short: "Query prices",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {
Use: "params",
Short: "shows the parameters of the module",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryPricesRequest{}

res, err := queryClient.Prices(cmd.Context(), params)
res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{})
if err != nil {
return err
}
Expand Down

0 comments on commit 3281043

Please sign in to comment.