Skip to content

Commit

Permalink
feat: add total-borrowed query (#1099)
Browse files Browse the repository at this point in the history
* feat: add total-borrowed query

* cl++

* spec++

* make proto-gen
  • Loading branch information
toteki authored Jul 5, 2022
1 parent ce71ea4 commit 7d45ca6
Show file tree
Hide file tree
Showing 7 changed files with 633 additions and 105 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- [918](https://github.com/umee-network/umee/pull/918) Add MarketSummary query to CLI.
- [1068](https://github.com/umee-network/umee/pull/1068) Add a cache layer for token registry.
- [1094](https://github.com/umee-network/umee/pull/1094) Added TotalCollateral query.
- [1099](https://github.com/umee-network/umee/pull/1099) Added TotalBorrowed query.

### Improvements

Expand Down
26 changes: 26 additions & 0 deletions proto/umee/leverage/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ service Query {
returns (QueryTotalCollateralResponse) {
option (google.api.http).get = "/umee/leverage/v1/total_collateral/{denom}";
}

// TotalBorrowed returns the total borrowed system-wide of a given
// token denomination.
rpc TotalBorrowed(QueryTotalBorrowedRequest)
returns (QueryTotalBorrowedResponse) {
option (google.api.http).get = "/umee/leverage/v1/total_borrowed/{denom}";
}
}

// QueryRegisteredTokens defines the request structure for the RegisteredTokens
Expand Down Expand Up @@ -448,6 +455,10 @@ message QueryMarketSummaryResponse {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string borrowed = 11 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

// QueryTotalCollateralRequest defines the request structure for the
Expand All @@ -463,4 +474,19 @@ message QueryTotalCollateralResponse {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

// QueryTotalBorrowedRequest defines the request structure for the
// TotalBorrowed gRPC service handler.
message QueryTotalBorrowedRequest {
string denom = 1;
}

// QueryTotalBorrowedResponse defines the response structure for the
// TotalBorrowed gRPC service handler.
message QueryTotalBorrowedResponse {
string amount = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}
36 changes: 34 additions & 2 deletions x/leverage/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
GetCmdQueryLiquidationTargets(),
GetCmdQueryMarketSummary(),
GetCmdQueryTotalCollateral(),
GetCmdQueryTotalBorrowed(),
)

return cmd
Expand Down Expand Up @@ -692,12 +693,12 @@ func GetCmdQueryLiquidationTargets() *cobra.Command {
}

// GetCmdQueryTotalCollateral creates a Cobra command to query for the
// reserved amount of a specific token.
// total collateral amount of a specific token.
func GetCmdQueryTotalCollateral() *cobra.Command {
cmd := &cobra.Command{
Use: "total-collateral [denom]",
Args: cobra.ExactArgs(1),
Short: "Query for the amount of collateral of a uToken denomination",
Short: "Query for the total amount of collateral of a uToken denomination",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
Expand All @@ -721,3 +722,34 @@ func GetCmdQueryTotalCollateral() *cobra.Command {

return cmd
}

// GetCmdQueryTotalBorrowed creates a Cobra command to query for the
// total borrowed amount of a specific token.
func GetCmdQueryTotalBorrowed() *cobra.Command {
cmd := &cobra.Command{
Use: "total-borrowed [denom]",
Args: cobra.ExactArgs(1),
Short: "Query for the total amount borrowed of a token denomination",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)
req := &types.QueryTotalBorrowedRequest{
Denom: args[0],
}
resp, err := queryClient.TotalBorrowed(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(resp)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
24 changes: 24 additions & 0 deletions x/leverage/client/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,18 @@ func (s *IntegrationTestSuite) TestLeverageScenario() {
),
},
},
testQuery{
"query total borrowed - denom",
cli.GetCmdQueryTotalBorrowed(),
[]string{
"uumee",
},
false,
&types.QueryTotalBorrowedResponse{},
&types.QueryTotalBorrowedResponse{
Amount: sdk.NewInt(47),
},
},
testQuery{
"query collateral - all",
cli.GetCmdQueryCollateral(),
Expand Down Expand Up @@ -529,6 +541,18 @@ func (s *IntegrationTestSuite) TestLeverageScenario() {
),
},
},
testQuery{
"query total collateral - denom",
cli.GetCmdQueryTotalCollateral(),
[]string{
"u/uumee",
},
false,
&types.QueryTotalCollateralResponse{},
&types.QueryTotalCollateralResponse{
Amount: sdk.NewInt(1000),
},
},
testQuery{
"query supplied value - all",
cli.GetCmdQuerySuppliedValue(),
Expand Down
13 changes: 13 additions & 0 deletions x/leverage/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,16 @@ func (q Querier) TotalCollateral(
collateral := q.Keeper.GetTotalCollateral(ctx, req.Denom)
return &types.QueryTotalCollateralResponse{Amount: collateral}, nil
}

func (q Querier) TotalBorrowed(
goCtx context.Context,
req *types.QueryTotalBorrowedRequest,
) (*types.QueryTotalBorrowedResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(goCtx)
borrowed := q.Keeper.GetTotalBorrowed(ctx, req.Denom)
return &types.QueryTotalBorrowedResponse{Amount: borrowed.Amount}, nil
}
1 change: 1 addition & 0 deletions x/leverage/spec/03_queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Queries on accepted asset types:
- **Borrow APY** queries for the [Borrow APY](01_concepts.md#Borrow-APY) of a specified denomination.
- **Supply APY** queries for the [Supplying APY](01_concepts.md#Supplying-APY) of a specified denomination.
- **Reserve Amount** queries for the amount reserved of a specified denomination.
- **Total Borrowed** queries for the total borrowed amount of a specified token denomination.
- **Total Collateral** queries for the total collateral amount of a specified uToken denomination.
- **Exchange Rate** queries the [uToken Exchange Rate](01_concepts.md#uToken-Exchange-Rate) of a given uToken denomination.
- **Market Size** queries the [Market Size](01_concepts.md#Market-Size) of a specified denomination.
Expand Down
Loading

0 comments on commit 7d45ca6

Please sign in to comment.