Skip to content

Commit d1e143f

Browse files
committed
external reward fixed
1 parent ac46554 commit d1e143f

9 files changed

+38
-37
lines changed

_deploy/r/gnoswap/gns/halving.gno

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,21 @@ var (
6262
perBlockMint = avl.NewTree() // height => uint64
6363
)
6464

65-
func EmissionUpdates(startHeight uint64, endHeight uint64) ([]uint64, []uint64) {
66-
heights := make([]uint64, 0)
67-
updates := make([]uint64, 0)
68-
perBlockMint.ReverseIterate("", EncodeUint(endHeight), func(key string, value interface{}) bool {
69-
heights = append(heights, DecodeUint(key))
70-
updates = append(updates, value.(uint64) * 75 / 100) // FIXME!!!
65+
func GetCurrentEmission() uint64 {
66+
emision := uint64(0)
67+
perBlockMint.ReverseIterate("", EncodeUint(uint64(std.GetHeight())), func(key string, value interface{}) bool {
68+
emision = value.(uint64)
7169
return true
7270
})
71+
return emision
72+
}
7373

74+
func EmissionUpdates(startHeight uint64, endHeight uint64) ([]uint64, []uint64) {
75+
heights := make([]uint64, 0)
76+
updates := make([]uint64, 0)
7477
perBlockMint.Iterate(EncodeUint(startHeight), EncodeUint(endHeight), func(key string, value interface{}) bool {
7578
heights = append(heights, DecodeUint(key))
76-
updates = append(updates, value.(uint64) * 75 / 100) // FIXME!!!
79+
updates = append(updates, value.(uint64))
7780
return false
7881
})
7982

staker/filetests/z_staker_internal_external_02_position_range_change_filetest.gno

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ func testMintWugnotGns01() {
129129
fee3000,
130130
int32(-60),
131131
int32(60),
132-
"5000",
133-
"5000",
132+
"500000000",
133+
"500000000",
134134
"1",
135135
"1",
136136
max_timeout,

staker/reward_calculation_canonical_test.gno

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,10 @@ func TestTickCross_5(t *testing.T) {
475475

476476
// eligible
477477
// entered range
478-
println(">>>> MoveTick", gnousdc, 100)
479478
canonical.MoveTick(gnousdc, 100)
480479
canonical.NextBlock()
481480

482481
// block 3
483-
println(">>>> NextBlock", gnousdc, 100)
484482
canonical.AssertCanonicalRewardOf(0, expected)
485483
canonical.AssertEmulatedRewardOf(0, expected)
486484

staker/reward_calculation_incentives.gno

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Incentives struct {
2828

2929
incentiveBound *UintTree // blockNumber -> []IncentiveBound
3030

31-
currentIncentives []string
31+
// currentIncentives []string
3232

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

4545
rewardCache: NewRewardCacheTree(),
4646
lastRewardCacheHeight: &currentHeight,
47-
48-
currentIncentives: []string{},
4947
}
5048
}
5149

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

142140
self.incentiveBound.Iterate(startHeight, endHeight, func(key uint64, value interface{}) bool {
143141
bound := value.([]IncentiveBound)
142+
for _, bound := range bound {
143+
}
144144
reward := IncentiveRewardEntry{
145-
ActiveIncentives: []string{},
145+
ActiveIncentives: make([]string, len(currentReward.ActiveIncentives)),
146146
TotalStakedLiquidity: currentReward.TotalStakedLiquidity.Clone(),
147147
}
148148
copy(reward.ActiveIncentives, currentReward.ActiveIncentives)
@@ -159,17 +159,19 @@ func (self *Incentives) cacheRewardPerLiquidityUnit(startHeight, endHeight uint6
159159
}
160160
}
161161
self.rewardCache.Set(key, reward)
162+
currentReward = reward
162163
return false
163164
})
164165
*self.lastRewardCacheHeight = endHeight
165166
}
166167

167168
func (self *Incentives) updateRewardByLiquidityChange(currentHeight uint64, liquidity *u256.Uint) {
169+
currentReward := self.CurrentReward(currentHeight)
170+
168171
entry := IncentiveRewardEntry{
169-
ActiveIncentives: []string{},
172+
ActiveIncentives: make([]string, len(currentReward.ActiveIncentives)),
170173
TotalStakedLiquidity: liquidity.Clone(),
171174
}
172-
copy(entry.ActiveIncentives, self.currentIncentives)
173-
175+
copy(entry.ActiveIncentives, currentReward.ActiveIncentives)
174176
self.rewardCache.Set(currentHeight, entry)
175177
}

staker/reward_calculation_pool.gno

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ func (self *Pool) cacheRewardPerLiquidityUnit(startHeight, endHeight uint64, cur
156156
}
157157

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

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

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

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

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

233+
236234
for i := range self.rewards {
237235
rewards[i] = make(map[string]uint64)
238236
penalties[i] = make(map[string]uint64)
@@ -244,17 +242,20 @@ func (self *ExternalRewardState) Calculate(startHeight, endHeight int64, current
244242
}
245243
}
246244

247-
248245
return rewards, penalties
249246
}
250247

251248
func (self *ExternalRewardState) AccumulateReward(startHeight, endHeight uint64) {
252249
self.pool.incentives.rewardCache.RewardPerInterval(startHeight, endHeight, func(blockNumber uint64, poolRewardI interface{}) {
253-
poolRewardRatios := map[string]*u256.Uint{}
254-
if poolRewardI != nil {
255-
poolRewardRatios = poolRewardI.(map[string]*u256.Uint)
256-
}
257-
for incentiveId, rewardRatio := range poolRewardRatios {
250+
incentiveRewardEntry := poolRewardI.(IncentiveRewardEntry)
251+
for _, incentiveId := range incentiveRewardEntry.ActiveIncentives {
252+
incentive, ok := self.pool.incentives.GetByIncentiveId(incentiveId)
253+
if !ok {
254+
panic("incentive not found")
255+
}
256+
rewardRatio := u256.NewUint(incentive.rewardPerBlock)
257+
rewardRatio.Mul(rewardRatio, q192)
258+
rewardRatio.Div(rewardRatio, incentiveRewardEntry.TotalStakedLiquidity)
258259
positionReward := u256.Zero().Mul(self.deposit.liquidity, rewardRatio)
259260
positionReward = u256.Zero().Mul(positionReward, u256.NewUint(blockNumber))
260261
acc, ok := self.rewards[self.currentWarmup.Index][incentiveId]
@@ -265,6 +266,7 @@ func (self *ExternalRewardState) AccumulateReward(startHeight, endHeight uint64)
265266
self.rewards[self.currentWarmup.Index][incentiveId] = acc
266267
}
267268
})
269+
268270
}
269271

270272
func (self *ExternalRewardState) ApplyWarmup() {
@@ -367,7 +369,6 @@ func (self *InternalRewardState) Calculate(startHeight, endHeight int64, current
367369
}
368370

369371
func (self *InternalRewardState) AccumulateReward(startHeight, endHeight uint64) {
370-
println("!AccumulateReward", startHeight, endHeight)
371372
self.pool.rewardCache.RewardPerInterval(startHeight, endHeight, func(blockNumber uint64, poolRewardI interface{}) {
372373
poolRewardRatio := poolRewardI.(*u256.Uint)
373374
positionReward := u256.Zero().Mul(self.deposit.liquidity, poolRewardRatio)

staker/reward_calculation_pool_tier.gno

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ func (self *PoolTier) changeTier(currentHeight uint64, pools *Pools, poolPath st
195195
if !ok {
196196
panic("changeTier: pool not found")
197197
}
198-
*pool.tierReward = 0
199198
} else {
200199
self.membership.Set(poolPath, nextTier)
201200
}

staker/reward_calculation_tick.gno

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,11 @@ func TickCrossHook(pools *Pools, height func() int64) func(poolPath string, tick
348348
if stakedLiquidity.Sign() == 1 {
349349
// StakedLiquidity moved from positive to zero, start unclaimable period
350350
pool.startInternalUnclaimablePeriod(blockNumber)
351-
pool.startExternalUnclaimablePeriod(blockNumber)
352351
}
353352
case 1:
354353
if stakedLiquidity.Sign() == 0 {
355354
// StakedLiquidity moved from zero to positive, end unclaimable period
356355
pool.endInternalUnclaimablePeriod(blockNumber)
357-
pool.endExternalUnclaimablePeriod(blockNumber)
358356
}
359357
}
360358

staker/staker.gno

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ func StakeToken(tokenId uint64) (string, string, string) {
217217
currentTick := pl.PoolGetSlot0Tick(poolPath)
218218
isInRange := false
219219

220+
// TODO: call cache functions at the every staker related state change
221+
poolTier.cacheReward(uint64(currentHeight), pools)
222+
pool.cacheExternalReward(uint64(currentHeight))
223+
220224
if pn.PositionIsInRange(tokenId) {
221225
isInRange = true
222226
pool.modifyDeposit(tokenId, signedLiquidity, uint64(currentHeight))
@@ -351,8 +355,6 @@ func CollectReward(tokenId uint64, unwrapResult bool) (string, string) {
351355
// get all internal and external rewards
352356
reward := calcPositionReward(uint64(currentHeight), tokenId)
353357

354-
println("reward", reward)
355-
356358
// update lastCollectHeight to current height
357359
deposit.lastCollectHeight = uint64(currentHeight)
358360

@@ -650,7 +652,7 @@ func getTickOf(tokenId uint64) (int32, int32) {
650652
tickLower := pn.PositionGetPositionTickLower(tokenId)
651653
tickUpper := pn.PositionGetPositionTickUpper(tokenId)
652654
if tickUpper < tickLower {
653-
panic(ufmt.Sprintf("tickUpper(%d) is less than tickLower(%d)", tickUpper.id, tickLower.id))
655+
panic(ufmt.Sprintf("tickUpper(%d) is less than tickLower(%d)", tickUpper, tickLower))
654656
}
655-
return tickLower.id, tickUpper.id
657+
return tickLower, tickUpper
656658
}

staker/type.gno

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ func NewExternalIncentive(
112112
blocksLeftUntilEndHeight := (endTimestamp - currentTime) * 1000 / msPerBlock
113113

114114
startHeight := std.GetHeight() + blocksLeftUntilStartHeight
115-
println("startHeight", startHeight)
116115
endHeight := std.GetHeight() + blocksLeftUntilEndHeight
117-
println("endHeight", endHeight)
118116

119117
return &ExternalIncentive{
120118
incentiveId: incentiveId,

0 commit comments

Comments
 (0)