mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 00:05:18 +00:00
Query rewards via incentive module (#781)
* query rewards types * query rewards keeper * query rewards cli/rest * query rewards alias * implement branched query * mirror changes in rest querier * clean up rest querier
This commit is contained in:
parent
3a08fc582b
commit
92a2425668
@ -23,8 +23,7 @@ const (
|
|||||||
ModuleName = types.ModuleName
|
ModuleName = types.ModuleName
|
||||||
QuerierRoute = types.QuerierRoute
|
QuerierRoute = types.QuerierRoute
|
||||||
QueryGetClaimPeriods = types.QueryGetClaimPeriods
|
QueryGetClaimPeriods = types.QueryGetClaimPeriods
|
||||||
QueryGetCdpClaims = types.QueryGetCdpClaims
|
QueryGetRewards = types.QueryGetRewards
|
||||||
QueryGetHardClaims = types.QueryGetHardClaims
|
|
||||||
QueryGetParams = types.QueryGetParams
|
QueryGetParams = types.QueryGetParams
|
||||||
QueryGetRewardPeriods = types.QueryGetRewardPeriods
|
QueryGetRewardPeriods = types.QueryGetRewardPeriods
|
||||||
RestClaimCollateralType = types.RestClaimCollateralType
|
RestClaimCollateralType = types.RestClaimCollateralType
|
||||||
@ -49,8 +48,7 @@ var (
|
|||||||
NewMultiplier = types.NewMultiplier
|
NewMultiplier = types.NewMultiplier
|
||||||
NewParams = types.NewParams
|
NewParams = types.NewParams
|
||||||
NewPeriod = types.NewPeriod
|
NewPeriod = types.NewPeriod
|
||||||
NewQueryCdpClaimsParams = types.NewQueryCdpClaimsParams
|
NewQueryRewardsParams = types.NewQueryRewardsParams
|
||||||
NewQueryHardClaimsParams = types.NewQueryHardClaimsParams
|
|
||||||
NewRewardIndex = types.NewRewardIndex
|
NewRewardIndex = types.NewRewardIndex
|
||||||
NewRewardPeriod = types.NewRewardPeriod
|
NewRewardPeriod = types.NewRewardPeriod
|
||||||
NewUSDXMintingClaim = types.NewUSDXMintingClaim
|
NewUSDXMintingClaim = types.NewUSDXMintingClaim
|
||||||
@ -103,8 +101,7 @@ type (
|
|||||||
Multipliers = types.Multipliers
|
Multipliers = types.Multipliers
|
||||||
Params = types.Params
|
Params = types.Params
|
||||||
PostClaimReq = types.PostClaimReq
|
PostClaimReq = types.PostClaimReq
|
||||||
QueryCdpClaimsParams = types.QueryCdpClaimsParams
|
QueryRewardsParams = types.QueryRewardsParams
|
||||||
QueryHardClaimsParams = types.QueryHardClaimsParams
|
|
||||||
RewardIndex = types.RewardIndex
|
RewardIndex = types.RewardIndex
|
||||||
RewardIndexes = types.RewardIndexes
|
RewardIndexes = types.RewardIndexes
|
||||||
RewardPeriod = types.RewardPeriod
|
RewardPeriod = types.RewardPeriod
|
||||||
|
@ -16,6 +16,11 @@ import (
|
|||||||
"github.com/kava-labs/kava/x/incentive/types"
|
"github.com/kava-labs/kava/x/incentive/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
flagOwner = "owner"
|
||||||
|
flagType = "type"
|
||||||
|
)
|
||||||
|
|
||||||
// GetQueryCmd returns the cli query commands for the incentive module
|
// GetQueryCmd returns the cli query commands for the incentive module
|
||||||
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||||
incentiveQueryCmd := &cobra.Command{
|
incentiveQueryCmd := &cobra.Command{
|
||||||
@ -25,120 +30,85 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|||||||
|
|
||||||
incentiveQueryCmd.AddCommand(flags.GetCommands(
|
incentiveQueryCmd.AddCommand(flags.GetCommands(
|
||||||
queryParamsCmd(queryRoute, cdc),
|
queryParamsCmd(queryRoute, cdc),
|
||||||
queryCdpClaimsCmd(queryRoute, cdc),
|
queryRewardsCmd(queryRoute, cdc),
|
||||||
queryHardClaimsCmd(queryRoute, cdc),
|
|
||||||
)...)
|
)...)
|
||||||
|
|
||||||
return incentiveQueryCmd
|
return incentiveQueryCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
func queryRewardsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||||
flagOwner = "owner"
|
|
||||||
)
|
|
||||||
|
|
||||||
func queryCdpClaimsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "cdp-claims",
|
Use: "rewards",
|
||||||
Short: "query USDX minting claims",
|
Short: "query claimable rewards",
|
||||||
Long: strings.TrimSpace(
|
Long: strings.TrimSpace(
|
||||||
fmt.Sprintf(`Query USDX minting claims with optional flag for finding claims for a specifc owner
|
fmt.Sprintf(`Query rewards with optional flags for owner and type
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
$ %s query %s cdp-claims
|
$ %s query %s rewards
|
||||||
$ %s query %s cdp-claims --owner kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw
|
$ %s query %s rewards --owner kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw
|
||||||
|
$ %s query %s rewards --type hard
|
||||||
|
$ %s query %s rewards --type usdx-minting
|
||||||
|
$ %s query %s rewards --type hard --owner kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw
|
||||||
`,
|
`,
|
||||||
version.ClientName, types.ModuleName, version.ClientName, types.ModuleName)),
|
version.ClientName, types.ModuleName, version.ClientName, types.ModuleName,
|
||||||
|
version.ClientName, types.ModuleName, version.ClientName, types.ModuleName,
|
||||||
|
version.ClientName, types.ModuleName)),
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||||
|
|
||||||
strOwner := viper.GetString(flagOwner)
|
|
||||||
page := viper.GetInt(flags.FlagPage)
|
page := viper.GetInt(flags.FlagPage)
|
||||||
limit := viper.GetInt(flags.FlagLimit)
|
limit := viper.GetInt(flags.FlagLimit)
|
||||||
|
strOwner := viper.GetString(flagOwner)
|
||||||
|
strType := viper.GetString(flagType)
|
||||||
|
|
||||||
// Prepare params for querier
|
// Prepare params for querier
|
||||||
owner, err := sdk.AccAddressFromBech32(strOwner)
|
owner, err := sdk.AccAddressFromBech32(strOwner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
params := types.NewQueryCdpClaimsParams(page, limit, owner)
|
|
||||||
bz, err := cdc.MarshalJSON(params)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Query
|
switch strings.ToLower(strType) {
|
||||||
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetCdpClaims)
|
case "hard":
|
||||||
res, height, err := cliCtx.QueryWithData(route, bz)
|
params := types.NewQueryHardRewardsParams(page, limit, owner)
|
||||||
if err != nil {
|
claims, err := executeHardRewardsQuery(queryRoute, cdc, cliCtx, params)
|
||||||
return err
|
if err != nil {
|
||||||
}
|
return err
|
||||||
cliCtx = cliCtx.WithHeight(height)
|
}
|
||||||
|
return cliCtx.PrintOutput(claims)
|
||||||
|
case "usdx-minting":
|
||||||
|
params := types.NewQueryUSDXMintingRewardsParams(page, limit, owner)
|
||||||
|
claims, err := executeUSDXMintingRewardsQuery(queryRoute, cdc, cliCtx, params)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return cliCtx.PrintOutput(claims)
|
||||||
|
default:
|
||||||
|
paramsHard := types.NewQueryHardRewardsParams(page, limit, owner)
|
||||||
|
hardClaims, err := executeHardRewardsQuery(queryRoute, cdc, cliCtx, paramsHard)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(hardClaims) > 0 {
|
||||||
|
cliCtx.PrintOutput(hardClaims)
|
||||||
|
}
|
||||||
|
|
||||||
var claims types.USDXMintingClaims
|
paramsUSDXMinting := types.NewQueryUSDXMintingRewardsParams(page, limit, owner)
|
||||||
if err := cdc.UnmarshalJSON(res, &claims); err != nil {
|
usdxMintingClaims, err := executeUSDXMintingRewardsQuery(queryRoute, cdc, cliCtx, paramsUSDXMinting)
|
||||||
return fmt.Errorf("failed to unmarshal claims: %w", err)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(usdxMintingClaims) > 0 {
|
||||||
|
cliCtx.PrintOutput(usdxMintingClaims)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return cliCtx.PrintOutput(claims)
|
return nil
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().String(flagOwner, "", "(optional) filter by claim owner address")
|
cmd.Flags().String(flagOwner, "", "(optional) filter by owner address")
|
||||||
cmd.Flags().Int(flags.FlagPage, 1, "pagination page of CDPs to to query for")
|
cmd.Flags().String(flagType, "", "(optional) filter by reward type")
|
||||||
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of CDPs to query for")
|
cmd.Flags().Int(flags.FlagPage, 1, "pagination page rewards of to to query for")
|
||||||
return cmd
|
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of rewards to query for")
|
||||||
}
|
|
||||||
|
|
||||||
func queryHardClaimsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "hard-claims",
|
|
||||||
Short: "query Hard liquidity provider claims",
|
|
||||||
Long: strings.TrimSpace(
|
|
||||||
fmt.Sprintf(`Query Hard liquidity provider claims with optional flag for finding claims for a specifc owner
|
|
||||||
|
|
||||||
Example:
|
|
||||||
$ %s query %s hard-claims
|
|
||||||
$ %s query %s hard-claims --owner kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw
|
|
||||||
`,
|
|
||||||
version.ClientName, types.ModuleName, version.ClientName, types.ModuleName)),
|
|
||||||
Args: cobra.NoArgs,
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
||||||
|
|
||||||
strOwner := viper.GetString(flagOwner)
|
|
||||||
page := viper.GetInt(flags.FlagPage)
|
|
||||||
limit := viper.GetInt(flags.FlagLimit)
|
|
||||||
|
|
||||||
// Prepare params for querier
|
|
||||||
owner, err := sdk.AccAddressFromBech32(strOwner)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
params := types.NewQueryHardClaimsParams(page, limit, owner)
|
|
||||||
bz, err := cdc.MarshalJSON(params)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Query
|
|
||||||
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetHardClaims)
|
|
||||||
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
cliCtx = cliCtx.WithHeight(height)
|
|
||||||
|
|
||||||
var claims types.USDXMintingClaims
|
|
||||||
if err := cdc.UnmarshalJSON(res, &claims); err != nil {
|
|
||||||
return fmt.Errorf("failed to unmarshal claims: %w", err)
|
|
||||||
}
|
|
||||||
return cliCtx.PrintOutput(claims)
|
|
||||||
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmd.Flags().String(flagOwner, "", "(optional) filter by claim owner address")
|
|
||||||
cmd.Flags().Int(flags.FlagPage, 1, "pagination page of CDPs to to query for")
|
|
||||||
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of CDPs to query for")
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,3 +138,49 @@ func queryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func executeHardRewardsQuery(queryRoute string, cdc *codec.Codec, cliCtx context.CLIContext,
|
||||||
|
params types.QueryHardRewardsParams) (types.HardLiquidityProviderClaims, error) {
|
||||||
|
bz, err := cdc.MarshalJSON(params)
|
||||||
|
if err != nil {
|
||||||
|
return types.HardLiquidityProviderClaims{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetHardRewards)
|
||||||
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
||||||
|
if err != nil {
|
||||||
|
return types.HardLiquidityProviderClaims{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cliCtx = cliCtx.WithHeight(height)
|
||||||
|
|
||||||
|
var claims types.HardLiquidityProviderClaims
|
||||||
|
if err := cdc.UnmarshalJSON(res, &claims); err != nil {
|
||||||
|
return types.HardLiquidityProviderClaims{}, fmt.Errorf("failed to unmarshal claims: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return claims, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func executeUSDXMintingRewardsQuery(queryRoute string, cdc *codec.Codec, cliCtx context.CLIContext,
|
||||||
|
params types.QueryUSDXMintingRewardsParams) (types.USDXMintingClaims, error) {
|
||||||
|
bz, err := cdc.MarshalJSON(params)
|
||||||
|
if err != nil {
|
||||||
|
return types.USDXMintingClaims{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetUSDXMintingRewards)
|
||||||
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
||||||
|
if err != nil {
|
||||||
|
return types.USDXMintingClaims{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cliCtx = cliCtx.WithHeight(height)
|
||||||
|
|
||||||
|
var claims types.USDXMintingClaims
|
||||||
|
if err := cdc.UnmarshalJSON(res, &claims); err != nil {
|
||||||
|
return types.USDXMintingClaims{}, fmt.Errorf("failed to unmarshal claims: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return claims, nil
|
||||||
|
}
|
||||||
|
@ -15,12 +15,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
||||||
r.HandleFunc(fmt.Sprintf("/%s/cdp-claims", types.ModuleName), queryCdpClaimsHandlerFn(cliCtx)).Methods("GET")
|
r.HandleFunc(fmt.Sprintf("/%s/rewards", types.ModuleName), queryRewardsHandlerFn(cliCtx)).Methods("GET")
|
||||||
r.HandleFunc(fmt.Sprintf("/%s/hard-claims", types.ModuleName), queryHardClaimsHandlerFn(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")
|
||||||
}
|
}
|
||||||
|
|
||||||
func queryCdpClaimsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
func queryRewardsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -42,61 +41,23 @@ func queryCdpClaimsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
queryParams := types.NewQueryCdpClaimsParams(page, limit, owner)
|
var rewardType string
|
||||||
bz, err := cliCtx.Codec.MarshalJSON(queryParams)
|
if x := r.URL.Query().Get(types.RestClaimType); len(x) != 0 {
|
||||||
if err != nil {
|
rewardType = strings.ToLower(strings.TrimSpace(x))
|
||||||
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/incentive/%s", types.QueryGetCdpClaims), bz)
|
switch strings.ToLower(rewardType) {
|
||||||
if err != nil {
|
case "hard":
|
||||||
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
params := types.NewQueryHardRewardsParams(page, limit, owner)
|
||||||
return
|
executeHardRewardsQuery(w, cliCtx, params)
|
||||||
|
case "usdx_minting":
|
||||||
|
params := types.NewQueryUSDXMintingRewardsParams(page, limit, owner)
|
||||||
|
executeUSDXMintingRewardsQuery(w, cliCtx, params)
|
||||||
|
default:
|
||||||
|
hardParams := types.NewQueryHardRewardsParams(page, limit, owner)
|
||||||
|
usdxMintingParams := types.NewQueryUSDXMintingRewardsParams(page, limit, owner)
|
||||||
|
executeBothRewardQueries(w, cliCtx, hardParams, usdxMintingParams)
|
||||||
}
|
}
|
||||||
|
|
||||||
cliCtx = cliCtx.WithHeight(height)
|
|
||||||
rest.PostProcessResponse(w, cliCtx, res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func queryHardClaimsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
|
||||||
if err != nil {
|
|
||||||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var owner sdk.AccAddress
|
|
||||||
if x := r.URL.Query().Get(types.RestClaimOwner); len(x) != 0 {
|
|
||||||
ownerStr := strings.ToLower(strings.TrimSpace(x))
|
|
||||||
owner, err = sdk.AccAddressFromBech32(ownerStr)
|
|
||||||
if err != nil {
|
|
||||||
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("cannot parse address from claim owner %s", ownerStr))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
queryParams := types.NewQueryHardClaimsParams(page, limit, owner)
|
|
||||||
bz, err := cliCtx.Codec.MarshalJSON(queryParams)
|
|
||||||
if err != nil {
|
|
||||||
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/incentive/%s", types.QueryGetHardClaims), bz)
|
|
||||||
if err != nil {
|
|
||||||
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cliCtx = cliCtx.WithHeight(height)
|
|
||||||
rest.PostProcessResponse(w, cliCtx, res)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,3 +80,67 @@ func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|||||||
rest.PostProcessResponse(w, cliCtx, res)
|
rest.PostProcessResponse(w, cliCtx, res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func executeHardRewardsQuery(w http.ResponseWriter, cliCtx context.CLIContext, params types.QueryHardRewardsParams) {
|
||||||
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/incentive/%s", types.QueryGetHardRewards), bz)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cliCtx = cliCtx.WithHeight(height)
|
||||||
|
rest.PostProcessResponse(w, cliCtx, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func executeUSDXMintingRewardsQuery(w http.ResponseWriter, cliCtx context.CLIContext, params types.QueryUSDXMintingRewardsParams) {
|
||||||
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/incentive/%s", types.QueryGetUSDXMintingRewards), bz)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cliCtx = cliCtx.WithHeight(height)
|
||||||
|
rest.PostProcessResponse(w, cliCtx, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func executeBothRewardQueries(w http.ResponseWriter, cliCtx context.CLIContext,
|
||||||
|
hardParams types.QueryHardRewardsParams, usdxMintingParams types.QueryUSDXMintingRewardsParams) {
|
||||||
|
hardBz, err := cliCtx.Codec.MarshalJSON(hardParams)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hardRes, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/incentive/%s", types.QueryGetHardRewards), hardBz)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
usdxMintingBz, err := cliCtx.Codec.MarshalJSON(usdxMintingParams)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
usdxMintingRes, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/incentive/%s", types.QueryGetUSDXMintingRewards), usdxMintingBz)
|
||||||
|
if err != nil {
|
||||||
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cliCtx = cliCtx.WithHeight(height)
|
||||||
|
rest.PostProcessResponse(w, cliCtx, append(hardRes, usdxMintingRes...))
|
||||||
|
}
|
||||||
|
@ -17,10 +17,10 @@ func NewQuerier(k Keeper) sdk.Querier {
|
|||||||
switch path[0] {
|
switch path[0] {
|
||||||
case types.QueryGetParams:
|
case types.QueryGetParams:
|
||||||
return queryGetParams(ctx, req, k)
|
return queryGetParams(ctx, req, k)
|
||||||
case types.QueryGetCdpClaims:
|
case types.QueryGetHardRewards:
|
||||||
return queryGetCdpClaims(ctx, req, k)
|
return queryGetHardRewards(ctx, req, k)
|
||||||
case types.QueryGetHardClaims:
|
case types.QueryGetUSDXMintingRewards:
|
||||||
return queryGetHardClaims(ctx, req, k)
|
return queryGetUSDXMintingRewards(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)
|
||||||
}
|
}
|
||||||
@ -40,60 +40,70 @@ func queryGetParams(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, e
|
|||||||
return bz, nil
|
return bz, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func queryGetCdpClaims(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
func queryGetHardRewards(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
||||||
var requestParams types.QueryCdpClaimsParams
|
var params types.QueryHardRewardsParams
|
||||||
err := k.cdc.UnmarshalJSON(req.Data, &requestParams)
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
||||||
}
|
}
|
||||||
var claims types.USDXMintingClaims
|
owner := len(params.Owner) > 0
|
||||||
if len(requestParams.Owner) > 0 {
|
|
||||||
claim, _ := k.GetUSDXMintingClaim(ctx, requestParams.Owner)
|
var hardClaims types.HardLiquidityProviderClaims
|
||||||
claims = append(claims, claim)
|
switch {
|
||||||
} else {
|
case owner:
|
||||||
claims = k.GetAllUSDXMintingClaims(ctx)
|
hardClaim, foundHardClaim := k.GetHardLiquidityProviderClaim(ctx, params.Owner)
|
||||||
|
if foundHardClaim {
|
||||||
|
hardClaims = append(hardClaims, hardClaim)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
hardClaims = k.GetAllHardLiquidityProviderClaims(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
var paginatedClaims types.USDXMintingClaims
|
var paginatedHardClaims types.HardLiquidityProviderClaims
|
||||||
|
startH, endH := client.Paginate(len(hardClaims), params.Page, params.Limit, 100)
|
||||||
start, end := client.Paginate(len(claims), requestParams.Page, requestParams.Limit, 100)
|
if startH < 0 || endH < 0 {
|
||||||
if start < 0 || end < 0 {
|
paginatedHardClaims = types.HardLiquidityProviderClaims{}
|
||||||
paginatedClaims = types.USDXMintingClaims{}
|
|
||||||
} else {
|
} else {
|
||||||
paginatedClaims = claims[start:end]
|
paginatedHardClaims = hardClaims[startH:endH]
|
||||||
}
|
}
|
||||||
|
|
||||||
bz, err := codec.MarshalJSONIndent(k.cdc, paginatedClaims)
|
// Marshal Hard claims
|
||||||
|
bz, err := codec.MarshalJSONIndent(k.cdc, paginatedHardClaims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
||||||
}
|
}
|
||||||
return bz, nil
|
return bz, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func queryGetHardClaims(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
func queryGetUSDXMintingRewards(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
||||||
var requestParams types.QueryHardClaimsParams
|
var params types.QueryUSDXMintingRewardsParams
|
||||||
err := k.cdc.UnmarshalJSON(req.Data, &requestParams)
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
||||||
}
|
}
|
||||||
var claims types.HardLiquidityProviderClaims
|
owner := len(params.Owner) > 0
|
||||||
if len(requestParams.Owner) > 0 {
|
|
||||||
claim, _ := k.GetHardLiquidityProviderClaim(ctx, requestParams.Owner)
|
var usdxMintingClaims types.USDXMintingClaims
|
||||||
claims = append(claims, claim)
|
switch {
|
||||||
} else {
|
case owner:
|
||||||
claims = k.GetAllHardLiquidityProviderClaims(ctx)
|
usdxMintingClaim, foundUsdxMintingClaim := k.GetUSDXMintingClaim(ctx, params.Owner)
|
||||||
|
if foundUsdxMintingClaim {
|
||||||
|
usdxMintingClaims = append(usdxMintingClaims, usdxMintingClaim)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
usdxMintingClaims = k.GetAllUSDXMintingClaims(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
var paginatedClaims types.HardLiquidityProviderClaims
|
var paginatedUsdxMintingClaims types.USDXMintingClaims
|
||||||
|
startU, endU := client.Paginate(len(usdxMintingClaims), params.Page, params.Limit, 100)
|
||||||
start, end := client.Paginate(len(claims), requestParams.Page, requestParams.Limit, 100)
|
if startU < 0 || endU < 0 {
|
||||||
if start < 0 || end < 0 {
|
paginatedUsdxMintingClaims = types.USDXMintingClaims{}
|
||||||
paginatedClaims = types.HardLiquidityProviderClaims{}
|
|
||||||
} else {
|
} else {
|
||||||
paginatedClaims = claims[start:end]
|
paginatedUsdxMintingClaims = usdxMintingClaims[startU:endU]
|
||||||
}
|
}
|
||||||
|
|
||||||
bz, err := codec.MarshalJSONIndent(k.cdc, paginatedClaims)
|
// Marshal USDX minting claims
|
||||||
|
bz, err := codec.MarshalJSONIndent(k.cdc, paginatedUsdxMintingClaims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
||||||
}
|
}
|
||||||
|
@ -77,9 +77,15 @@ func NewUSDXMintingClaim(owner sdk.AccAddress, reward sdk.Coin, rewardIndexes Re
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetType returns the claim type, used to identify auctions in event attributes
|
// GetType returns the claim's type
|
||||||
func (c USDXMintingClaim) GetType() string { return USDXMintingClaimType }
|
func (c USDXMintingClaim) GetType() string { return USDXMintingClaimType }
|
||||||
|
|
||||||
|
// GetReward returns the claim's reward coin
|
||||||
|
func (c USDXMintingClaim) GetReward() sdk.Coin { return c.Reward }
|
||||||
|
|
||||||
|
// GetOwner returns the claim's owner
|
||||||
|
func (c USDXMintingClaim) GetOwner() sdk.AccAddress { return c.Owner }
|
||||||
|
|
||||||
// Validate performs a basic check of a Claim fields
|
// Validate performs a basic check of a Claim fields
|
||||||
func (c USDXMintingClaim) Validate() error {
|
func (c USDXMintingClaim) Validate() error {
|
||||||
if err := c.RewardIndexes.Validate(); err != nil {
|
if err := c.RewardIndexes.Validate(); err != nil {
|
||||||
@ -143,9 +149,15 @@ func NewHardLiquidityProviderClaim(owner sdk.AccAddress, reward sdk.Coin, supply
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetType returns the claim type, used to identify auctions in event attributes
|
// GetType returns the claim's type
|
||||||
func (c HardLiquidityProviderClaim) GetType() string { return HardLiquidityProviderClaimType }
|
func (c HardLiquidityProviderClaim) GetType() string { return HardLiquidityProviderClaimType }
|
||||||
|
|
||||||
|
// GetReward returns the claim's reward coin
|
||||||
|
func (c HardLiquidityProviderClaim) GetReward() sdk.Coin { return c.Reward }
|
||||||
|
|
||||||
|
// GetOwner returns the claim's owner
|
||||||
|
func (c HardLiquidityProviderClaim) GetOwner() sdk.AccAddress { return c.Owner }
|
||||||
|
|
||||||
// Validate performs a basic check of a HardLiquidityProviderClaim fields
|
// Validate performs a basic check of a HardLiquidityProviderClaim fields
|
||||||
func (c HardLiquidityProviderClaim) Validate() error {
|
func (c HardLiquidityProviderClaim) Validate() error {
|
||||||
if err := c.SupplyRewardIndexes.Validate(); err != nil {
|
if err := c.SupplyRewardIndexes.Validate(); err != nil {
|
||||||
|
@ -17,4 +17,5 @@ var (
|
|||||||
ErrInvalidMultiplier = sdkerrors.Register(ModuleName, 8, "invalid rewards multiplier")
|
ErrInvalidMultiplier = sdkerrors.Register(ModuleName, 8, "invalid rewards multiplier")
|
||||||
ErrZeroClaim = sdkerrors.Register(ModuleName, 9, "cannot claim - claim amount rounds to zero")
|
ErrZeroClaim = sdkerrors.Register(ModuleName, 9, "cannot claim - claim amount rounds to zero")
|
||||||
ErrClaimExpired = sdkerrors.Register(ModuleName, 10, "claim has expired")
|
ErrClaimExpired = sdkerrors.Register(ModuleName, 10, "claim has expired")
|
||||||
|
ErrInvalidClaimType = sdkerrors.Register(ModuleName, 11, "invalid claim type")
|
||||||
)
|
)
|
||||||
|
@ -7,41 +7,61 @@ import (
|
|||||||
|
|
||||||
// Querier routes for the incentive module
|
// Querier routes for the incentive module
|
||||||
const (
|
const (
|
||||||
QueryGetCdpClaims = "cdp-claims"
|
QueryGetRewards = "rewards"
|
||||||
QueryGetHardClaims = "hard-claims"
|
QueryGetHardRewards = "hard-rewards"
|
||||||
RestClaimOwner = "owner"
|
QueryGetUSDXMintingRewards = "usdx-minting-rewards"
|
||||||
RestClaimCollateralType = "collateral_type"
|
QueryGetParams = "parameters"
|
||||||
QueryGetParams = "parameters"
|
QueryGetRewardPeriods = "reward-periods"
|
||||||
QueryGetRewardPeriods = "reward-periods"
|
QueryGetClaimPeriods = "claim-periods"
|
||||||
QueryGetClaimPeriods = "claim-periods"
|
RestClaimCollateralType = "collateral_type"
|
||||||
|
RestClaimOwner = "owner"
|
||||||
|
RestClaimType = "type"
|
||||||
)
|
)
|
||||||
|
|
||||||
// QueryCdpClaimsParams params for query /incentive/claims
|
// QueryRewardsParams params for query /incentive/rewards
|
||||||
type QueryCdpClaimsParams struct {
|
type QueryRewardsParams struct {
|
||||||
|
Page int `json:"page" yaml:"page"`
|
||||||
|
Limit int `json:"limit" yaml:"limit"`
|
||||||
|
Owner sdk.AccAddress
|
||||||
|
Type string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewQueryRewardsParams returns QueryRewardsParams
|
||||||
|
func NewQueryRewardsParams(page, limit int, owner sdk.AccAddress, rewardType string) QueryRewardsParams {
|
||||||
|
return QueryRewardsParams{
|
||||||
|
Page: page,
|
||||||
|
Limit: limit,
|
||||||
|
Owner: owner,
|
||||||
|
Type: rewardType,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryUSDXMintingRewardsParams params for query /incentive/rewards type usdx-minting
|
||||||
|
type QueryUSDXMintingRewardsParams struct {
|
||||||
Page int `json:"page" yaml:"page"`
|
Page int `json:"page" yaml:"page"`
|
||||||
Limit int `json:"limit" yaml:"limit"`
|
Limit int `json:"limit" yaml:"limit"`
|
||||||
Owner sdk.AccAddress
|
Owner sdk.AccAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewQueryCdpClaimsParams returns QueryCdpClaimsParams
|
// NewQueryUSDXMintingRewardsParams returns QueryUSDXMintingRewardsParams
|
||||||
func NewQueryCdpClaimsParams(page, limit int, owner sdk.AccAddress) QueryCdpClaimsParams {
|
func NewQueryUSDXMintingRewardsParams(page, limit int, owner sdk.AccAddress) QueryUSDXMintingRewardsParams {
|
||||||
return QueryCdpClaimsParams{
|
return QueryUSDXMintingRewardsParams{
|
||||||
Page: page,
|
Page: page,
|
||||||
Limit: limit,
|
Limit: limit,
|
||||||
Owner: owner,
|
Owner: owner,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryHardClaimsParams params for query /incentive/claims
|
// QueryHardRewardsParams params for query /incentive/rewards type hard
|
||||||
type QueryHardClaimsParams struct {
|
type QueryHardRewardsParams struct {
|
||||||
Page int `json:"page" yaml:"page"`
|
Page int `json:"page" yaml:"page"`
|
||||||
Limit int `json:"limit" yaml:"limit"`
|
Limit int `json:"limit" yaml:"limit"`
|
||||||
Owner sdk.AccAddress
|
Owner sdk.AccAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewQueryHardClaimsParams returns QueryHardClaimsParams
|
// NewQueryHardRewardsParams returns QueryHardRewardsParams
|
||||||
func NewQueryHardClaimsParams(page, limit int, owner sdk.AccAddress) QueryHardClaimsParams {
|
func NewQueryHardRewardsParams(page, limit int, owner sdk.AccAddress) QueryHardRewardsParams {
|
||||||
return QueryHardClaimsParams{
|
return QueryHardRewardsParams{
|
||||||
Page: page,
|
Page: page,
|
||||||
Limit: limit,
|
Limit: limit,
|
||||||
Owner: owner,
|
Owner: owner,
|
||||||
|
Loading…
Reference in New Issue
Block a user