R4R: add params query to pricefeed module (#311)

* fix: remove redundant debt limit param

* wip: test pricefeed genesis

* fix: pricefeed querier

* fix: comments, naming

* fix: query path

* fix: store methods

* fix: query methods

* add params query to pricefeed module

* minor formatting updates, removed commented out legacy code, etc.

Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
This commit is contained in:
Denali Marsh 2020-01-17 13:55:17 +01:00 committed by Kevin Davis
parent d04aad5cc9
commit 710a29980f
8 changed files with 61 additions and 8 deletions

View File

@ -25,6 +25,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
GetCmdCurrentPrice(queryRoute, cdc),
GetCmdRawPrices(queryRoute, cdc),
GetCmdMarkets(queryRoute, cdc),
GetCmdQueryParams(queryRoute, cdc),
)...)
return pricefeedQueryCmd
@ -107,3 +108,28 @@ func GetCmdMarkets(queryRoute string, cdc *codec.Codec) *cobra.Command {
},
}
}
// GetCmdQueryParams queries the pricefeed module parameters
func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *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 {
cliCtx := context.NewCLIContext().WithCodec(cdc)
// Query
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetParams)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}
// Decode and print results
var out types.Params
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
}

View File

@ -42,9 +42,7 @@ func GetCmdPostPrice(cdc *codec.Codec) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
// if err := cliCtx.EnsureAccountExists(); err != nil {
// return err
// }
price, err := sdk.NewDecFromStr(args[1])
if err != nil {
return err

View File

@ -17,6 +17,7 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(fmt.Sprintf("/%s/rawprices/{%s}", types.ModuleName, restName), queryRawPricesHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/currentprice/{%s}", types.ModuleName, restName), queryCurrentPriceHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/markets", types.ModuleName), queryMarketsHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/params", types.ModuleName), queryParamsHandlerFn(cliCtx)).Methods("GET")
}
func queryRawPricesHandler(cliCtx context.CLIContext) http.HandlerFunc {
@ -55,3 +56,17 @@ func queryMarketsHandler(cliCtx context.CLIContext) http.HandlerFunc {
rest.PostProcessResponse(w, cliCtx, res)
}
}
func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Get the params
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/pricefeed/%s", types.QueryGetParams), nil)
cliCtx = cliCtx.WithHeight(height)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
// Return the params
rest.PostProcessResponse(w, cliCtx, res)
}
}

View File

@ -21,6 +21,8 @@ func NewQuerier(keeper Keeper) sdk.Querier {
return queryRawPrices(ctx, req, keeper)
case types.QueryMarkets:
return queryMarkets(ctx, req, keeper)
case types.QueryGetParams:
return queryGetParams(ctx, req, keeper)
default:
return nil, sdk.ErrUnknownRequest("unknown pricefeed query endpoint")
}
@ -80,3 +82,16 @@ func queryMarkets(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (res []
return bz, nil
}
// query params in the auction store
func queryGetParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
// Get params
params := keeper.GetParams(ctx)
// Encode results
bz, err := codec.MarshalJSONIndent(keeper.cdc, params)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
}
return bz, nil
}

View File

@ -10,7 +10,7 @@ func init() {
RegisterCodec(ModuleCdc)
}
// RegisterCode registers concrete types on the Amino code
// RegisterCodec registers concrete types on the Amino code
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(MsgPostPrice{}, "pricefeed/MsgPostPrice", nil)
}

View File

@ -37,7 +37,7 @@ func ErrNoValidPrice(codespace sdk.CodespaceType) sdk.Error {
return sdk.NewError(codespace, CodeInvalidPrice, fmt.Sprintf("All input prices are expired."))
}
// ErrInvalidAsset Error constructor for posted price messages for invalid markets
// ErrInvalidMarket Error constructor for posted price messages for invalid markets
func ErrInvalidMarket(codespace sdk.CodespaceType, marketId string) sdk.Error {
return sdk.NewError(codespace, CodeInvalidAsset, fmt.Sprintf("market %s does not exist", marketId))
}

View File

@ -10,9 +10,6 @@ import (
func TestMsgPlaceBid_ValidateBasic(t *testing.T) {
addr := sdk.AccAddress([]byte("someName"))
// oracles := []Oracle{Oracle{
// OracleAddress: addr.String(),
// }}
price, _ := sdk.NewDecFromStr("0.3005")
expiry := tmtime.Now()
negativePrice, _ := sdk.NewDecFromStr("-3.05")

View File

@ -11,6 +11,8 @@ const (
QueryRawPrices = "rawprices"
// QueryMarkets command for assets query
QueryMarkets = "markets"
// QueryGetParams command for params query
QueryGetParams = "params"
)
// QueryPricesParams fields for querying prices