0g-chain/x/pricefeed/client/cli/query.go

205 lines
4.7 KiB
Go
Raw Normal View History

2019-11-25 19:46:02 +00:00
package cli
import (
"context"
2019-11-25 19:46:02 +00:00
2020-04-30 14:23:41 +00:00
"github.com/spf13/cobra"
2019-11-25 19:46:02 +00:00
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
2020-04-30 14:13:31 +00:00
2024-05-01 03:17:24 +00:00
"github.com/0glabs/0g-chain/x/pricefeed/types"
2019-11-25 19:46:02 +00:00
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
2019-11-25 19:46:02 +00:00
// Group nameservice queries under a subcommand
pricefeedQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the pricefeed module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmds := []*cobra.Command{
GetCmdPrice(),
GetCmdQueryPrices(),
GetCmdRawPrices(),
GetCmdOracles(),
GetCmdMarkets(),
GetCmdQueryParams(),
}
for _, cmd := range cmds {
flags.AddQueryFlagsToCmd(cmd)
}
pricefeedQueryCmd.AddCommand(cmds...)
2019-11-25 19:46:02 +00:00
return pricefeedQueryCmd
}
// GetCmdOracles queries the oracle set of an asset
func GetCmdOracles() *cobra.Command {
return &cobra.Command{
Use: "oracles [marketID]",
Short: "get the oracle set for a market",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
marketID := args[0]
params := types.QueryOraclesRequest{
MarketId: marketID,
}
res, err := queryClient.Oracles(context.Background(), &params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
}
// GetCmdPrice queries the current price of an asset
func GetCmdPrice() *cobra.Command {
2019-11-25 19:46:02 +00:00
return &cobra.Command{
Use: "price [marketID]",
Short: "get the current price for the input market",
2019-11-25 19:46:02 +00:00
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
2019-11-25 19:46:02 +00:00
if err != nil {
return err
2019-11-25 19:46:02 +00:00
}
queryClient := types.NewQueryClient(clientCtx)
marketID := args[0]
params := types.QueryPriceRequest{
MarketId: marketID,
}
res, err := queryClient.Price(context.Background(), &params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
2019-11-25 19:46:02 +00:00
},
}
}
// GetCmdQueryPrices queries the pricefeed module for current prices
func GetCmdQueryPrices() *cobra.Command {
return &cobra.Command{
Use: "prices",
Short: "get the current price of each market",
Long: "Get the current prices of each market in the pricefeed 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)
res, err := queryClient.Prices(context.Background(), &types.QueryPricesRequest{})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
}
2019-11-25 19:46:02 +00:00
// GetCmdRawPrices queries the current price of an asset
func GetCmdRawPrices() *cobra.Command {
2019-11-25 19:46:02 +00:00
return &cobra.Command{
Use: "rawprices [marketID]",
Short: "get the raw oracle prices for the input market",
2019-11-25 19:46:02 +00:00
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
marketID := args[0]
params := types.QueryRawPricesRequest{
MarketId: marketID,
}
res, err := queryClient.RawPrices(context.Background(), &params)
2019-11-25 19:46:02 +00:00
if err != nil {
return err
2019-11-25 19:46:02 +00:00
}
return clientCtx.PrintProto(res)
2019-11-25 19:46:02 +00:00
},
}
}
// GetCmdMarkets queries list of markets in the pricefeed
func GetCmdMarkets() *cobra.Command {
2019-11-25 19:46:02 +00:00
return &cobra.Command{
Use: "markets",
Short: "get the markets in the pricefeed",
2019-11-25 19:46:02 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
2019-11-25 19:46:02 +00:00
if err != nil {
return err
2019-11-25 19:46:02 +00:00
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Markets(context.Background(), &types.QueryMarketsRequest{})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
2019-11-25 19:46:02 +00:00
},
}
}
// GetCmdQueryParams queries the pricefeed module parameters
func GetCmdQueryParams() *cobra.Command {
return &cobra.Command{
Use: "params",
Short: "get the pricefeed module parameters",
Long: "Get the current global pricefeed module parameters.",
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)
res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
if err != nil {
return err
}
return clientCtx.PrintProto(&res.Params)
},
}
}