2019-11-25 19:46:02 +00:00
|
|
|
package pricefeed
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InitGenesis sets distribution information for genesis.
|
2020-01-17 12:29:19 +00:00
|
|
|
func InitGenesis(ctx sdk.Context, keeper Keeper, gs GenesisState) {
|
|
|
|
// Set the markets and oracles from params
|
|
|
|
keeper.SetParams(ctx, gs.Params)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-09-30 18:33:48 +00:00
|
|
|
// Iterate through the posted prices and set them in the store if they are not expired
|
2020-01-17 12:29:19 +00:00
|
|
|
for _, pp := range gs.PostedPrices {
|
2020-09-30 18:33:48 +00:00
|
|
|
if pp.Expiry.After(ctx.BlockTime()) {
|
|
|
|
_, err := keeper.SetPrice(ctx, pp.OracleAddress, pp.MarketID, pp.Price, pp.Expiry)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-17 12:29:19 +00:00
|
|
|
params := keeper.GetParams(ctx)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
// Set the current price (if any) based on what's now in the store
|
2020-01-17 12:29:19 +00:00
|
|
|
for _, market := range params.Markets {
|
2020-05-21 04:49:27 +00:00
|
|
|
if !market.Active {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rps, err := keeper.GetRawPrices(ctx, market.MarketID)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if len(rps) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = keeper.SetCurrentPrices(ctx, market.MarketID)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-11-27 14:45:59 +00:00
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportGenesis returns a GenesisState for a given context and keeper.
|
|
|
|
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
|
|
|
|
|
2020-01-17 12:29:19 +00:00
|
|
|
// Get the params for markets and oracles
|
2019-11-27 14:45:59 +00:00
|
|
|
params := keeper.GetParams(ctx)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
var postedPrices []PostedPrice
|
2020-01-17 12:29:19 +00:00
|
|
|
for _, market := range keeper.GetMarkets(ctx) {
|
2020-04-13 18:08:14 +00:00
|
|
|
pp, err := keeper.GetRawPrices(ctx, market.MarketID)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
postedPrices = append(postedPrices, pp...)
|
|
|
|
}
|
|
|
|
|
2020-02-25 15:11:09 +00:00
|
|
|
return NewGenesisState(params, postedPrices)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|