Skip to content

Commit

Permalink
feat(vpool): expose markPrice, markTwap, indexPrice, (and blockHeight…
Browse files Browse the repository at this point in the history
…?) on the all-pools query #789 (#813)

* (vpool): add logger to keeper.go

* feat: GetPoolPrices, fn to return the mark price, mark twap, index price, and swap inv. for a pool

* docs: fix the fn description for GetCurrentTWAP

* feat(vpool): implemenet PoolPrices and add it to 'nibid q vpool all-pools' command

* update changelog

* (vpool): add block number to all-pools.prices. Fix changelog

Co-authored-by: AgentSmithMatrix <[email protected]>
Co-authored-by: Agent Smith <[email protected]>
  • Loading branch information
3 people authored Aug 10, 2022
1 parent 674da1b commit 75e8f03
Show file tree
Hide file tree
Showing 9 changed files with 571 additions and 83 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Features

* [#791](https://github.com/NibiruChain/nibiru/pull/791) Add the x/oracle module
- [#813](https://github.com/NibiruChain/nibiru/pull/813) - (vpool): Expose mark price, mark TWAP, index price, and k (swap invariant) in the all-pools query
* [#810](https://github.com/NibiruChain/nibiru/pull/810) - feat(x/perp): expose 'marginRatioIndex' and block number on QueryTraderPosition

## [v0.12.1](https://github.com/NibiruChain/nibiru/releases/tag/v0.12.1) - 2022-08-04
Expand Down
3 changes: 3 additions & 0 deletions proto/vpool/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ message QueryAllPoolsRequest {

message QueryAllPoolsResponse {
repeated Pool pools = 1;
repeated PoolPrices prices = 2 [
(gogoproto.nullable) = false
];
}

// ---------------------------------------- BaseAssetPrice
Expand Down
37 changes: 34 additions & 3 deletions proto/vpool/v1/vpool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,42 @@ message Pool {
string maintenance_margin_ratio = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
];

// max_leverage
string max_leverage = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}
];
}

message PoolPrices {
// MarkPrice is the instantaneous price of the perp.
// Equivalent to quoteAssetReserve / baseAssetReserve.
string mark_price = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

// IndexPrice is the price of the "underlying" for the perp
string index_price = 11 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

// TwapMark is the time-weighted average (mark) price.
string twap_mark = 12 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

// SwapInvariant is the product of the reserves, commonly referred to as "k".
string swap_invariant = 13 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"swap_invariant\"",
(gogoproto.nullable) = false
];

// The block number corresponding to each price
int64 block_number = 14;
}
5 changes: 5 additions & 0 deletions x/vpool/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/libs/log"

"github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/vpool/types"
Expand All @@ -28,6 +29,10 @@ type Keeper struct {
pricefeedKeeper types.PricefeedKeeper
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}

/*
Trades baseAssets in exchange for quoteAssets.
The base asset is a crypto asset like BTC.
Expand Down
21 changes: 21 additions & 0 deletions x/vpool/keeper/pool_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,24 @@ func (k Keeper) GetAllPools(ctx sdk.Context) []*types.Pool {

return pools
}

// GetPoolPrices returns the mark price, twap (mark) price, and index price for a vpool.
func (k Keeper) GetPoolPrices(ctx sdk.Context, pool types.Pool) types.PoolPrices {
indexPrice, err := k.GetUnderlyingPrice(ctx, pool.Pair)
if err != nil {
// fail gracefully so that vpool queries run even if the oracle price feeds stop
k.Logger(ctx).Error(err.Error())
}
twapMark, err := k.GetCurrentTWAP(ctx, pool.Pair)
if err != nil {
// fail gracefully so that vpool queries run even if the TWAP is undefined.
k.Logger(ctx).Error(err.Error())
}
return types.PoolPrices{
MarkPrice: pool.QuoteAssetReserve.Quo(pool.BaseAssetReserve),
IndexPrice: indexPrice,
TwapMark: twapMark.Price,
SwapInvariant: pool.BaseAssetReserve.Mul(pool.QuoteAssetReserve).RoundInt(),
BlockNumber: ctx.BlockHeight(),
}
}
7 changes: 5 additions & 2 deletions x/vpool/keeper/prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,11 @@ func (k Keeper) calcTwap(
return cumulativePrice.QuoInt64(cumulativePeriodMs), nil
}

// GetCurrentTWAP fetches the current median price of all oracles for a specific market
func (k Keeper) GetCurrentTWAP(ctx sdk.Context, pair common.AssetPair) (types.CurrentTWAP, error) {
// GetCurrentTWAP fetches the instantaneous time-weighted average (mark) price
// for the given asset pair.
func (k Keeper) GetCurrentTWAP(
ctx sdk.Context, pair common.AssetPair,
) (types.CurrentTWAP, error) {
// Ensure we still have valid prices
_, err := k.GetSpotPrice(ctx, pair)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion x/vpool/keeper/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ func (q queryServer) AllPools(
if err != nil {
return nil, err
}
var pricesForPools []types.PoolPrices
for _, pool := range pools {
pricesForPools = append(pricesForPools, q.GetPoolPrices(ctx, *pool))
}

return &types.QueryAllPoolsResponse{
Pools: pools,
Pools: pools,
Prices: pricesForPools,
}, nil
}

Expand Down
138 changes: 101 additions & 37 deletions x/vpool/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 75e8f03

Please sign in to comment.