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: add historic pricing info to leverage queries #1711

Merged
merged 5 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- [1685](https://github.com/umee-network/umee/pull/1685) Add medians param to Token registry.
- [1683](https://github.com/umee-network/umee/pull/1683) Add MaxBorrow query and allow returning all denoms from MaxWithdraw.
- [1690](https://github.com/umee-network/umee/pull/1690) Add MaxBorrow message type.
- [1711](https://github.com/umee-network/umee/pull/1711) Add historic pricing information to leverage MarketSummary and AccountSummary queries.

## [v3.3.0](https://github.com/umee-network/umee/releases/tag/v3.3.0) - 2022-12-20

Expand Down
17 changes: 17 additions & 0 deletions proto/umee/leverage/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ message QueryMarketSummaryResponse {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
// Oracle Historic Price is the historic USD value of a token. Historic price is defined as the median of the last N historic median prices from the oracle module, with N being this token's HistoricMedians in the leverage registry. Current price is used if required medians is zero. Price is nil when the oracle is down or insufficient historic medians are available.
string oracle_historic_price = 19 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];
}

// QueryAccountBalances defines the request structure for the AccountBalances gRPC service handler.
Expand Down Expand Up @@ -240,6 +245,18 @@ message QueryAccountSummaryResponse {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// Historic Borrowed Value is the sum of the USD value of all tokens the account has borrowed,
// including interest owed, using historic prices.
string historic_borrowed_value = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// Historic Borrow Limit is the maximum Borrowed Value the account is allowed to reach through direct borrowing,
// using historic prices.
string historic_borrow_limit = 7 [
(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
47 changes: 47 additions & 0 deletions swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ paths:
description: >-
Liquidation Threshold is the Borrowed Value at which the
account becomes eligible for liquidation.
historic_borrowed_value:
type: string
description: >-
Historic Borrowed Value is the sum of the USD value of all
tokens the account has borrowed,

including interest owed, using historic prices.
historic_borrow_limit:
type: string
description: >-
Historic Borrow Limit is the maximum Borrowed Value the
account is allowed to reach through direct borrowing,

using historic prices.
description: >-
QueryAccountSummaryResponse defines the response structure for the
AccountSummary gRPC service handler.
Expand Down Expand Up @@ -439,6 +453,16 @@ paths:
applied to convert to symbol denom. A negative availability
means safety limits have been exceeded and additional
collateral cannot be created until more liquidity is present.
oracle_historic_price:
type: string
description: >-
Oracle Historic Price is the historic USD value of a token.
Historic price is defined as the median of the last N historic
median prices from the oracle module, with N being this
token's HistoricMedians in the leverage registry. Current
price is used if required medians is zero. Price is nil when
the oracle is down or insufficient historic medians are
available.
description: >-
QueryMarketSummaryResponse defines the response structure for the
MarketSummary gRPC service handler.
Expand Down Expand Up @@ -2020,6 +2044,20 @@ definitions:
description: >-
Liquidation Threshold is the Borrowed Value at which the account
becomes eligible for liquidation.
historic_borrowed_value:
type: string
description: >-
Historic Borrowed Value is the sum of the USD value of all tokens the
account has borrowed,

including interest owed, using historic prices.
historic_borrow_limit:
type: string
description: >-
Historic Borrow Limit is the maximum Borrowed Value the account is
allowed to reach through direct borrowing,

using historic prices.
description: >-
QueryAccountSummaryResponse defines the response structure for the
AccountSummary gRPC service handler.
Expand Down Expand Up @@ -2194,6 +2232,15 @@ definitions:
negative availability means safety limits have been exceeded and
additional collateral cannot be created until more liquidity is
present.
oracle_historic_price:
type: string
description: >-
Oracle Historic Price is the historic USD value of a token. Historic
price is defined as the median of the last N historic median prices
from the oracle module, with N being this token's HistoricMedians in
the leverage registry. Current price is used if required medians is
zero. Price is nil when the oracle is down or insufficient historic
medians are available.
description: >-
QueryMarketSummaryResponse defines the response structure for the
MarketSummary gRPC service handler.
Expand Down
13 changes: 9 additions & 4 deletions x/leverage/client/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ func (s *IntegrationTestSuite) TestLeverageScenario() {
false,
&types.QueryMarketSummaryResponse{},
&types.QueryMarketSummaryResponse{
SymbolDenom: "UMEE",
Exponent: 6,
OraclePrice: &oracleSymbolPrice,
UTokenExchangeRate: sdk.OneDec(),
SymbolDenom: "UMEE",
Exponent: 6,
OraclePrice: &oracleSymbolPrice,
OracleHistoricPrice: &oracleSymbolPrice,
UTokenExchangeRate: sdk.OneDec(),
// Borrow rate * (1.52 - ReserveFactor - OracleRewardFactor)
// 1.52 * (1 - 0.2 - 0.01) = 1.2008
Supply_APY: sdk.MustNewDecFromStr("1.2008"),
Expand Down Expand Up @@ -292,6 +293,10 @@ func (s *IntegrationTestSuite) TestLeverageScenario() {
BorrowedValue: sdk.MustNewDecFromStr("0.00858671"),
// (1001 / 1000000) * 34.21 * 0.25 = 0.0085610525
BorrowLimit: sdk.MustNewDecFromStr("0.0085610525"),
// (251 / 1000000) * 34.21 = 0.00858671
HistoricBorrowedValue: sdk.MustNewDecFromStr("0.00858671"),
// (1001 / 1000000) * 34.21 * 0.25 = 0.0085610525
HistoricBorrowLimit: sdk.MustNewDecFromStr("0.0085610525"),
// (1001 / 1000000) * 0.25 * 34.21 = 0.0085610525
LiquidationThreshold: sdk.MustNewDecFromStr("0.0085610525"),
},
Expand Down
21 changes: 15 additions & 6 deletions x/leverage/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,13 @@ func (q Querier) MarketSummary(
AvailableCollateralize: availableCollateralize,
}

// Oracle price in response will be nil if it is unavailable
// Oracle prices in response will be nil if it is unavailable
if oraclePrice, _, oracleErr := q.Keeper.TokenDefaultDenomPrice(ctx, req.Denom, false); oracleErr == nil {
resp.OraclePrice = &oraclePrice
}
if historicPrice, _, historicErr := q.Keeper.TokenDefaultDenomPrice(ctx, req.Denom, true); historicErr == nil {
resp.OracleHistoricPrice = &historicPrice
}

return &resp, nil
}
Expand Down Expand Up @@ -220,12 +223,18 @@ func (q Querier) AccountSummary(
return nil, err
}

// must still return current values if historic prices are down - they will display as zero
historicBorrowedValue, _ := q.Keeper.TotalTokenValue(ctx, borrowed, true)
historicBorrowLimit, _ := q.Keeper.CalculateBorrowLimit(ctx, collateral, true)

return &types.QueryAccountSummaryResponse{
SuppliedValue: suppliedValue,
CollateralValue: collateralValue,
BorrowedValue: borrowedValue,
BorrowLimit: borrowLimit,
LiquidationThreshold: liquidationThreshold,
SuppliedValue: suppliedValue,
CollateralValue: collateralValue,
BorrowedValue: borrowedValue,
BorrowLimit: borrowLimit,
LiquidationThreshold: liquidationThreshold,
HistoricBorrowedValue: historicBorrowedValue,
HistoricBorrowLimit: historicBorrowLimit,
}, nil
}

Expand Down
5 changes: 5 additions & 0 deletions x/leverage/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (s *IntegrationTestSuite) TestQuerier_MarketSummary() {
SymbolDenom: "UMEE",
Exponent: 6,
OraclePrice: &oracleSymbolPrice,
OracleHistoricPrice: &oracleSymbolPrice,
UTokenExchangeRate: sdk.OneDec(),
Supply_APY: sdk.MustNewDecFromStr("1.2008"),
Borrow_APY: sdk.MustNewDecFromStr("1.52"),
Expand Down Expand Up @@ -109,6 +110,10 @@ func (s *IntegrationTestSuite) TestQuerier_AccountSummary() {
BorrowedValue: sdk.ZeroDec(),
// (1000) * 4.21 * 0.25 = 1052.5
BorrowLimit: sdk.MustNewDecFromStr("1052.5"),
// Nothing borrowed
HistoricBorrowedValue: sdk.ZeroDec(),
// (1000) * 4.21 * 0.25 = 1052.5
HistoricBorrowLimit: sdk.MustNewDecFromStr("1052.5"),
// (1000) * 4.21 * 0.25 = 1052.5
LiquidationThreshold: sdk.MustNewDecFromStr("1052.5"),
}
Expand Down
Loading