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) {
|
|
|
|
err := gs.Validate()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// Set the markets and oracles from params
|
|
|
|
keeper.SetParams(ctx, gs.Params)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
// Iterate through the posted prices and set them in the store
|
2020-01-17 12:29:19 +00:00
|
|
|
for _, pp := range gs.PostedPrices {
|
2019-12-04 16:32:08 +00:00
|
|
|
_, err := keeper.SetPrice(ctx, pp.OracleAddress, pp.MarketID, pp.Price, pp.Expiry)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
if market.Active {
|
2020-01-30 00:12:47 +00:00
|
|
|
rps := keeper.GetRawPrices(ctx, market.MarketID)
|
|
|
|
if len(rps) > 0 {
|
|
|
|
err := keeper.SetCurrentPrices(ctx, market.MarketID)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-06 23:53:20 +00:00
|
|
|
}
|
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) {
|
|
|
|
pp := keeper.GetRawPrices(ctx, market.MarketID)
|
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
|
|
|
}
|