cosmos-sdk/x/supply/genesis.go

38 lines
1.1 KiB
Go
Raw Normal View History

2019-06-28 13:11:27 -07:00
package supply
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/supply/types"
2019-06-28 13:11:27 -07:00
)
// InitGenesis sets supply information for genesis.
//
// CONTRACT: all types of accounts must have been already initialized/created
2020-01-30 13:31:16 -08:00
func InitGenesis(ctx sdk.Context, keeper Keeper, bk types.BankKeeper, data GenesisState) {
2019-06-28 13:11:27 -07:00
// manually set the total supply based on accounts if not provided
if data.Supply.Empty() {
2019-06-28 13:11:27 -07:00
var totalSupply sdk.Coins
2020-01-30 13:31:16 -08:00
bk.IterateAllBalances(ctx,
func(_ sdk.AccAddress, balance sdk.Coin) (stop bool) {
totalSupply = totalSupply.Add(balance)
2019-06-28 13:11:27 -07:00
return false
},
)
data.Supply = totalSupply
2019-06-28 13:11:27 -07:00
}
keeper.SetSupply(ctx, types.NewSupply(data.Supply))
2019-06-28 13:11:27 -07:00
}
// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
return NewGenesisState(keeper.GetSupply(ctx).GetTotal())
2019-06-28 13:11:27 -07:00
}
// ValidateGenesis performs basic validation of supply genesis data returning an
// error for any failed validation criteria.
func ValidateGenesis(data GenesisState) error {
return types.NewSupply(data.Supply).ValidateBasic()
2019-06-28 13:11:27 -07:00
}