2020-04-24 15:20:34 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-04-30 14:23:41 +00:00
|
|
|
"github.com/spf13/cobra"
|
2021-01-18 19:12:37 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2020-04-24 15:20:34 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/version"
|
2020-04-30 14:13:31 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
2020-04-24 15:20:34 +00:00
|
|
|
)
|
|
|
|
|
2021-01-27 13:33:36 +00:00
|
|
|
const (
|
|
|
|
flagOwner = "owner"
|
|
|
|
flagType = "type"
|
|
|
|
)
|
|
|
|
|
2020-04-24 15:20:34 +00:00
|
|
|
// GetQueryCmd returns the cli query commands for the incentive module
|
|
|
|
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
incentiveQueryCmd := &cobra.Command{
|
|
|
|
Use: types.ModuleName,
|
|
|
|
Short: "Querying commands for the incentive module",
|
|
|
|
}
|
|
|
|
|
|
|
|
incentiveQueryCmd.AddCommand(flags.GetCommands(
|
|
|
|
queryParamsCmd(queryRoute, cdc),
|
2021-01-27 13:33:36 +00:00
|
|
|
queryRewardsCmd(queryRoute, cdc),
|
2020-04-24 15:20:34 +00:00
|
|
|
)...)
|
|
|
|
|
|
|
|
return incentiveQueryCmd
|
|
|
|
}
|
|
|
|
|
2021-01-27 13:33:36 +00:00
|
|
|
func queryRewardsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
2021-01-18 19:12:37 +00:00
|
|
|
cmd := &cobra.Command{
|
2021-01-27 13:33:36 +00:00
|
|
|
Use: "rewards",
|
|
|
|
Short: "query claimable rewards",
|
2020-04-24 15:20:34 +00:00
|
|
|
Long: strings.TrimSpace(
|
2021-01-27 13:33:36 +00:00
|
|
|
fmt.Sprintf(`Query rewards with optional flags for owner and type
|
2020-04-24 15:20:34 +00:00
|
|
|
|
|
|
|
Example:
|
2021-01-27 13:33:36 +00:00
|
|
|
$ %s query %s rewards
|
|
|
|
$ %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
|
2021-01-18 19:12:37 +00:00
|
|
|
`,
|
2021-01-27 13:33:36 +00:00
|
|
|
version.ClientName, types.ModuleName, version.ClientName, types.ModuleName,
|
|
|
|
version.ClientName, types.ModuleName, version.ClientName, types.ModuleName,
|
|
|
|
version.ClientName, types.ModuleName)),
|
2021-01-18 19:12:37 +00:00
|
|
|
Args: cobra.NoArgs,
|
2020-04-24 15:20:34 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
2021-01-18 19:12:37 +00:00
|
|
|
|
|
|
|
page := viper.GetInt(flags.FlagPage)
|
|
|
|
limit := viper.GetInt(flags.FlagLimit)
|
2021-01-26 11:52:34 +00:00
|
|
|
strOwner := viper.GetString(flagOwner)
|
2021-01-27 13:33:36 +00:00
|
|
|
strType := viper.GetString(flagType)
|
2021-01-26 11:52:34 +00:00
|
|
|
|
|
|
|
// Prepare params for querier
|
|
|
|
owner, err := sdk.AccAddressFromBech32(strOwner)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-27 13:33:36 +00:00
|
|
|
switch strings.ToLower(strType) {
|
|
|
|
case "hard":
|
|
|
|
params := types.NewQueryHardRewardsParams(page, limit, owner)
|
|
|
|
claims, err := executeHardRewardsQuery(queryRoute, cdc, cliCtx, params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
paramsUSDXMinting := types.NewQueryUSDXMintingRewardsParams(page, limit, owner)
|
|
|
|
usdxMintingClaims, err := executeUSDXMintingRewardsQuery(queryRoute, cdc, cliCtx, paramsUSDXMinting)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(usdxMintingClaims) > 0 {
|
|
|
|
cliCtx.PrintOutput(usdxMintingClaims)
|
|
|
|
}
|
2020-04-24 15:20:34 +00:00
|
|
|
}
|
2021-01-27 13:33:36 +00:00
|
|
|
return nil
|
2020-04-24 15:20:34 +00:00
|
|
|
},
|
|
|
|
}
|
2021-01-27 13:33:36 +00:00
|
|
|
cmd.Flags().String(flagOwner, "", "(optional) filter by owner address")
|
|
|
|
cmd.Flags().String(flagType, "", "(optional) filter by reward type")
|
|
|
|
cmd.Flags().Int(flags.FlagPage, 1, "pagination page rewards of to to query for")
|
|
|
|
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of rewards to query for")
|
2021-01-18 19:12:37 +00:00
|
|
|
return cmd
|
2020-04-24 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func queryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "params",
|
|
|
|
Short: "get the incentive module parameters",
|
|
|
|
Long: "Get the current global incentive 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)
|
2020-05-28 14:57:22 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(route, nil)
|
2020-04-24 15:20:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-28 14:57:22 +00:00
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
2020-04-24 15:20:34 +00:00
|
|
|
|
|
|
|
// Decode and print results
|
|
|
|
var params types.Params
|
|
|
|
if err := cdc.UnmarshalJSON(res, ¶ms); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal params: %w", err)
|
|
|
|
}
|
|
|
|
return cliCtx.PrintOutput(params)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 13:33:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|