Add incentive queries (#528)

* add incentive queries

* fix: add query route for claim periods

* address review comments
This commit is contained in:
Kevin Davis 2020-05-28 10:57:22 -04:00 committed by GitHub
parent d130734c2e
commit eb3cb55fd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 151 additions and 6 deletions

View File

@ -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)
},
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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))
}

View File

@ -11,6 +11,8 @@ const (
RestClaimOwner = "owner"
RestClaimDenom = "denom"
QueryGetParams = "parameters"
QueryGetRewardPeriods = "reward-periods"
QueryGetClaimPeriods = "claim-periods"
)
// QueryClaimsParams params for query /incentive/claims