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

49 lines
1.2 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, data GenesisState) {
// Set the assets and oracles from params
2019-11-27 14:45:59 +00:00
keeper.SetParams(ctx, data.Params)
2019-11-25 19:46:02 +00:00
// Iterate through the posted prices and set them in the store
for _, pp := range data.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)
}
}
// Set the current price (if any) based on what's now in the store
2019-12-04 16:32:08 +00:00
for _, a := range data.Params.Markets {
2019-11-27 14:45:59 +00:00
if a.Active {
2019-12-06 23:53:20 +00:00
err := keeper.SetCurrentPrices(ctx, a.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 {
// Get the params for assets 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
2019-12-04 16:32:08 +00:00
for _, asset := range keeper.GetMarketParams(ctx) {
pp := keeper.GetRawPrices(ctx, asset.MarketID)
2019-11-25 19:46:02 +00:00
postedPrices = append(postedPrices, pp...)
}
return GenesisState{
2019-11-27 14:45:59 +00:00
Params: params,
2019-11-25 19:46:02 +00:00
PostedPrices: postedPrices,
}
}