Skip to content

Commit

Permalink
clean up helper logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AlpinYukseloglu committed Mar 19, 2024
1 parent 16022dd commit 3896fc7
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions x/concentrated-liquidity/incentives.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,40 +855,45 @@ func (k Keeper) redepositForfeitedIncentives(ctx sdk.Context, poolId uint64, own
}
activeLiquidity := pool.GetLiquidity()

// If pool has active liquidity on current tick, redeposit forfeited incentives into uptime accumulators.
if activeLiquidity.GT(sdk.OneDec()) {
uptimeAccums, err := k.GetUptimeAccumulators(ctx, poolId)
// If no active liquidity, give the forfeited incentives to the sender.
if activeLiquidity.LT(sdk.OneDec()) {
err := k.bankKeeper.SendCoins(ctx, pool.GetIncentivesAddress(), owner, totalForefeitedIncentives)
if err != nil {
return err
}
return nil
}

for uptimeIndex := range uptimeAccums {
curUptimeForfeited := scaledForfeitedIncentivesByUptime[uptimeIndex]
if curUptimeForfeited.IsZero() {
continue
}
// If pool has active liquidity on current tick, redeposit forfeited incentives into uptime accumulators.
uptimeAccums, err := k.GetUptimeAccumulators(ctx, poolId)
if err != nil {
return err
}

incentivesToAddToCurAccum := sdk.NewDecCoins()
for _, forfeitedCoin := range curUptimeForfeited {
// Calculate the amount to add to the accumulator by dividing the forfeited coin amount by the current uptime duration
forfeitedAmountPerLiquidity := forfeitedCoin.Amount.ToLegacyDec().QuoTruncate(activeLiquidity)
// Loop through each uptime accumulator for the pool and redeposit forfeited incentives.
for uptimeIndex := range uptimeAccums {
curUptimeForfeited := scaledForfeitedIncentivesByUptime[uptimeIndex]
if curUptimeForfeited.IsZero() {
continue
}

// Create a DecCoin from the calculated amount
decCoinToAdd := sdk.NewDecCoinFromDec(forfeitedCoin.Denom, forfeitedAmountPerLiquidity)
// Note that this logic is a simplified version of the regular incentive distribution logic.
// It leans on the fact that the tracked forfeited incentives are already scaled appropriately
// so we do not need to run any additional computations beyond diving by the active liquidity.
incentivesToAddToCurAccum := sdk.NewDecCoins()
for _, forfeitedCoin := range curUptimeForfeited {
// Calculate the amount to add to the accumulator by dividing the forfeited coin amount by the current uptime duration
forfeitedAmountPerLiquidity := forfeitedCoin.Amount.ToLegacyDec().QuoTruncate(activeLiquidity)

// Add the calculated DecCoin to the incentives to add to current accumulator
incentivesToAddToCurAccum = incentivesToAddToCurAccum.Add(decCoinToAdd)
}
// Create a DecCoin from the calculated amount
decCoinToAdd := sdk.NewDecCoinFromDec(forfeitedCoin.Denom, forfeitedAmountPerLiquidity)

// Emit incentives to current uptime accumulator
uptimeAccums[uptimeIndex].AddToAccumulator(incentivesToAddToCurAccum)
}
} else {
// If no active liquidity, give the forfeited incentives to the sender.
err := k.bankKeeper.SendCoins(ctx, pool.GetIncentivesAddress(), owner, totalForefeitedIncentives)
if err != nil {
return err
// Add the calculated DecCoin to the incentives to add to current accumulator
incentivesToAddToCurAccum = incentivesToAddToCurAccum.Add(decCoinToAdd)
}

// Emit incentives to current uptime accumulator
uptimeAccums[uptimeIndex].AddToAccumulator(incentivesToAddToCurAccum)
}

return nil
Expand Down

0 comments on commit 3896fc7

Please sign in to comment.