Skip to content

Commit 200d8d1

Browse files
committed
add unclaimed
1 parent 0953c97 commit 200d8d1

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

staker/reward_calculation_pool.gno

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type Pool struct {
5959
// conceptually equal with Pool.liquidity but only for the staked positions
6060
// updated each time when the pool crosses a staked tick
6161
stakedLiquidity *UintTree // blockNumber -> *u256.Uint
62+
lastUnclaimableHeight *uint64
6263

6364
// cache of the internal reward per liquidity for each pool in tier
6465
// value: (internal reward / total staked liquidity) * Q192
@@ -74,6 +75,7 @@ func NewPool(poolPath string, currentHeight uint64) *Pool {
7475
return &Pool{
7576
poolPath: poolPath,
7677
stakedLiquidity: NewUintTree(),
78+
lastUnclaimableHeight: &currentHeight,
7779
rewardCache: NewRewardCacheTree(),
7880
lastRewardCacheHeight: &currentHeight,
7981
incentives: NewIncentives(currentHeight),
@@ -211,7 +213,10 @@ func (self *ExternalRewardState) Calculate(startHeight, endHeight int64, current
211213

212214
func (self *ExternalRewardState) AccumulateReward(startHeight, endHeight uint64) {
213215
self.pool.incentives.rewardCache.RewardPerInterval(startHeight, endHeight, func(blockNumber uint64, poolRewardI interface{}) {
214-
poolRewardRatios := poolRewardI.(map[string]*u256.Uint)
216+
poolRewardRatios := map[string]*u256.Uint{}
217+
if poolRewardI != nil {
218+
poolRewardRatios = poolRewardI.(map[string]*u256.Uint)
219+
}
215220
for incentiveId, rewardRatio := range poolRewardRatios {
216221
positionReward := u256.Zero().Mul(self.deposit.liquidity, rewardRatio)
217222
positionReward = u256.Zero().Mul(positionReward, u256.NewUint(blockNumber))
@@ -401,6 +406,50 @@ func (self *Pool) modifyDeposit(tokenId uint64, liquidity *i256.Int, currentHeig
401406
self.stakedLiquidity.Set(currentHeight, liquidityMathAddDelta(lastStakedLiquidity, liquidity))
402407
}
403408

409+
func (self *Pool) processUnclaimableInternalReward(poolTier *PoolTier, endHeight uint64) uint64 {
410+
startHeight := *self.lastUnclaimableHeight
411+
unclaimable := self.UnclaimableInternalReward(poolTier, startHeight, endHeight)
412+
self.lastUnclaimableHeight = &endHeight
413+
return unclaimable
414+
}
415+
416+
func (self *Pool) UnclaimableInternalReward(poolTier *PoolTier, startHeight, endHeight uint64) uint64 {
417+
tierRewardHeights, tierRewardUpdates := poolTier.TierRewardUpdates(self.poolPath, startHeight, endHeight)
418+
419+
currentTierReward := tierRewardUpdates[0]
420+
421+
unclaimable := uint64(0)
422+
423+
currentStakedLiquidity := self.CurrentStakedLiquidity(startHeight)
424+
425+
for i := 1; i < len(tierRewardHeights); i++ {
426+
endHeight := tierRewardHeights[i]
427+
428+
currentStakedLiquidity = self.CurrentStakedLiquidity(startHeight)
429+
self.stakedLiquidity.Iterate(startHeight, endHeight, func(height uint64, value interface{}) bool {
430+
if currentStakedLiquidity.IsZero() {
431+
unclaimable += currentTierReward * (height - startHeight)
432+
}
433+
startHeight = height
434+
currentStakedLiquidity = value.(*u256.Uint)
435+
return false
436+
})
437+
438+
if currentStakedLiquidity.IsZero() {
439+
unclaimable += currentTierReward * (endHeight - startHeight)
440+
}
441+
442+
startHeight = endHeight
443+
currentTierReward = tierRewardUpdates[i]
444+
}
445+
446+
if currentStakedLiquidity.IsZero() {
447+
unclaimable += currentTierReward * (endHeight - startHeight)
448+
}
449+
450+
return unclaimable
451+
}
452+
404453
// ============================================
405454
// API/helpers
406455
// ============================================

0 commit comments

Comments
 (0)