github.com/Finschia/finschia-sdk@v0.48.1/x/staking/keeper/pool.go (about)

     1  package keeper
     2  
     3  import (
     4  	sdk "github.com/Finschia/finschia-sdk/types"
     5  	authtypes "github.com/Finschia/finschia-sdk/x/auth/types"
     6  	"github.com/Finschia/finschia-sdk/x/staking/types"
     7  )
     8  
     9  // GetBondedPool returns the bonded tokens pool's module account
    10  func (k Keeper) GetBondedPool(ctx sdk.Context) (bondedPool authtypes.ModuleAccountI) {
    11  	return k.authKeeper.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 authtypes.ModuleAccountI) {
    16  	return k.authKeeper.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  	if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.BondedPoolName, types.NotBondedPoolName, coins); err != nil {
    23  		panic(err)
    24  	}
    25  }
    26  
    27  // notBondedTokensToBonded transfers coins from the not bonded to the bonded pool within staking
    28  func (k Keeper) notBondedTokensToBonded(ctx sdk.Context, tokens sdk.Int) {
    29  	coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), tokens))
    30  	if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.NotBondedPoolName, types.BondedPoolName, coins); err != nil {
    31  		panic(err)
    32  	}
    33  }
    34  
    35  // burnBondedTokens removes coins from the bonded pool module account
    36  func (k Keeper) burnBondedTokens(ctx sdk.Context, amt sdk.Int) error {
    37  	if !amt.IsPositive() {
    38  		// skip as no coins need to be burned
    39  		return nil
    40  	}
    41  
    42  	coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), amt))
    43  
    44  	return k.bankKeeper.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  
    54  	coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), amt))
    55  
    56  	return k.bankKeeper.BurnCoins(ctx, types.NotBondedPoolName, coins)
    57  }
    58  
    59  // TotalBondedTokens total staking tokens supply which is bonded
    60  func (k Keeper) TotalBondedTokens(ctx sdk.Context) sdk.Int {
    61  	bondedPool := k.GetBondedPool(ctx)
    62  	return k.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), k.BondDenom(ctx)).Amount
    63  }
    64  
    65  // StakingTokenSupply staking tokens from the total supply
    66  func (k Keeper) StakingTokenSupply(ctx sdk.Context) sdk.Int {
    67  	return k.bankKeeper.GetSupply(ctx, k.BondDenom(ctx)).Amount
    68  }
    69  
    70  // BondedRatio the fraction of the staking tokens which are currently bonded
    71  func (k Keeper) BondedRatio(ctx sdk.Context) sdk.Dec {
    72  	stakeSupply := k.StakingTokenSupply(ctx)
    73  	if stakeSupply.IsPositive() {
    74  		return k.TotalBondedTokens(ctx).ToDec().QuoInt(stakeSupply)
    75  	}
    76  
    77  	return sdk.ZeroDec()
    78  }