Skip to content

Commit 822661e

Browse files
committed
add emission side modification
1 parent 62531b3 commit 822661e

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

emission/distribution.gno

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package emission
33
import (
44
"std"
55
"strconv"
6+
"strings"
67

78
"gno.land/r/gnoswap/v1/consts"
89

@@ -47,6 +48,109 @@ func init() {
4748
distributionBpsPct.Set(strconv.Itoa(DEVOPS), 2000)
4849
distributionBpsPct.Set(strconv.Itoa(COMMUNITY_POOL), 500)
4950
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)
50154
}
51155

52156
// ChangeDistributionPctByAdmin changes the distribution percentage for the given targets.
@@ -97,6 +201,8 @@ func ChangeDistributionPct(
97201
assertSumDistributionPct(pct01, pct02, pct03, pct04)
98202
assertOnlyNotHalted()
99203

204+
addStakerPerBlockMintUpdate(uint64(std.GetHeight()), gns.GetCurrentEmission() * pct01 / 10000)
205+
100206
changeDistributionPcts(
101207
target01, pct01,
102208
target02, pct02,
@@ -116,6 +222,8 @@ func changeDistributionPcts(
116222
setDistributionBpsPct(target03, pct03)
117223
setDistributionBpsPct(target04, pct04)
118224

225+
addStakerPerBlockMintUpdate(uint64(std.GetHeight()), gns.GetCurrentEmission() * pct01)
226+
119227
prevAddr, prevPkgPath := getPrevAsString()
120228
std.Emit(
121229
"ChangeDistributionPct",
@@ -146,8 +254,8 @@ func distributeToTarget(amount uint64) uint64 {
146254
))
147255
}
148256

149-
pct := uint64(iPct)
150-
distAmount := calculateAmount(amount, pct)
257+
pct := iPct.(int)
258+
distAmount := calculateAmount(amount, uint64(pct))
151259
totalSent += distAmount
152260

153261
transferToTarget(targetInt, distAmount)
@@ -212,7 +320,7 @@ func GetDistributionBpsPct(target int) uint64 {
212320
))
213321
}
214322

215-
return uint64(iUint64)
323+
return iUint64.(uint64)
216324
}
217325

218326
func GetDistributedToStaker() uint64 {

emission/emission.gno

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"gno.land/p/demo/ufmt"
88

9-
"gno.land/r/gnoswap/v1/common"
109
"gno.land/r/gnoswap/v1/consts"
1110
"gno.land/r/gnoswap/v1/gns"
1211
)
@@ -23,26 +22,30 @@ var (
2322
func MintAndDistributeGns() uint64 {
2423
assertOnlyNotHalted()
2524

25+
println("AAA")
2626
currentHeight := std.GetHeight()
2727
lastMintedHeight := gns.GetLastMintedHeight()
2828
if lastMintedHeight >= currentHeight {
2929
// Skip if we've already minted tokens at this height
3030
return 0
3131
}
32+
println("BBB")
3233

3334
// Mint new tokens and add any leftover amounts from previous distribution
3435
mintedEmissionRewardAmount := gns.MintGns(a2u(consts.EMISSION_ADDR))
3536
if hasLeftGNSAmount() {
3637
mintedEmissionRewardAmount += GetLeftGNSAmount()
3738
setLeftGNSAmount(0)
3839
}
39-
40+
println("CCC")
4041
// Distribute tokens and track any undistributed amount
4142
distributedGNSAmount := distributeToTarget(mintedEmissionRewardAmount)
43+
println("DDD")
4244
if mintedEmissionRewardAmount != distributedGNSAmount {
4345
setLeftGNSAmount(mintedEmissionRewardAmount - distributedGNSAmount)
4446
}
4547

48+
println("DDD")
4649
// Emit event with distribution details
4750
prevAddr, prevPkgPath := getPrevAsString()
4851
std.Emit(
@@ -56,8 +59,10 @@ func MintAndDistributeGns() uint64 {
5659
"internal_totalSupply", ufmt.Sprintf("%d", gns.TotalSupply()),
5760
)
5861

62+
println("EEE")
5963
setLastExecutedHeight(currentHeight)
6064

65+
println("FFF")
6166
return distributedGNSAmount
6267
}
6368

0 commit comments

Comments
 (0)