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
|
|
|
|
|
|
|
"github.com/kava-labs/kava/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.
|
|
|
|
func InitGenesis(ctx sdk.Context, keeper Keeper, supplyKeeper types.SupplyKeeper, gs GenesisState) {
|
|
|
|
if err := gs.Validate(); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to validate %s genesis state: %s", ModuleName, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
for _, a := range gs.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
|
|
|
|
totalAuctionCoins.Add(a.GetModuleAccountCoins())
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the module account exists
|
|
|
|
moduleAcc := supplyKeeper.GetModuleAccount(ctx, ModuleName)
|
|
|
|
if moduleAcc == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", ModuleName))
|
|
|
|
}
|
|
|
|
// 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.
|
|
|
|
if !moduleAcc.GetCoins().IsEqual(totalAuctionCoins) {
|
|
|
|
panic(fmt.Sprintf("total auction coins (%s) do not equal (%s) module account (%s) ", moduleAcc.GetCoins(), ModuleName, totalAuctionCoins))
|
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 {
|
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
|
|
|
|
2020-01-14 14:00:37 +00:00
|
|
|
genAuctions := GenesisAuctions{} // return empty list instead of nil if no auctions
|
2020-01-12 15:12:22 +00:00
|
|
|
keeper.IterateAuctions(ctx, func(a 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
|
|
|
|
2020-01-12 15:12:22 +00:00
|
|
|
return NewGenesisState(nextAuctionID, params, genAuctions)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|