mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 18:15:19 +00:00
6f193c7f2a
* add test for validate multi reward periods * tidy up: combine files * don't accumulate global indexes containing zeros Previously if the time since last block was 0, indexes were added containing 0s. Now leave them out. Missing is assumed to be 0. * move state independent test to types folder * clarify reward source concept to "source shares" - rename variables and update doc comments - extract method from swap accumulation * tidy up and expand swap accumulation unit tests * rename swap test file to match others * update swap pool id format in tests * refactor borrow accumulation, use new accumulator * refactor supply accumulation, use new accumulator * refactor delegator accumulation, use accumulator * refactor usdx accumulation, use new accumulator * fix types const * remove unsed methods * more usdx minting param validation. Protect against the rewards per second denom changing. It should always be "ukava". * add safety check in InitGenesis It prevents huge accumulations on the first block by limiting all previous accumulation times to be within one year of genesis * add todo for adding swp token distirbution info
196 lines
5.2 KiB
Go
196 lines
5.2 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"
|
|
)
|
|
|
|
// InitializeSwapRewardTests runs unit tests for the keeper.InitializeSwapReward method
|
|
//
|
|
// inputs
|
|
// - claim in store if it exists
|
|
// - global indexes in store
|
|
//
|
|
// outputs
|
|
// - sets or creates a claim
|
|
type InitializeSwapRewardTests struct {
|
|
unitTester
|
|
}
|
|
|
|
func TestInitializeSwapReward(t *testing.T) {
|
|
suite.Run(t, new(InitializeSwapRewardTests))
|
|
}
|
|
|
|
func (suite *InitializeSwapRewardTests) 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
|
|
|
|
poolID := "base:quote"
|
|
|
|
// no global indexes stored as this pool is not rewarded
|
|
|
|
owner := arbitraryAddress()
|
|
|
|
suite.keeper.InitializeSwapReward(suite.ctx, poolID, owner)
|
|
|
|
syncedClaim, found := suite.keeper.GetSwapClaim(suite.ctx, owner)
|
|
suite.True(found)
|
|
// A new claim should have empty indexes. It doesn't strictly need the poolID either.
|
|
expectedIndexes := types.MultiRewardIndexes{{
|
|
CollateralType: poolID,
|
|
RewardIndexes: nil,
|
|
}}
|
|
suite.Equal(expectedIndexes, syncedClaim.RewardIndexes)
|
|
// a new claim should start with 0 rewards
|
|
suite.Equal(sdk.Coins(nil), syncedClaim.Reward)
|
|
}
|
|
|
|
func (suite *InitializeSwapRewardTests) 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
|
|
|
|
poolID := "base:quote"
|
|
|
|
globalIndexes := types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: poolID,
|
|
RewardIndexes: types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom",
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
suite.storeGlobalSwapIndexes(globalIndexes)
|
|
|
|
owner := arbitraryAddress()
|
|
|
|
suite.keeper.InitializeSwapReward(suite.ctx, poolID, owner)
|
|
|
|
syncedClaim, found := suite.keeper.GetSwapClaim(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 *InitializeSwapRewardTests) TestClaimUpdatedWhenClaimExistsAndNoRewards() {
|
|
// When a claim exists, and a user deposits to a new non-rewarded pool;
|
|
// then the claim's rewards don't change
|
|
|
|
preexistingPoolID := "preexisting"
|
|
preexistingIndexes := types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom",
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
}
|
|
|
|
newPoolID := "btcb:usdx"
|
|
|
|
claim := types.SwapClaim{
|
|
BaseMultiClaim: types.BaseMultiClaim{
|
|
Owner: arbitraryAddress(),
|
|
Reward: arbitraryCoins(),
|
|
},
|
|
RewardIndexes: types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: preexistingPoolID,
|
|
RewardIndexes: preexistingIndexes,
|
|
},
|
|
},
|
|
}
|
|
suite.storeSwapClaim(claim)
|
|
|
|
// no global indexes stored as the new pool is not rewarded
|
|
|
|
suite.keeper.InitializeSwapReward(suite.ctx, newPoolID, claim.Owner)
|
|
|
|
syncedClaim, _ := suite.keeper.GetSwapClaim(suite.ctx, claim.Owner)
|
|
// The preexisting indexes shouldn't be changed. It doesn't strictly need the new poolID either.
|
|
expectedIndexes := types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: preexistingPoolID,
|
|
RewardIndexes: preexistingIndexes,
|
|
},
|
|
{
|
|
CollateralType: newPoolID,
|
|
RewardIndexes: nil,
|
|
},
|
|
}
|
|
suite.Equal(expectedIndexes, syncedClaim.RewardIndexes)
|
|
// init should never alter the rewards
|
|
suite.Equal(claim.Reward, syncedClaim.Reward)
|
|
}
|
|
|
|
func (suite *InitializeSwapRewardTests) 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
|
|
|
|
preexistingPoolID := "preexisting"
|
|
preexistingIndexes := types.RewardIndexes{
|
|
{
|
|
CollateralType: "rewarddenom",
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
}
|
|
|
|
newPoolID := "btcb:usdx"
|
|
newIndexes := types.RewardIndexes{
|
|
{
|
|
CollateralType: "otherrewarddenom",
|
|
RewardFactor: d("1000.001"),
|
|
},
|
|
}
|
|
|
|
claim := types.SwapClaim{
|
|
BaseMultiClaim: types.BaseMultiClaim{
|
|
Owner: arbitraryAddress(),
|
|
Reward: arbitraryCoins(),
|
|
},
|
|
RewardIndexes: types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: preexistingPoolID,
|
|
RewardIndexes: preexistingIndexes,
|
|
},
|
|
},
|
|
}
|
|
suite.storeSwapClaim(claim)
|
|
|
|
globalIndexes := types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: preexistingPoolID,
|
|
RewardIndexes: increaseRewardFactors(preexistingIndexes),
|
|
},
|
|
{
|
|
CollateralType: newPoolID,
|
|
RewardIndexes: newIndexes,
|
|
},
|
|
}
|
|
suite.storeGlobalSwapIndexes(globalIndexes)
|
|
|
|
suite.keeper.InitializeSwapReward(suite.ctx, newPoolID, claim.Owner)
|
|
|
|
syncedClaim, _ := suite.keeper.GetSwapClaim(suite.ctx, claim.Owner)
|
|
// only the indexes for the new pool should be updated
|
|
expectedIndexes := types.MultiRewardIndexes{
|
|
{
|
|
CollateralType: preexistingPoolID,
|
|
RewardIndexes: preexistingIndexes,
|
|
},
|
|
{
|
|
CollateralType: newPoolID,
|
|
RewardIndexes: newIndexes,
|
|
},
|
|
}
|
|
suite.Equal(expectedIndexes, syncedClaim.RewardIndexes)
|
|
// init should never alter the rewards
|
|
suite.Equal(claim.Reward, syncedClaim.Reward)
|
|
}
|