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

57 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"
"github.com/kava-labs/kava/x/pricefeed/keeper"
"github.com/kava-labs/kava/x/pricefeed/types"
2019-11-25 19:46:02 +00:00
)
// InitGenesis sets distribution information for genesis.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) {
// Set the markets and oracles from params
k.SetParams(ctx, gs.Params)
2019-11-25 19:46:02 +00:00
// Iterate through the posted prices and set them in the store if they are not expired
for _, pp := range gs.PostedPrices {
if pp.Expiry.After(ctx.BlockTime()) {
_, err := k.SetPrice(ctx, pp.OracleAddress, pp.MarketID, pp.Price, pp.Expiry)
if err != nil {
panic(err)
}
2019-11-25 19:46:02 +00:00
}
}
params := k.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 {
continue
}
rps := k.GetRawPrices(ctx, market.MarketID)
if len(rps) == 0 {
continue
}
err := k.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, k keeper.Keeper) types.GenesisState {
2019-11-25 19:46:02 +00:00
// Get the params for markets and oracles
params := k.GetParams(ctx)
2019-11-25 19:46:02 +00:00
var postedPrices []types.PostedPrice
for _, market := range k.GetMarkets(ctx) {
pp := k.GetRawPrices(ctx, market.MarketID)
2019-11-25 19:46:02 +00:00
postedPrices = append(postedPrices, pp...)
}
return types.NewGenesisState(params, postedPrices)
2019-11-25 19:46:02 +00:00
}