2020-04-24 15:20:34 +00:00
package keeper
import (
"time"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
2020-04-30 14:13:31 +00:00
2020-04-24 15:20:34 +00:00
cdptypes "github.com/kava-labs/kava/x/cdp/types"
"github.com/kava-labs/kava/x/incentive/types"
)
// HandleRewardPeriodExpiry deletes expired RewardPeriods from the store and creates a ClaimPeriod in the store for each expired RewardPeriod
func ( k Keeper ) HandleRewardPeriodExpiry ( ctx sdk . Context , rp types . RewardPeriod ) {
2020-08-21 19:42:46 +00:00
k . CreateUniqueClaimPeriod ( ctx , rp . CollateralType , rp . ClaimEnd , rp . ClaimTimeLock )
2020-04-24 15:20:34 +00:00
store := prefix . NewStore ( ctx . KVStore ( k . key ) , types . RewardPeriodKeyPrefix )
2020-08-21 19:42:46 +00:00
store . Delete ( [ ] byte ( rp . CollateralType ) )
2020-04-24 15:20:34 +00:00
return
}
// CreateNewRewardPeriod creates a new reward period from the input reward
func ( k Keeper ) CreateNewRewardPeriod ( ctx sdk . Context , reward types . Reward ) {
2020-05-07 17:46:40 +00:00
rp := types . NewRewardPeriodFromReward ( reward , ctx . BlockTime ( ) )
2020-04-24 15:20:34 +00:00
k . SetRewardPeriod ( ctx , rp )
2020-05-07 17:46:40 +00:00
ctx . EventManager ( ) . EmitEvent (
sdk . NewEvent (
types . EventTypeRewardPeriod ,
sdk . NewAttribute ( types . AttributeKeyRewardPeriod , rp . String ( ) ) ,
) ,
)
2020-04-24 15:20:34 +00:00
}
// CreateAndDeleteRewardPeriods creates reward periods for active rewards that don't already have a reward period and deletes reward periods for inactive rewards that currently have a reward period
func ( k Keeper ) CreateAndDeleteRewardPeriods ( ctx sdk . Context ) {
params := k . GetParams ( ctx )
for _ , r := range params . Rewards {
2020-08-21 19:42:46 +00:00
_ , found := k . GetRewardPeriod ( ctx , r . CollateralType )
2020-04-24 15:20:34 +00:00
// if governance has made a reward inactive, delete the current period
if found && ! r . Active {
2020-08-21 19:42:46 +00:00
k . DeleteRewardPeriod ( ctx , r . CollateralType )
2020-04-24 15:20:34 +00:00
}
// if a reward period for an active reward is not found, create one
if ! found && r . Active {
k . CreateNewRewardPeriod ( ctx , r )
}
}
}
2020-06-17 09:09:44 +00:00
// ApplyRewardsToCdps iterates over the reward periods and creates a claim for each
// cdp owner that created usdx with the collateral specified in the reward period.
2020-04-24 15:20:34 +00:00
func ( k Keeper ) ApplyRewardsToCdps ( ctx sdk . Context ) {
previousBlockTime , found := k . GetPreviousBlockTime ( ctx )
if ! found {
previousBlockTime = ctx . BlockTime ( )
k . SetPreviousBlockTime ( ctx , previousBlockTime )
return
}
2020-06-17 09:09:44 +00:00
2020-04-24 15:20:34 +00:00
k . IterateRewardPeriods ( ctx , func ( rp types . RewardPeriod ) bool {
expired := false
// the total amount of usdx created with the collateral type being incentivized
2020-08-21 19:42:46 +00:00
totalPrincipal := k . cdpKeeper . GetTotalPrincipal ( ctx , rp . CollateralType , types . PrincipalDenom )
2020-04-24 15:20:34 +00:00
// the number of seconds since last payout
timeElapsed := sdk . NewInt ( ctx . BlockTime ( ) . Unix ( ) - previousBlockTime . Unix ( ) )
if rp . End . Before ( ctx . BlockTime ( ) ) {
timeElapsed = sdk . NewInt ( rp . End . Unix ( ) - previousBlockTime . Unix ( ) )
expired = true
}
2020-06-17 09:09:44 +00:00
2020-04-24 15:20:34 +00:00
// the amount of rewards to pay (rewardAmount * timeElapsed)
rewardsThisPeriod := rp . Reward . Amount . Mul ( timeElapsed )
2020-08-21 19:42:46 +00:00
id := k . GetNextClaimPeriodID ( ctx , rp . CollateralType )
k . cdpKeeper . IterateCdpsByCollateralType ( ctx , rp . CollateralType , func ( cdp cdptypes . CDP ) bool {
2020-07-10 13:16:05 +00:00
rewardsShare := sdk . NewDecFromInt ( cdp . GetTotalPrincipal ( ) . Amount ) . Quo ( sdk . NewDecFromInt ( totalPrincipal ) )
2020-04-24 15:20:34 +00:00
// sanity check - don't create zero claims
if rewardsShare . IsZero ( ) {
return false
}
rewardsEarned := rewardsShare . Mul ( sdk . NewDecFromInt ( rewardsThisPeriod ) ) . RoundInt ( )
2020-08-21 19:42:46 +00:00
k . AddToClaim ( ctx , cdp . Owner , rp . CollateralType , id , sdk . NewCoin ( types . GovDenom , rewardsEarned ) )
2020-04-24 15:20:34 +00:00
return false
} )
if ! expired {
return false
}
k . HandleRewardPeriodExpiry ( ctx , rp )
return false
} )
2020-06-17 09:09:44 +00:00
2020-04-24 15:20:34 +00:00
k . SetPreviousBlockTime ( ctx , ctx . BlockTime ( ) )
}
// CreateUniqueClaimPeriod creates a new claim period in the store and updates the highest claim period id
2020-08-21 19:42:46 +00:00
func ( k Keeper ) CreateUniqueClaimPeriod ( ctx sdk . Context , collateralType string , end time . Time , timeLock time . Duration ) {
id := k . GetNextClaimPeriodID ( ctx , collateralType )
claimPeriod := types . NewClaimPeriod ( collateralType , id , end , timeLock )
2020-05-07 17:46:40 +00:00
ctx . EventManager ( ) . EmitEvent (
sdk . NewEvent (
types . EventTypeClaimPeriod ,
sdk . NewAttribute ( types . AttributeKeyClaimPeriod , claimPeriod . String ( ) ) ,
) ,
)
2020-04-24 15:20:34 +00:00
k . SetClaimPeriod ( ctx , claimPeriod )
2020-08-21 19:42:46 +00:00
k . SetNextClaimPeriodID ( ctx , collateralType , id + 1 )
2020-04-24 15:20:34 +00:00
}
// AddToClaim adds the amount to an existing claim or creates a new one for that amount
2020-08-21 19:42:46 +00:00
func ( k Keeper ) AddToClaim ( ctx sdk . Context , addr sdk . AccAddress , collateralType string , id uint64 , amount sdk . Coin ) {
claim , found := k . GetClaim ( ctx , addr , collateralType , id )
2020-04-24 15:20:34 +00:00
if found {
claim . Reward = claim . Reward . Add ( amount )
} else {
2020-08-21 19:42:46 +00:00
claim = types . NewClaim ( addr , amount , collateralType , id )
2020-04-24 15:20:34 +00:00
}
k . SetClaim ( ctx , claim )
}