Skip to content

Commit

Permalink
add unclaimable refund
Browse files Browse the repository at this point in the history
  • Loading branch information
mconcat committed Dec 31, 2024
1 parent e830de0 commit 93564b2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
12 changes: 11 additions & 1 deletion staker/calculate_pool_position_reward.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

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

ufmt "gno.land/p/demo/ufmt"

//pn "gno.land/r/gnoswap/v1/position"

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

pool.cacheInternalReward(poolTier, currentHeight)

// 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()))

pool.cacheExternalReward(currentHeight)

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

return internalRewards, internalPenalties, externalRewards, externalPenalties
}

func ProcessUnclaimableReward(poolPath string, endHeight uint64) (uint64, map[string]uint64) {
pool, ok := pools.Get(poolPath)
if !ok {
return 0, make(map[string]uint64)
}
return pool.processUnclaimableReward(poolTier, endHeight)
}

60 changes: 53 additions & 7 deletions staker/reward_calculation_pool.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"gno.land/p/demo/avl"

ufmt "gno.land/p/demo/ufmt"

i256 "gno.land/p/gnoswap/int256"
u256 "gno.land/p/gnoswap/uint256"
)
Expand Down Expand Up @@ -404,13 +406,6 @@ func (self *Pool) modifyDeposit(tokenId uint64, liquidity *i256.Int, currentHeig
self.stakedLiquidity.Set(currentHeight, liquidityMathAddDelta(lastStakedLiquidity, liquidity))
}

func (self *Pool) processUnclaimableInternalReward(poolTier *PoolTier, endHeight uint64) uint64 {
startHeight := *self.lastUnclaimableHeight
unclaimable := self.UnclaimableInternalReward(poolTier, startHeight, endHeight)
self.lastUnclaimableHeight = &endHeight
return unclaimable
}

func (self *Pool) UnclaimableInternalReward(poolTier *PoolTier, startHeight, endHeight uint64) uint64 {
tierRewardHeights, tierRewardUpdates := poolTier.TierRewardUpdates(self.poolPath, startHeight, endHeight)

Expand Down Expand Up @@ -448,6 +443,57 @@ func (self *Pool) UnclaimableInternalReward(poolTier *PoolTier, startHeight, end
return unclaimable
}

func (self *Pool) UnclaimableExternalReward(incentiveId string, startHeight, endHeight uint64) uint64 {
incentive, ok := self.incentives.GetByIncentiveId(incentiveId)
if !ok {
return 0
}

if startHeight > uint64(incentive.endHeight) || endHeight < uint64(incentive.startHeight) {
return 0
}

if startHeight < uint64(incentive.startHeight) {
startHeight = uint64(incentive.startHeight)
}

if endHeight > uint64(incentive.endHeight) {
endHeight = uint64(incentive.endHeight)
}

rewardPerBlock := incentive.rewardPerBlock

unclaimable := uint64(0)

currentStakedLiquidity := self.CurrentStakedLiquidity(startHeight)

self.stakedLiquidity.Iterate(startHeight, endHeight, func(height uint64, value interface{}) bool {
if currentStakedLiquidity.IsZero() {
unclaimable += rewardPerBlock * (height - startHeight)
}
startHeight = height
currentStakedLiquidity = value.(*u256.Uint)
return false
})

if currentStakedLiquidity.IsZero() {
unclaimable += rewardPerBlock * (endHeight - startHeight)
}

return unclaimable
}

func (self *Pool) processUnclaimableReward(poolTier *PoolTier, endHeight uint64) (uint64, map[string]uint64) {
startHeight := *self.lastUnclaimableHeight
internalUnclaimable := self.UnclaimableInternalReward(poolTier, startHeight, endHeight)
externalUnclaimable := make(map[string]uint64)
for incentiveId := range self.incentives.CurrentReward(startHeight) {
externalUnclaimable[incentiveId] = self.UnclaimableExternalReward(incentiveId, startHeight, endHeight)
}
self.lastUnclaimableHeight = &endHeight
return internalUnclaimable, externalUnclaimable
}

// ============================================
// API/helpers
// ============================================
Expand Down
6 changes: 6 additions & 0 deletions staker/staker.gno
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ func CollectReward(tokenId uint64, unwrapResult bool) string {
// internal penalty to community pool
gns.Transfer(a2u(consts.COMMUNITY_POOL_ADDR), reward.InternalPenalty)

unclaimableInternal, unclaimableExternal := ProcessUnclaimableReward(deposit.targetPoolPath, uint64(std.GetHeight()))

// internal unclaimable to community pool
gns.Transfer(a2u(consts.COMMUNITY_POOL_ADDR), unclaimableInternal)

prevAddr, prevRealm := getPrev()
std.Emit(
"CollectRewardEmission",
Expand All @@ -396,6 +401,7 @@ func CollectReward(tokenId uint64, unwrapResult bool) string {
"internal_amount", ufmt.Sprintf("%d", toUser),
"internal_unstakingFee", ufmt.Sprintf("%d", reward.Internal-toUser),
"internal_left", ufmt.Sprintf("%d", reward.InternalPenalty),
"internal_unclaimable", ufmt.Sprintf("%d", unclaimableInternal),
)

return ""
Expand Down

0 comments on commit 93564b2

Please sign in to comment.