2020-09-21 21:08:43 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"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"
|
|
|
|
supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported"
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
"github.com/kava-labs/kava/x/hard/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// flags for cli queries
|
|
|
|
const (
|
|
|
|
flagName = "name"
|
|
|
|
flagDepositDenom = "deposit-denom"
|
|
|
|
flagOwner = "owner"
|
2020-11-12 16:43:28 +00:00
|
|
|
flagClaimType = "claim-type"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
// GetQueryCmd returns the cli query commands for the module
|
2020-09-21 21:08:43 +00:00
|
|
|
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
2020-12-21 17:18:55 +00:00
|
|
|
hardQueryCmd := &cobra.Command{
|
2020-09-21 21:08:43 +00:00
|
|
|
Use: types.ModuleName,
|
2020-12-21 17:18:55 +00:00
|
|
|
Short: "Querying commands for the hard module",
|
2020-09-21 21:08:43 +00:00
|
|
|
DisableFlagParsing: true,
|
|
|
|
SuggestionsMinimumDistance: 2,
|
|
|
|
RunE: client.ValidateCmd,
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
hardQueryCmd.AddCommand(flags.GetCommands(
|
2020-09-21 21:08:43 +00:00
|
|
|
queryParamsCmd(queryRoute, cdc),
|
|
|
|
queryModAccountsCmd(queryRoute, cdc),
|
|
|
|
queryDepositsCmd(queryRoute, cdc),
|
|
|
|
queryClaimsCmd(queryRoute, cdc),
|
2020-10-30 09:59:47 +00:00
|
|
|
queryBorrowsCmd(queryRoute, cdc),
|
2020-12-04 19:04:05 +00:00
|
|
|
queryBorrowCmd(queryRoute, cdc),
|
2020-09-21 21:08:43 +00:00
|
|
|
)...)
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
return hardQueryCmd
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "params",
|
2020-12-21 17:18:55 +00:00
|
|
|
Short: "get the hard module parameters",
|
|
|
|
Long: "Get the current global hard module parameters.",
|
2020-09-21 21:08:43 +00:00
|
|
|
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)
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryModAccountsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "accounts",
|
2020-12-21 17:18:55 +00:00
|
|
|
Short: "query hard module accounts with optional filter",
|
|
|
|
Long: strings.TrimSpace(`Query for all hard module accounts or a specific account using the name flag:
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
Example:
|
2020-12-21 17:18:55 +00:00
|
|
|
$ kvcli q hard accounts
|
|
|
|
$ kvcli q hard accounts --name hard|hard_delegator_distribution|hard_lp_distribution`,
|
2020-09-21 21:08:43 +00:00
|
|
|
),
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
|
|
|
|
name := viper.GetString(flagName)
|
|
|
|
page := viper.GetInt(flags.FlagPage)
|
|
|
|
limit := viper.GetInt(flags.FlagLimit)
|
|
|
|
|
|
|
|
params := types.NewQueryAccountParams(page, limit, name)
|
|
|
|
bz, err := cdc.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetModuleAccounts)
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
|
|
|
|
var modAccounts []supplyexported.ModuleAccountI
|
|
|
|
if err := cdc.UnmarshalJSON(res, &modAccounts); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal module accounts: %w", err)
|
|
|
|
}
|
|
|
|
return cliCtx.PrintOutput(modAccounts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryDepositsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
2020-09-30 18:44:56 +00:00
|
|
|
cmd := &cobra.Command{
|
2020-09-21 21:08:43 +00:00
|
|
|
Use: "deposits",
|
2020-12-21 17:18:55 +00:00
|
|
|
Short: "query hard module deposits with optional filters",
|
|
|
|
Long: strings.TrimSpace(`query for all hard module deposits or a specific deposit using flags:
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
Example:
|
2020-12-21 17:18:55 +00:00
|
|
|
$ kvcli q hard deposits
|
|
|
|
$ kvcli q hard deposits --owner kava1l0xsq2z7gqd7yly0g40y5836g0appumark77ny --deposit-denom bnb
|
|
|
|
$ kvcli q hard deposits --deposit-denom ukava
|
|
|
|
$ kvcli q hard deposits --deposit-denom btcb`,
|
2020-09-21 21:08:43 +00:00
|
|
|
),
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
|
|
|
|
var owner sdk.AccAddress
|
|
|
|
|
|
|
|
ownerBech := viper.GetString(flagOwner)
|
|
|
|
depositDenom := viper.GetString(flagDepositDenom)
|
|
|
|
|
|
|
|
if len(ownerBech) != 0 {
|
|
|
|
depositOwner, err := sdk.AccAddressFromBech32(ownerBech)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
owner = depositOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
page := viper.GetInt(flags.FlagPage)
|
|
|
|
limit := viper.GetInt(flags.FlagLimit)
|
|
|
|
|
2020-11-12 16:43:28 +00:00
|
|
|
params := types.NewQueryDepositParams(page, limit, depositDenom, owner)
|
2020-09-21 21:08:43 +00:00
|
|
|
bz, err := cdc.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetDeposits)
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
|
|
|
|
var deposits []types.Deposit
|
|
|
|
if err := cdc.UnmarshalJSON(res, &deposits); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal deposits: %w", err)
|
|
|
|
}
|
|
|
|
return cliCtx.PrintOutput(deposits)
|
|
|
|
},
|
|
|
|
}
|
2020-09-30 18:44:56 +00:00
|
|
|
cmd.Flags().Int(flags.FlagPage, 1, "pagination page to query for")
|
|
|
|
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit (max 100)")
|
|
|
|
cmd.Flags().String(flagOwner, "", "(optional) filter for deposits by owner address")
|
|
|
|
cmd.Flags().String(flagDepositDenom, "", "(optional) filter for deposits by denom")
|
|
|
|
return cmd
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func queryClaimsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
2020-09-30 18:44:56 +00:00
|
|
|
cmd := &cobra.Command{
|
2020-09-21 21:08:43 +00:00
|
|
|
Use: "claims",
|
2020-12-21 17:18:55 +00:00
|
|
|
Short: "query hard module claims with optional filters",
|
|
|
|
Long: strings.TrimSpace(`query for all hard module claims or a specific claim using flags:
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
Example:
|
2020-12-21 17:18:55 +00:00
|
|
|
$ kvcli q hard claims
|
|
|
|
$ kvcli q hard claims --owner kava1l0xsq2z7gqd7yly0g40y5836g0appumark77ny --claim-type lp --deposit-denom bnb
|
|
|
|
$ kvcli q hard claims --claim-type stake --deposit-denom ukava
|
|
|
|
$ kvcli q hard claims --deposit-denom btcb`,
|
2020-09-21 21:08:43 +00:00
|
|
|
),
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
|
|
|
|
var owner sdk.AccAddress
|
2020-11-12 16:43:28 +00:00
|
|
|
var claimType types.ClaimType
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
ownerBech := viper.GetString(flagOwner)
|
|
|
|
depositDenom := viper.GetString(flagDepositDenom)
|
2020-11-12 16:43:28 +00:00
|
|
|
claimTypeStr := viper.GetString(flagClaimType)
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
if len(ownerBech) != 0 {
|
|
|
|
claimOwner, err := sdk.AccAddressFromBech32(ownerBech)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
owner = claimOwner
|
|
|
|
}
|
|
|
|
|
2020-11-12 16:43:28 +00:00
|
|
|
if len(claimTypeStr) != 0 {
|
|
|
|
if err := types.ClaimType(claimTypeStr).IsValid(); err != nil {
|
2020-09-21 21:08:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-12 16:43:28 +00:00
|
|
|
claimType = types.ClaimType(claimTypeStr)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
page := viper.GetInt(flags.FlagPage)
|
|
|
|
limit := viper.GetInt(flags.FlagLimit)
|
|
|
|
|
2020-11-12 16:43:28 +00:00
|
|
|
params := types.NewQueryClaimParams(page, limit, depositDenom, owner, claimType)
|
2020-09-21 21:08:43 +00:00
|
|
|
bz, err := cdc.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetClaims)
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
|
|
|
|
var claims []types.Claim
|
|
|
|
if err := cdc.UnmarshalJSON(res, &claims); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal claims: %w", err)
|
|
|
|
}
|
|
|
|
return cliCtx.PrintOutput(claims)
|
|
|
|
},
|
|
|
|
}
|
2020-09-30 18:44:56 +00:00
|
|
|
cmd.Flags().Int(flags.FlagPage, 1, "pagination page to query for")
|
|
|
|
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit (max 100)")
|
|
|
|
cmd.Flags().String(flagOwner, "", "(optional) filter for claims by owner address")
|
|
|
|
cmd.Flags().String(flagDepositDenom, "", "(optional) filter for claims by denom")
|
2020-11-12 16:43:28 +00:00
|
|
|
cmd.Flags().String(flagClaimType, "", "(optional) filter for claims by type (lp or staking)")
|
2020-09-30 18:44:56 +00:00
|
|
|
return cmd
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2020-10-30 09:59:47 +00:00
|
|
|
|
|
|
|
func queryBorrowsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "borrows",
|
2020-12-21 17:18:55 +00:00
|
|
|
Short: "query hard module borrows with optional filters",
|
|
|
|
Long: strings.TrimSpace(`query for all hard module borrows or a specific borrow using flags:
|
2020-10-30 09:59:47 +00:00
|
|
|
|
|
|
|
Example:
|
2020-12-21 17:18:55 +00:00
|
|
|
$ kvcli q hard borrows
|
|
|
|
$ kvcli q hard borrows --borrower kava1l0xsq2z7gqd7yly0g40y5836g0appumark77ny
|
|
|
|
$ kvcli q hard borrows --borrow-denom bnb`,
|
2020-10-30 09:59:47 +00:00
|
|
|
),
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
|
|
|
|
var owner sdk.AccAddress
|
|
|
|
|
|
|
|
ownerBech := viper.GetString(flagOwner)
|
|
|
|
depositDenom := viper.GetString(flagDepositDenom)
|
|
|
|
|
|
|
|
if len(ownerBech) != 0 {
|
|
|
|
borrowOwner, err := sdk.AccAddressFromBech32(ownerBech)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
owner = borrowOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
page := viper.GetInt(flags.FlagPage)
|
|
|
|
limit := viper.GetInt(flags.FlagLimit)
|
|
|
|
|
|
|
|
params := types.NewQueryBorrowParams(page, limit, owner, depositDenom)
|
|
|
|
bz, err := cdc.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetBorrows)
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
|
|
|
|
var borrows []types.Borrow
|
|
|
|
if err := cdc.UnmarshalJSON(res, &borrows); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal borrows: %w", err)
|
|
|
|
}
|
|
|
|
return cliCtx.PrintOutput(borrows)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmd.Flags().Int(flags.FlagPage, 1, "pagination page to query for")
|
|
|
|
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit (max 100)")
|
|
|
|
cmd.Flags().String(flagOwner, "", "(optional) filter for borrows by owner address")
|
|
|
|
return cmd
|
|
|
|
}
|
2020-11-12 15:50:54 +00:00
|
|
|
|
|
|
|
func queryBorrowedCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "borrowed",
|
|
|
|
Short: "get total current borrowed amount",
|
2020-12-21 17:18:55 +00:00
|
|
|
Long: "get the total amount of coins currently borrowed for the Hard protocol",
|
2020-11-12 15:50:54 +00:00
|
|
|
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.QueryGetBorrowed)
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
|
|
|
|
// Decode and print results
|
|
|
|
var borrowedCoins sdk.Coins
|
|
|
|
if err := cdc.UnmarshalJSON(res, &borrowedCoins); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal borrowed coins: %w", err)
|
|
|
|
}
|
|
|
|
return cliCtx.PrintOutput(borrowedCoins)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 19:04:05 +00:00
|
|
|
|
|
|
|
func queryBorrowCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "borrow",
|
|
|
|
Short: "query outstanding borrow balance for a user",
|
|
|
|
Long: strings.TrimSpace(`query outstanding borrow balance for a user:
|
|
|
|
Example:
|
2020-12-21 17:18:55 +00:00
|
|
|
$ kvcli q hard borrow --owner kava1l0xsq2z7gqd7yly0g40y5836g0appumark77ny`,
|
2020-12-04 19:04:05 +00:00
|
|
|
),
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
|
|
|
|
var owner sdk.AccAddress
|
|
|
|
|
|
|
|
ownerBech := viper.GetString(flagOwner)
|
|
|
|
if len(ownerBech) != 0 {
|
|
|
|
borrowOwner, err := sdk.AccAddressFromBech32(ownerBech)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
owner = borrowOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
params := types.NewQueryBorrow(owner)
|
|
|
|
bz, err := cdc.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetBorrow)
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
|
|
|
|
var balance sdk.Coins
|
|
|
|
if err := cdc.UnmarshalJSON(res, &balance); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal borrow balance: %w", err)
|
|
|
|
}
|
|
|
|
return cliCtx.PrintOutput(balance)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmd.Flags().String(flagOwner, "", "filter for borrows by owner address")
|
|
|
|
return cmd
|
|
|
|
}
|