0g-chain/x/community/keeper/keeper_test.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

92 lines
3.2 KiB
Go

package keeper_test
import (
"testing"
"time"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
"github.com/kava-labs/kava/x/community/testutil"
"github.com/kava-labs/kava/x/community/types"
)
// Test suite used for all keeper tests
type KeeperTestSuite struct {
testutil.Suite
}
// The default state used by each test
func (suite *KeeperTestSuite) SetupTest() {
suite.Suite.SetupTest()
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}
func (suite *KeeperTestSuite) TestCommunityPool() {
suite.SetupTest()
maccAddr := suite.App.GetAccountKeeper().GetModuleAddress(types.ModuleAccountName)
funds := sdk.NewCoins(
sdk.NewCoin("ukava", sdkmath.NewInt(10000)),
sdk.NewCoin("usdx", sdkmath.NewInt(100)),
)
sender := suite.CreateFundedAccount(funds)
suite.Run("FundCommunityPool", func() {
err := suite.Keeper.FundCommunityPool(suite.Ctx, sender, funds)
suite.Require().NoError(err)
// check that community pool received balance
suite.App.CheckBalance(suite.T(), suite.Ctx, maccAddr, funds)
suite.Equal(funds, suite.Keeper.GetModuleAccountBalance(suite.Ctx))
// check that sender had balance deducted
suite.App.CheckBalance(suite.T(), suite.Ctx, sender, sdk.NewCoins())
})
// send it back
suite.Run("DistributeFromCommunityPool - valid", func() {
err := suite.Keeper.DistributeFromCommunityPool(suite.Ctx, sender, funds)
suite.Require().NoError(err)
// community pool has funds deducted
suite.App.CheckBalance(suite.T(), suite.Ctx, maccAddr, sdk.NewCoins())
suite.Equal(sdk.NewCoins(), suite.Keeper.GetModuleAccountBalance(suite.Ctx))
// receiver receives the funds
suite.App.CheckBalance(suite.T(), suite.Ctx, sender, funds)
})
// can't send more than we have!
suite.Run("DistributeFromCommunityPool - insufficient funds", func() {
suite.Equal(sdk.NewCoins(), suite.Keeper.GetModuleAccountBalance(suite.Ctx))
err := suite.Keeper.DistributeFromCommunityPool(suite.Ctx, sender, funds)
suite.Require().ErrorContains(err, "insufficient funds")
})
}
func (suite *KeeperTestSuite) TestGetAndSetStakingRewardsState() {
keeper := suite.Keeper
defaultParams := keeper.GetStakingRewardsState(suite.Ctx)
suite.Equal(time.Time{}, defaultParams.LastAccumulationTime, "expected default returned accumulation time to be zero")
suite.Equal(sdkmath.LegacyZeroDec(), defaultParams.LastTruncationError, "expected default truncation error to be zero")
suite.NotPanics(func() { keeper.SetStakingRewardsState(suite.Ctx, defaultParams) }, "expected setting default state to not panic")
invalidParams := defaultParams
invalidParams.LastTruncationError = sdkmath.LegacyDec{}
suite.Panics(func() { keeper.SetStakingRewardsState(suite.Ctx, invalidParams) }, "expected setting invalid state to panic")
validParams := defaultParams
validParams.LastAccumulationTime = time.Date(2023, 9, 29, 11, 42, 53, 123456789, time.UTC)
validParams.LastTruncationError = sdkmath.LegacyMustNewDecFromStr("0.50000000000000000")
suite.NotPanics(func() { keeper.SetStakingRewardsState(suite.Ctx, validParams) }, "expected setting valid state to not panic")
suite.Equal(validParams, keeper.GetStakingRewardsState(suite.Ctx), "expected fetched state to equal set state")
}