github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/distribution/genesis.go (about) 1 package distribution 2 3 import ( 4 "fmt" 5 6 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 7 8 "github.com/fibonacci-chain/fbc/x/distribution/types" 9 ) 10 11 // InitGenesis sets distribution information for genesis 12 func InitGenesis(ctx sdk.Context, keeper Keeper, supplyKeeper types.SupplyKeeper, data types.GenesisState) { 13 14 keeper.SetFeePool(ctx, data.FeePool) 15 keeper.SetParamsForInitGenesis(ctx, data.Params) 16 keeper.SetPreviousProposerConsAddr(ctx, data.PreviousProposer) 17 18 for _, dwi := range data.DelegatorWithdrawInfos { 19 keeper.SetDelegatorWithdrawAddr(ctx, dwi.DelegatorAddress, dwi.WithdrawAddress) 20 } 21 22 moduleHoldings := sdk.SysCoins{} 23 for _, acc := range data.ValidatorAccumulatedCommissions { 24 keeper.SetValidatorAccumulatedCommission(ctx, acc.ValidatorAddress, acc.Accumulated) 25 moduleHoldings = moduleHoldings.Add(acc.Accumulated...) 26 } 27 moduleHoldings = moduleHoldings.Add(data.FeePool.CommunityPool...) 28 29 // check if the module account exists 30 moduleAcc := keeper.GetDistributionAccount(ctx) 31 if moduleAcc == nil { 32 panic(fmt.Sprintf("%s module account has not been set", types.ModuleName)) 33 } 34 if moduleAcc.GetCoins().IsZero() { 35 if err := moduleAcc.SetCoins(moduleHoldings); err != nil { 36 panic(err) 37 } 38 supplyKeeper.SetModuleAccount(ctx, moduleAcc) 39 } 40 } 41 42 // ExportGenesis returns a GenesisState for a given context and keeper. 43 func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState { 44 feePool := keeper.GetFeePool(ctx) 45 params := keeper.GetParamsForInitGenesis(ctx) 46 47 dwi := make([]types.DelegatorWithdrawInfo, 0) 48 keeper.IterateDelegatorWithdrawAddrs(ctx, func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool) { 49 dwi = append(dwi, types.DelegatorWithdrawInfo{ 50 DelegatorAddress: del, 51 WithdrawAddress: addr, 52 }) 53 return false 54 }) 55 pp := keeper.GetPreviousProposerConsAddr(ctx) 56 acc := make([]types.ValidatorAccumulatedCommissionRecord, 0) 57 keeper.IterateValidatorAccumulatedCommissions(ctx, 58 func(addr sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool) { 59 acc = append(acc, types.ValidatorAccumulatedCommissionRecord{ 60 ValidatorAddress: addr, 61 Accumulated: commission, 62 }) 63 return false 64 }, 65 ) 66 67 return types.NewGenesisState(params, feePool, dwi, pp, acc) 68 }