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

46 lines
1.1 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-11-27 14:45:59 +00:00
_, err := keeper.SetPrice(ctx, pp.OracleAddress, pp.AssetCode, 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-11-27 14:45:59 +00:00
for _, a := range data.Params.Assets {
if a.Active {
_ = keeper.SetCurrentPrices(ctx, a.AssetCode)
}
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-11-27 14:45:59 +00:00
for _, asset := range keeper.GetAssetParams(ctx) {
2019-11-25 19:46:02 +00:00
pp := keeper.GetRawPrices(ctx, asset.AssetCode)
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,
}
}