2019-11-25 19:46:02 +00:00
|
|
|
package auction
|
|
|
|
|
|
|
|
import (
|
2020-01-14 14:00:37 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-01-14 14:00:37 +00:00
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/auction/keeper"
|
|
|
|
"github.com/0glabs/0g-chain/x/auction/types"
|
2019-11-25 19:46:02 +00:00
|
|
|
)
|
|
|
|
|
2020-01-14 14:00:37 +00:00
|
|
|
// InitGenesis initializes the store state from a genesis state.
|
2022-01-08 00:39:27 +00:00
|
|
|
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, bankKeeper types.BankKeeper, accountKeeper types.AccountKeeper, gs *types.GenesisState) {
|
2020-01-14 14:00:37 +00:00
|
|
|
if err := gs.Validate(); err != nil {
|
2022-01-08 00:39:27 +00:00
|
|
|
panic(fmt.Sprintf("failed to validate %s genesis state: %s", types.ModuleName, err))
|
2020-01-14 14:00:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
keeper.SetNextAuctionID(ctx, gs.NextAuctionId)
|
2020-01-12 15:12:22 +00:00
|
|
|
|
2020-01-14 14:00:37 +00:00
|
|
|
keeper.SetParams(ctx, gs.Params)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-01-14 14:00:37 +00:00
|
|
|
totalAuctionCoins := sdk.NewCoins()
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
auctions, err := types.UnpackGenesisAuctions(gs.Auctions)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to unpack genesis auctions: %s", err))
|
|
|
|
}
|
|
|
|
for _, a := range auctions {
|
2019-11-25 19:46:02 +00:00
|
|
|
keeper.SetAuction(ctx, a)
|
2020-01-14 14:00:37 +00:00
|
|
|
// find the total coins that should be present in the module account
|
2020-04-23 16:35:58 +00:00
|
|
|
totalAuctionCoins = totalAuctionCoins.Add(a.GetModuleAccountCoins()...)
|
2020-01-14 14:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if the module account exists
|
2022-01-08 00:39:27 +00:00
|
|
|
moduleAcc := accountKeeper.GetModuleAccount(ctx, types.ModuleName)
|
2020-01-14 14:00:37 +00:00
|
|
|
if moduleAcc == nil {
|
2022-01-08 00:39:27 +00:00
|
|
|
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
|
2020-01-14 14:00:37 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
maccCoins := bankKeeper.GetAllBalances(ctx, moduleAcc.GetAddress())
|
|
|
|
|
2020-01-14 14:00:37 +00:00
|
|
|
// check module coins match auction coins
|
|
|
|
// Note: Other sdk modules do not check this, instead just using the existing module account coins, or if zero, setting them.
|
2022-01-08 00:39:27 +00:00
|
|
|
if !maccCoins.IsEqual(totalAuctionCoins) {
|
|
|
|
panic(fmt.Sprintf("total auction coins (%s) do not equal (%s) module account (%s) ", maccCoins, types.ModuleName, totalAuctionCoins))
|
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, keeper keeper.Keeper) *types.GenesisState {
|
2020-01-12 15:12:22 +00:00
|
|
|
nextAuctionID, err := keeper.GetNextAuctionID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-01-12 15:12:22 +00:00
|
|
|
params := keeper.GetParams(ctx)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
genAuctions := []types.GenesisAuction{} // return empty list instead of nil if no auctions
|
|
|
|
keeper.IterateAuctions(ctx, func(a types.Auction) bool {
|
2020-01-14 14:00:37 +00:00
|
|
|
ga, ok := a.(types.GenesisAuction)
|
|
|
|
if !ok {
|
|
|
|
panic("could not convert stored auction to GenesisAuction type")
|
|
|
|
}
|
|
|
|
genAuctions = append(genAuctions, ga)
|
2020-01-12 15:12:22 +00:00
|
|
|
return false
|
|
|
|
})
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
gs, err := types.NewGenesisState(nextAuctionID, params, genAuctions)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return gs
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|