Skip to content

Commit

Permalink
external reward fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mconcat committed Jan 10, 2025
1 parent ac46554 commit d1e143f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 37 deletions.
17 changes: 10 additions & 7 deletions _deploy/r/gnoswap/gns/halving.gno
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,21 @@ var (
perBlockMint = avl.NewTree() // height => uint64
)

func EmissionUpdates(startHeight uint64, endHeight uint64) ([]uint64, []uint64) {
heights := make([]uint64, 0)
updates := make([]uint64, 0)
perBlockMint.ReverseIterate("", EncodeUint(endHeight), func(key string, value interface{}) bool {
heights = append(heights, DecodeUint(key))
updates = append(updates, value.(uint64) * 75 / 100) // FIXME!!!
func GetCurrentEmission() uint64 {
emision := uint64(0)
perBlockMint.ReverseIterate("", EncodeUint(uint64(std.GetHeight())), func(key string, value interface{}) bool {
emision = value.(uint64)
return true
})
return emision
}

func EmissionUpdates(startHeight uint64, endHeight uint64) ([]uint64, []uint64) {
heights := make([]uint64, 0)
updates := make([]uint64, 0)
perBlockMint.Iterate(EncodeUint(startHeight), EncodeUint(endHeight), func(key string, value interface{}) bool {
heights = append(heights, DecodeUint(key))
updates = append(updates, value.(uint64) * 75 / 100) // FIXME!!!
updates = append(updates, value.(uint64))
return false
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func testMintWugnotGns01() {
fee3000,
int32(-60),
int32(60),
"5000",
"5000",
"500000000",
"500000000",
"1",
"1",
max_timeout,
Expand Down
2 changes: 0 additions & 2 deletions staker/reward_calculation_canonical_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,10 @@ func TestTickCross_5(t *testing.T) {

// eligible
// entered range
println(">>>> MoveTick", gnousdc, 100)
canonical.MoveTick(gnousdc, 100)
canonical.NextBlock()

// block 3
println(">>>> NextBlock", gnousdc, 100)
canonical.AssertCanonicalRewardOf(0, expected)
canonical.AssertEmulatedRewardOf(0, expected)

Expand Down
16 changes: 9 additions & 7 deletions staker/reward_calculation_incentives.gno
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Incentives struct {

incentiveBound *UintTree // blockNumber -> []IncentiveBound

currentIncentives []string
// currentIncentives []string

rewardCache *RewardCacheTree // blockNumber -> IncentiveRewardEntry
lastRewardCacheHeight *uint64
Expand All @@ -44,8 +44,6 @@ func NewIncentives(currentHeight uint64) Incentives {

rewardCache: NewRewardCacheTree(),
lastRewardCacheHeight: &currentHeight,

currentIncentives: []string{},
}
}

Expand Down Expand Up @@ -141,8 +139,10 @@ func (self *Incentives) cacheRewardPerLiquidityUnit(startHeight, endHeight uint6

self.incentiveBound.Iterate(startHeight, endHeight, func(key uint64, value interface{}) bool {
bound := value.([]IncentiveBound)
for _, bound := range bound {
}
reward := IncentiveRewardEntry{
ActiveIncentives: []string{},
ActiveIncentives: make([]string, len(currentReward.ActiveIncentives)),
TotalStakedLiquidity: currentReward.TotalStakedLiquidity.Clone(),
}
copy(reward.ActiveIncentives, currentReward.ActiveIncentives)
Expand All @@ -159,17 +159,19 @@ func (self *Incentives) cacheRewardPerLiquidityUnit(startHeight, endHeight uint6
}
}
self.rewardCache.Set(key, reward)
currentReward = reward
return false
})
*self.lastRewardCacheHeight = endHeight
}

func (self *Incentives) updateRewardByLiquidityChange(currentHeight uint64, liquidity *u256.Uint) {
currentReward := self.CurrentReward(currentHeight)

entry := IncentiveRewardEntry{
ActiveIncentives: []string{},
ActiveIncentives: make([]string, len(currentReward.ActiveIncentives)),
TotalStakedLiquidity: liquidity.Clone(),
}
copy(entry.ActiveIncentives, self.currentIncentives)

copy(entry.ActiveIncentives, currentReward.ActiveIncentives)
self.rewardCache.Set(currentHeight, entry)
}
21 changes: 11 additions & 10 deletions staker/reward_calculation_pool.gno
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ func (self *Pool) cacheRewardPerLiquidityUnit(startHeight, endHeight uint64, cur
}

func (self *Pool) cacheInternalReward(currentHeight uint64, emissionUpdate en.EmissionUpdate, rewardDenominator uint64) {
println(">>>> cacheInternalReward", currentHeight, emissionUpdate, rewardDenominator)
currentEmission := emissionUpdate.LastEmissionUpdate

startHeight := *self.lastRewardCacheHeight
Expand All @@ -176,7 +175,6 @@ func (self *Pool) cacheInternalReward(currentHeight uint64, emissionUpdate en.Em

func (self *Pool) cacheExternalReward(endHeight uint64) {
self.stakedLiquidity.Iterate(0, 9999999, func(key uint64, value interface{}) bool {
println("(((cacheExternalReward)))", key, value.(*u256.Uint).ToString())
return false
})

Expand All @@ -193,7 +191,6 @@ func (self *Pool) cacheExternalReward(endHeight uint64) {
self.incentives.cacheRewardPerLiquidityUnit(startHeight, endHeight, currentStakedLiquidity)

self.incentives.rewardCache.Iterate(0, 9999999, func(key uint64, value interface{}) bool {
println("cacheExternalReward", key, value)
return false
})
}
Expand Down Expand Up @@ -233,6 +230,7 @@ func (self *ExternalRewardState) Calculate(startHeight, endHeight int64, current
rewards := make([]map[string]uint64, len(self.deposit.warmups))
penalties := make([]map[string]uint64, len(self.deposit.warmups))


for i := range self.rewards {
rewards[i] = make(map[string]uint64)
penalties[i] = make(map[string]uint64)
Expand All @@ -244,17 +242,20 @@ func (self *ExternalRewardState) Calculate(startHeight, endHeight int64, current
}
}


return rewards, penalties
}

func (self *ExternalRewardState) AccumulateReward(startHeight, endHeight uint64) {
self.pool.incentives.rewardCache.RewardPerInterval(startHeight, endHeight, func(blockNumber uint64, poolRewardI interface{}) {
poolRewardRatios := map[string]*u256.Uint{}
if poolRewardI != nil {
poolRewardRatios = poolRewardI.(map[string]*u256.Uint)
}
for incentiveId, rewardRatio := range poolRewardRatios {
incentiveRewardEntry := poolRewardI.(IncentiveRewardEntry)
for _, incentiveId := range incentiveRewardEntry.ActiveIncentives {
incentive, ok := self.pool.incentives.GetByIncentiveId(incentiveId)
if !ok {
panic("incentive not found")
}
rewardRatio := u256.NewUint(incentive.rewardPerBlock)
rewardRatio.Mul(rewardRatio, q192)
rewardRatio.Div(rewardRatio, incentiveRewardEntry.TotalStakedLiquidity)
positionReward := u256.Zero().Mul(self.deposit.liquidity, rewardRatio)
positionReward = u256.Zero().Mul(positionReward, u256.NewUint(blockNumber))
acc, ok := self.rewards[self.currentWarmup.Index][incentiveId]
Expand All @@ -265,6 +266,7 @@ func (self *ExternalRewardState) AccumulateReward(startHeight, endHeight uint64)
self.rewards[self.currentWarmup.Index][incentiveId] = acc
}
})

}

func (self *ExternalRewardState) ApplyWarmup() {
Expand Down Expand Up @@ -367,7 +369,6 @@ func (self *InternalRewardState) Calculate(startHeight, endHeight int64, current
}

func (self *InternalRewardState) AccumulateReward(startHeight, endHeight uint64) {
println("!AccumulateReward", startHeight, endHeight)
self.pool.rewardCache.RewardPerInterval(startHeight, endHeight, func(blockNumber uint64, poolRewardI interface{}) {
poolRewardRatio := poolRewardI.(*u256.Uint)
positionReward := u256.Zero().Mul(self.deposit.liquidity, poolRewardRatio)
Expand Down
1 change: 0 additions & 1 deletion staker/reward_calculation_pool_tier.gno
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ func (self *PoolTier) changeTier(currentHeight uint64, pools *Pools, poolPath st
if !ok {
panic("changeTier: pool not found")
}
*pool.tierReward = 0
} else {
self.membership.Set(poolPath, nextTier)
}
Expand Down
2 changes: 0 additions & 2 deletions staker/reward_calculation_tick.gno
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,11 @@ func TickCrossHook(pools *Pools, height func() int64) func(poolPath string, tick
if stakedLiquidity.Sign() == 1 {
// StakedLiquidity moved from positive to zero, start unclaimable period
pool.startInternalUnclaimablePeriod(blockNumber)
pool.startExternalUnclaimablePeriod(blockNumber)
}
case 1:
if stakedLiquidity.Sign() == 0 {
// StakedLiquidity moved from zero to positive, end unclaimable period
pool.endInternalUnclaimablePeriod(blockNumber)
pool.endExternalUnclaimablePeriod(blockNumber)
}
}

Expand Down
10 changes: 6 additions & 4 deletions staker/staker.gno
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ func StakeToken(tokenId uint64) (string, string, string) {
currentTick := pl.PoolGetSlot0Tick(poolPath)
isInRange := false

// TODO: call cache functions at the every staker related state change
poolTier.cacheReward(uint64(currentHeight), pools)
pool.cacheExternalReward(uint64(currentHeight))

if pn.PositionIsInRange(tokenId) {
isInRange = true
pool.modifyDeposit(tokenId, signedLiquidity, uint64(currentHeight))
Expand Down Expand Up @@ -351,8 +355,6 @@ func CollectReward(tokenId uint64, unwrapResult bool) (string, string) {
// get all internal and external rewards
reward := calcPositionReward(uint64(currentHeight), tokenId)

println("reward", reward)

// update lastCollectHeight to current height
deposit.lastCollectHeight = uint64(currentHeight)

Expand Down Expand Up @@ -650,7 +652,7 @@ func getTickOf(tokenId uint64) (int32, int32) {
tickLower := pn.PositionGetPositionTickLower(tokenId)
tickUpper := pn.PositionGetPositionTickUpper(tokenId)
if tickUpper < tickLower {
panic(ufmt.Sprintf("tickUpper(%d) is less than tickLower(%d)", tickUpper.id, tickLower.id))
panic(ufmt.Sprintf("tickUpper(%d) is less than tickLower(%d)", tickUpper, tickLower))
}
return tickLower.id, tickUpper.id
return tickLower, tickUpper
}
2 changes: 0 additions & 2 deletions staker/type.gno
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ func NewExternalIncentive(
blocksLeftUntilEndHeight := (endTimestamp - currentTime) * 1000 / msPerBlock

startHeight := std.GetHeight() + blocksLeftUntilStartHeight
println("startHeight", startHeight)
endHeight := std.GetHeight() + blocksLeftUntilEndHeight
println("endHeight", endHeight)

return &ExternalIncentive{
incentiveId: incentiveId,
Expand Down

0 comments on commit d1e143f

Please sign in to comment.