0g-chain/x/kavamint/keeper/keeper.go

185 lines
5.9 KiB
Go
Raw Normal View History

add x/kavamint & x/community modules (#1400) * add `x/kavamint` module (#1376) * stub out the x/kavamint module * mint staking rewards in x/kavamint BeginBlocker * calculate cumulative inflation from x/kavamint * cleanup, abstract bond denom, grpc query tests * store & use previous block time to calculate mints * move test suite to testutil * add test of x/kavamint BeginBlocker * cleanup & comments from @DracoLi review * fix x/incentive integration test * remove rest client grpc test * add x/kavamint module spec * track previous block time in genesis * suggested cleanup from PR review * bugfix incorrect module account check * add x/community module (#1388) * add x/community module * refactor and setup fund-community-pool tx * remove unused features (params, genesis state) * test x/community query and message * update with master branch deps * updates from review * indicate deprecated methods on module (still needed to fulfill interface) * remove legacy querier * derive msg Route() and Type() from sdk.MsgTypeURL(&msg) * use module name for message Route() * mint community pool inflation in x/kavamint (#1389) * mint community pool inflation in x/kavamint * refactor x/kavamint abci inflation test * test inflation from x/kavamint * default kavamint rates to 0% * rename kavamint account name variables * panic if genesis kavamint previous block time not set * emit event on kavamint begin blocker * add kavamint year of inflation sanity check test * update fund_community_pool message event * add x/community spec * refactor inflation accumulation into single func * use new comm pool in kavadist multispend proposal (#1403) the x/community module houses the new community pool. this commit points the CommunityPoolMultiSpendProposal at the new community pool so that multispend proposals can continue to be processed once original fee pool funds are move to x/community * updates from proto lint changes * update earn proposals to use new community pool originally the CommunityPoolDepositProposal & CommunityPoolWithdrawProposal used the vanilla community pool fee pool. this commit updates them both to use the new x/community module account. * spec formatting update * add x/community & x/kavamint to internal testnet genesis * increase test coverage of x/kavamint * increase test coverage of x/community * use string type for sdk.Dec proto fields; disable generation of getters for genesis and params (and other store objects) * follow sdk conventions for params endpoint naming * add kavamint to swagger; update swagger & proto docs * remove legacy querier; touch up module to match others * reduce max minting rate; refactor genesis to allow nil/zero block time; repace proto stringers and remove getters; keep sdk.Dec values strings in protos; increase validation of sdk.Dec values in params; increase coverage; add module account permission checks; ensure import and export of genesis does not change state * fix scaling of rate values -- should be yearly rates * revert key change * fix typo in test name * fix regression in running tests -- rename method Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
2022-12-09 21:24:35 +00:00
package keeper
import (
"time"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/kava-labs/kava/x/kavamint/types"
)
type KeeperI interface {
GetParams(ctx sdk.Context) (params types.Params)
SetParams(ctx sdk.Context, params types.Params)
BondDenom(ctx sdk.Context) string
TotalSupply(ctx sdk.Context) sdk.Int
TotalBondedTokens(ctx sdk.Context) sdk.Int
MintCoins(ctx sdk.Context, newCoins sdk.Coins) error
AddCollectedFees(ctx sdk.Context, fees sdk.Coins) error
FundCommunityPool(ctx sdk.Context, funds sdk.Coins) error
GetPreviousBlockTime(ctx sdk.Context) (blockTime time.Time)
SetPreviousBlockTime(ctx sdk.Context, blockTime time.Time)
CumulativeInflation(ctx sdk.Context) sdk.Dec
AccumulateInflation(
ctx sdk.Context,
rate sdk.Dec,
basis sdk.Int,
secondsSinceLastMint float64,
) (sdk.Coins, error)
}
// Keeper of the kavamint store
type Keeper struct {
cdc codec.BinaryCodec
storeKey sdk.StoreKey
paramSpace paramtypes.Subspace
stakingKeeper types.StakingKeeper
bankKeeper types.BankKeeper
stakingRewardsFeeCollectorName string
communityPoolModuleAccountName string
}
var _ KeeperI = Keeper{}
// NewKeeper creates a new kavamint Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
sk types.StakingKeeper, ak types.AccountKeeper, bk types.BankKeeper,
stakingRewardsFeeCollectorName string, communityPoolModuleAccountName string,
) Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
cdc: cdc,
storeKey: key,
paramSpace: paramSpace,
stakingKeeper: sk,
bankKeeper: bk,
stakingRewardsFeeCollectorName: stakingRewardsFeeCollectorName,
communityPoolModuleAccountName: communityPoolModuleAccountName,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
// GetParams returns the total set of minting parameters.
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramSpace.GetParamSet(ctx, &params)
return params
}
// SetParams sets the total set of minting parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}
// GetStakingApy returns the APY minted for staking rewards
func (k Keeper) GetStakingApy(ctx sdk.Context) sdk.Dec {
params := k.GetParams(ctx)
return params.StakingRewardsApy
}
add x/kavamint & x/community modules (#1400) * add `x/kavamint` module (#1376) * stub out the x/kavamint module * mint staking rewards in x/kavamint BeginBlocker * calculate cumulative inflation from x/kavamint * cleanup, abstract bond denom, grpc query tests * store & use previous block time to calculate mints * move test suite to testutil * add test of x/kavamint BeginBlocker * cleanup & comments from @DracoLi review * fix x/incentive integration test * remove rest client grpc test * add x/kavamint module spec * track previous block time in genesis * suggested cleanup from PR review * bugfix incorrect module account check * add x/community module (#1388) * add x/community module * refactor and setup fund-community-pool tx * remove unused features (params, genesis state) * test x/community query and message * update with master branch deps * updates from review * indicate deprecated methods on module (still needed to fulfill interface) * remove legacy querier * derive msg Route() and Type() from sdk.MsgTypeURL(&msg) * use module name for message Route() * mint community pool inflation in x/kavamint (#1389) * mint community pool inflation in x/kavamint * refactor x/kavamint abci inflation test * test inflation from x/kavamint * default kavamint rates to 0% * rename kavamint account name variables * panic if genesis kavamint previous block time not set * emit event on kavamint begin blocker * add kavamint year of inflation sanity check test * update fund_community_pool message event * add x/community spec * refactor inflation accumulation into single func * use new comm pool in kavadist multispend proposal (#1403) the x/community module houses the new community pool. this commit points the CommunityPoolMultiSpendProposal at the new community pool so that multispend proposals can continue to be processed once original fee pool funds are move to x/community * updates from proto lint changes * update earn proposals to use new community pool originally the CommunityPoolDepositProposal & CommunityPoolWithdrawProposal used the vanilla community pool fee pool. this commit updates them both to use the new x/community module account. * spec formatting update * add x/community & x/kavamint to internal testnet genesis * increase test coverage of x/kavamint * increase test coverage of x/community * use string type for sdk.Dec proto fields; disable generation of getters for genesis and params (and other store objects) * follow sdk conventions for params endpoint naming * add kavamint to swagger; update swagger & proto docs * remove legacy querier; touch up module to match others * reduce max minting rate; refactor genesis to allow nil/zero block time; repace proto stringers and remove getters; keep sdk.Dec values strings in protos; increase validation of sdk.Dec values in params; increase coverage; add module account permission checks; ensure import and export of genesis does not change state * fix scaling of rate values -- should be yearly rates * revert key change * fix typo in test name * fix regression in running tests -- rename method Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
2022-12-09 21:24:35 +00:00
// BondDenom implements an alias call to the underlying staking keeper's BondDenom.
func (k Keeper) BondDenom(ctx sdk.Context) string {
return k.stakingKeeper.BondDenom(ctx)
}
// TotalBondedTokens implements an alias call to the underlying staking keeper's
// TotalBondedTokens to be used in BeginBlocker.
func (k Keeper) TotalBondedTokens(ctx sdk.Context) sdk.Int {
return k.stakingKeeper.TotalBondedTokens(ctx)
}
// MintCoins implements an alias call to the underlying supply keeper's
// MintCoins to be used in BeginBlocker.
func (k Keeper) MintCoins(ctx sdk.Context, newCoins sdk.Coins) error {
if newCoins.Empty() {
// skip as no coins need to be minted
return nil
}
return k.bankKeeper.MintCoins(ctx, types.ModuleName, newCoins)
}
// AddCollectedFees implements an alias call to the underlying supply keeper's
// AddCollectedFees to be used in BeginBlocker.
func (k Keeper) AddCollectedFees(ctx sdk.Context, fees sdk.Coins) error {
return k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.stakingRewardsFeeCollectorName, fees)
}
// FundCommunityPool implements an alias call to the underlying supply keeper's
// FundCommunityPool to be used in BeginBlocker.
func (k Keeper) FundCommunityPool(ctx sdk.Context, funds sdk.Coins) error {
return k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.communityPoolModuleAccountName, funds)
}
// TotalSupply implements an alias call to the underlying supply keeper's
// GetSupply for the mint denom to be used in calculating cumulative inflation.
func (k Keeper) TotalSupply(ctx sdk.Context) sdk.Int {
return k.bankKeeper.GetSupply(ctx, k.BondDenom(ctx)).Amount
}
func (k Keeper) CumulativeInflation(ctx sdk.Context) sdk.Dec {
params := k.GetParams(ctx)
totalInflation := sdk.NewDec(0)
// community pool contribution is simply the inflation param
totalInflation = totalInflation.Add(params.CommunityPoolInflation)
// staking rewards contribution is the apy * bonded_ratio
bondedSupply := k.TotalBondedTokens(ctx)
totalSupply := k.TotalSupply(ctx)
bondedRatio := sdk.NewDecFromInt(bondedSupply).QuoInt(totalSupply)
inflationFromStakingRewards := params.StakingRewardsApy.Mul(bondedRatio)
totalInflation = totalInflation.Add(inflationFromStakingRewards)
return totalInflation
}
// GetPreviousBlockTime get the blocktime for the previous block
func (k Keeper) GetPreviousBlockTime(ctx sdk.Context) (blockTime time.Time) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PreviousBlockTimeKey)
b := store.Get(types.PreviousBlockTimeKey)
if b == nil {
return blockTime
}
if err := blockTime.UnmarshalBinary(b); err != nil {
panic(err)
}
return
}
// SetPreviousBlockTime set the time of the previous block
func (k Keeper) SetPreviousBlockTime(ctx sdk.Context, blockTime time.Time) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PreviousBlockTimeKey)
if blockTime.IsZero() {
store.Delete(types.PreviousBlockTimeKey)
return
}
b, err := blockTime.MarshalBinary()
if err != nil {
panic(err)
}
store.Set(types.PreviousBlockTimeKey, b)
}