mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-29 09:45:18 +00:00
651de460ca
* Add bkava handler for earn incentives * Add bkava accum tests * Add bkava denoms in index state * Set storeTimeEquals to default value * Add supply expected keepers * Add tests for proportional adjustment * Add liquid keeper to incentive keeper * Use weighted reward periods for bkava * Add liquid keeper to tests * Add Accumulate override rewards period with deccoins * Adjust test to handle sub unit coins * Add liquid keeper to test * Fix div by zero for proportional rewards * Update test for actual expected values * Update expected indexes to be same for different vaults * Allow no stored time for vaults that have no indexes or state * Add test for partial bkava deposit * Add math check to test * Deterministically iterate over bkava denoms * Remove unused expected liquid method GetAllDerivativeDenoms
98 lines
3.3 KiB
Go
98 lines
3.3 KiB
Go
package keeper_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
|
)
|
|
|
|
// InitializeDelegatorRewardTests runs unit tests for the keeper.InitializeDelegatorReward method
|
|
//
|
|
// inputs
|
|
// - claim in store if it exists (only claim.DelegatorRewardIndexes)
|
|
// - global indexes in store
|
|
// - delegator function arg
|
|
//
|
|
// outputs
|
|
// - sets or creates a claim
|
|
type InitializeDelegatorRewardTests struct {
|
|
unitTester
|
|
}
|
|
|
|
func TestInitializeDelegatorReward(t *testing.T) {
|
|
suite.Run(t, new(InitializeDelegatorRewardTests))
|
|
}
|
|
|
|
// Hardcoded to use bond denom
|
|
func (suite *InitializeDelegatorRewardTests) storeGlobalDelegatorFactor(multiRewardIndexes types.MultiRewardIndexes) {
|
|
multiRewardIndex, _ := multiRewardIndexes.GetRewardIndex(types.BondDenom)
|
|
suite.keeper.SetDelegatorRewardIndexes(suite.ctx, types.BondDenom, multiRewardIndex.RewardIndexes)
|
|
}
|
|
|
|
func (suite *InitializeDelegatorRewardTests) TestClaimIndexesAreSetWhenClaimDoesNotExist() {
|
|
globalIndex := arbitraryDelegatorRewardIndexes
|
|
suite.storeGlobalDelegatorIndexes(globalIndex)
|
|
|
|
delegator := arbitraryAddress()
|
|
suite.keeper.InitializeDelegatorReward(suite.ctx, delegator)
|
|
|
|
syncedClaim, f := suite.keeper.GetDelegatorClaim(suite.ctx, delegator)
|
|
suite.True(f)
|
|
suite.Equal(globalIndex, syncedClaim.RewardIndexes)
|
|
}
|
|
|
|
func (suite *InitializeDelegatorRewardTests) TestClaimIsSyncedAndIndexesAreSetWhenClaimDoesExist() {
|
|
validatorAddress := arbitraryValidatorAddress()
|
|
sk := &fakeStakingKeeper{
|
|
delegations: stakingtypes.Delegations{{
|
|
ValidatorAddress: validatorAddress.String(),
|
|
Shares: d("1000"),
|
|
}},
|
|
validators: stakingtypes.Validators{{
|
|
OperatorAddress: validatorAddress.String(),
|
|
Status: stakingtypes.Bonded,
|
|
Tokens: i(1000),
|
|
DelegatorShares: d("1000"),
|
|
}},
|
|
}
|
|
suite.keeper = suite.NewKeeper(&fakeParamSubspace{}, nil, nil, nil, nil, sk, nil, nil, nil, nil)
|
|
|
|
claim := types.DelegatorClaim{
|
|
BaseMultiClaim: types.BaseMultiClaim{
|
|
Owner: arbitraryAddress(),
|
|
},
|
|
RewardIndexes: arbitraryDelegatorRewardIndexes,
|
|
}
|
|
suite.storeDelegatorClaim(claim)
|
|
|
|
// Set the global factor to a value different to one in claim so
|
|
// we can detect if it is overwritten.
|
|
rewardIndexes, _ := claim.RewardIndexes.Get(types.BondDenom)
|
|
globalIndexes := increaseRewardFactors(rewardIndexes)
|
|
|
|
// Update the claim object with the new global factor
|
|
bondIndex, _ := claim.RewardIndexes.GetRewardIndexIndex(types.BondDenom)
|
|
claim.RewardIndexes[bondIndex].RewardIndexes = globalIndexes
|
|
suite.storeGlobalDelegatorFactor(claim.RewardIndexes)
|
|
|
|
suite.keeper.InitializeDelegatorReward(suite.ctx, claim.Owner)
|
|
|
|
syncedClaim, _ := suite.keeper.GetDelegatorClaim(suite.ctx, claim.Owner)
|
|
suite.Equal(globalIndexes, syncedClaim.RewardIndexes[bondIndex].RewardIndexes)
|
|
suite.Truef(syncedClaim.Reward.IsAllGT(claim.Reward), "'%s' not greater than '%s'", syncedClaim.Reward, claim.Reward)
|
|
}
|
|
|
|
// arbitraryDelegatorRewardIndexes contains only one reward index as there is only ever one bond denom
|
|
var arbitraryDelegatorRewardIndexes = types.MultiRewardIndexes{
|
|
types.NewMultiRewardIndex(
|
|
types.BondDenom,
|
|
types.RewardIndexes{
|
|
types.NewRewardIndex("hard", d("0.2")),
|
|
types.NewRewardIndex("swp", d("0.2")),
|
|
},
|
|
),
|
|
}
|