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

     1  package keeper
     2  
     3  import (
     4  	"github.com/Finschia/ostracon/libs/log"
     5  
     6  	"github.com/Finschia/finschia-sdk/codec"
     7  	sdk "github.com/Finschia/finschia-sdk/types"
     8  	"github.com/Finschia/finschia-sdk/x/mint/types"
     9  	paramtypes "github.com/Finschia/finschia-sdk/x/params/types"
    10  )
    11  
    12  // Keeper of the mint store
    13  type Keeper struct {
    14  	cdc              codec.BinaryCodec
    15  	storeKey         sdk.StoreKey
    16  	paramSpace       paramtypes.Subspace
    17  	stakingKeeper    types.StakingKeeper
    18  	bankKeeper       types.BankKeeper
    19  	feeCollectorName string
    20  }
    21  
    22  // NewKeeper creates a new mint Keeper instance
    23  func NewKeeper(
    24  	cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
    25  	sk types.StakingKeeper, ak types.AccountKeeper, bk types.BankKeeper,
    26  	feeCollectorName string,
    27  ) Keeper {
    28  	// ensure mint module account is set
    29  	if addr := ak.GetModuleAddress(types.ModuleName); addr.Empty() {
    30  		panic("the mint module account has not been set")
    31  	}
    32  
    33  	// set KeyTable if it has not already been set
    34  	if !paramSpace.HasKeyTable() {
    35  		paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
    36  	}
    37  
    38  	return Keeper{
    39  		cdc:              cdc,
    40  		storeKey:         key,
    41  		paramSpace:       paramSpace,
    42  		stakingKeeper:    sk,
    43  		bankKeeper:       bk,
    44  		feeCollectorName: feeCollectorName,
    45  	}
    46  }
    47  
    48  // Logger returns a module-specific logger.
    49  func (k Keeper) Logger(ctx sdk.Context) log.Logger {
    50  	return ctx.Logger().With("module", "x/"+types.ModuleName)
    51  }
    52  
    53  // get the minter
    54  func (k Keeper) GetMinter(ctx sdk.Context) (minter types.Minter) {
    55  	store := ctx.KVStore(k.storeKey)
    56  	b := store.Get(types.MinterKey)
    57  	if b == nil {
    58  		panic("stored minter should not have been nil")
    59  	}
    60  
    61  	k.cdc.MustUnmarshal(b, &minter)
    62  	return
    63  }
    64  
    65  // set the minter
    66  func (k Keeper) SetMinter(ctx sdk.Context, minter types.Minter) {
    67  	store := ctx.KVStore(k.storeKey)
    68  	b := k.cdc.MustMarshal(&minter)
    69  	store.Set(types.MinterKey, b)
    70  }
    71  
    72  // GetParams returns the total set of minting parameters.
    73  func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
    74  	k.paramSpace.GetParamSet(ctx, &params)
    75  	return params
    76  }
    77  
    78  // SetParams sets the total set of minting parameters.
    79  func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
    80  	k.paramSpace.SetParamSet(ctx, &params)
    81  }
    82  
    83  // StakingTokenSupply implements an alias call to the underlying staking keeper's
    84  // StakingTokenSupply to be used in BeginBlocker.
    85  func (k Keeper) StakingTokenSupply(ctx sdk.Context) sdk.Int {
    86  	return k.stakingKeeper.StakingTokenSupply(ctx)
    87  }
    88  
    89  // BondedRatio implements an alias call to the underlying staking keeper's
    90  // BondedRatio to be used in BeginBlocker.
    91  func (k Keeper) BondedRatio(ctx sdk.Context) sdk.Dec {
    92  	return k.stakingKeeper.BondedRatio(ctx)
    93  }
    94  
    95  // MintCoins implements an alias call to the underlying supply keeper's
    96  // MintCoins to be used in BeginBlocker.
    97  func (k Keeper) MintCoins(ctx sdk.Context, newCoins sdk.Coins) error {
    98  	if newCoins.Empty() {
    99  		// skip as no coins need to be minted
   100  		return nil
   101  	}
   102  
   103  	return k.bankKeeper.MintCoins(ctx, types.ModuleName, newCoins)
   104  }
   105  
   106  // AddCollectedFees implements an alias call to the underlying supply keeper's
   107  // AddCollectedFees to be used in BeginBlocker.
   108  func (k Keeper) AddCollectedFees(ctx sdk.Context, fees sdk.Coins) error {
   109  	return k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.feeCollectorName, fees)
   110  }