2019-11-27 14:45:59 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/pricefeed/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetParams gets params from the store
|
|
|
|
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
|
2019-12-04 16:32:08 +00:00
|
|
|
return types.NewParams(k.GetMarketParams(ctx))
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetParams updates params in the store
|
|
|
|
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
|
|
|
|
k.paramstore.SetParamSet(ctx, ¶ms)
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:32:08 +00:00
|
|
|
// GetMarketParams get asset params from store
|
|
|
|
func (k Keeper) GetMarketParams(ctx sdk.Context) types.Markets {
|
|
|
|
var markets types.Markets
|
|
|
|
k.paramstore.Get(ctx, types.KeyMarkets, &markets)
|
|
|
|
return markets
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOracles returns the oracles in the pricefeed store
|
2019-12-04 16:32:08 +00:00
|
|
|
func (k Keeper) GetOracles(ctx sdk.Context, marketID string) (types.Oracles, error) {
|
2019-11-27 14:45:59 +00:00
|
|
|
|
2019-12-04 16:32:08 +00:00
|
|
|
for _, m := range k.GetMarketParams(ctx) {
|
|
|
|
if marketID == m.MarketID {
|
|
|
|
return m.Oracles, nil
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:32:08 +00:00
|
|
|
return types.Oracles{}, fmt.Errorf("asset %s not found", marketID)
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOracle returns the oracle from the store or an error if not found
|
2019-12-04 16:32:08 +00:00
|
|
|
func (k Keeper) GetOracle(ctx sdk.Context, marketID string, address sdk.AccAddress) (types.Oracle, error) {
|
|
|
|
oracles, err := k.GetOracles(ctx, marketID)
|
2019-11-27 14:45:59 +00:00
|
|
|
if err != nil {
|
2019-12-04 16:32:08 +00:00
|
|
|
return types.Oracle{}, fmt.Errorf("asset %s not found", marketID)
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
for _, o := range oracles {
|
|
|
|
if address.Equals(o.Address) {
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:32:08 +00:00
|
|
|
return types.Oracle{}, fmt.Errorf("oracle %s not found for asset %s", address, marketID)
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 16:32:08 +00:00
|
|
|
// GetMarket returns the market if it is in the pricefeed system
|
|
|
|
func (k Keeper) GetMarket(ctx sdk.Context, marketID string) (types.Market, bool) {
|
|
|
|
markets := k.GetMarketParams(ctx)
|
2019-11-27 14:45:59 +00:00
|
|
|
|
2019-12-04 16:32:08 +00:00
|
|
|
for i := range markets {
|
|
|
|
if markets[i].MarketID == marketID {
|
|
|
|
return markets[i], true
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:32:08 +00:00
|
|
|
return types.Market{}, false
|
2019-11-27 14:45:59 +00:00
|
|
|
|
|
|
|
}
|