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

59 lines
1.4 KiB
Go
Raw Normal View History

2019-11-25 19:46:02 +00:00
package pricefeed
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// InitGenesis sets distribution information for genesis.
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
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)
}
}
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
for _, market := range params.Markets {
if market.Active {
2020-04-13 18:08:14 +00:00
rps, err := keeper.GetRawPrices(ctx, market.MarketID)
if err != nil {
panic(err)
}
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 {
// 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
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...)
}
return NewGenesisState(params, postedPrices)
2019-11-25 19:46:02 +00:00
}