Skip to content

Commit

Permalink
feat: add historic pricing info to leverage queries (#1711)
Browse files Browse the repository at this point in the history
## Description

closes: #XXXX

---

### Author Checklist

_All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues._

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] added appropriate labels to the PR
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/umee-network/umee/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

_All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items._

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
toteki authored Jan 15, 2023
1 parent 63251f4 commit 6edb0f1
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 99 deletions.
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

0 comments on commit 6edb0f1

Please sign in to comment.