github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/farm/genesis.go (about) 1 package farm 2 3 import ( 4 "fmt" 5 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 6 "github.com/fibonacci-chain/fbc/x/farm/keeper" 7 "github.com/fibonacci-chain/fbc/x/farm/types" 8 ) 9 10 // InitGenesis initialize default parameters and the keeper's address to pubkey map 11 func InitGenesis(ctx sdk.Context, k keeper.Keeper, data types.GenesisState) { 12 var yieldModuleAccHoldings sdk.SysCoins 13 var moduleAccHoldings sdk.SysCoins 14 15 for _, pool := range data.Pools { 16 moduleAccHoldings = moduleAccHoldings.Add2(sdk.SysCoins{pool.TotalValueLocked}) 17 moduleAccHoldings = moduleAccHoldings.Add2(sdk.SysCoins{pool.DepositAmount}) 18 yieldModuleAccHoldings = yieldModuleAccHoldings.Add2(pool.TotalAccumulatedRewards) 19 k.SetFarmPool(ctx, pool) 20 } 21 22 for _, lockInfo := range data.LockInfos { 23 k.SetLockInfo(ctx, lockInfo) 24 } 25 26 for _, historical := range data.PoolHistoricalRewards { 27 k.SetPoolHistoricalRewards(ctx, historical.PoolName, historical.Period, historical.Rewards) 28 } 29 30 for _, current := range data.PoolCurrentRewards { 31 k.SetPoolCurrentRewards(ctx, current.PoolName, current.Rewards) 32 } 33 34 for _, poolName := range data.WhiteList { 35 k.SetWhitelist(ctx, poolName) 36 } 37 38 k.SetParams(ctx, data.Params) 39 40 // init module account 41 moduleAcc := k.SupplyKeeper().GetModuleAccount(ctx, types.ModuleName) 42 if moduleAcc == nil { 43 panic(fmt.Sprintf("%s module account has not been set", types.ModuleName)) 44 } 45 if moduleAcc.GetCoins().IsZero() { 46 if err := moduleAcc.SetCoins(moduleAccHoldings); err != nil { 47 panic(err) 48 } 49 k.SupplyKeeper().SetModuleAccount(ctx, moduleAcc) 50 } 51 52 yieldModuleAcc := k.SupplyKeeper().GetModuleAccount(ctx, types.YieldFarmingAccount) 53 if yieldModuleAcc == nil { 54 panic(fmt.Sprintf("%s module account has not been set", types.YieldFarmingAccount)) 55 } 56 if yieldModuleAcc.GetCoins().IsZero() { 57 if err := yieldModuleAcc.SetCoins(yieldModuleAccHoldings); err != nil { 58 panic(err) 59 } 60 k.SupplyKeeper().SetModuleAccount(ctx, yieldModuleAcc) 61 } 62 63 mintModuleAcc := k.SupplyKeeper().GetModuleAccount(ctx, types.MintFarmingAccount) 64 if mintModuleAcc == nil { 65 panic(fmt.Sprintf("%s module account has not been set", types.MintFarmingAccount)) 66 } 67 } 68 69 // ExportGenesis writes the current store values to a genesis file, which can be imported again with InitGenesis 70 func ExportGenesis(ctx sdk.Context, k keeper.Keeper) (data types.GenesisState) { 71 pools := k.GetFarmPools(ctx) 72 73 lockInfos := make([]types.LockInfo, 0) 74 k.IterateAllLockInfos(ctx, 75 func(lockInfo types.LockInfo) (stop bool) { 76 lockInfos = append(lockInfos, lockInfo) 77 return false 78 }, 79 ) 80 81 allHistoricalRewards := make([]types.PoolHistoricalRewardsRecord, 0) 82 k.IterateAllPoolHistoricalRewards(ctx, 83 func(poolName string, period uint64, rewards types.PoolHistoricalRewards) (stop bool) { 84 allHistoricalRewards = append(allHistoricalRewards, types.PoolHistoricalRewardsRecord{ 85 PoolName: poolName, 86 Period: period, 87 Rewards: rewards, 88 }) 89 return false 90 }, 91 ) 92 93 allCurRewards := make([]types.PoolCurrentRewardsRecord, 0) 94 k.IterateAllPoolCurrentRewards(ctx, 95 func(poolName string, rewards types.PoolCurrentRewards) (stop bool) { 96 allCurRewards = append(allCurRewards, types.PoolCurrentRewardsRecord{ 97 PoolName: poolName, 98 Rewards: rewards, 99 }) 100 return false 101 }, 102 ) 103 104 whiteList := k.GetWhitelist(ctx) 105 106 params := k.GetParams(ctx) 107 108 return types.NewGenesisState(pools, lockInfos, allHistoricalRewards, allCurRewards, whiteList, params) 109 }