@@ -3,6 +3,7 @@ package staker
3
3
import (
4
4
"gno.land/r/gnoswap/v1/consts"
5
5
"gno.land/r/gnoswap/v1/gns"
6
+ en "gno.land/r/gnoswap/v1/emission"
6
7
7
8
u256 "gno.land/p/gnoswap/uint256"
8
9
)
@@ -25,49 +26,51 @@ type Reward struct {
25
26
}
26
27
27
28
func calcPositionRewardByWarmups(currentHeight uint64, tokenId uint64) []Reward {
28
- deposit := deposits.Get(tokenId)
29
- emissionUpdateHeights, emissionUpdates := gns.EmissionUpdates(deposit.lastCollectHeight, currentHeight)
30
- internalRewards, internalPenalties, externalRewards, externalPenalties := CalcPositionReward(currentHeight, tokenId, deposits, pools, poolTier, emissionUpdateHeights, emissionUpdates)
29
+ rewards := CalcPositionReward(CalcPositionRewardParam{
30
+ CurrentHeight: currentHeight,
31
+ Deposits: deposits,
32
+ Pools: pools,
33
+ PoolTier: poolTier,
34
+ TokenId: tokenId,
35
+ })
31
36
32
- rewards := make([]Reward, len(internalRewards))
33
- for i := range internalRewards {
34
- rewards[i] = Reward{}
35
- rewards[i].Internal = internalRewards[i]
36
- rewards[i].InternalPenalty = internalPenalties[i]
37
- if len(externalRewards) > i {
38
- rewards[i].External = externalRewards[i]
39
- rewards[i].ExternalPenalty = externalPenalties[i]
40
- }
41
- }
42
37
return rewards
43
38
}
44
39
45
40
func calcPositionReward(currentHeight uint64, tokenId uint64) Reward {
46
- deposit := deposits.Get(tokenId)
47
- emissionUpdateHeights, emissionUpdates := gns.EmissionUpdates(deposit.lastCollectHeight, currentHeight)
48
- internalRewards, internalPenalties, externalRewards, externalPenalties := CalcPositionReward(currentHeight, tokenId, deposits, pools, poolTier, emissionUpdateHeights, emissionUpdates)
41
+ rewards := CalcPositionReward(CalcPositionRewardParam{
42
+ CurrentHeight: currentHeight,
43
+ Deposits: deposits,
44
+ Pools: pools,
45
+ PoolTier: poolTier,
46
+ TokenId: tokenId,
47
+ })
49
48
50
49
internal := uint64(0)
51
- for _, reward := range internalRewards {
52
- internal += reward
50
+ for _, reward := range rewards {
51
+ internal += reward.Internal
53
52
}
54
53
55
54
internalPenalty := uint64(0)
56
- for _, penalty := range internalPenalties {
57
- internalPenalty += penalty
55
+ for _, reward := range rewards {
56
+ internalPenalty += reward.InternalPenalty
58
57
}
59
58
60
59
externalReward := make(map[string]uint64)
61
- for _, external := range externalRewards {
62
- for incentive, reward := range external {
63
- externalReward[incentive] += reward
60
+ for _, reward := range rewards {
61
+ if reward.External != nil {
62
+ for incentive, reward := range reward.External {
63
+ externalReward[incentive] += reward
64
+ }
64
65
}
65
66
}
66
67
67
68
externalPenalty := make(map[string]uint64)
68
- for _, external := range externalPenalties {
69
- for incentive, penalty := range external {
70
- externalPenalty[incentive] += penalty
69
+ for _, reward := range rewards {
70
+ if reward.ExternalPenalty != nil {
71
+ for incentive, penalty := range reward.ExternalPenalty {
72
+ externalPenalty[incentive] += penalty
73
+ }
71
74
}
72
75
}
73
76
@@ -79,41 +82,74 @@ func calcPositionReward(currentHeight uint64, tokenId uint64) Reward {
79
82
}
80
83
}
81
84
82
- func CalcPositionReward(currentHeight uint64, tokenId uint64, deposits *Deposits, pools *Pools, poolTier *PoolTier, emissionUpdateHeights []uint64, emissionUpdates []uint64) ([]uint64, []uint64, []map[string]uint64, []map[string]uint64) {
83
- // cache per-tier and per-pool rewards
84
- poolTier.cacheReward(currentHeight, emissionUpdateHeights, emissionUpdates)
85
+ type CalcPositionRewardParam struct {
86
+ // Environmental variables
87
+ CurrentHeight uint64
88
+ Deposits *Deposits
89
+ Pools *Pools
90
+ PoolTier *PoolTier
85
91
86
- deposit := deposits.Get(tokenId)
92
+ // Position variables
93
+ TokenId uint64
94
+ }
95
+
96
+ func CalcPositionReward(param CalcPositionRewardParam) []Reward {
97
+ println("===== CalcPositionReward", param.TokenId)
98
+
99
+ // cache per-pool rewards in the internal incentive(tiers)
100
+ param.PoolTier.cacheReward(param.CurrentHeight, param.Pools)
101
+
102
+ deposit := param.Deposits.Get(param.TokenId)
87
103
poolPath := deposit.targetPoolPath
88
104
89
- pool, ok := pools .Get(poolPath)
105
+ pool, ok := param.Pools .Get(poolPath)
90
106
if !ok {
91
- pool = NewPool(poolPath, currentHeight )
92
- pools .Set(poolPath, pool)
107
+ pool = NewPool(poolPath, param.CurrentHeight )
108
+ param.Pools .Set(poolPath, pool)
93
109
}
94
110
95
- pool.cacheInternalReward(poolTier, currentHeight)
96
-
97
- pool.cacheExternalReward(currentHeight)
98
-
111
+ // cacheInternalReward is called by poolTier.cacheReward
112
+ pool.cacheExternalReward(param.CurrentHeight)
99
113
// eligible(in-range) intervals for a position
100
- upperTick := pool.ticks.Get(deposit.tickUpper)
101
- lowerTick := pool.ticks.Get(deposit.tickLower)
114
+ // XXX: Tick ordering code, memoing for future
115
+ tickUpper := deposit.tickUpper
116
+ tickLower := deposit.tickLower
117
+ token0, token1, _ := poolPathDivide(poolPath)
118
+ if token1 < token0 {
119
+ tickUpper, tickLower = -tickLower, -tickUpper
120
+ }
121
+ upperTick := pool.ticks.Get(tickUpper)
122
+ lowerTick := pool.ticks.Get(tickLower)
123
+ // XXX: Tick ordering code, memoing for future
102
124
103
125
lastCollectHeight := deposit.lastCollectHeight
126
+ println("===== lastCollectHeight", param.TokenId, lastCollectHeight)
104
127
105
128
initialUpperCross := upperTick.previousCross(lastCollectHeight)
106
129
initialLowerCross := lowerTick.previousCross(lastCollectHeight)
107
130
currentlyInRange := initialUpperCross && !initialLowerCross
108
131
109
- tickUpperCrosses := upperTick.crossInfo(lastCollectHeight, currentHeight )
110
- tickLowerCrosses := lowerTick.crossInfo(lastCollectHeight, currentHeight )
132
+ tickUpperCrosses := upperTick.crossInfo(lastCollectHeight, param.CurrentHeight )
133
+ tickLowerCrosses := lowerTick.crossInfo(lastCollectHeight, param.CurrentHeight )
111
134
112
- internalRewards, internalPenalties := pool.InternalRewardOf(deposit).Calculate(int64(lastCollectHeight), int64(currentHeight ), currentlyInRange, tickUpperCrosses, tickLowerCrosses)
135
+ internalRewards, internalPenalties := pool.InternalRewardOf(deposit).Calculate(int64(lastCollectHeight), int64(param.CurrentHeight ), currentlyInRange, tickUpperCrosses, tickLowerCrosses)
113
136
114
- externalRewards, externalPenalties := pool.ExternalRewardOf(deposit).Calculate(int64(lastCollectHeight), int64(currentHeight ), currentlyInRange, tickUpperCrosses, tickLowerCrosses)
137
+ externalRewards, externalPenalties := pool.ExternalRewardOf(deposit).Calculate(int64(lastCollectHeight), int64(param.CurrentHeight ), currentlyInRange, tickUpperCrosses, tickLowerCrosses)
115
138
116
- return internalRewards, internalPenalties, externalRewards, externalPenalties
139
+ rewards := make([]Reward, len(internalRewards))
140
+ for i := range internalRewards {
141
+ rewards[i] = Reward{
142
+ Internal: internalRewards[i],
143
+ InternalPenalty: internalPenalties[i],
144
+ }
145
+ if externalRewards != nil {
146
+ if len(externalRewards[i]) > 0 {
147
+ rewards[i].External = externalRewards[i]
148
+ rewards[i].ExternalPenalty = externalPenalties[i]
149
+ }
150
+ }
151
+ }
152
+ return rewards
117
153
}
118
154
119
155
func ProcessUnClaimableReward(poolPath string, endHeight uint64) (uint64, map[string]uint64) {
0 commit comments