From eb3cb55fd0dfcb25d2d8a9b6e1d4f96a26e92fb5 Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Thu, 28 May 2020 10:57:22 -0400 Subject: [PATCH] Add incentive queries (#528) * add incentive queries * fix: add query route for claim periods * address review comments --- x/incentive/client/cli/query.go | 62 +++++++++++++++++++++++++++++- x/incentive/client/rest/query.go | 42 ++++++++++++++++++++ x/incentive/keeper/querier.go | 30 +++++++++++++++ x/incentive/keeper/querier_test.go | 13 +++++++ x/incentive/types/querier.go | 10 +++-- 5 files changed, 151 insertions(+), 6 deletions(-) diff --git a/x/incentive/client/cli/query.go b/x/incentive/client/cli/query.go index ea37bd97..1c82a39c 100644 --- a/x/incentive/client/cli/query.go +++ b/x/incentive/client/cli/query.go @@ -25,6 +25,8 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { incentiveQueryCmd.AddCommand(flags.GetCommands( queryParamsCmd(queryRoute, cdc), queryClaimsCmd(queryRoute, cdc), + queryRewardPeriodsCmd(queryRoute, cdc), + queryClaimPeriodsCmd(queryRoute, cdc), )...) return incentiveQueryCmd @@ -58,10 +60,11 @@ func queryClaimsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { // Query route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetClaims) - res, _, err := cliCtx.QueryWithData(route, bz) + res, height, err := cliCtx.QueryWithData(route, bz) if err != nil { return err } + cliCtx = cliCtx.WithHeight(height) var claims types.Claims if err := cdc.UnmarshalJSON(res, &claims); err != nil { @@ -84,10 +87,11 @@ func queryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { // Query route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetParams) - res, _, err := cliCtx.QueryWithData(route, nil) + res, height, err := cliCtx.QueryWithData(route, nil) if err != nil { return err } + cliCtx = cliCtx.WithHeight(height) // Decode and print results var params types.Params @@ -98,3 +102,57 @@ func queryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { }, } } + +func queryRewardPeriodsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "reward-periods", + Short: "get active reward periods", + Long: "Get the current set of active incentive reward periods.", + 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.QueryGetRewardPeriods) + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + return err + } + cliCtx = cliCtx.WithHeight(height) + + // Decode and print results + var rewardPeriods types.RewardPeriods + if err := cdc.UnmarshalJSON(res, &rewardPeriods); err != nil { + return fmt.Errorf("failed to unmarshal reward periods: %w", err) + } + return cliCtx.PrintOutput(rewardPeriods) + }, + } +} + +func queryClaimPeriodsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "claim-periods", + Short: "get active claim periods", + Long: "Get the current set of active incentive claim periods.", + 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.QueryGetClaimPeriods) + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + return err + } + cliCtx = cliCtx.WithHeight(height) + + // Decode and print results + var claimPeriods types.ClaimPeriods + if err := cdc.UnmarshalJSON(res, &claimPeriods); err != nil { + return fmt.Errorf("failed to unmarshal claim periods: %w", err) + } + return cliCtx.PrintOutput(claimPeriods) + }, + } +} diff --git a/x/incentive/client/rest/query.go b/x/incentive/client/rest/query.go index 50921ea9..e8369e9e 100644 --- a/x/incentive/client/rest/query.go +++ b/x/incentive/client/rest/query.go @@ -20,6 +20,8 @@ const ( func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { r.HandleFunc(fmt.Sprintf("/%s/claims/{%s}/{%s}", types.ModuleName, restOwner, restDenom), queryClaimsHandlerFn(cliCtx)).Methods("GET") + r.HandleFunc(fmt.Sprintf("/%s/rewardperiods", types.ModuleName), queryRewardPeriodsHandlerFn(cliCtx)).Methods("GET") + r.HandleFunc(fmt.Sprintf("/%s/claimperiods", types.ModuleName), queryClaimPeriodsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc(fmt.Sprintf("/%s/parameters", types.ModuleName), queryParamsHandlerFn(cliCtx)).Methods("GET") } @@ -58,6 +60,46 @@ func queryClaimsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } } +func queryRewardPeriodsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryGetRewardPeriods) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + +func queryClaimPeriodsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryGetClaimPeriods) + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) diff --git a/x/incentive/keeper/querier.go b/x/incentive/keeper/querier.go index 428c8635..463a4cc0 100644 --- a/x/incentive/keeper/querier.go +++ b/x/incentive/keeper/querier.go @@ -18,6 +18,10 @@ func NewQuerier(k Keeper) sdk.Querier { return queryGetParams(ctx, req, k) case types.QueryGetClaims: return queryGetClaims(ctx, req, k) + case types.QueryGetRewardPeriods: + return queryGetRewardPeriods(ctx, req, k) + case types.QueryGetClaimPeriods: + return queryGetClaimPeriods(ctx, req, k) default: return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName) } @@ -37,6 +41,32 @@ func queryGetParams(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, e return bz, nil } +// query reward periods in the store +func queryGetRewardPeriods(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) { + // Get params + rewardPeriods := k.GetAllRewardPeriods(ctx) + + // Encode results + bz, err := codec.MarshalJSONIndent(k.cdc, rewardPeriods) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return bz, nil +} + +// query claim periods in the store +func queryGetClaimPeriods(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) { + // Get params + claimPeriods := k.GetAllClaimPeriods(ctx) + + // Encode results + bz, err := codec.MarshalJSONIndent(k.cdc, claimPeriods) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return bz, nil +} + func queryGetClaims(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) { var requestParams types.QueryClaimsParams err := k.cdc.UnmarshalJSON(req.Data, &requestParams) diff --git a/x/incentive/keeper/querier_test.go b/x/incentive/keeper/querier_test.go index 9e1e9d17..39fb9c21 100644 --- a/x/incentive/keeper/querier_test.go +++ b/x/incentive/keeper/querier_test.go @@ -30,4 +30,17 @@ func (suite *KeeperTestSuite) TestQuerier() { suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &claims)) suite.Equal(1, len(claims)) suite.Equal(types.Claims{types.NewClaim(suite.addrs[0], c("ukava", 1000000), "bnb", 1)}, claims) + + var rp types.RewardPeriods + bz, err = querier(suite.ctx, []string{types.QueryGetRewardPeriods}, abci.RequestQuery{}) + suite.Require().NoError(err) + suite.NotNil(bz) + suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &rp)) + + var cp types.ClaimPeriods + bz, err = querier(suite.ctx, []string{types.QueryGetClaimPeriods}, abci.RequestQuery{}) + suite.Require().NoError(err) + suite.NotNil(bz) + suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &cp)) + } diff --git a/x/incentive/types/querier.go b/x/incentive/types/querier.go index d6271048..c798bb13 100644 --- a/x/incentive/types/querier.go +++ b/x/incentive/types/querier.go @@ -7,10 +7,12 @@ import ( // Querier routes for the incentive module const ( - QueryGetClaims = "claims" - RestClaimOwner = "owner" - RestClaimDenom = "denom" - QueryGetParams = "parameters" + QueryGetClaims = "claims" + RestClaimOwner = "owner" + RestClaimDenom = "denom" + QueryGetParams = "parameters" + QueryGetRewardPeriods = "reward-periods" + QueryGetClaimPeriods = "claim-periods" ) // QueryClaimsParams params for query /incentive/claims