Fix delegator rewards initialization (#859)

* fix: sync delegator rewards if they exist before creating new delegation

* remove comments
This commit is contained in:
Kevin Davis 2021-03-01 18:36:40 -07:00 committed by GitHub
parent 998454a4bc
commit 203d7e0063
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,6 @@
package keeper package keeper
import ( import (
"fmt"
"math" "math"
"time" "time"
@ -305,7 +304,6 @@ func (k Keeper) SynchronizeHardSupplyReward(ctx sdk.Context, deposit hardtypes.D
userRewardIndexIndex, foundUserRewardIndexIndex := claim.SupplyRewardIndexes.GetRewardIndexIndex(coin.Denom) userRewardIndexIndex, foundUserRewardIndexIndex := claim.SupplyRewardIndexes.GetRewardIndexIndex(coin.Denom)
if !foundUserRewardIndexIndex { if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: factor index for %s should always be found", coin.Denom) // TODO: remove before production
continue continue
} }
@ -333,7 +331,6 @@ func (k Keeper) SynchronizeHardSupplyReward(ctx sdk.Context, deposit hardtypes.D
factorIndex, foundFactorIndex := userMultiRewardIndex.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType) factorIndex, foundFactorIndex := userMultiRewardIndex.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType)
if !foundFactorIndex { if !foundFactorIndex {
fmt.Printf("[LOG]: factor index for %s should always be found", coin.Denom) // TODO: remove before production
continue continue
} }
claim.SupplyRewardIndexes[userRewardIndexIndex].RewardIndexes[factorIndex].RewardFactor = globalRewardIndex.RewardFactor claim.SupplyRewardIndexes[userRewardIndexIndex].RewardIndexes[factorIndex].RewardFactor = globalRewardIndex.RewardFactor
@ -389,7 +386,6 @@ func (k Keeper) SynchronizeHardBorrowReward(ctx sdk.Context, borrow hardtypes.Bo
userRewardIndexIndex, foundUserRewardIndexIndex := claim.BorrowRewardIndexes.GetRewardIndexIndex(coin.Denom) userRewardIndexIndex, foundUserRewardIndexIndex := claim.BorrowRewardIndexes.GetRewardIndexIndex(coin.Denom)
if !foundUserRewardIndexIndex { if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: factor index for %s should always be found", coin.Denom) // TODO: remove before production
continue continue
} }
@ -417,7 +413,6 @@ func (k Keeper) SynchronizeHardBorrowReward(ctx sdk.Context, borrow hardtypes.Bo
factorIndex, foundFactorIndex := userMultiRewardIndex.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType) factorIndex, foundFactorIndex := userMultiRewardIndex.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType)
if !foundFactorIndex { if !foundFactorIndex {
fmt.Printf("\n[LOG]: factor index for %s should always be found", coin.Denom) // TODO: remove before production
continue continue
} }
claim.BorrowRewardIndexes[userRewardIndexIndex].RewardIndexes[factorIndex].RewardFactor = globalRewardIndex.RewardFactor claim.BorrowRewardIndexes[userRewardIndexIndex].RewardIndexes[factorIndex].RewardFactor = globalRewardIndex.RewardFactor
@ -510,9 +505,7 @@ func (k Keeper) SynchronizeHardDelegatorRewards(ctx sdk.Context, delegator sdk.A
totalDelegated := sdk.ZeroDec() totalDelegated := sdk.ZeroDec()
// TODO: set reasonable max limit on delegation iteration delegations := k.stakingKeeper.GetDelegatorDelegations(ctx, delegator, 200)
maxUInt := ^uint16(0)
delegations := k.stakingKeeper.GetDelegatorDelegations(ctx, delegator, maxUInt)
for _, delegation := range delegations { for _, delegation := range delegations {
validator, found := k.stakingKeeper.GetValidator(ctx, delegation.GetValidatorAddr()) validator, found := k.stakingKeeper.GetValidator(ctx, delegation.GetValidatorAddr())
if !found { if !found {
@ -594,6 +587,9 @@ func (k Keeper) InitializeHardDelegatorReward(ctx sdk.Context, delegator sdk.Acc
if !found { if !found {
// Instantiate claim object // Instantiate claim object
claim = types.NewHardLiquidityProviderClaim(delegator, sdk.Coins{}, nil, nil, nil) claim = types.NewHardLiquidityProviderClaim(delegator, sdk.Coins{}, nil, nil, nil)
} else {
k.SynchronizeHardDelegatorRewards(ctx, delegator)
claim, _ = k.GetHardLiquidityProviderClaim(ctx, delegator)
} }
claim.DelegatorRewardIndexes = types.RewardIndexes{delegatorRewardIndexes} claim.DelegatorRewardIndexes = types.RewardIndexes{delegatorRewardIndexes}
@ -696,7 +692,6 @@ func (k Keeper) SimulateHardSynchronization(ctx sdk.Context, claim types.HardLiq
userRewardIndexIndex, foundUserRewardIndexIndex := claim.SupplyRewardIndexes.GetRewardIndexIndex(ri.CollateralType) userRewardIndexIndex, foundUserRewardIndexIndex := claim.SupplyRewardIndexes.GetRewardIndexIndex(ri.CollateralType)
if !foundUserRewardIndexIndex { if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: claim.SupplyRewardIndexes.GetRewardIndexIndex for %s should always be found", ri.CollateralType) // TODO: remove before production
continue continue
} }
@ -725,7 +720,6 @@ func (k Keeper) SimulateHardSynchronization(ctx sdk.Context, claim types.HardLiq
factorIndex, foundFactorIndex := userRewardIndexes.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType) factorIndex, foundFactorIndex := userRewardIndexes.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType)
if !foundFactorIndex { if !foundFactorIndex {
fmt.Printf("[LOG]: userRewardIndexes.RewardIndexes.GetFactorIndex for %s should always be found", globalRewardIndex.CollateralType) // TODO: remove before production
continue continue
} }
claim.SupplyRewardIndexes[userRewardIndexIndex].RewardIndexes[factorIndex].RewardFactor = globalRewardIndex.RewardFactor claim.SupplyRewardIndexes[userRewardIndexIndex].RewardIndexes[factorIndex].RewardFactor = globalRewardIndex.RewardFactor
@ -748,7 +742,6 @@ func (k Keeper) SimulateHardSynchronization(ctx sdk.Context, claim types.HardLiq
userRewardIndexIndex, foundUserRewardIndexIndex := claim.BorrowRewardIndexes.GetRewardIndexIndex(ri.CollateralType) userRewardIndexIndex, foundUserRewardIndexIndex := claim.BorrowRewardIndexes.GetRewardIndexIndex(ri.CollateralType)
if !foundUserRewardIndexIndex { if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: claim.BorrowRewardIndexes.GetRewardIndexIndex for %s should always be found", ri.CollateralType) // TODO: remove before production
continue continue
} }
@ -777,7 +770,6 @@ func (k Keeper) SimulateHardSynchronization(ctx sdk.Context, claim types.HardLiq
factorIndex, foundFactorIndex := userRewardIndexes.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType) factorIndex, foundFactorIndex := userRewardIndexes.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType)
if !foundFactorIndex { if !foundFactorIndex {
fmt.Printf("[LOG]: userRewardIndexes.RewardIndexes.GetFactorIndex for %s should always be found", globalRewardIndex.CollateralType) // TODO: remove before production
continue continue
} }
claim.BorrowRewardIndexes[userRewardIndexIndex].RewardIndexes[factorIndex].RewardFactor = globalRewardIndex.RewardFactor claim.BorrowRewardIndexes[userRewardIndexIndex].RewardIndexes[factorIndex].RewardFactor = globalRewardIndex.RewardFactor
@ -806,9 +798,7 @@ func (k Keeper) SimulateHardSynchronization(ctx sdk.Context, claim types.HardLiq
totalDelegated := sdk.ZeroDec() totalDelegated := sdk.ZeroDec()
// TODO: set reasonable max limit on delegation iteration delegations := k.stakingKeeper.GetDelegatorDelegations(ctx, claim.GetOwner(), 200)
maxUInt := ^uint16(0)
delegations := k.stakingKeeper.GetDelegatorDelegations(ctx, claim.GetOwner(), maxUInt)
for _, delegation := range delegations { for _, delegation := range delegations {
validator, found := k.stakingKeeper.GetValidator(ctx, delegation.GetValidatorAddr()) validator, found := k.stakingKeeper.GetValidator(ctx, delegation.GetValidatorAddr())
if !found { if !found {