2019-08-28 07:58:25 -07:00
|
|
|
package types
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/exported"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ exported.GenesisBalance = (*Balance)(nil)
|
|
|
|
|
|
|
|
// GenesisState defines the bank module's genesis state.
|
2019-08-28 07:58:25 -07:00
|
|
|
type GenesisState struct {
|
2020-01-30 13:31:16 -08:00
|
|
|
SendEnabled bool `json:"send_enabled" yaml:"send_enabled"`
|
|
|
|
Balances []Balance `json:"balances" yaml:"balances"`
|
2020-04-20 08:22:12 -07:00
|
|
|
Supply sdk.Coins `json:"supply" yaml:"supply"`
|
2020-01-30 13:31:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Balance defines an account address and balance pair used in the bank module's
|
|
|
|
// genesis state.
|
|
|
|
type Balance struct {
|
|
|
|
Address sdk.AccAddress `json:"address" yaml:"address"`
|
|
|
|
Coins sdk.Coins `json:"coins" yaml:"coins"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAddress returns the account address of the Balance object.
|
|
|
|
func (b Balance) GetAddress() sdk.AccAddress {
|
|
|
|
return b.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAddress returns the account coins of the Balance object.
|
|
|
|
func (b Balance) GetCoins() sdk.Coins {
|
|
|
|
return b.Coins
|
|
|
|
}
|
|
|
|
|
|
|
|
// SanitizeGenesisAccounts sorts addresses and coin sets.
|
|
|
|
func SanitizeGenesisBalances(balances []Balance) []Balance {
|
|
|
|
sort.Slice(balances, func(i, j int) bool {
|
|
|
|
return bytes.Compare(balances[i].Address.Bytes(), balances[j].Address.Bytes()) < 0
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, balance := range balances {
|
|
|
|
balance.Coins = balance.Coins.Sort()
|
|
|
|
}
|
|
|
|
|
|
|
|
return balances
|
2019-08-28 07:58:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGenesisState creates a new genesis state.
|
2020-04-20 08:22:12 -07:00
|
|
|
func NewGenesisState(sendEnabled bool, balances []Balance, supply sdk.Coins) GenesisState {
|
|
|
|
return GenesisState{
|
|
|
|
SendEnabled: sendEnabled,
|
|
|
|
Balances: balances,
|
|
|
|
Supply: supply,
|
|
|
|
}
|
2019-08-28 07:58:25 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
// DefaultGenesisState returns a default bank module genesis state.
|
2020-04-20 08:22:12 -07:00
|
|
|
func DefaultGenesisState() GenesisState {
|
|
|
|
return NewGenesisState(true, []Balance{}, DefaultSupply().GetTotal())
|
|
|
|
}
|
2020-01-30 13:31:16 -08:00
|
|
|
|
|
|
|
// GetGenesisStateFromAppState returns x/bank GenesisState given raw application
|
|
|
|
// genesis state.
|
2020-06-12 09:23:08 -07:00
|
|
|
func GetGenesisStateFromAppState(cdc codec.JSONMarshaler, appState map[string]json.RawMessage) GenesisState {
|
2020-01-30 13:31:16 -08:00
|
|
|
var genesisState GenesisState
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
if appState[ModuleName] != nil {
|
|
|
|
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
|
|
|
|
}
|
|
|
|
|
|
|
|
return genesisState
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenesisAccountIterator implements genesis account iteration.
|
|
|
|
type GenesisBalancesIterator struct{}
|
|
|
|
|
|
|
|
// IterateGenesisAccounts iterates over all the genesis accounts found in
|
|
|
|
// appGenesis and invokes a callback on each genesis account. If any call
|
|
|
|
// returns true, iteration stops.
|
|
|
|
func (GenesisBalancesIterator) IterateGenesisBalances(
|
2020-06-12 09:23:08 -07:00
|
|
|
cdc codec.JSONMarshaler, appState map[string]json.RawMessage, cb func(exported.GenesisBalance) (stop bool),
|
2020-01-30 13:31:16 -08:00
|
|
|
) {
|
|
|
|
for _, balance := range GetGenesisStateFromAppState(cdc, appState).Balances {
|
|
|
|
if cb(balance) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|