2020-07-12 23:55:58 -07:00
|
|
|
package keeper
|
2019-01-28 19:06:48 -08:00
|
|
|
|
|
|
|
import (
|
2020-01-30 13:31:16 -08:00
|
|
|
"fmt"
|
|
|
|
|
2019-01-28 19:06:48 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-04-06 07:43:08 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/query"
|
2020-04-20 08:22:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
2019-01-28 19:06:48 -08:00
|
|
|
)
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
// InitGenesis initializes the bank module's state from a given genesis state.
|
2020-12-03 00:39:11 -08:00
|
|
|
func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
|
2020-07-12 23:55:58 -07:00
|
|
|
k.SetParams(ctx, genState.Params)
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2021-03-22 11:15:29 -07:00
|
|
|
totalSupply := sdk.Coins{}
|
2020-04-20 08:22:12 -07:00
|
|
|
|
2020-06-14 16:06:16 -07:00
|
|
|
genState.Balances = types.SanitizeGenesisBalances(genState.Balances)
|
2020-01-30 13:31:16 -08:00
|
|
|
for _, balance := range genState.Balances {
|
2020-09-25 03:25:37 -07:00
|
|
|
addr, err := sdk.AccAddressFromBech32(balance.Address)
|
|
|
|
if err != nil {
|
2020-01-30 13:31:16 -08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-06-01 13:03:32 -07:00
|
|
|
if err := k.initBalances(ctx, addr, balance.Coins); err != nil {
|
2020-03-20 12:24:18 -07:00
|
|
|
panic(fmt.Errorf("error on setting balances %w", err))
|
|
|
|
}
|
2020-04-20 08:22:12 -07:00
|
|
|
|
|
|
|
totalSupply = totalSupply.Add(balance.Coins...)
|
|
|
|
}
|
|
|
|
|
2021-03-22 11:15:29 -07:00
|
|
|
if !genState.Supply.Empty() && !genState.Supply.IsEqual(totalSupply) {
|
|
|
|
panic(fmt.Errorf("genesis supply is incorrect, expected %v, got %v", genState.Supply, totalSupply))
|
2020-01-30 13:31:16 -08:00
|
|
|
}
|
2020-04-20 08:22:12 -07:00
|
|
|
|
2021-03-25 03:03:13 -07:00
|
|
|
for _, supply := range totalSupply {
|
|
|
|
k.setSupply(ctx, supply)
|
|
|
|
}
|
2020-12-03 00:39:11 -08:00
|
|
|
|
|
|
|
for _, meta := range genState.DenomMetadata {
|
|
|
|
k.SetDenomMetaData(ctx, meta)
|
|
|
|
}
|
2019-01-28 19:06:48 -08:00
|
|
|
}
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
// ExportGenesis returns the bank module's genesis state.
|
2020-08-11 07:22:30 -07:00
|
|
|
func (k BaseKeeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
|
2021-04-06 07:43:08 -07:00
|
|
|
totalSupply, _, err := k.GetPaginatedTotalSupply(ctx, &query.PageRequest{Limit: query.MaxLimit})
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("unable to fetch total supply %v", err))
|
|
|
|
}
|
|
|
|
|
2020-08-06 02:10:07 -07:00
|
|
|
return types.NewGenesisState(
|
|
|
|
k.GetParams(ctx),
|
|
|
|
k.GetAccountsBalances(ctx),
|
2021-04-06 07:43:08 -07:00
|
|
|
totalSupply,
|
2020-08-06 02:10:07 -07:00
|
|
|
k.GetAllDenomMetaData(ctx),
|
|
|
|
)
|
2019-01-28 19:06:48 -08:00
|
|
|
}
|