github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/staking/keeper/pool.go (about) 1 package keeper 2 3 import ( 4 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 5 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/staking/types" 6 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply/exported" 7 ) 8 9 // GetBondedPool returns the bonded tokens pool's module account 10 func (k Keeper) GetBondedPool(ctx sdk.Context) (bondedPool exported.ModuleAccountI) { 11 return k.supplyKeeper.GetModuleAccount(ctx, types.BondedPoolName) 12 } 13 14 // GetNotBondedPool returns the not bonded tokens pool's module account 15 func (k Keeper) GetNotBondedPool(ctx sdk.Context) (notBondedPool exported.ModuleAccountI) { 16 return k.supplyKeeper.GetModuleAccount(ctx, types.NotBondedPoolName) 17 } 18 19 // bondedTokensToNotBonded transfers coins from the bonded to the not bonded pool within staking 20 func (k Keeper) bondedTokensToNotBonded(ctx sdk.Context, tokens sdk.Int) { 21 coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), tokens)) 22 err := k.supplyKeeper.SendCoinsFromModuleToModule(ctx, types.BondedPoolName, types.NotBondedPoolName, coins) 23 if err != nil { 24 panic(err) 25 } 26 } 27 28 // notBondedTokensToBonded transfers coins from the not bonded to the bonded pool within staking 29 func (k Keeper) notBondedTokensToBonded(ctx sdk.Context, tokens sdk.Int) { 30 coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), tokens)) 31 err := k.supplyKeeper.SendCoinsFromModuleToModule(ctx, types.NotBondedPoolName, types.BondedPoolName, coins) 32 if err != nil { 33 panic(err) 34 } 35 } 36 37 // burnBondedTokens removes coins from the bonded pool module account 38 func (k Keeper) burnBondedTokens(ctx sdk.Context, amt sdk.Int) error { 39 if !amt.IsPositive() { 40 // skip as no coins need to be burned 41 return nil 42 } 43 coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), amt)) 44 return k.supplyKeeper.BurnCoins(ctx, types.BondedPoolName, coins) 45 } 46 47 // burnNotBondedTokens removes coins from the not bonded pool module account 48 func (k Keeper) burnNotBondedTokens(ctx sdk.Context, amt sdk.Int) error { 49 if !amt.IsPositive() { 50 // skip as no coins need to be burned 51 return nil 52 } 53 coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), amt)) 54 return k.supplyKeeper.BurnCoins(ctx, types.NotBondedPoolName, coins) 55 } 56 57 // TotalBondedTokens total staking tokens supply which is bonded 58 func (k Keeper) TotalBondedTokens(ctx sdk.Context) sdk.Dec { 59 bondedPool := k.GetBondedPool(ctx) 60 return bondedPool.GetCoins().AmountOf(k.BondDenom(ctx)) 61 } 62 63 // StakingTokenSupply staking tokens from the total supply 64 func (k Keeper) StakingTokenSupply(ctx sdk.Context) sdk.Dec { 65 return k.supplyKeeper.GetSupply(ctx).GetTotal().AmountOf(k.BondDenom(ctx)) 66 } 67 68 // BondedRatio the fraction of the staking tokens which are currently bonded 69 func (k Keeper) BondedRatio(ctx sdk.Context) sdk.Dec { 70 bondedPool := k.GetBondedPool(ctx) 71 72 stakeSupply := k.StakingTokenSupply(ctx) 73 if stakeSupply.IsPositive() { 74 return bondedPool.GetCoins().AmountOf(k.BondDenom(ctx)).Quo(stakeSupply) 75 } 76 return sdk.ZeroDec() 77 }