0g-chain/x/pricefeed/keeper/keeper.go

169 lines
4.8 KiB
Go
Raw Normal View History

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"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/kava-labs/kava/x/pricefeed/types"
)
// Keeper struct for pricefeed module
type Keeper struct {
// The keys used to access the stores from Context
storeKey sdk.StoreKey
// Codec for binary encoding/decoding
cdc *codec.Codec
// The reference to the Paramstore to get and set pricefeed specific params
paramstore params.Subspace
// Reserved codespace
codespace sdk.CodespaceType
}
// NewKeeper returns a new keeper for the pricefeed module. It handles:
// - adding oracles
// - adding/removing assets from the pricefeed
func NewKeeper(
cdc *codec.Codec, storeKey sdk.StoreKey, paramstore params.Subspace, codespace sdk.CodespaceType,
2019-11-27 14:45:59 +00:00
) Keeper {
return Keeper{
paramstore: paramstore.WithKeyTable(types.ParamKeyTable()),
storeKey: storeKey,
cdc: cdc,
codespace: codespace,
}
}
// 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,
expiry time.Time) (types.PostedPrice, sdk.Error) {
// If the expiry is less than or equal to the current blockheight, we consider the price valid
if expiry.After(ctx.BlockTime()) {
store := ctx.KVStore(k.storeKey)
2019-12-04 16:32:08 +00:00
prices := k.GetRawPrices(ctx, marketID)
2019-11-27 14:45:59 +00:00
var index int
found := false
for i := range prices {
if prices[i].OracleAddress.Equals(oracle) {
index = i
found = true
break
}
}
// set the price for that particular oracle
if found {
prices[index] = types.PostedPrice{
2019-12-04 16:32:08 +00:00
MarketID: marketID, OracleAddress: oracle,
2019-11-27 14:45:59 +00:00
Price: price, Expiry: expiry}
} else {
prices = append(prices, types.PostedPrice{
2019-12-04 16:32:08 +00:00
MarketID: marketID, OracleAddress: oracle,
2019-11-27 14:45:59 +00:00
Price: price, Expiry: expiry})
index = len(prices) - 1
}
store.Set(
2019-12-04 16:32:08 +00:00
[]byte(types.RawPriceFeedPrefix+marketID), k.cdc.MustMarshalBinaryBare(prices),
2019-11-27 14:45:59 +00:00
)
return prices[index], nil
}
return types.PostedPrice{}, types.ErrExpired(k.codespace)
}
// SetCurrentPrices updates the price of an asset to the meadian of all valid oracle inputs
2019-12-04 16:32:08 +00:00
func (k Keeper) SetCurrentPrices(ctx sdk.Context, marketID string) sdk.Error {
_, ok := k.GetMarket(ctx, marketID)
2019-11-27 14:45:59 +00:00
if !ok {
return types.ErrInvalidAsset(k.codespace)
}
2019-12-04 16:32:08 +00:00
prices := k.GetRawPrices(ctx, marketID)
2019-11-27 14:45:59 +00:00
var notExpiredPrices []types.CurrentPrice
// filter out expired prices
for _, v := range prices {
if v.Expiry.After(ctx.BlockTime()) {
notExpiredPrices = append(notExpiredPrices, types.CurrentPrice{
2019-12-04 16:32:08 +00:00
MarketID: v.MarketID,
Price: v.Price,
2019-11-27 14:45:59 +00:00
})
}
}
2019-12-04 16:32:08 +00:00
medianPrice, err := k.CalculateMedianPrice(ctx, notExpiredPrices)
if err != nil {
return err
}
store := ctx.KVStore(k.storeKey)
currentPrice := types.CurrentPrice{
MarketID: marketID,
Price: medianPrice,
}
store.Set(
[]byte(types.CurrentPricePrefix+marketID), k.cdc.MustMarshalBinaryBare(currentPrice),
)
return nil
}
// CalculateMedianPrice calculates the median prices for the input prices.
func (k Keeper) CalculateMedianPrice(ctx sdk.Context, prices []types.CurrentPrice) (sdk.Dec, sdk.Error) {
l := len(prices)
2019-11-27 14:45:59 +00:00
if l == 0 {
// Error if there are no valid prices in the raw pricefeed
2019-12-04 16:32:08 +00:00
return sdk.Dec{}, types.ErrNoValidPrice(k.codespace)
2019-11-27 14:45:59 +00:00
} else if l == 1 {
// Return immediately if there's only one price
2019-12-04 16:32:08 +00:00
return prices[0].Price, nil
2019-11-27 14:45:59 +00:00
} else {
// sort the prices
2019-12-04 16:32:08 +00:00
sort.Slice(prices, func(i, j int) bool {
return prices[i].Price.LT(prices[j].Price)
2019-11-27 14:45:59 +00:00
})
2019-12-04 16:32:08 +00:00
// for even numbers of prices, the median is calculated as the mean of the two middle prices
2019-11-27 14:45:59 +00:00
if l%2 == 0 {
2019-12-04 16:32:08 +00:00
median := k.calculateMeanPrice(ctx, prices[l/2-1:l/2+1])
return median, nil
2019-11-27 14:45:59 +00:00
}
2019-12-04 16:32:08 +00:00
// for odd numbers of prices, return the middle element
return prices[l/2].Price, nil
2019-11-27 14:45:59 +00:00
}
2019-12-04 16:32:08 +00:00
}
2019-11-27 14:45:59 +00:00
2019-12-04 16:32:08 +00:00
func (k Keeper) calculateMeanPrice(ctx sdk.Context, prices []types.CurrentPrice) sdk.Dec {
sum := prices[0].Price.Add(prices[1].Price)
mean := sum.Quo(sdk.NewDec(2))
return mean
2019-11-27 14:45:59 +00:00
}
// GetCurrentPrice fetches the current median price of all oracles for a specific asset
2019-12-04 16:32:08 +00:00
func (k Keeper) GetCurrentPrice(ctx sdk.Context, marketID string) types.CurrentPrice {
2019-11-27 14:45:59 +00:00
store := ctx.KVStore(k.storeKey)
2019-12-04 16:32:08 +00:00
bz := store.Get([]byte(types.CurrentPricePrefix + marketID))
2019-11-27 14:45:59 +00:00
// TODO panic or return error if not found
var price types.CurrentPrice
k.cdc.MustUnmarshalBinaryBare(bz, &price)
return price
}
// GetRawPrices fetches the set of all prices posted by oracles for an asset
2019-12-04 16:32:08 +00:00
func (k Keeper) GetRawPrices(ctx sdk.Context, marketID string) []types.PostedPrice {
2019-11-27 14:45:59 +00:00
store := ctx.KVStore(k.storeKey)
2019-12-04 16:32:08 +00:00
bz := store.Get([]byte(types.RawPriceFeedPrefix + marketID))
2019-11-27 14:45:59 +00:00
var prices []types.PostedPrice
k.cdc.MustUnmarshalBinaryBare(bz, &prices)
return prices
}
2019-12-04 16:32:08 +00:00
// Codespace return the codespace for the keeper
2019-11-27 14:45:59 +00:00
func (k Keeper) Codespace() sdk.CodespaceType {
return k.codespace
}