mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-27 16:55:21 +00:00
d849d690e5
* wip: tpyes and keeper methods * wip: iterators * wip: types and keeper methods * wip: add msgs * wip: client methods * wip: rebase develop * wip: types tests * wip: keeper tests, small fixes * wip: add cdp tests * wip: deposit tests * wip: keeper tests * wip: tests and module methods * feat: error when fetching expired price * feat: conversion factor for external assets * feat: debt floor for new cdps * feat: save deposits on export genesis * feat: ensure messages implement msg * feat: index deposits by status * fix: stray comment * wip: address review comments * address review comments
172 lines
4.9 KiB
Go
172 lines
4.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
)
|
|
|
|
// GetQueryCmd returns the cli query commands for this module
|
|
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
// Group nameservice queries under a subcommand
|
|
cdpQueryCmd := &cobra.Command{
|
|
Use: "cdp",
|
|
Short: "Querying commands for the cdp module",
|
|
}
|
|
|
|
cdpQueryCmd.AddCommand(client.GetCommands(
|
|
QueryCdpCmd(queryRoute, cdc),
|
|
QueryCdpsByDenomCmd(queryRoute, cdc),
|
|
QueryCdpsByDenomAndRatioCmd(queryRoute, cdc),
|
|
QueryParamsCmd(queryRoute, cdc),
|
|
)...)
|
|
|
|
return cdpQueryCmd
|
|
}
|
|
|
|
// QueryCdpCmd returns the command handler for querying a particular cdp
|
|
func QueryCdpCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "cdp [ownerAddress] [collateralType]",
|
|
Short: "get info about a cdp",
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
// Prepare params for querier
|
|
ownerAddress, err := sdk.AccAddressFromBech32(args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
collateralType := args[1] // TODO validation?
|
|
bz, err := cdc.MarshalJSON(types.QueryCdpsParams{
|
|
CollateralDenom: collateralType,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Query
|
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetCdp)
|
|
res, _, err := cliCtx.QueryWithData(route, bz)
|
|
if err != nil {
|
|
fmt.Printf("error when getting cdp info - %s", err)
|
|
fmt.Printf("could not get current cdp info - %s %s \n", string(ownerAddress), string(collateralType))
|
|
return err
|
|
}
|
|
|
|
// Decode and print results
|
|
var cdp types.CDP
|
|
cdc.MustUnmarshalJSON(res, &cdp)
|
|
return cliCtx.PrintOutput(cdp)
|
|
},
|
|
}
|
|
}
|
|
|
|
// QueryCdpsByDenomCmd returns the command handler for querying cdps for a collateral type
|
|
func QueryCdpsByDenomCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "cdps [collateralType]",
|
|
Short: "Query cdps by collateral type",
|
|
Long: strings.TrimSpace(`Query cdps by a specific collateral type, or query all cdps if none is specifed:
|
|
|
|
$ <appcli> query cdp cdps atom
|
|
`),
|
|
Args: cobra.MaximumNArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
// Prepare params for querier
|
|
bz, err := cdc.MarshalJSON(types.QueryCdpsParams{CollateralDenom: args[0]})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Query
|
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetCdps)
|
|
res, _, err := cliCtx.QueryWithData(route, bz)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Decode and print results
|
|
var out types.CDPs
|
|
cdc.MustUnmarshalJSON(res, &out)
|
|
return cliCtx.PrintOutput(out)
|
|
},
|
|
}
|
|
}
|
|
|
|
// QueryCdpsByDenomAndRatioCmd returns the command handler for querying cdps
|
|
// by specified collateral type and collateralization ratio
|
|
func QueryCdpsByDenomAndRatioCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "cdps [collateralType] [ratio]",
|
|
Short: "get cdps with matching collateral type and below the specified ratio",
|
|
Long: strings.TrimSpace(`Get all CDPS of a particular collateral type with collateralization
|
|
ratio below the specified input.`),
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
|
|
|
// Prepare params for querier
|
|
price, errSdk := sdk.NewDecFromStr(args[1])
|
|
if errSdk != nil {
|
|
return fmt.Errorf(errSdk.Error())
|
|
}
|
|
bz, err := cdc.MarshalJSON(types.QueryCdpsByRatioParams{
|
|
CollateralDenom: args[0],
|
|
Ratio: price,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Query
|
|
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetCdps)
|
|
res, _, err := cliCtx.QueryWithData(route, bz)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Decode and print results
|
|
var out types.CDPs
|
|
cdc.MustUnmarshalJSON(res, &out)
|
|
return cliCtx.PrintOutput(out)
|
|
},
|
|
}
|
|
}
|
|
|
|
// QueryParamsCmd returns the command handler for cdp parameter querying
|
|
func QueryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "params",
|
|
Short: "get the cdp module parameters",
|
|
Long: "Get the current global cdp 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)
|
|
res, _, err := cliCtx.QueryWithData(route, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Decode and print results
|
|
var out types.QueryCdpParams
|
|
cdc.MustUnmarshalJSON(res, &out)
|
|
return cliCtx.PrintOutput(out)
|
|
},
|
|
}
|
|
}
|