github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/supply/genesis.go (about) 1 package supply 2 3 import ( 4 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 5 authexported "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/exported" 6 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply/internal/types" 7 ) 8 9 // InitGenesis sets supply information for genesis. 10 // 11 // CONTRACT: all types of accounts must have been already initialized/created 12 func InitGenesis(ctx sdk.Context, keeper Keeper, ak types.AccountKeeper, data GenesisState) { 13 // manually set the total supply based on accounts if not provided 14 if data.Supply.Empty() { 15 var totalSupply sdk.Coins 16 ak.IterateAccounts(ctx, 17 func(acc authexported.Account) (stop bool) { 18 totalSupply = totalSupply.Add(acc.GetCoins()...) 19 return false 20 }, 21 ) 22 23 data.Supply = totalSupply 24 } 25 26 keeper.SetSupply(ctx, types.NewSupply(data.Supply)) 27 } 28 29 // ExportGenesis returns a GenesisState for a given context and keeper. 30 func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState { 31 var coins sdk.DecCoins 32 for _, coin := range keeper.GetSupply(ctx).GetTotal() { 33 if coin.Amount.IsZero() { 34 continue 35 } 36 coins = coins.Add(coin) 37 } 38 return NewGenesisState(coins) 39 } 40 41 // ValidateGenesis performs basic validation of supply genesis data returning an 42 // error for any failed validation criteria. 43 func ValidateGenesis(data GenesisState) error { 44 return types.NewSupply(data.Supply).ValidateBasic() 45 }