0g-chain/x/community/keeper/keeper.go
Nick DeLuca 102cc0fff3
Community Pool Staking Rewards Implementation & Improvements (#1742)
* add new field upgrade_time_set_staking_rewards_per_second with intention
of integrating into the disable inflation logic to set an initial
staking reward time

* when the disable inflation upgrade time occurs, set the staking rewards
per second to the value specified by the new
upgrade_time_set_staking_rewards_per_second.  This will allow a decoupled
implementation between the ugprade switching logic, and the core
functionality of paying staking rewards from the pool

* add staking rewards state to community keeper and community module
genesis that is required to calculate and track staking reward payouts
accross blocks

* add implementation of staking reward payouts

* remove unused error

* touch up tests and add a test case that fully tests behavior when pool
is drained

* add function comments

* refactor and pull out main calculation to private pure function with
no dependence on keeper

* zero out default parameters -- these are too chain specific to have
useful defaults

* small touch ups on comments, test cases

* use correct Int from sdkmath, not old sdk types; update protonet genesis
for new parmater

* fix copy pasta comment

* use bond denom from staking keeper instead of referncing ukava directly

* add staking reward state for valid genesis

* update kvtool genesis for new params and rewards state
2023-10-03 08:41:54 -07:00

117 lines
3.3 KiB
Go

package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/kava-labs/kava/x/community/types"
)
// Keeper of the community store
type Keeper struct {
key storetypes.StoreKey
cdc codec.Codec
bankKeeper types.BankKeeper
cdpKeeper types.CdpKeeper
distrKeeper types.DistributionKeeper
hardKeeper types.HardKeeper
moduleAddress sdk.AccAddress
mintKeeper types.MintKeeper
kavadistKeeper types.KavadistKeeper
stakingKeeper types.StakingKeeper
legacyCommunityPoolAddress sdk.AccAddress
}
// NewKeeper creates a new community Keeper instance
func NewKeeper(
cdc codec.Codec,
key storetypes.StoreKey,
ak types.AccountKeeper,
bk types.BankKeeper,
ck types.CdpKeeper,
dk types.DistributionKeeper,
hk types.HardKeeper,
mk types.MintKeeper,
kk types.KavadistKeeper,
sk types.StakingKeeper,
) Keeper {
// ensure community module account is set
addr := ak.GetModuleAddress(types.ModuleAccountName)
if addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleAccountName))
}
legacyAddr := ak.GetModuleAddress(types.LegacyCommunityPoolModuleName)
if addr == nil {
panic("legacy community pool address not found")
}
return Keeper{
key: key,
cdc: cdc,
bankKeeper: bk,
cdpKeeper: ck,
distrKeeper: dk,
hardKeeper: hk,
mintKeeper: mk,
kavadistKeeper: kk,
stakingKeeper: sk,
moduleAddress: addr,
legacyCommunityPoolAddress: legacyAddr,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
// GetModuleAccountBalance returns all the coins held by the community module account
func (k Keeper) GetModuleAccountBalance(ctx sdk.Context) sdk.Coins {
return k.bankKeeper.GetAllBalances(ctx, k.moduleAddress)
}
// FundCommunityPool transfers coins from the sender to the community module account.
func (k Keeper) FundCommunityPool(ctx sdk.Context, sender sdk.AccAddress, amount sdk.Coins) error {
return k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleAccountName, amount)
}
// DistributeFromCommunityPool transfers coins from the community pool to recipient.
func (k Keeper) DistributeFromCommunityPool(ctx sdk.Context, recipient sdk.AccAddress, amount sdk.Coins) error {
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, recipient, amount)
}
// GetStakingRewardsState returns the staking reward state or the default state if not set
func (k Keeper) GetStakingRewardsState(ctx sdk.Context) types.StakingRewardsState {
store := ctx.KVStore(k.key)
b := store.Get(types.StakingRewardsStateKey)
if b == nil {
return types.DefaultStakingRewardsState()
}
state := types.StakingRewardsState{}
k.cdc.MustUnmarshal(b, &state)
return state
}
// SetStakingRewardsState validates and sets the staking rewards state in the store
func (k Keeper) SetStakingRewardsState(ctx sdk.Context, state types.StakingRewardsState) {
if err := state.Validate(); err != nil {
panic(fmt.Sprintf("invalid state: %s", err))
}
store := ctx.KVStore(k.key)
b := k.cdc.MustMarshal(&state)
store.Set(types.StakingRewardsStateKey, b)
}