2019-11-27 14:45:59 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2023-04-05 23:21:59 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
2019-11-27 14:45:59 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/pricefeed/types"
|
|
|
|
)
|
|
|
|
|
2020-01-17 12:29:19 +00:00
|
|
|
// GetParams returns the params from the store
|
2019-11-27 14:45:59 +00:00
|
|
|
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
|
2020-01-17 12:29:19 +00:00
|
|
|
var p types.Params
|
|
|
|
k.paramSubspace.GetParamSet(ctx, &p)
|
|
|
|
return p
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 12:29:19 +00:00
|
|
|
// SetParams sets params on the store
|
2019-11-27 14:45:59 +00:00
|
|
|
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
|
2020-01-17 12:29:19 +00:00
|
|
|
k.paramSubspace.SetParamSet(ctx, ¶ms)
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 12:29:19 +00:00
|
|
|
// GetMarkets returns the markets from params
|
|
|
|
func (k Keeper) GetMarkets(ctx sdk.Context) types.Markets {
|
|
|
|
return k.GetParams(ctx).Markets
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOracles returns the oracles in the pricefeed store
|
2020-04-23 16:35:58 +00:00
|
|
|
func (k Keeper) GetOracles(ctx sdk.Context, marketID string) ([]sdk.AccAddress, error) {
|
2020-01-17 12:29:19 +00:00
|
|
|
for _, m := range k.GetMarkets(ctx) {
|
2019-12-04 16:32:08 +00:00
|
|
|
if marketID == m.MarketID {
|
|
|
|
return m.Oracles, nil
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(types.ErrInvalidMarket, marketID)
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOracle returns the oracle from the store or an error if not found
|
2020-04-23 16:35:58 +00:00
|
|
|
func (k Keeper) GetOracle(ctx sdk.Context, marketID string, address sdk.AccAddress) (sdk.AccAddress, error) {
|
2019-12-04 16:32:08 +00:00
|
|
|
oracles, err := k.GetOracles(ctx, marketID)
|
2019-11-27 14:45:59 +00:00
|
|
|
if err != nil {
|
2022-01-08 00:39:27 +00:00
|
|
|
// Error already wrapped
|
|
|
|
return nil, err
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
2020-01-17 12:29:19 +00:00
|
|
|
for _, addr := range oracles {
|
2022-01-08 00:39:27 +00:00
|
|
|
if addr.Equals(address) {
|
2020-01-17 12:29:19 +00:00
|
|
|
return addr, nil
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(types.ErrInvalidOracle, address.String())
|
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) {
|
2020-01-17 12:29:19 +00:00
|
|
|
markets := k.GetMarkets(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
|
|
|
}
|
2020-12-02 17:37:11 +00:00
|
|
|
|
|
|
|
// GetAuthorizedAddresses returns a list of addresses that have special authorization within this module, eg the oracles of all markets.
|
|
|
|
func (k Keeper) GetAuthorizedAddresses(ctx sdk.Context) []sdk.AccAddress {
|
2022-01-08 00:39:27 +00:00
|
|
|
var oracles []sdk.AccAddress
|
2020-12-02 17:37:11 +00:00
|
|
|
uniqueOracles := map[string]bool{}
|
|
|
|
|
|
|
|
for _, m := range k.GetMarkets(ctx) {
|
|
|
|
for _, o := range m.Oracles {
|
|
|
|
// de-dup list of oracles
|
|
|
|
if _, found := uniqueOracles[o.String()]; !found {
|
|
|
|
oracles = append(oracles, o)
|
|
|
|
}
|
|
|
|
uniqueOracles[o.String()] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return oracles
|
|
|
|
}
|