cosmos-sdk/x/bank/keeper/genesis.go

45 lines
1.1 KiB
Go
Raw Normal View History

2020-07-12 23:55:58 -07:00
package keeper
import (
2020-01-30 13:31:16 -08:00
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
2020-01-30 13:31:16 -08:00
// InitGenesis initializes the bank module's state from a given genesis state.
2020-07-12 23:55:58 -07:00
func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
k.SetParams(ctx, genState.Params)
2020-01-30 13:31:16 -08:00
var totalSupply sdk.Coins
genState.Balances = types.SanitizeGenesisBalances(genState.Balances)
2020-01-30 13:31:16 -08:00
for _, balance := range genState.Balances {
2020-07-12 23:55:58 -07:00
if err := k.ValidateBalance(ctx, balance.Address); err != nil {
2020-01-30 13:31:16 -08:00
panic(err)
}
2020-07-12 23:55:58 -07:00
if err := k.SetBalances(ctx, balance.Address, balance.Coins); err != nil {
panic(fmt.Errorf("error on setting balances %w", err))
}
totalSupply = totalSupply.Add(balance.Coins...)
}
if genState.Supply.Empty() {
genState.Supply = totalSupply
2020-01-30 13:31:16 -08:00
}
2020-07-12 23:55:58 -07:00
k.SetSupply(ctx, types.NewSupply(genState.Supply))
}
2020-01-30 13:31:16 -08:00
// ExportGenesis returns the bank module's genesis state.
func (k BaseKeeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
return types.NewGenesisState(
k.GetParams(ctx),
k.GetAccountsBalances(ctx),
k.GetSupply(ctx).GetTotal(),
k.GetAllDenomMetaData(ctx),
)
}