2022-11-10 19:05:11 +00:00
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
2022-11-29 22:23:33 +00:00
"github.com/kava-labs/kava/x/incentive/keeper/accumulators"
2022-11-10 19:05:11 +00:00
"github.com/kava-labs/kava/x/incentive/types"
)
2022-11-29 21:59:11 +00:00
// AccumulateRewards calculates new rewards to distribute this block and updates the global indexes to reflect this.
// The provided rewardPeriod must be valid to avoid panics in calculating time durations.
func ( k Keeper ) AccumulateRewards (
ctx sdk . Context ,
claimType types . ClaimType ,
rewardPeriod types . MultiRewardPeriod ,
2022-11-29 22:23:33 +00:00
) error {
var accumulator types . RewardAccumulator
switch claimType {
case types . CLAIM_TYPE_EARN :
accumulator = accumulators . NewEarnAccumulator ( k . Store , k . liquidKeeper , k . earnKeeper , k . Adapters )
default :
accumulator = accumulators . NewBasicAccumulator ( k . Store , k . Adapters )
2022-11-29 21:59:11 +00:00
}
2022-11-29 22:23:33 +00:00
return accumulator . AccumulateRewards ( ctx , claimType , rewardPeriod )
2022-11-29 21:59:11 +00:00
}
2022-11-10 19:05:11 +00:00
// InitializeClaim creates a new claim with zero rewards and indexes matching
// the global indexes. If the claim already exists it just updates the indexes.
func ( k Keeper ) InitializeClaim (
ctx sdk . Context ,
claimType types . ClaimType ,
sourceID string ,
owner sdk . AccAddress ,
) {
2022-11-29 22:23:33 +00:00
claim , found := k . Store . GetClaim ( ctx , claimType , owner )
2022-11-10 19:05:11 +00:00
if ! found {
claim = types . NewClaim ( claimType , owner , sdk . Coins { } , nil )
}
2022-11-29 22:23:33 +00:00
globalRewardIndexes , found := k . Store . GetRewardIndexesOfClaimType ( ctx , claimType , sourceID )
2022-11-10 19:05:11 +00:00
if ! found {
globalRewardIndexes = types . RewardIndexes { }
}
claim . RewardIndexes = claim . RewardIndexes . With ( sourceID , globalRewardIndexes )
2022-11-29 22:23:33 +00:00
k . Store . SetClaim ( ctx , claim )
2022-11-10 19:05:11 +00:00
}
// SynchronizeClaim updates the claim object by adding any accumulated rewards
// and updating the reward index value.
func ( k Keeper ) SynchronizeClaim (
ctx sdk . Context ,
claimType types . ClaimType ,
sourceID string ,
owner sdk . AccAddress ,
2022-11-15 01:55:10 +00:00
shares sdk . Dec ,
2022-11-10 19:05:11 +00:00
) {
2022-11-29 22:23:33 +00:00
claim , found := k . Store . GetClaim ( ctx , claimType , owner )
2022-11-10 19:05:11 +00:00
if ! found {
return
}
claim = k . synchronizeClaim ( ctx , claim , sourceID , owner , shares )
2022-11-29 22:23:33 +00:00
k . Store . SetClaim ( ctx , claim )
2022-11-10 19:05:11 +00:00
}
// synchronizeClaim updates the reward and indexes in a claim for one sourceID.
func ( k * Keeper ) synchronizeClaim (
ctx sdk . Context ,
claim types . Claim ,
sourceID string ,
owner sdk . AccAddress ,
2022-11-15 01:55:10 +00:00
shares sdk . Dec ,
2022-11-10 19:05:11 +00:00
) types . Claim {
2022-11-29 22:23:33 +00:00
globalRewardIndexes , found := k . Store . GetRewardIndexesOfClaimType ( ctx , claim . Type , sourceID )
2022-11-10 19:05:11 +00:00
if ! found {
// The global factor is only not found if
// - the pool has not started accumulating rewards yet (either there is no reward specified in params, or the reward start time hasn't been hit)
// - OR it was wrongly deleted from state (factors should never be removed while unsynced claims exist)
// If not found we could either skip this sync, or assume the global factor is zero.
// Skipping will avoid storing unnecessary factors in the claim for non rewarded pools.
// And in the event a global factor is wrongly deleted, it will avoid this function panicking when calculating rewards.
return claim
}
userRewardIndexes , found := claim . RewardIndexes . Get ( sourceID )
if ! found {
// Normally the reward indexes should always be found.
// But if a pool was not rewarded then becomes rewarded (ie a reward period is added to params), then the indexes will be missing from claims for that pool.
// So given the reward period was just added, assume the starting value for any global reward indexes, which is an empty slice.
userRewardIndexes = types . RewardIndexes { }
}
2022-11-15 01:55:10 +00:00
newRewards , err := k . CalculateRewards ( userRewardIndexes , globalRewardIndexes , shares )
2022-11-10 19:05:11 +00:00
if err != nil {
// Global reward factors should never decrease, as it would lead to a negative update to claim.Rewards.
// This panics if a global reward factor decreases or disappears between the old and new indexes.
panic ( fmt . Sprintf ( "corrupted global reward indexes found: %v" , err ) )
}
claim . Reward = claim . Reward . Add ( newRewards ... )
claim . RewardIndexes = claim . RewardIndexes . With ( sourceID , globalRewardIndexes )
return claim
}
2022-11-15 01:55:10 +00:00
// GetSynchronizedClaim fetches a claim from the store and syncs rewards for all
// rewarded sourceIDs.
func ( k Keeper ) GetSynchronizedClaim (
ctx sdk . Context ,
claimType types . ClaimType ,
owner sdk . AccAddress ,
) ( types . Claim , bool ) {
2022-11-29 22:23:33 +00:00
claim , found := k . Store . GetClaim ( ctx , claimType , owner )
2022-11-15 01:55:10 +00:00
if ! found {
return types . Claim { } , false
}
// Fetch all source IDs from indexes
var sourceIDs [ ] string
2022-11-29 22:23:33 +00:00
k . Store . IterateRewardIndexesByClaimType ( ctx , claimType , func ( rewardIndexes types . TypedRewardIndexes ) bool {
2022-11-15 01:55:10 +00:00
sourceIDs = append ( sourceIDs , rewardIndexes . CollateralType )
return false
} )
2022-11-29 22:23:33 +00:00
accShares := k . Adapters . OwnerSharesBySource ( ctx , claimType , owner , sourceIDs )
2022-11-15 01:55:10 +00:00
// Synchronize claim for each source ID
2022-11-29 21:59:11 +00:00
for _ , share := range accShares {
claim = k . synchronizeClaim ( ctx , claim , share . ID , owner , share . Shares )
2022-11-15 01:55:10 +00:00
}
return claim , true
}