Skip to content

Commit 93564b2

Browse files
committed
add unclaimable refund
1 parent e830de0 commit 93564b2

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

staker/calculate_pool_position_reward.gno

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"gno.land/r/gnoswap/v1/gns"
99

10+
ufmt "gno.land/p/demo/ufmt"
11+
1012
//pn "gno.land/r/gnoswap/v1/position"
1113

1214
u256 "gno.land/p/gnoswap/uint256"
@@ -99,7 +101,6 @@ func CalcPositionReward(currentHeight uint64, tokenId uint64, deposits *Deposits
99101

100102
pool.cacheInternalReward(poolTier, currentHeight)
101103

102-
// println(ufmt.Sprintf("pool.currentInternalReward || poolPath(%s), poolTier(%d), currentHeight(%d), currentReward(%s)", poolPath, poolTier.CurrentTier(poolPath, currentHeight), currentHeight, pool.rewardCache.CurrentReward(currentHeight).(*u256.Uint).ToString()))
103104

104105
pool.cacheExternalReward(currentHeight)
105106

@@ -122,3 +123,12 @@ func CalcPositionReward(currentHeight uint64, tokenId uint64, deposits *Deposits
122123

123124
return internalRewards, internalPenalties, externalRewards, externalPenalties
124125
}
126+
127+
func ProcessUnclaimableReward(poolPath string, endHeight uint64) (uint64, map[string]uint64) {
128+
pool, ok := pools.Get(poolPath)
129+
if !ok {
130+
return 0, make(map[string]uint64)
131+
}
132+
return pool.processUnclaimableReward(poolTier, endHeight)
133+
}
134+

staker/reward_calculation_pool.gno

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"gno.land/p/demo/avl"
99

10+
ufmt "gno.land/p/demo/ufmt"
11+
1012
i256 "gno.land/p/gnoswap/int256"
1113
u256 "gno.land/p/gnoswap/uint256"
1214
)
@@ -404,13 +406,6 @@ func (self *Pool) modifyDeposit(tokenId uint64, liquidity *i256.Int, currentHeig
404406
self.stakedLiquidity.Set(currentHeight, liquidityMathAddDelta(lastStakedLiquidity, liquidity))
405407
}
406408

407-
func (self *Pool) processUnclaimableInternalReward(poolTier *PoolTier, endHeight uint64) uint64 {
408-
startHeight := *self.lastUnclaimableHeight
409-
unclaimable := self.UnclaimableInternalReward(poolTier, startHeight, endHeight)
410-
self.lastUnclaimableHeight = &endHeight
411-
return unclaimable
412-
}
413-
414409
func (self *Pool) UnclaimableInternalReward(poolTier *PoolTier, startHeight, endHeight uint64) uint64 {
415410
tierRewardHeights, tierRewardUpdates := poolTier.TierRewardUpdates(self.poolPath, startHeight, endHeight)
416411

@@ -448,6 +443,57 @@ func (self *Pool) UnclaimableInternalReward(poolTier *PoolTier, startHeight, end
448443
return unclaimable
449444
}
450445

446+
func (self *Pool) UnclaimableExternalReward(incentiveId string, startHeight, endHeight uint64) uint64 {
447+
incentive, ok := self.incentives.GetByIncentiveId(incentiveId)
448+
if !ok {
449+
return 0
450+
}
451+
452+
if startHeight > uint64(incentive.endHeight) || endHeight < uint64(incentive.startHeight) {
453+
return 0
454+
}
455+
456+
if startHeight < uint64(incentive.startHeight) {
457+
startHeight = uint64(incentive.startHeight)
458+
}
459+
460+
if endHeight > uint64(incentive.endHeight) {
461+
endHeight = uint64(incentive.endHeight)
462+
}
463+
464+
rewardPerBlock := incentive.rewardPerBlock
465+
466+
unclaimable := uint64(0)
467+
468+
currentStakedLiquidity := self.CurrentStakedLiquidity(startHeight)
469+
470+
self.stakedLiquidity.Iterate(startHeight, endHeight, func(height uint64, value interface{}) bool {
471+
if currentStakedLiquidity.IsZero() {
472+
unclaimable += rewardPerBlock * (height - startHeight)
473+
}
474+
startHeight = height
475+
currentStakedLiquidity = value.(*u256.Uint)
476+
return false
477+
})
478+
479+
if currentStakedLiquidity.IsZero() {
480+
unclaimable += rewardPerBlock * (endHeight - startHeight)
481+
}
482+
483+
return unclaimable
484+
}
485+
486+
func (self *Pool) processUnclaimableReward(poolTier *PoolTier, endHeight uint64) (uint64, map[string]uint64) {
487+
startHeight := *self.lastUnclaimableHeight
488+
internalUnclaimable := self.UnclaimableInternalReward(poolTier, startHeight, endHeight)
489+
externalUnclaimable := make(map[string]uint64)
490+
for incentiveId := range self.incentives.CurrentReward(startHeight) {
491+
externalUnclaimable[incentiveId] = self.UnclaimableExternalReward(incentiveId, startHeight, endHeight)
492+
}
493+
self.lastUnclaimableHeight = &endHeight
494+
return internalUnclaimable, externalUnclaimable
495+
}
496+
451497
// ============================================
452498
// API/helpers
453499
// ============================================

staker/staker.gno

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ func CollectReward(tokenId uint64, unwrapResult bool) string {
382382
// internal penalty to community pool
383383
gns.Transfer(a2u(consts.COMMUNITY_POOL_ADDR), reward.InternalPenalty)
384384

385+
unclaimableInternal, unclaimableExternal := ProcessUnclaimableReward(deposit.targetPoolPath, uint64(std.GetHeight()))
386+
387+
// internal unclaimable to community pool
388+
gns.Transfer(a2u(consts.COMMUNITY_POOL_ADDR), unclaimableInternal)
389+
385390
prevAddr, prevRealm := getPrev()
386391
std.Emit(
387392
"CollectRewardEmission",
@@ -396,6 +401,7 @@ func CollectReward(tokenId uint64, unwrapResult bool) string {
396401
"internal_amount", ufmt.Sprintf("%d", toUser),
397402
"internal_unstakingFee", ufmt.Sprintf("%d", reward.Internal-toUser),
398403
"internal_left", ufmt.Sprintf("%d", reward.InternalPenalty),
404+
"internal_unclaimable", ufmt.Sprintf("%d", unclaimableInternal),
399405
)
400406

401407
return ""

0 commit comments

Comments
 (0)