mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 00:05:18 +00:00
Add incentive queries (#528)
* add incentive queries * fix: add query route for claim periods * address review comments
This commit is contained in:
parent
d130734c2e
commit
eb3cb55fd0
@ -25,6 +25,8 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|||||||
incentiveQueryCmd.AddCommand(flags.GetCommands(
|
incentiveQueryCmd.AddCommand(flags.GetCommands(
|
||||||
queryParamsCmd(queryRoute, cdc),
|
queryParamsCmd(queryRoute, cdc),
|
||||||
queryClaimsCmd(queryRoute, cdc),
|
queryClaimsCmd(queryRoute, cdc),
|
||||||
|
queryRewardPeriodsCmd(queryRoute, cdc),
|
||||||
|
queryClaimPeriodsCmd(queryRoute, cdc),
|
||||||
)...)
|
)...)
|
||||||
|
|
||||||
return incentiveQueryCmd
|
return incentiveQueryCmd
|
||||||
@ -58,10 +60,11 @@ func queryClaimsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|||||||
|
|
||||||
// Query
|
// Query
|
||||||
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetClaims)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
cliCtx = cliCtx.WithHeight(height)
|
||||||
|
|
||||||
var claims types.Claims
|
var claims types.Claims
|
||||||
if err := cdc.UnmarshalJSON(res, &claims); err != nil {
|
if err := cdc.UnmarshalJSON(res, &claims); err != nil {
|
||||||
@ -84,10 +87,11 @@ func queryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|||||||
|
|
||||||
// Query
|
// Query
|
||||||
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetParams)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
cliCtx = cliCtx.WithHeight(height)
|
||||||
|
|
||||||
// Decode and print results
|
// Decode and print results
|
||||||
var params types.Params
|
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)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -20,6 +20,8 @@ const (
|
|||||||
|
|
||||||
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
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/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")
|
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 {
|
func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
||||||
|
@ -18,6 +18,10 @@ func NewQuerier(k Keeper) sdk.Querier {
|
|||||||
return queryGetParams(ctx, req, k)
|
return queryGetParams(ctx, req, k)
|
||||||
case types.QueryGetClaims:
|
case types.QueryGetClaims:
|
||||||
return queryGetClaims(ctx, req, k)
|
return queryGetClaims(ctx, req, k)
|
||||||
|
case types.QueryGetRewardPeriods:
|
||||||
|
return queryGetRewardPeriods(ctx, req, k)
|
||||||
|
case types.QueryGetClaimPeriods:
|
||||||
|
return queryGetClaimPeriods(ctx, req, k)
|
||||||
default:
|
default:
|
||||||
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
|
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
|
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) {
|
func queryGetClaims(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
||||||
var requestParams types.QueryClaimsParams
|
var requestParams types.QueryClaimsParams
|
||||||
err := k.cdc.UnmarshalJSON(req.Data, &requestParams)
|
err := k.cdc.UnmarshalJSON(req.Data, &requestParams)
|
||||||
|
@ -30,4 +30,17 @@ func (suite *KeeperTestSuite) TestQuerier() {
|
|||||||
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &claims))
|
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &claims))
|
||||||
suite.Equal(1, len(claims))
|
suite.Equal(1, len(claims))
|
||||||
suite.Equal(types.Claims{types.NewClaim(suite.addrs[0], c("ukava", 1000000), "bnb", 1)}, 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))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@ const (
|
|||||||
RestClaimOwner = "owner"
|
RestClaimOwner = "owner"
|
||||||
RestClaimDenom = "denom"
|
RestClaimDenom = "denom"
|
||||||
QueryGetParams = "parameters"
|
QueryGetParams = "parameters"
|
||||||
|
QueryGetRewardPeriods = "reward-periods"
|
||||||
|
QueryGetClaimPeriods = "claim-periods"
|
||||||
)
|
)
|
||||||
|
|
||||||
// QueryClaimsParams params for query /incentive/claims
|
// QueryClaimsParams params for query /incentive/claims
|
||||||
|
Loading…
Reference in New Issue
Block a user