Skip to content

Commit 11dd6d2

Browse files
committed
Merge branch 'mconcat/refactor-emission-part-1' of https://github.com/gnoswap-labs/gnoswap into mconcat/refactor-emission-part-1
2 parents 5610b7e + 93564b2 commit 11dd6d2

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
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: 92 additions & 0 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
)
@@ -57,6 +59,7 @@ type Pool struct {
5759
// conceptually equal with Pool.liquidity but only for the staked positions
5860
// updated each time when the pool crosses a staked tick
5961
stakedLiquidity *UintTree // blockNumber -> *u256.Uint
62+
lastUnclaimableHeight *uint64
6063

6164
// cache of the internal reward per liquidity for each pool in tier
6265
// value: (internal reward / total staked liquidity) * Q192
@@ -72,6 +75,7 @@ func NewPool(poolPath string, currentHeight uint64) *Pool {
7275
return &Pool{
7376
poolPath: poolPath,
7477
stakedLiquidity: NewUintTree(),
78+
lastUnclaimableHeight: &currentHeight,
7579
rewardCache: NewRewardCacheTree(),
7680
lastRewardCacheHeight: &currentHeight,
7781
incentives: NewIncentives(currentHeight),
@@ -402,6 +406,94 @@ func (self *Pool) modifyDeposit(tokenId uint64, liquidity *i256.Int, currentHeig
402406
self.stakedLiquidity.Set(currentHeight, liquidityMathAddDelta(lastStakedLiquidity, liquidity))
403407
}
404408

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

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)