@@ -11,48 +11,36 @@ import (
11
11
u256 "gno.land/p/gnoswap/uint256"
12
12
)
13
13
14
+ type IncentiveBound struct {
15
+ Incentive ExternalIncentive
16
+ IsEnter bool
17
+ }
18
+
14
19
// per-pool incentives
15
20
type Incentives struct {
16
21
byTime *avl.Tree // (startTime, endTime, creator, rewardToken) => ExternalIncentive
17
22
byHeight *avl.Tree // (startHeight, endHeight, creator, rewardToken) => ExternalIncentive
18
23
byCreator *avl.Tree // (creator, startHeight, endHeight, rewardToken) => ExternalIncentive
19
24
20
- // the value map denotes delta of the external incentive per block for the token
21
- delta *UintTree // blockNumber -> map[string]int64
25
+ incentiveBound *UintTree // blockNumber -> []IncentiveBound
22
26
23
27
rewardCache *RewardCacheTree // blockNumber -> map[string]*u256.Uint
24
28
lastRewardCacheHeight *uint64
25
-
26
- // refund to the refundee
27
- totalPenalty *uint64
28
- claimedPenalty *uint64
29
29
}
30
30
31
31
func NewIncentives(currentHeight uint64) Incentives {
32
- totalPenalty := uint64(0)
33
- claimedPenalty := uint64(0)
34
-
35
32
return Incentives{
36
33
byTime: avl.NewTree(),
37
34
byHeight: avl.NewTree(),
38
35
byCreator: avl.NewTree(),
39
36
40
- delta : NewUintTree(),
37
+ incentiveBound : NewUintTree(),
41
38
42
39
rewardCache: NewRewardCacheTree(),
43
40
lastRewardCacheHeight: ¤tHeight,
44
-
45
- totalPenalty: &totalPenalty,
46
- claimedPenalty: &claimedPenalty,
47
41
}
48
42
}
49
43
50
- func (self *Incentives) claimPenalty() uint64 {
51
- amount := *self.totalPenalty - *self.claimedPenalty
52
- *self.claimedPenalty += amount
53
- return amount
54
- }
55
-
56
44
func (self *Incentives) Exists(startTime, endTime int64, creator std.Address, rewardToken string) bool {
57
45
byTimeId := incentiveIdByTime(uint64(startTime), uint64(endTime), creator, rewardToken)
58
46
return self.byTime.Has(byTimeId)
@@ -77,12 +65,12 @@ func (self *Incentives) remove(incentive *ExternalIncentive) {
77
65
self.byCreator.Remove(byCreatorId)
78
66
}
79
67
80
- func (self *Incentives) GetDelta (height uint64) map[string]int64 {
81
- delta , ok := self.delta .Get(height)
68
+ func (self *Incentives) GetBound (height uint64) []IncentiveBound {
69
+ value , ok := self.incentiveBound .Get(height)
82
70
if !ok {
83
- return make(map[string]int64)
71
+ return []IncentiveBound{}
84
72
}
85
- return delta.(map[string]int64 )
73
+ return value.([]IncentiveBound )
86
74
}
87
75
88
76
// cacheReward() MUST be called before this function
@@ -112,30 +100,31 @@ func (self *Incentives) create(
112
100
self.byHeight.Set(byHeightId, incentive)
113
101
self.byCreator.Set(byCreatorId, incentive)
114
102
115
- startIncentiveDelta := self.GetDelta(uint64(incentive.startHeight))
116
- startIncentiveDelta[incentive.rewardToken] += int64(incentive.rewardPerBlock)
117
- startHeight := uint64(incentive.startHeight)
118
- self.delta.Set(startHeight, startIncentiveDelta)
119
-
120
- println("startIncentiveDelta")
121
- for tokenId, deltaAmount := range startIncentiveDelta {
122
- println(startHeight, tokenId, deltaAmount)
123
- }
103
+ startIncentiveBound := self.GetBound(uint64(incentive.startHeight))
104
+ startIncentiveBound = append(startIncentiveBound, IncentiveBound{
105
+ Incentive: *incentive,
106
+ IsEnter: true,
107
+ })
108
+ self.incentiveBound.Set(uint64(incentive.startHeight), startIncentiveBound)
124
109
125
110
endHeight := uint64(incentive.endHeight)
126
- endIncentiveDelta := self.GetDelta(endHeight)
127
- endIncentiveDelta[incentive.rewardToken] -= int64(incentive.rewardPerBlock)
128
- self.delta.Set(endHeight, endIncentiveDelta)
111
+ endIncentiveBound := self.GetBound(endHeight)
112
+ endIncentiveBound = append(endIncentiveBound, IncentiveBound{
113
+ Incentive: *incentive,
114
+ IsEnter: false,
115
+ })
116
+ self.incentiveBound.Set(endHeight, endIncentiveBound)
129
117
130
- println("endIncentiveDelta ")
131
- for tokenId, deltaAmount := range endIncentiveDelta {
132
- println(endHeight, tokenId, deltaAmount )
118
+ println("endIncentiveBound ")
119
+ for _, bound := range endIncentiveBound {
120
+ println(bound.Incentive.rewardToken, bound.IsEnter )
133
121
}
134
122
}
135
123
136
124
// endHeight MUST be less than or equal to the current block height
137
125
func (self *Incentives) cacheRewardPerLiquidityUnit(startHeight, endHeight uint64, stakedLiquidity *u256.Uint) {
138
126
currentReward := self.CurrentReward(startHeight)
127
+
139
128
/*
140
129
delta := self.GetDelta(startHeight)
141
130
reward := make(map[string]*u256.Uint)
@@ -156,25 +145,24 @@ func (self *Incentives) cacheRewardPerLiquidityUnit(startHeight, endHeight uint6
156
145
157
146
self.rewardCache.Set(startHeight, reward)
158
147
*/
159
- self.delta.Iterate(startHeight, endHeight, func(key uint64, value interface{}) bool {
160
- delta := value.(map[string]int64)
148
+
149
+ self.incentiveBound.Iterate(startHeight, endHeight, func(key uint64, value interface{}) bool {
150
+ bound := value.([]IncentiveBound)
161
151
reward := make(map[string]*u256.Uint)
162
- for token, deltaAmount := range delta {
163
- delta := i256.NewInt(deltaAmount)
164
- deltaQ96 := i256.Zero().Mul(delta, _iQ96)
165
- ratio := i256.Zero().Div(deltaQ96, i256.FromUint256(stakedLiquidity))
166
- currentRewardToken, ok := currentReward[token]
167
- if !ok {
168
- currentRewardToken = u256.Zero( )
169
- }
170
- deltaApplied := liquidityMathAddDelta(currentRewardToken, ratio)
171
- if deltaApplied.IsZero() {
172
- continue
152
+ for incentiveId, ratio := range currentReward {
153
+ reward[incentiveId] = ratio
154
+ }
155
+ for _, bound := range bound {
156
+ if bound.IsEnter {
157
+ delta := u256.NewUint(bound.Incentive.rewardPerBlock)
158
+ deltaQ96 := delta.Mul(delta, q96 )
159
+ ratio := u256.Zero().Div(deltaQ96, stakedLiquidity)
160
+ reward[bound.Incentive.incentiveId] = ratio
161
+ } else {
162
+ delete(reward, bound.Incentive.incentiveId)
173
163
}
174
- reward[token] = deltaApplied
175
164
}
176
165
self.rewardCache.Set(key, reward)
177
- currentReward = reward
178
166
return false
179
167
})
180
168
*self.lastRewardCacheHeight = endHeight
0 commit comments