Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CollateralValue and LiquidationLimit queries (backport #658) #659

Merged
merged 2 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions proto/umee/leverage/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ service Query {
option (google.api.http).get = "/umee/leverage/v1beta1/collateral";
}

// CollateralValue queries for the total USD value of a user's collateral, or
// the USD value held as a given base asset's associated uToken denomination.
rpc CollateralValue(QueryCollateralValueRequest) returns (QueryCollateralValueResponse) {
option (google.api.http).get = "/umee/leverage/v1beta1/collateral_value";
}

// ExchangeRate queries the uToken exchange rate of a given uToken denomination.
rpc ExchangeRate(QueryExchangeRateRequest) returns (QueryExchangeRateResponse) {
option (google.api.http).get = "/umee/leverage/v1beta1/exchange_rate";
Expand All @@ -105,6 +111,11 @@ service Query {
option (google.api.http).get = "/umee/leverage/v1beta1/borrow_limit";
}

// LiquidationLimit queries the limit in USD above which a given borrower is eligible for liquidation.
rpc LiquidationLimit(QueryLiquidationLimitRequest) returns (QueryLiquidationLimitResponse) {
option (google.api.http).get = "/umee/leverage/v1beta1/liquidation_limit";
}

// LiquidationTargets queries a list of all borrower addresses eligible for liquidation.
rpc LiquidationTargets(QueryLiquidationTargetsRequest) returns (QueryLiquidationTargetsResponse) {
option (google.api.http).get = "/umee/leverage/v1beta1/liquidation_targets";
Expand All @@ -116,13 +127,13 @@ service Query {
message QueryRegisteredTokens {}

// QueryAvailableBorrowRequest defines the request structure for the
// available amount for borrow gRPC service handler.
// AvailableBorrow gRPC service handler.
message QueryAvailableBorrowRequest {
string denom = 1;
}

// QueryAvailableBorrowResponse defines the response structure for the
// available amount for borrow gRPC service handler.
// AvailableBorrow gRPC service handler.
message QueryAvailableBorrowResponse {
string amount = 1
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
Expand Down Expand Up @@ -222,6 +233,18 @@ message QueryBorrowedValueResponse {
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

// QueryCollateralValueRequest defines the request structure for the CollateralValue gRPC service handler.
message QueryCollateralValueRequest {
string address = 1;
string denom = 2;
}

// QueryCollateralValueResponse defines the response structure for the CollateralValue gRPC service handler.
message QueryCollateralValueResponse {
string collateral_value = 1
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

// QueryLoanedRequest defines the request structure for the Loaned gRPC service handler.
message QueryLoanedRequest {
string address = 1;
Expand Down Expand Up @@ -304,18 +327,31 @@ message QueryBorrowLimitRequest {
string address = 1;
}

// QueryBorrowLimitResponse defines the response structure for the ExchangeRate
// QueryBorrowLimitResponse defines the response structure for the BorrowLimit
// gRPC service handler.
message QueryBorrowLimitResponse {
string borrow_limit = 1
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

// QueryLiquidationTargetsRequest defines the request structure for the BorrowLimit
// QueryLiquidationLimitRequest defines the request structure for the LiquidationLimit
// gRPC service handler.
message QueryLiquidationLimitRequest {
string address = 1;
}

// QueryLiquidationLimitResponse defines the response structure for the LiquidationLimit
// gRPC service handler.
message QueryLiquidationLimitResponse {
string liquidation_limit = 1
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

// QueryLiquidationTargetsRequest defines the request structure for the LiquidationTargets
// gRPC service handler.
message QueryLiquidationTargetsRequest {}

// QueryLiquidationTargetsResponse defines the response structure for the ExchangeRate
// QueryLiquidationTargetsResponse defines the response structure for the LiquidationTargets
// gRPC service handler.
message QueryLiquidationTargetsResponse {
repeated string targets = 1;
Expand Down
72 changes: 72 additions & 0 deletions x/leverage/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
GetCmdQueryLoanedValue(),
GetCmdQueryReserveAmount(),
GetCmdQueryCollateral(),
GetCmdQueryCollateralValue(),
GetCmdQueryCollateralSetting(),
GetCmdQueryExchangeRate(),
GetCmdQueryLendAPY(),
GetCmdQueryBorrowAPY(),
GetCmdQueryMarketSize(),
GetCmdQueryTokenMarketSize(),
GetCmdQueryBorrowLimit(),
GetCmdQueryLiquidationLimit(),
GetCmdQueryLiquidationTargets(),
)

Expand Down Expand Up @@ -357,6 +359,43 @@ func GetCmdQueryCollateral() *cobra.Command {
return cmd
}

// GetCmdQueryCollateralValue returns a CLI command handler to query for the USD
// value of total collateral tokens for a given address.
func GetCmdQueryCollateralValue() *cobra.Command {
cmd := &cobra.Command{
Use: "collateral-value [addr]",
Args: cobra.ExactArgs(1),
Short: "Query for the total USD value of collateral tokens for an address",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryCollateralValueRequest{
Address: args[0],
}
if d, err := cmd.Flags().GetString(FlagDenom); len(d) > 0 && err == nil {
req.Denom = d
}

resp, err := queryClient.CollateralValue(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(resp)
},
}

cmd.Flags().String(FlagDenom, "", "Query for value of only a specific denomination")
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// GetCmdQueryExchangeRate returns a CLI command handler to query for the
// exchange rate of a specific uToken.
func GetCmdQueryExchangeRate() *cobra.Command {
Expand Down Expand Up @@ -588,6 +627,39 @@ func GetCmdQueryBorrowLimit() *cobra.Command {
return cmd
}

// GetCmdQueryLiquidationLimit returns a CLI command handler to query for the
// liquidation limit of a specific borrower.
func GetCmdQueryLiquidationLimit() *cobra.Command {
cmd := &cobra.Command{
Use: "liquidation-limit [addr]",
Args: cobra.ExactArgs(1),
Short: "Query for the liquidation limit of a specified borrower",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryLiquidationLimitRequest{
Address: args[0],
}

resp, err := queryClient.LiquidationLimit(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(resp)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// GetCmdQueryLiquidationTargets returns a CLI command handler to query for
// all eligible liquidation targets
func GetCmdQueryLiquidationTargets() *cobra.Command {
Expand Down
Loading