mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
cf009647e6
* Add accumulators * Move accumulator back to keeper package * Add earn specific accumulators * Move store methods to sub-package * Move earn accumulator * Rename accumulator files * Add store doc comment * Add earn accumulator tests, panic if accumulator not used with earn claim type * Update earn accumulator tests to use new methods * Add staking test for earn accumulator * Add test for accumulator proportional rewards * Remove old copy of GetProportionalRewardsPerSecond * Add test for basic accumulator * Fix AddIncentiveMultiRewardPeriod replacement * Deduplicate base earn reward accumulator * Check errors in tests * Validate RewardPeriods in Params.Validate() * Use adapter to fetch earn total shares
473 lines
13 KiB
Go
473 lines
13 KiB
Go
package keeper_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
|
)
|
|
|
|
// SynchronizeClaimTests runs unit tests for the keeper.SynchronizeClaim method
|
|
//
|
|
// inputs
|
|
// - claim in store (only claim.RewardIndexes, claim.Reward)
|
|
// - global indexes in store
|
|
// - shares function arg
|
|
//
|
|
// outputs
|
|
// - sets a claim
|
|
type SynchronizeClaimTests struct {
|
|
unitTester
|
|
}
|
|
|
|
func TestSynchronizeClaim(t *testing.T) {
|
|
suite.Run(t, new(SynchronizeClaimTests))
|
|
}
|
|
|
|
func (suite *SynchronizeClaimTests) TestClaimUpdatedWhenGlobalIndexesHaveIncreased() {
|
|
// This is the normal case
|
|
// Given some time has passed (meaning the global indexes have increased)
|
|
// When the claim is synced
|
|
// The user earns rewards for the time passed, and the claim indexes are updated
|
|
|
|
originalReward := arbitraryCoins()
|
|
collateralType := "base:quote"
|
|
claimType := types.CLAIM_TYPE_USDX_MINTING
|
|
|
|
claim := types.Claim{
|
|
Type: claimType,
|
|
Owner: arbitraryAddress(),
|
|
Reward: originalReward,
|
|
RewardIndexes: types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom",
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.keeper.Store.SetClaim(suite.ctx, claim)
|
|
|
|
globalIndexes := types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom",
|
|
RewardFactor: d("2000.002"),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.storeGlobalIndexes(claimType, globalIndexes)
|
|
|
|
userShares := i(1e9)
|
|
|
|
suite.keeper.SynchronizeClaim(suite.ctx, claimType, collateralType, claim.Owner, userShares.ToDec())
|
|
|
|
syncedClaim, _ := suite.keeper.Store.GetClaim(suite.ctx, claimType, claim.Owner)
|
|
// indexes updated from global
|
|
suite.Equal(globalIndexes, syncedClaim.RewardIndexes)
|
|
// new reward is (new index - old index) * user shares
|
|
suite.Equal(
|
|
cs(c("rewarddenom", 1_000_001_000_000)).Add(originalReward...),
|
|
syncedClaim.Reward,
|
|
)
|
|
}
|
|
|
|
func (suite *SynchronizeClaimTests) TestClaimUnchangedWhenGlobalIndexesUnchanged() {
|
|
// It should be safe to call SynchronizeClaim multiple times
|
|
|
|
collateralType := "base:quote"
|
|
claimType := types.CLAIM_TYPE_USDX_MINTING
|
|
|
|
unchangingIndexes := types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom",
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
claim := types.Claim{
|
|
Type: claimType,
|
|
Owner: arbitraryAddress(),
|
|
Reward: arbitraryCoins(),
|
|
RewardIndexes: unchangingIndexes,
|
|
}
|
|
suite.keeper.Store.SetClaim(suite.ctx, claim)
|
|
|
|
suite.storeGlobalIndexes(claimType, unchangingIndexes)
|
|
|
|
userShares := i(1e9)
|
|
|
|
suite.keeper.SynchronizeClaim(suite.ctx, claimType, collateralType, claim.Owner, userShares.ToDec())
|
|
|
|
syncedClaim, _ := suite.keeper.Store.GetClaim(suite.ctx, claimType, claim.Owner)
|
|
// claim should have the same rewards and indexes as before
|
|
suite.Equal(claim, syncedClaim)
|
|
}
|
|
|
|
func (suite *SynchronizeClaimTests) TestClaimUpdatedWhenNewRewardAdded() {
|
|
// When a new reward is added (via gov) for a pool the user has already deposited to, and the claim is synced;
|
|
// Then the user earns rewards for the time since the reward was added, and the indexes are added to the claim.
|
|
|
|
originalReward := arbitraryCoins()
|
|
newlyRewardcollateralType := "newlyRewardedPool"
|
|
claimType := types.CLAIM_TYPE_USDX_MINTING
|
|
|
|
claim := types.Claim{
|
|
Type: claimType,
|
|
Owner: arbitraryAddress(),
|
|
Reward: originalReward,
|
|
RewardIndexes: types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: "currentlyRewardedPool",
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "reward",
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.keeper.Store.SetClaim(suite.ctx, claim)
|
|
|
|
globalIndexes := types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: "currentlyRewardedPool",
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "reward",
|
|
RewardFactor: d("2000.002"),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
CollateralType: newlyRewardcollateralType,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "otherreward",
|
|
// Indexes start at 0 when the reward is added by gov,
|
|
// so this represents the syncing happening some time later.
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.storeGlobalIndexes(claimType, globalIndexes)
|
|
|
|
userShares := i(1e9)
|
|
|
|
suite.keeper.SynchronizeClaim(suite.ctx, claimType, newlyRewardcollateralType, claim.Owner, userShares.ToDec())
|
|
|
|
syncedClaim, _ := suite.keeper.Store.GetClaim(suite.ctx, claimType, claim.Owner)
|
|
// the new indexes should be added to the claim, but the old ones should be unchanged
|
|
newlyRewrdedIndexes, _ := globalIndexes.Get(newlyRewardcollateralType)
|
|
expectedIndexes := claim.RewardIndexes.With(newlyRewardcollateralType, newlyRewrdedIndexes)
|
|
suite.Equal(expectedIndexes, syncedClaim.RewardIndexes)
|
|
// new reward is (new index - old index) * shares for the synced pool
|
|
// The old index for `newlyrewarded` isn't in the claim, so it's added starting at 0 for calculating the reward.
|
|
suite.Equal(
|
|
cs(c("otherreward", 1_000_001_000_000)).Add(originalReward...),
|
|
syncedClaim.Reward,
|
|
)
|
|
}
|
|
|
|
func (suite *SynchronizeClaimTests) TestClaimUnchangedWhenNoReward() {
|
|
// When a pool is not rewarded but the user has deposited to that pool, and the claim is synced;
|
|
// Then the claim should be the same.
|
|
|
|
collateralType := "nonRewardPool"
|
|
claimType := types.CLAIM_TYPE_USDX_MINTING
|
|
|
|
claim := types.Claim{
|
|
Type: claimType,
|
|
Owner: arbitraryAddress(),
|
|
Reward: arbitraryCoins(),
|
|
RewardIndexes: nonEmptyMultiRewardIndexes,
|
|
}
|
|
suite.keeper.Store.SetClaim(suite.ctx, claim)
|
|
|
|
// No global indexes stored as this pool is not rewarded
|
|
|
|
userShares := i(1e9)
|
|
|
|
suite.keeper.SynchronizeClaim(suite.ctx, claimType, collateralType, claim.Owner, userShares.ToDec())
|
|
|
|
syncedClaim, _ := suite.keeper.Store.GetClaim(suite.ctx, claimType, claim.Owner)
|
|
suite.Equal(claim, syncedClaim)
|
|
}
|
|
|
|
func (suite *SynchronizeClaimTests) TestClaimUpdatedWhenNewRewardDenomAdded() {
|
|
// When a new reward coin is added (via gov) to an already rewarded pool (that the user has already deposited to), and the claim is synced;
|
|
// Then the user earns rewards for the time since the reward was added, and the new indexes are added.
|
|
|
|
originalReward := arbitraryCoins()
|
|
collateralType := "base:quote"
|
|
claimType := types.CLAIM_TYPE_USDX_MINTING
|
|
|
|
claim := types.Claim{
|
|
Type: claimType,
|
|
Owner: arbitraryAddress(),
|
|
Reward: originalReward,
|
|
RewardIndexes: types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "reward",
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.keeper.Store.SetClaim(suite.ctx, claim)
|
|
|
|
globalIndexes := types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "reward",
|
|
RewardFactor: d("2000.002"),
|
|
},
|
|
{
|
|
CollateralType: "otherreward",
|
|
// Indexes start at 0 when the reward is added by gov,
|
|
// so this represents the syncing happening some time later.
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.storeGlobalIndexes(claimType, globalIndexes)
|
|
|
|
userShares := i(1e9)
|
|
|
|
suite.keeper.SynchronizeClaim(suite.ctx, claimType, collateralType, claim.Owner, userShares.ToDec())
|
|
|
|
syncedClaim, _ := suite.keeper.Store.GetClaim(suite.ctx, claimType, claim.Owner)
|
|
// indexes should have the new reward denom added
|
|
suite.Equal(globalIndexes, syncedClaim.RewardIndexes)
|
|
// new reward is (new index - old index) * shares
|
|
// The old index for `otherreward` isn't in the claim, so it's added starting at 0 for calculating the reward.
|
|
suite.Equal(
|
|
cs(c("reward", 1_000_001_000_000), c("otherreward", 1_000_001_000_000)).Add(originalReward...),
|
|
syncedClaim.Reward,
|
|
)
|
|
}
|
|
|
|
func (suite *SynchronizeClaimTests) TestClaimUpdatedWhenGlobalIndexesIncreasedAndSourceIsZero() {
|
|
// Given some time has passed (meaning the global indexes have increased)
|
|
// When the claim is synced, but the user has no shares
|
|
// The user earns no rewards for the time passed, but the claim indexes are updated
|
|
|
|
collateralType := "base:quote"
|
|
claimType := types.CLAIM_TYPE_USDX_MINTING
|
|
|
|
claim := types.Claim{
|
|
Type: claimType,
|
|
Owner: arbitraryAddress(),
|
|
Reward: arbitraryCoins(),
|
|
RewardIndexes: types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom",
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.keeper.Store.SetClaim(suite.ctx, claim)
|
|
|
|
globalIndexes := types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom",
|
|
RewardFactor: d("2000.002"),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.storeGlobalIndexes(claimType, globalIndexes)
|
|
|
|
userShares := i(0)
|
|
|
|
suite.keeper.SynchronizeClaim(suite.ctx, claimType, collateralType, claim.Owner, userShares.ToDec())
|
|
|
|
syncedClaim, _ := suite.keeper.Store.GetClaim(suite.ctx, claimType, claim.Owner)
|
|
// indexes updated from global
|
|
suite.Equal(globalIndexes, syncedClaim.RewardIndexes)
|
|
// reward is unchanged
|
|
suite.Equal(claim.Reward, syncedClaim.Reward)
|
|
}
|
|
|
|
func (suite *SynchronizeClaimTests) TestGetSyncedClaim_ClaimUnchangedWhenNoGlobalIndexes() {
|
|
collateralType_1 := "btcb:usdx"
|
|
owner := arbitraryAddress()
|
|
claimType := types.CLAIM_TYPE_SWAP
|
|
|
|
swapKeeper := newFakeSwapKeeper().
|
|
addDeposit(collateralType_1, owner, i(1e9))
|
|
suite.keeper = suite.NewKeeper(&fakeParamSubspace{}, nil, nil, nil, nil, nil, swapKeeper, nil, nil, nil)
|
|
|
|
claim := types.Claim{
|
|
Type: claimType,
|
|
Owner: owner,
|
|
Reward: nil,
|
|
RewardIndexes: types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType_1,
|
|
RewardIndexes: nil, // this state only happens because Init stores empty indexes
|
|
},
|
|
},
|
|
}
|
|
suite.keeper.Store.SetClaim(suite.ctx, claim)
|
|
|
|
// no global indexes for any pool
|
|
|
|
syncedClaim, f := suite.keeper.GetSynchronizedClaim(suite.ctx, claimType, claim.Owner)
|
|
suite.True(f)
|
|
|
|
// indexes are unchanged
|
|
suite.Equal(claim.RewardIndexes, syncedClaim.RewardIndexes)
|
|
// reward is unchanged
|
|
suite.Equal(claim.Reward, syncedClaim.Reward)
|
|
}
|
|
|
|
func (suite *SynchronizeClaimTests) TestGetSyncedClaim_ClaimUpdatedWhenMissingIndexAndHasNoSourceShares() {
|
|
collateralType_1 := "btcb:usdx"
|
|
collateralType_2 := "ukava:usdx"
|
|
owner := arbitraryAddress()
|
|
claimType := types.CLAIM_TYPE_SWAP
|
|
|
|
// owner has no shares in any pool
|
|
suite.keeper = suite.NewKeeper(&fakeParamSubspace{}, nil, nil, nil, nil, nil, newFakeSwapKeeper(), nil, nil, nil)
|
|
|
|
claim := types.Claim{
|
|
Type: claimType,
|
|
Owner: owner,
|
|
Reward: arbitraryCoins(),
|
|
RewardIndexes: types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType_1,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom1",
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.keeper.Store.SetClaim(suite.ctx, claim)
|
|
|
|
globalIndexes := types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType_1,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom1",
|
|
RewardFactor: d("2000.002"),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
CollateralType: collateralType_2,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom2",
|
|
RewardFactor: d("2000.002"),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.storeGlobalIndexes(claimType, globalIndexes)
|
|
|
|
syncedClaim, f := suite.keeper.GetSynchronizedClaim(suite.ctx, claimType, claim.Owner)
|
|
suite.True(f)
|
|
|
|
// indexes updated from global
|
|
suite.Equal(globalIndexes, syncedClaim.RewardIndexes)
|
|
// reward is unchanged
|
|
suite.Equal(claim.Reward, syncedClaim.Reward)
|
|
}
|
|
|
|
func (suite *SynchronizeClaimTests) TestGetSyncedClaim_ClaimUpdatedWhenMissingIndexButHasSourceShares() {
|
|
collateralType_1 := "btcb:usdx"
|
|
collateralType_2 := "ukava:usdx"
|
|
owner := arbitraryAddress()
|
|
claimType := types.CLAIM_TYPE_SWAP
|
|
|
|
swapKeeper := newFakeSwapKeeper().
|
|
addDeposit(collateralType_1, owner, i(1e9)).
|
|
addDeposit(collateralType_2, owner, i(1e9))
|
|
suite.keeper = suite.NewKeeper(&fakeParamSubspace{}, nil, nil, nil, nil, nil, swapKeeper, nil, nil, nil)
|
|
|
|
claim := types.Claim{
|
|
Type: claimType,
|
|
Owner: owner,
|
|
Reward: arbitraryCoins(),
|
|
RewardIndexes: types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType_1,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom1",
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.keeper.Store.SetClaim(suite.ctx, claim)
|
|
|
|
globalIndexes := types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: collateralType_1,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom1",
|
|
RewardFactor: d("2000.002"),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
CollateralType: collateralType_2,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom2",
|
|
RewardFactor: d("2000.002"),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.storeGlobalIndexes(claimType, globalIndexes)
|
|
|
|
syncedClaim, f := suite.keeper.GetSynchronizedClaim(suite.ctx, claimType, claim.Owner)
|
|
suite.True(f)
|
|
|
|
// indexes updated from global
|
|
suite.Equal(globalIndexes, syncedClaim.RewardIndexes)
|
|
// reward is incremented
|
|
expectedReward := cs(c("rewarddenom1", 1_000_001_000_000), c("rewarddenom2", 2_000_002_000_000))
|
|
suite.Equal(claim.Reward.Add(expectedReward...), syncedClaim.Reward)
|
|
}
|