0g-chain/x/incentive/keeper/rewards_earn_init_test.go
Derrick Lee 26a4b93588
Add base earn incentives (#1292)
* Add incentive types

* Add earn state methods

* Update earn incentives

* Update unit test NewKeeper to include earn keeper

* Fix sharesOwned typo

* Shares not used for AfterVaultDepositCreated

* Add earn to init genesis

* Pass earn keeper to incentive keeper

* Add sdk.Msg impl, disable legacy migrations

* Disable migrations

* Update incentive earn sync test

* Add earn to client and querier

* Add accum and init tests

* Add additional earn tests

* Fill in fakeEarnKeeper methods to fix tests

* Add earn reward periods to params, begin blocker earn reward

* Add earn to query

* Update genesis_test with missing DefaultMultiRewardPeriods parameter

* Remove disable of migration package

* Fix proto type comments

* Remove unused migration state

* Add legacytx.LegacyMsg compile time interface check

* add earn state validation to genesis validation

Co-authored-by: rhuairahrighairigh <ruaridh.odonnell@gmail.com>
2022-09-22 11:26:08 -07:00

196 lines
5.3 KiB
Go

package keeper_test
import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
"github.com/kava-labs/kava/x/incentive/types"
)
// InitializeEarnRewardTests runs unit tests for the keeper.InitializeEarnReward method
//
// inputs
// - claim in store if it exists
// - global indexes in store
//
// outputs
// - sets or creates a claim
type InitializeEarnRewardTests struct {
unitTester
}
func TestInitializeEarnReward(t *testing.T) {
suite.Run(t, new(InitializeEarnRewardTests))
}
func (suite *InitializeEarnRewardTests) TestClaimAddedWhenClaimDoesNotExistAndNoRewards() {
// When a claim doesn't exist, and a user deposits to a non-rewarded pool;
// then a claim is added with no rewards and no indexes
vaultDenom := "usdx"
// no global indexes stored as this pool is not rewarded
owner := arbitraryAddress()
suite.keeper.InitializeEarnReward(suite.ctx, vaultDenom, owner)
syncedClaim, found := suite.keeper.GetEarnClaim(suite.ctx, owner)
suite.True(found)
// A new claim should have empty indexes. It doesn't strictly need the vaultDenom either.
expectedIndexes := types.MultiRewardIndexes{{
CollateralType: vaultDenom,
RewardIndexes: nil,
}}
suite.Equal(expectedIndexes, syncedClaim.RewardIndexes)
// a new claim should start with 0 rewards
suite.Equal(sdk.Coins(nil), syncedClaim.Reward)
}
func (suite *InitializeEarnRewardTests) TestClaimAddedWhenClaimDoesNotExistAndRewardsExist() {
// When a claim doesn't exist, and a user deposits to a rewarded pool;
// then a claim is added with no rewards and indexes matching the global indexes
vaultDenom := "usdx"
globalIndexes := types.MultiRewardIndexes{
{
CollateralType: vaultDenom,
RewardIndexes: types.RewardIndexes{
{
CollateralType: "rewarddenom",
RewardFactor: d("1000.001"),
},
},
},
}
suite.storeGlobalEarnIndexes(globalIndexes)
owner := arbitraryAddress()
suite.keeper.InitializeEarnReward(suite.ctx, vaultDenom, owner)
syncedClaim, found := suite.keeper.GetEarnClaim(suite.ctx, owner)
suite.True(found)
// a new claim should start with the current global indexes
suite.Equal(globalIndexes, syncedClaim.RewardIndexes)
// a new claim should start with 0 rewards
suite.Equal(sdk.Coins(nil), syncedClaim.Reward)
}
func (suite *InitializeEarnRewardTests) TestClaimUpdatedWhenClaimExistsAndNoRewards() {
// When a claim exists, and a user deposits to a new non-rewarded pool;
// then the claim's rewards don't change
preexistingvaultDenom := "preexisting"
preexistingIndexes := types.RewardIndexes{
{
CollateralType: "rewarddenom",
RewardFactor: d("1000.001"),
},
}
newVaultDenom := "btcb:usdx"
claim := types.EarnClaim{
BaseMultiClaim: types.BaseMultiClaim{
Owner: arbitraryAddress(),
Reward: arbitraryCoins(),
},
RewardIndexes: types.MultiRewardIndexes{
{
CollateralType: preexistingvaultDenom,
RewardIndexes: preexistingIndexes,
},
},
}
suite.storeEarnClaim(claim)
// no global indexes stored as the new pool is not rewarded
suite.keeper.InitializeEarnReward(suite.ctx, newVaultDenom, claim.Owner)
syncedClaim, _ := suite.keeper.GetEarnClaim(suite.ctx, claim.Owner)
// The preexisting indexes shouldn't be changed. It doesn't strictly need the new vaultDenom either.
expectedIndexes := types.MultiRewardIndexes{
{
CollateralType: preexistingvaultDenom,
RewardIndexes: preexistingIndexes,
},
{
CollateralType: newVaultDenom,
RewardIndexes: nil,
},
}
suite.Equal(expectedIndexes, syncedClaim.RewardIndexes)
// init should never alter the rewards
suite.Equal(claim.Reward, syncedClaim.Reward)
}
func (suite *InitializeEarnRewardTests) TestClaimUpdatedWhenClaimExistsAndRewardsExist() {
// When a claim exists, and a user deposits to a new rewarded pool;
// then the claim's rewards don't change and the indexes are updated to match the global indexes
preexistingvaultDenom := "preexisting"
preexistingIndexes := types.RewardIndexes{
{
CollateralType: "rewarddenom",
RewardFactor: d("1000.001"),
},
}
newVaultDenom := "btcb:usdx"
newIndexes := types.RewardIndexes{
{
CollateralType: "otherrewarddenom",
RewardFactor: d("1000.001"),
},
}
claim := types.EarnClaim{
BaseMultiClaim: types.BaseMultiClaim{
Owner: arbitraryAddress(),
Reward: arbitraryCoins(),
},
RewardIndexes: types.MultiRewardIndexes{
{
CollateralType: preexistingvaultDenom,
RewardIndexes: preexistingIndexes,
},
},
}
suite.storeEarnClaim(claim)
globalIndexes := types.MultiRewardIndexes{
{
CollateralType: preexistingvaultDenom,
RewardIndexes: increaseRewardFactors(preexistingIndexes),
},
{
CollateralType: newVaultDenom,
RewardIndexes: newIndexes,
},
}
suite.storeGlobalEarnIndexes(globalIndexes)
suite.keeper.InitializeEarnReward(suite.ctx, newVaultDenom, claim.Owner)
syncedClaim, _ := suite.keeper.GetEarnClaim(suite.ctx, claim.Owner)
// only the indexes for the new pool should be updated
expectedIndexes := types.MultiRewardIndexes{
{
CollateralType: preexistingvaultDenom,
RewardIndexes: preexistingIndexes,
},
{
CollateralType: newVaultDenom,
RewardIndexes: newIndexes,
},
}
suite.Equal(expectedIndexes, syncedClaim.RewardIndexes)
// init should never alter the rewards
suite.Equal(claim.Reward, syncedClaim.Reward)
}