2019-11-25 19:46:02 +00:00
|
|
|
package pricefeed
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/pricefeed/keeper"
|
|
|
|
"github.com/0glabs/0g-chain/x/pricefeed/types"
|
2019-11-25 19:46:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InitGenesis sets distribution information for genesis.
|
2022-01-08 00:39:27 +00:00
|
|
|
func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) {
|
2020-01-17 12:29:19 +00:00
|
|
|
// Set the markets and oracles from params
|
2022-01-08 00:39:27 +00:00
|
|
|
k.SetParams(ctx, gs.Params)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-09-30 18:33:48 +00:00
|
|
|
// Iterate through the posted prices and set them in the store if they are not expired
|
2020-01-17 12:29:19 +00:00
|
|
|
for _, pp := range gs.PostedPrices {
|
2020-09-30 18:33:48 +00:00
|
|
|
if pp.Expiry.After(ctx.BlockTime()) {
|
2022-01-08 00:39:27 +00:00
|
|
|
_, err := k.SetPrice(ctx, pp.OracleAddress, pp.MarketID, pp.Price, pp.Expiry)
|
2020-09-30 18:33:48 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-08 00:39:27 +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
|
2020-01-17 12:29:19 +00:00
|
|
|
for _, market := range params.Markets {
|
2020-05-21 04:49:27 +00:00
|
|
|
if !market.Active {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
rps := k.GetRawPrices(ctx, market.MarketID)
|
|
|
|
|
2020-05-21 04:49:27 +00:00
|
|
|
if len(rps) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
err := k.SetCurrentPrices(ctx, market.MarketID)
|
2020-05-21 04:49:27 +00:00
|
|
|
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.
|
2022-01-08 00:39:27 +00:00
|
|
|
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
|
2020-01-17 12:29:19 +00:00
|
|
|
// Get the params for markets and oracles
|
2022-01-08 00:39:27 +00:00
|
|
|
params := k.GetParams(ctx)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2022-01-08 00:39:27 +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...)
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
return types.NewGenesisState(params, postedPrices)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|