2019-01-28 19:06:48 -08:00
|
|
|
package bank
|
|
|
|
|
|
|
|
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"
|
2020-06-14 16:06:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
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-06-14 16:06:16 -07:00
|
|
|
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, genState types.GenesisState) {
|
2020-07-08 10:43:08 -07:00
|
|
|
keeper.SetParams(ctx, genState.Params)
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2020-04-20 08:22:12 -07:00
|
|
|
var totalSupply sdk.Coins
|
|
|
|
|
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 {
|
|
|
|
if err := keeper.ValidateBalance(ctx, balance.Address); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-03-20 12:24:18 -07:00
|
|
|
if err := keeper.SetBalances(ctx, balance.Address, balance.Coins); err != nil {
|
|
|
|
panic(fmt.Errorf("error on setting balances %w", err))
|
|
|
|
}
|
2020-04-20 08:22:12 -07:00
|
|
|
|
|
|
|
totalSupply = totalSupply.Add(balance.Coins...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if genState.Supply.Empty() {
|
|
|
|
genState.Supply = totalSupply
|
2020-01-30 13:31:16 -08:00
|
|
|
}
|
2020-04-20 08:22:12 -07:00
|
|
|
|
2020-06-14 16:06:16 -07:00
|
|
|
keeper.SetSupply(ctx, types.NewSupply(genState.Supply))
|
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-06-14 16:06:16 -07:00
|
|
|
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState {
|
2020-01-30 13:31:16 -08:00
|
|
|
balancesSet := make(map[string]sdk.Coins)
|
|
|
|
|
|
|
|
keeper.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool {
|
|
|
|
balancesSet[addr.String()] = balancesSet[addr.String()].Add(balance)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2020-06-14 16:06:16 -07:00
|
|
|
balances := []types.Balance{}
|
2020-01-30 13:31:16 -08:00
|
|
|
|
|
|
|
for addrStr, coins := range balancesSet {
|
|
|
|
addr, err := sdk.AccAddressFromBech32(addrStr)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to convert address from string: %w", err))
|
|
|
|
}
|
|
|
|
|
2020-06-14 16:06:16 -07:00
|
|
|
balances = append(balances, types.Balance{
|
2020-01-30 13:31:16 -08:00
|
|
|
Address: addr,
|
|
|
|
Coins: coins,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-08 10:43:08 -07:00
|
|
|
return types.NewGenesisState(keeper.GetParams(ctx), balances, keeper.GetSupply(ctx).GetTotal())
|
2020-04-20 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateGenesis performs basic validation of supply genesis data returning an
|
|
|
|
// error for any failed validation criteria.
|
2020-06-14 16:06:16 -07:00
|
|
|
func ValidateGenesis(data types.GenesisState) error {
|
2020-07-08 10:43:08 -07:00
|
|
|
if err := data.Params.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-20 08:22:12 -07:00
|
|
|
return types.NewSupply(data.Supply).ValidateBasic()
|
2019-01-28 19:06:48 -08:00
|
|
|
}
|