@@ -3,6 +3,7 @@ package emission
3
3
import (
4
4
"std"
5
5
"strconv"
6
+ "strings"
6
7
7
8
"gno.land/r/gnoswap/v1/consts"
8
9
@@ -47,6 +48,109 @@ func init() {
47
48
distributionBpsPct.Set(strconv.Itoa(DEVOPS), 2000)
48
49
distributionBpsPct.Set(strconv.Itoa(COMMUNITY_POOL), 500)
49
50
distributionBpsPct.Set(strconv.Itoa(GOV_STAKER), 0)
51
+
52
+ addStakerPerBlockMintUpdate(uint64(std.GetHeight()), gns.GetCurrentEmission() * 7500)
53
+ }
54
+
55
+ var (
56
+ stakerPerBlockMint = avl.NewTree() // height => uint64
57
+ )
58
+
59
+ func EncodeUint(num uint64) string {
60
+ // Convert the value to a decimal string.
61
+ s := strconv.FormatUint(num, 10)
62
+
63
+ // Zero-pad to a total length of 20 characters.
64
+ zerosNeeded := 20 - len(s)
65
+ return strings.Repeat("0", zerosNeeded) + s
66
+ }
67
+
68
+ func DecodeUint(s string) uint64 {
69
+ num, err := strconv.ParseUint(s, 10, 64)
70
+ if err != nil {
71
+ panic(err)
72
+ }
73
+ return num
74
+ }
75
+
76
+ var lastGNSEmissionUpdateHeight uint64
77
+
78
+ func updateStakerEmission() {
79
+ if lastGNSEmissionUpdateHeight == uint64(std.GetHeight()) {
80
+ return
81
+ }
82
+
83
+ emissionHeights, emissionUpdates := gns.EmissionUpdates(lastGNSEmissionUpdateHeight, uint64(std.GetHeight()))
84
+
85
+ for i, height := range emissionHeights {
86
+ stakerPerBlockMint.Set(EncodeUint(height), emissionUpdates[i] * GetDistributionBpsPct(LIQUIDITY_STAKER) / 10000)
87
+ }
88
+
89
+ lastGNSEmissionUpdateHeight = uint64(std.GetHeight())
90
+ }
91
+
92
+ func GetLatestStakerEmission(endHeight uint64) uint64 {
93
+ updateStakerEmission()
94
+
95
+ var emission uint64
96
+ stakerPerBlockMint.ReverseIterate("", EncodeUint(endHeight), func(key string, value interface{}) bool {
97
+ emission = value.(uint64)
98
+ return true
99
+ })
100
+
101
+ return emission
102
+ }
103
+
104
+ func GetCurrentStakerEmission() uint64 {
105
+ updateStakerEmission()
106
+
107
+ var emission uint64
108
+ stakerPerBlockMint.ReverseIterate("", EncodeUint(uint64(std.GetHeight())), func(key string, value interface{}) bool {
109
+ emission = value.(uint64)
110
+ return true
111
+ })
112
+
113
+ return emission
114
+ }
115
+
116
+ func GetStakerEmissionUpdates(startHeight uint64, endHeight uint64) EmissionUpdate {
117
+ updateStakerEmission()
118
+
119
+ heights := make([]uint64, 0)
120
+ updates := make([]uint64, 0)
121
+ stakerPerBlockMint.Iterate(EncodeUint(startHeight), EncodeUint(endHeight), func(key string, value interface{}) bool {
122
+ heights = append(heights, DecodeUint(key))
123
+ updates = append(updates, value.(uint64))
124
+ return false
125
+ })
126
+
127
+ return EmissionUpdate {
128
+ LastEmissionUpdate: GetLatestStakerEmission(startHeight),
129
+ EmissionUpdateHeights: heights,
130
+ EmissionUpdates: updates,
131
+ }
132
+ }
133
+
134
+
135
+ // Stores the emission update information for a given interval.
136
+ // An EmissionUpdate is constructed by [startHeight, endHeight).
137
+ //
138
+ // Fields:
139
+ // - LastEmissionUpdate: the value of emission update, either at the startHeight, or before.
140
+ // - EmissionUpdateHeights: the heights of emission updates between [startHeight, endHeight).
141
+ // - EmissionUpdates: the values of emission updates between [startHeight, endHeight).
142
+ type EmissionUpdate struct {
143
+ LastEmissionUpdate uint64
144
+ EmissionUpdateHeights []uint64
145
+ EmissionUpdates []uint64
146
+ }
147
+
148
+ func (self *EmissionUpdate) IsEmpty() bool {
149
+ return len(self.EmissionUpdateHeights) == 0 && len(self.EmissionUpdates) == 0
150
+ }
151
+
152
+ func addStakerPerBlockMintUpdate(height uint64, amount uint64) {
153
+ stakerPerBlockMint.Set(EncodeUint(height), amount / 10000)
50
154
}
51
155
52
156
// ChangeDistributionPctByAdmin changes the distribution percentage for the given targets.
@@ -97,6 +201,8 @@ func ChangeDistributionPct(
97
201
assertSumDistributionPct(pct01, pct02, pct03, pct04)
98
202
assertOnlyNotHalted()
99
203
204
+ addStakerPerBlockMintUpdate(uint64(std.GetHeight()), gns.GetCurrentEmission() * pct01 / 10000)
205
+
100
206
changeDistributionPcts(
101
207
target01, pct01,
102
208
target02, pct02,
@@ -116,6 +222,8 @@ func changeDistributionPcts(
116
222
setDistributionBpsPct(target03, pct03)
117
223
setDistributionBpsPct(target04, pct04)
118
224
225
+ addStakerPerBlockMintUpdate(uint64(std.GetHeight()), gns.GetCurrentEmission() * pct01)
226
+
119
227
prevAddr, prevPkgPath := getPrevAsString()
120
228
std.Emit(
121
229
"ChangeDistributionPct",
@@ -146,8 +254,8 @@ func distributeToTarget(amount uint64) uint64 {
146
254
))
147
255
}
148
256
149
- pct := uint64( iPct)
150
- distAmount := calculateAmount(amount, pct)
257
+ pct := iPct.(int )
258
+ distAmount := calculateAmount(amount, uint64( pct) )
151
259
totalSent += distAmount
152
260
153
261
transferToTarget(targetInt, distAmount)
@@ -212,7 +320,7 @@ func GetDistributionBpsPct(target int) uint64 {
212
320
))
213
321
}
214
322
215
- return uint64( iUint64)
323
+ return iUint64.(uint64 )
216
324
}
217
325
218
326
func GetDistributedToStaker() uint64 {
0 commit comments