2019-11-27 14:45:59 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-23 16:35:58 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-01-17 12:29:19 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params/subspace"
|
2019-11-27 14:45:59 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/pricefeed/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper struct for pricefeed module
|
|
|
|
type Keeper struct {
|
2020-01-17 12:29:19 +00:00
|
|
|
// key used to access the stores from Context
|
|
|
|
key sdk.StoreKey
|
2019-11-27 14:45:59 +00:00
|
|
|
// Codec for binary encoding/decoding
|
|
|
|
cdc *codec.Codec
|
|
|
|
// The reference to the Paramstore to get and set pricefeed specific params
|
2020-01-17 12:29:19 +00:00
|
|
|
paramSubspace subspace.Subspace
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 12:29:19 +00:00
|
|
|
// NewKeeper returns a new keeper for the pricefeed module.
|
2019-11-27 14:45:59 +00:00
|
|
|
func NewKeeper(
|
2020-04-23 16:35:58 +00:00
|
|
|
cdc *codec.Codec, key sdk.StoreKey, paramstore subspace.Subspace,
|
2019-11-27 14:45:59 +00:00
|
|
|
) Keeper {
|
2020-04-23 16:35:58 +00:00
|
|
|
if !paramstore.HasKeyTable() {
|
|
|
|
paramstore = paramstore.WithKeyTable(types.ParamKeyTable())
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:45:59 +00:00
|
|
|
return Keeper{
|
2020-01-17 12:29:19 +00:00
|
|
|
cdc: cdc,
|
2020-04-23 16:35:58 +00:00
|
|
|
key: key,
|
|
|
|
paramSubspace: paramstore,
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPrice updates the posted price for a specific oracle
|
|
|
|
func (k Keeper) SetPrice(
|
|
|
|
ctx sdk.Context,
|
|
|
|
oracle sdk.AccAddress,
|
2019-12-04 16:32:08 +00:00
|
|
|
marketID string,
|
2019-11-27 14:45:59 +00:00
|
|
|
price sdk.Dec,
|
2020-04-23 16:35:58 +00:00
|
|
|
expiry time.Time) (types.PostedPrice, error) {
|
2019-11-27 14:45:59 +00:00
|
|
|
// If the expiry is less than or equal to the current blockheight, we consider the price valid
|
2020-04-23 16:35:58 +00:00
|
|
|
if !expiry.After(ctx.BlockTime()) {
|
|
|
|
return types.PostedPrice{}, types.ErrExpired
|
|
|
|
}
|
|
|
|
|
|
|
|
store := ctx.KVStore(k.key)
|
|
|
|
prices, err := k.GetRawPrices(ctx, marketID)
|
|
|
|
if err != nil {
|
|
|
|
return types.PostedPrice{}, err
|
|
|
|
}
|
|
|
|
var index int
|
|
|
|
found := false
|
|
|
|
for i := range prices {
|
|
|
|
if prices[i].OracleAddress.Equals(oracle) {
|
|
|
|
index = i
|
|
|
|
found = true
|
|
|
|
break
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
}
|
2019-11-27 14:45:59 +00:00
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
// set the price for that particular oracle
|
|
|
|
if found {
|
|
|
|
prices[index] = types.NewPostedPrice(marketID, oracle, price, expiry)
|
|
|
|
} else {
|
|
|
|
prices = append(prices, types.NewPostedPrice(marketID, oracle, price, expiry))
|
|
|
|
index = len(prices) - 1
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
// Emit an event containing the oracle's new price
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeOracleUpdatedPrice,
|
|
|
|
sdk.NewAttribute(types.AttributeMarketID, marketID),
|
|
|
|
sdk.NewAttribute(types.AttributeOracle, oracle.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeMarketPrice, price.String()),
|
|
|
|
sdk.NewAttribute(types.AttributeExpiry, expiry.UTC().String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
store.Set(types.RawPriceKey(marketID), k.cdc.MustMarshalBinaryBare(prices))
|
|
|
|
return prices[index], nil
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 18:07:42 +00:00
|
|
|
// SetCurrentPrices updates the price of an asset to the median of all valid oracle inputs
|
2020-04-23 16:35:58 +00:00
|
|
|
func (k Keeper) SetCurrentPrices(ctx sdk.Context, marketID string) error {
|
2019-12-04 16:32:08 +00:00
|
|
|
_, ok := k.GetMarket(ctx, marketID)
|
2019-11-27 14:45:59 +00:00
|
|
|
if !ok {
|
2020-04-23 16:35:58 +00:00
|
|
|
return sdkerrors.Wrap(types.ErrInvalidMarket, marketID)
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
2020-01-27 18:07:42 +00:00
|
|
|
// store current price
|
|
|
|
validPrevPrice := true
|
|
|
|
prevPrice, err := k.GetCurrentPrice(ctx, marketID)
|
|
|
|
if err != nil {
|
|
|
|
validPrevPrice = false
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:14 +00:00
|
|
|
prices, err := k.GetRawPrices(ctx, marketID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-06 13:56:59 +00:00
|
|
|
var notExpiredPrices types.CurrentPrices
|
2019-11-27 14:45:59 +00:00
|
|
|
// filter out expired prices
|
|
|
|
for _, v := range prices {
|
|
|
|
if v.Expiry.After(ctx.BlockTime()) {
|
2020-04-06 13:56:59 +00:00
|
|
|
notExpiredPrices = append(notExpiredPrices, types.NewCurrentPrice(v.MarketID, v.Price))
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-12 15:35:34 +00:00
|
|
|
if len(notExpiredPrices) == 0 {
|
2020-01-17 12:29:19 +00:00
|
|
|
store := ctx.KVStore(k.key)
|
2020-01-12 15:35:34 +00:00
|
|
|
store.Set(
|
2020-04-06 13:56:59 +00:00
|
|
|
types.CurrentPriceKey(marketID), k.cdc.MustMarshalBinaryBare(types.CurrentPrice{}),
|
2020-01-12 15:35:34 +00:00
|
|
|
)
|
2020-04-23 16:35:58 +00:00
|
|
|
return types.ErrNoValidPrice
|
2019-12-04 16:32:08 +00:00
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
medianPrice := k.CalculateMedianPrice(ctx, notExpiredPrices)
|
2019-12-04 16:32:08 +00:00
|
|
|
|
2020-01-27 18:07:42 +00:00
|
|
|
// check case that market price was not set in genesis
|
|
|
|
if validPrevPrice {
|
|
|
|
// only emit event if price has changed
|
|
|
|
if !medianPrice.Equal(prevPrice.Price) {
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeMarketPriceUpdated,
|
2020-04-23 16:35:58 +00:00
|
|
|
sdk.NewAttribute(types.AttributeMarketID, marketID),
|
|
|
|
sdk.NewAttribute(types.AttributeMarketPrice, medianPrice.String()),
|
2020-01-27 18:07:42 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:29:19 +00:00
|
|
|
store := ctx.KVStore(k.key)
|
2020-04-06 13:56:59 +00:00
|
|
|
currentPrice := types.NewCurrentPrice(marketID, medianPrice)
|
2020-01-27 18:07:42 +00:00
|
|
|
|
2019-12-04 16:32:08 +00:00
|
|
|
store.Set(
|
2020-04-06 13:56:59 +00:00
|
|
|
types.CurrentPriceKey(marketID), k.cdc.MustMarshalBinaryBare(currentPrice),
|
2019-12-04 16:32:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateMedianPrice calculates the median prices for the input prices.
|
2020-04-06 13:56:59 +00:00
|
|
|
func (k Keeper) CalculateMedianPrice(ctx sdk.Context, prices types.CurrentPrices) sdk.Dec {
|
2019-12-04 16:32:08 +00:00
|
|
|
l := len(prices)
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
if l == 1 {
|
2019-11-27 14:45:59 +00:00
|
|
|
// Return immediately if there's only one price
|
2020-01-12 15:35:34 +00:00
|
|
|
return prices[0].Price
|
|
|
|
}
|
|
|
|
// sort the prices
|
|
|
|
sort.Slice(prices, func(i, j int) bool {
|
|
|
|
return prices[i].Price.LT(prices[j].Price)
|
|
|
|
})
|
|
|
|
// for even numbers of prices, the median is calculated as the mean of the two middle prices
|
|
|
|
if l%2 == 0 {
|
|
|
|
median := k.calculateMeanPrice(ctx, prices[l/2-1:l/2+1])
|
|
|
|
return median
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
2020-01-12 15:35:34 +00:00
|
|
|
// for odd numbers of prices, return the middle element
|
|
|
|
return prices[l/2].Price
|
|
|
|
|
2019-12-04 16:32:08 +00:00
|
|
|
}
|
2019-11-27 14:45:59 +00:00
|
|
|
|
2020-04-06 13:56:59 +00:00
|
|
|
func (k Keeper) calculateMeanPrice(ctx sdk.Context, prices types.CurrentPrices) sdk.Dec {
|
2019-12-04 16:32:08 +00:00
|
|
|
sum := prices[0].Price.Add(prices[1].Price)
|
|
|
|
mean := sum.Quo(sdk.NewDec(2))
|
|
|
|
return mean
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 12:29:19 +00:00
|
|
|
// GetCurrentPrice fetches the current median price of all oracles for a specific market
|
2020-04-23 16:35:58 +00:00
|
|
|
func (k Keeper) GetCurrentPrice(ctx sdk.Context, marketID string) (types.CurrentPrice, error) {
|
2020-01-17 12:29:19 +00:00
|
|
|
store := ctx.KVStore(k.key)
|
2020-04-06 13:56:59 +00:00
|
|
|
bz := store.Get(types.CurrentPriceKey(marketID))
|
2020-01-12 15:35:34 +00:00
|
|
|
|
|
|
|
if bz == nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return types.CurrentPrice{}, types.ErrNoValidPrice
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
2019-11-27 14:45:59 +00:00
|
|
|
var price types.CurrentPrice
|
2020-04-13 18:08:14 +00:00
|
|
|
err := k.cdc.UnmarshalBinaryBare(bz, &price)
|
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return types.CurrentPrice{}, err
|
2020-04-13 18:08:14 +00:00
|
|
|
}
|
2020-01-12 15:35:34 +00:00
|
|
|
if price.Price.Equal(sdk.ZeroDec()) {
|
2020-04-23 16:35:58 +00:00
|
|
|
return types.CurrentPrice{}, types.ErrNoValidPrice
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
return price, nil
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawPrices fetches the set of all prices posted by oracles for an asset
|
2020-04-23 16:35:58 +00:00
|
|
|
func (k Keeper) GetRawPrices(ctx sdk.Context, marketID string) (types.PostedPrices, error) {
|
2020-01-17 12:29:19 +00:00
|
|
|
store := ctx.KVStore(k.key)
|
2020-04-06 13:56:59 +00:00
|
|
|
bz := store.Get(types.RawPriceKey(marketID))
|
2020-04-13 18:08:14 +00:00
|
|
|
if bz == nil {
|
|
|
|
return types.PostedPrices{}, nil
|
|
|
|
}
|
2020-04-06 13:56:59 +00:00
|
|
|
var prices types.PostedPrices
|
2020-04-13 18:08:14 +00:00
|
|
|
err := k.cdc.UnmarshalBinaryBare(bz, &prices)
|
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return types.PostedPrices{}, err
|
2020-04-13 18:08:14 +00:00
|
|
|
}
|
|
|
|
return prices, nil
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|