2021-05-28 13:32:19 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
2021-06-10 13:35:44 +00:00
|
|
|
"testing"
|
2021-05-28 13:32:19 +00:00
|
|
|
"time"
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-05-28 13:32:19 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
"github.com/kava-labs/kava/app"
|
2021-05-28 13:32:19 +00:00
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
|
|
|
)
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
func TestRiskyCDPsAccumulateRewards(t *testing.T) {
|
|
|
|
genesisTime := time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC)
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(5)
|
|
|
|
|
|
|
|
initialCollateral := c("bnb", 1_000_000_000)
|
|
|
|
user := addrs[0]
|
|
|
|
authBuilder := app.NewAuthGenesisBuilder().
|
|
|
|
WithSimpleAccount(user, cs(initialCollateral))
|
2021-05-28 13:32:19 +00:00
|
|
|
|
|
|
|
collateralType := "bnb-a"
|
|
|
|
rewardsPerSecond := c(types.USDXMintingRewardDenom, 1_000_000)
|
2021-06-10 13:35:44 +00:00
|
|
|
|
|
|
|
incentBuilder := NewIncentiveGenesisBuilder().
|
|
|
|
WithGenesisTime(genesisTime).
|
|
|
|
WithSimpleUSDXRewardPeriod(collateralType, rewardsPerSecond)
|
|
|
|
|
|
|
|
tApp := app.NewTestApp()
|
|
|
|
tApp.InitializeFromGenesisStates(
|
|
|
|
authBuilder.BuildMarshalled(),
|
|
|
|
NewPricefeedGenStateMultiFromTime(genesisTime),
|
|
|
|
NewCDPGenStateMulti(),
|
|
|
|
incentBuilder.BuildMarshalled(),
|
2021-05-28 13:32:19 +00:00
|
|
|
)
|
2021-06-10 13:35:44 +00:00
|
|
|
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: genesisTime})
|
2021-05-28 13:32:19 +00:00
|
|
|
|
|
|
|
// Setup cdp state containing one CDP
|
2021-06-10 13:35:44 +00:00
|
|
|
cdpKeeper := tApp.GetCDPKeeper()
|
|
|
|
err := cdpKeeper.AddCdp(ctx, user, initialCollateral, c("usdx", 100_000_000), collateralType)
|
|
|
|
require.NoError(t, err)
|
2021-05-28 13:32:19 +00:00
|
|
|
|
|
|
|
// Skip ahead two blocks to accumulate both interest and usdx reward for the cdp
|
|
|
|
// Two blocks are required because the cdp begin blocker runs before incentive begin blocker.
|
|
|
|
// In the first begin block the cdp is synced, which triggers its claim to sync. But no global rewards have accumulated yet so the sync does nothing.
|
|
|
|
// Global rewards accumulate immediately after during the incentive begin blocker.
|
|
|
|
// Rewards are added to the cdp's claim in the next block when the cdp is synced.
|
2021-06-10 13:35:44 +00:00
|
|
|
_ = tApp.EndBlocker(ctx, abci.RequestEndBlock{})
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(10 * time.Minute))
|
|
|
|
_ = tApp.BeginBlocker(ctx, abci.RequestBeginBlock{}) // height and time in header are ignored by module begin blockers
|
2021-05-28 13:32:19 +00:00
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
_ = tApp.EndBlocker(ctx, abci.RequestEndBlock{})
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(10 * time.Minute))
|
|
|
|
_ = tApp.BeginBlocker(ctx, abci.RequestBeginBlock{})
|
2021-05-28 13:32:19 +00:00
|
|
|
|
|
|
|
// check cdp rewards
|
2021-06-10 13:35:44 +00:00
|
|
|
cdp, found := cdpKeeper.GetCdpByOwnerAndCollateralType(ctx, user, collateralType)
|
|
|
|
require.True(t, found)
|
2021-05-28 13:32:19 +00:00
|
|
|
// This additional sync adds the rewards accumulated at the end of the last begin block.
|
|
|
|
// They weren't added during the begin blocker as the incentive BB runs after the CDP BB.
|
2021-06-10 13:35:44 +00:00
|
|
|
incentiveKeeper := tApp.GetIncentiveKeeper()
|
|
|
|
incentiveKeeper.SynchronizeUSDXMintingReward(ctx, cdp)
|
|
|
|
claim, found := incentiveKeeper.GetUSDXMintingClaim(ctx, user)
|
|
|
|
require.True(t, found)
|
2021-05-28 13:32:19 +00:00
|
|
|
|
|
|
|
// rewards are roughly rewardsPerSecond * secondsElapsed (10mins) * num blocks (2)
|
2021-06-10 13:35:44 +00:00
|
|
|
require.Equal(t, c(types.USDXMintingRewardDenom, 1_200_000_557), claim.Reward)
|
2021-05-28 13:32:19 +00:00
|
|
|
}
|