Skip to content

Commit

Permalink
new query fields and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
toteki committed Sep 22, 2023
1 parent dc564e6 commit 0ef0011
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 141 deletions.
19 changes: 18 additions & 1 deletion proto/umee/leverage/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,23 @@ message QueryAccountSummary {
// QueryAccountSummaryResponse defines the response structure for the AccountSummary gRPC service handler.
message QueryAccountSummaryResponse {
// Supplied Value is the sum of the USD value of all tokens the account has supplied, including interest earned.
// It uses the lower of spot or historic price for each token.
// Computation skips assets which are missing oracle prices, potentially resulting in a lower supplied
// value than if prices were all available.
string supplied_value = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// Collateral Value is the sum of the USD value of all uTokens the account has collateralized.
// It uses the lower of spot or historic price for each token.
// Computation skips collateral which is missing an oracle price, potentially resulting in a lower collateral
// value than if prices were all available.
string collateral_value = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// Borrowed Value is the sum of the USD value of all tokens the account has borrowed, including interest owed.
// It always uses spot prices.
// It uses the higher of spot or historic price for each token.
// Computation skips borrows which are missing oracle prices, potentially resulting in a lower borrowed
// value than if prices were all available.
string borrowed_value = 3 [
Expand All @@ -295,6 +297,21 @@ message QueryAccountSummaryResponse {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];
// Spot Supplied Value is supplied value but always uses spot prices.
string spot_supplied_value = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// Spot Collateral Value is collateral value but always uses spot prices.
string spot_collateral_value = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// Spot Borrowed Value is borrowed value but always uses spot prices.
string spot_borrowed_value = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

// QueryLiquidationTargets defines the request structure for the LiquidationTargets gRPC service handler.
Expand Down
36 changes: 34 additions & 2 deletions swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ paths:
Supplied Value is the sum of the USD value of all tokens the
account has supplied, including interest earned.
It uses the lower of spot or historic price for each token.
Computation skips assets which are missing oracle prices,
potentially resulting in a lower supplied
Expand All @@ -143,6 +145,8 @@ paths:
Collateral Value is the sum of the USD value of all uTokens
the account has collateralized.
It uses the lower of spot or historic price for each token.
Computation skips collateral which is missing an oracle price,
potentially resulting in a lower collateral
Expand All @@ -153,7 +157,7 @@ paths:
Borrowed Value is the sum of the USD value of all tokens the
account has borrowed, including interest owed.
It always uses spot prices.
It uses the higher of spot or historic price for each token.
Computation skips borrows which are missing oracle prices,
potentially resulting in a lower borrowed
Expand All @@ -180,6 +184,21 @@ paths:
Will be null if an oracle price required for computation is
missing.
spot_supplied_value:
type: string
description: >-
Spot Supplied Value is supplied value but always uses spot
prices.
spot_collateral_value:
type: string
description: >-
Spot Collateral Value is collateral value but always uses spot
prices.
spot_borrowed_value:
type: string
description: >-
Spot Borrowed Value is borrowed value but always uses spot
prices.
description: >-
QueryAccountSummaryResponse defines the response structure for the
AccountSummary gRPC service handler.
Expand Down Expand Up @@ -4696,6 +4715,8 @@ definitions:
Supplied Value is the sum of the USD value of all tokens the account
has supplied, including interest earned.
It uses the lower of spot or historic price for each token.
Computation skips assets which are missing oracle prices, potentially
resulting in a lower supplied
Expand All @@ -4706,6 +4727,8 @@ definitions:
Collateral Value is the sum of the USD value of all uTokens the
account has collateralized.
It uses the lower of spot or historic price for each token.
Computation skips collateral which is missing an oracle price,
potentially resulting in a lower collateral
Expand All @@ -4716,7 +4739,7 @@ definitions:
Borrowed Value is the sum of the USD value of all tokens the account
has borrowed, including interest owed.
It always uses spot prices.
It uses the higher of spot or historic price for each token.
Computation skips borrows which are missing oracle prices, potentially
resulting in a lower borrowed
Expand All @@ -4742,6 +4765,15 @@ definitions:
becomes eligible for liquidation.
Will be null if an oracle price required for computation is missing.
spot_supplied_value:
type: string
description: Spot Supplied Value is supplied value but always uses spot prices.
spot_collateral_value:
type: string
description: Spot Collateral Value is collateral value but always uses spot prices.
spot_borrowed_value:
type: string
description: Spot Borrowed Value is borrowed value but always uses spot prices.
description: >-
QueryAccountSummaryResponse defines the response structure for the
AccountSummary gRPC service handler.
Expand Down
53 changes: 31 additions & 22 deletions x/leverage/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,47 +237,56 @@ func (q Querier) AccountSummary(
collateral := q.Keeper.GetBorrowerCollateral(ctx, addr)
borrowed := q.Keeper.GetBorrowerBorrows(ctx, addr)

// supplied value always uses spot prices, and skips supplied assets that are missing prices
suppliedValue, err := q.Keeper.VisibleTokenValue(ctx, supplied, types.PriceModeSpot)
// the following spot price calculations skip assets missing prices, but otherwise always
// use the most up to date prices
spotSuppliedValue, err := q.Keeper.VisibleTokenValue(ctx, supplied, types.PriceModeSpot)
if err != nil {
return nil, err
}

// borrowed value uses spot prices here, but leverage logic instead uses
// the higher of spot or historic prices for each borrowed token when comparing it
// to borrow limit. This line also skips borrowed assets that are missing prices.
borrowedValue, err := q.Keeper.VisibleTokenValue(ctx, borrowed, types.PriceModeSpot)
spotBorrowedValue, err := q.Keeper.VisibleTokenValue(ctx, borrowed, types.PriceModeSpot)
if err != nil {
return nil, err
}

// collateral value always uses spot prices, and this line skips assets that are missing prices
collateralValue, err := q.Keeper.VisibleCollateralValue(ctx, collateral, types.PriceModeSpot)
spotCollateralValue, err := q.Keeper.VisibleCollateralValue(ctx, collateral, types.PriceModeSpot)
if err != nil {
return nil, err
}

// borrow limit shown here as it is used in leverage logic:
// using the lower of spot or historic prices for each collateral token
// skips collateral tokens with missing oracle prices
ap, err := q.Keeper.GetAccountPosition(ctx, addr, false)
// this supplied value uses leverage-like prices: the lower of spot or historic price for supplied tokens
suppliedValue, err := q.Keeper.VisibleTokenValue(ctx, supplied, types.PriceModeLow)
if err != nil {
return nil, err
}
borrowLimit := ap.Limit()

resp := &types.QueryAccountSummaryResponse{
SuppliedValue: suppliedValue,
CollateralValue: collateralValue,
BorrowedValue: borrowedValue,
BorrowLimit: borrowLimit,
SuppliedValue: suppliedValue,
SpotSuppliedValue: spotSuppliedValue,
SpotCollateralValue: spotCollateralValue,
SpotBorrowedValue: spotBorrowedValue,
}

// borrow limit shown here as it is used in leverage logic:
// values computed from position use the same prices found in leverage logic:
// using the lower of spot or historic prices for each collateral token
// error on collateral tokens with missing oracle prices, but skips
// borrow tokens missing collateral prices
// and the higher of spot or historic prices for each borrowed token
// skips collateral tokens with missing prices, but errors on borrow tokens missing prices
// (for oracle errors only the relevant response fields will be left nil)
ap, err := q.Keeper.GetAccountPosition(ctx, addr, false)
if nonOracleError(err) {
return nil, err
}
if err == nil {
resp.BorrowedValue = ap.BorrowedValue()
resp.CollateralValue = ap.CollateralValue()
resp.BorrowLimit = ap.Limit()
}

// liquidation threshold shown here as it is used in leverage logic: using spot prices.
// skips borrowed tokens with missing prices, but errors on collateral missing prices
// (for oracle errors only the relevant response fields will be left nil)
ap, err = q.Keeper.GetAccountPosition(ctx, addr, true)
if nonOracleError(err) {
return nil, err
}
if err == nil {
// on an error here, simply skip setting the response field
liquidationThreshold := ap.Limit()
Expand Down
Loading

0 comments on commit 0ef0011

Please sign in to comment.