Skip to content

Commit 5c66a51

Browse files
committed
!fixme: processing unclaimable reward in same height
1 parent 11dd6d2 commit 5c66a51

File tree

2 files changed

+292
-0
lines changed

2 files changed

+292
-0
lines changed
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
package staker
2+
3+
import (
4+
"math"
5+
"std"
6+
"testing"
7+
8+
"gno.land/p/demo/uassert"
9+
10+
"gno.land/r/gnoswap/v1/consts"
11+
12+
en "gno.land/r/gnoswap/v1/emission"
13+
pl "gno.land/r/gnoswap/v1/pool"
14+
pn "gno.land/r/gnoswap/v1/position"
15+
16+
"gno.land/r/gnoswap/v1/gnft"
17+
"gno.land/r/gnoswap/v1/gns"
18+
19+
"gno.land/r/onbloc/bar"
20+
"gno.land/r/onbloc/baz"
21+
"gno.land/r/onbloc/qux"
22+
)
23+
24+
func TestShortWarmUpInternalCollectRewards(t *testing.T) {
25+
testInit(t)
26+
testDoubleMint(t)
27+
testCreatePool(t)
28+
testMintBarQux100_1(t)
29+
testMintBarBaz100_2(t)
30+
testStakeToken_1(t)
31+
testSetPoolTier(t)
32+
testStakeToken_2(t)
33+
testCollect1_1(t)
34+
testCollect1_2(t)
35+
testCollect1_3(t)
36+
testCollect1_4(t)
37+
testCollect2_1(t)
38+
testCollectAll_1(t)
39+
testCollectNow(t)
40+
testCollectSameBlock(t)
41+
testCollectAfterSingleBlock(t)
42+
}
43+
44+
func testInit(t *testing.T) {
45+
t.Run("init pool tiers", func(t *testing.T) {
46+
std.TestSetRealm(adminRealm)
47+
48+
// init pool tiers
49+
// tier 1
50+
deletePoolTier(t, MUST_EXISTS_IN_TIER_1)
51+
addPoolTier(t, `gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:100`, 1)
52+
std.TestSkipHeights(1)
53+
54+
// override warm-up period for testing
55+
changeWarmup(t, 0, 150)
56+
changeWarmup(t, 1, 300)
57+
changeWarmup(t, 2, 900)
58+
changeWarmup(t, 3, math.MaxInt64)
59+
60+
// set unstaking fee to 0
61+
SetUnstakingFeeByAdmin(0)
62+
63+
// set pool creation fee to 0
64+
pl.SetPoolCreationFeeByAdmin(0)
65+
66+
// set community pool distribution to 0% (give it to devOps)
67+
en.ChangeDistributionPctByAdmin(
68+
1, 7500,
69+
2, 2500,
70+
3, 0,
71+
4, 0,
72+
)
73+
})
74+
}
75+
76+
func testDoubleMint(t *testing.T) {
77+
t.Run("double mint", func(t *testing.T) {
78+
en.MintAndDistributeGns()
79+
en.MintAndDistributeGns()
80+
81+
std.TestSkipHeights(1)
82+
})
83+
}
84+
85+
func testCreatePool(t *testing.T) {
86+
t.Run("create pool", func(t *testing.T) {
87+
std.TestSetRealm(adminRealm)
88+
89+
gns.Approve(a2u(consts.POOL_ADDR), pl.GetPoolCreationFee()*3)
90+
91+
pl.CreatePool(barPath, quxPath, 100, "79228162514264337593543950337")
92+
pl.CreatePool(barPath, bazPath, 3000, "79228162514264337593543950337")
93+
94+
std.TestSkipHeights(1)
95+
})
96+
}
97+
98+
func testMintBarQux100_1(t *testing.T) {
99+
100+
t.Run("mint position 01, bar:qux:100", func(t *testing.T) {
101+
std.TestSetRealm(adminRealm)
102+
103+
bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)
104+
qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)
105+
106+
tokenId, liquidity, amount0, amount1 := pn.Mint(
107+
barPath, // token0
108+
quxPath, // token1
109+
fee100, // fee
110+
int32(-1000), // tickLower
111+
int32(1000), // tickUpper
112+
"50", // amount0Desired
113+
"50", // amount1Desired
114+
"1", // amount0Min
115+
"1", // amount1Min
116+
max_timeout,
117+
adminAddr,
118+
adminAddr,
119+
)
120+
121+
uassert.Equal(t, tokenId, uint64(1))
122+
uassert.Equal(t, gnft.MustOwnerOf(tid(tokenId)), adminAddr)
123+
124+
gpi := getPrintInfo(t)
125+
// uassert.Equal(t, gpi, `{"height":126,"time":1234567896,"gns":{"staker":0,"devOps":8561643,"communityPool":34246574,"govStaker":0,"protocolFee":200000000,"GnoswapAdmin":99999800000000},"pool":[{"poolPath":"gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:100","startTimestamp":1234567890,"tier":1,"numPoolSameTier":1,"poolReward":0,"position":[]}]}`)
126+
127+
std.TestSkipHeights(1)
128+
})
129+
}
130+
131+
func testMintBarBaz100_2(t *testing.T) {
132+
t.Run("mint position 02, bar:baz:3000", func(t *testing.T) {
133+
134+
std.TestSetRealm(adminRealm)
135+
136+
bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)
137+
baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)
138+
139+
tokenId, liquidity, amount0, amount1 := pn.Mint(
140+
barPath, // token0
141+
bazPath, // token1
142+
fee3000, // fee
143+
int32(-1020), // tickLower
144+
int32(1020), // tickUpper
145+
"50", // amount0Desired
146+
"50", // amount1Desired
147+
"1", // amount0Min
148+
"1", // amount1Min
149+
max_timeout,
150+
adminAddr,
151+
adminAddr,
152+
)
153+
154+
uassert.Equal(t, tokenId, uint64(2))
155+
uassert.Equal(t, gnft.MustOwnerOf(tid(tokenId)), adminAddr)
156+
157+
std.TestSkipHeights(1)
158+
})
159+
}
160+
161+
func testStakeToken_1(t *testing.T) {
162+
t.Run("stake token 01", func(t *testing.T) {
163+
164+
std.TestSetRealm(adminRealm)
165+
166+
gnft.Approve(consts.STAKER_ADDR, tid(1))
167+
StakeToken(1)
168+
169+
std.TestSkipHeights(1)
170+
})
171+
}
172+
173+
func testSetPoolTier(t *testing.T) {
174+
t.Run("set pool tier", func(t *testing.T) {
175+
176+
std.TestSkipHeights(800) // this reward should go to bar:qux:100
177+
178+
std.TestSetRealm(adminRealm)
179+
addPoolTier(t, "gno.land/r/onbloc/bar:gno.land/r/onbloc/baz:3000", 2)
180+
181+
std.TestSkipHeights(1)
182+
})
183+
}
184+
185+
func testStakeToken_2(t *testing.T) {
186+
t.Run("stake token 02", func(t *testing.T) {
187+
188+
std.TestSetRealm(adminRealm)
189+
190+
gnft.Approve(consts.STAKER_ADDR, tid(2))
191+
StakeToken(2)
192+
193+
std.TestSkipHeights(1)
194+
})
195+
}
196+
197+
func testCollect1_1(t *testing.T) {
198+
t.Run("collect reward for position 01, 1st time", func(t *testing.T) {
199+
200+
std.TestSetRealm(adminRealm)
201+
202+
CollectReward(1, false)
203+
204+
std.TestSkipHeights(5)
205+
})
206+
}
207+
208+
func testCollect1_2(t *testing.T) {
209+
t.Run("collect reward for position 01, 2nd time", func(t *testing.T) {
210+
211+
std.TestSetRealm(adminRealm)
212+
213+
CollectReward(1, false)
214+
215+
std.TestSkipHeights(500)
216+
})
217+
}
218+
219+
func testCollect1_3(t *testing.T) {
220+
t.Run("collect reward for position 01, 3rd time", func(t *testing.T) {
221+
222+
std.TestSetRealm(adminRealm)
223+
224+
CollectReward(1, false)
225+
226+
std.TestSkipHeights(50)
227+
})
228+
}
229+
230+
func testCollect1_4(t *testing.T) {
231+
t.Run("collect reward for position 01, 4th time", func(t *testing.T) {
232+
233+
std.TestSetRealm(adminRealm)
234+
235+
CollectReward(1, false)
236+
237+
std.TestSkipHeights(50)
238+
})
239+
}
240+
241+
func testCollect2_1(t *testing.T) {
242+
t.Run("collect reward for position 02, 1st time", func(t *testing.T) {
243+
244+
std.TestSetRealm(adminRealm)
245+
246+
CollectReward(2, false)
247+
248+
std.TestSkipHeights(5)
249+
})
250+
}
251+
252+
func testCollectAll_1(t *testing.T) {
253+
t.Run("collect reward for all positions, 1st time", func(t *testing.T) {
254+
255+
std.TestSetRealm(adminRealm)
256+
257+
CollectReward(1, false)
258+
CollectReward(2, false)
259+
260+
std.TestSkipHeights(5)
261+
})
262+
}
263+
264+
func testCollectNow(t *testing.T) {
265+
t.Run("collect reward for position 01, now", func(t *testing.T) {
266+
std.TestSetRealm(adminRealm)
267+
268+
CollectReward(1, false)
269+
})
270+
}
271+
272+
func testCollectSameBlock(t *testing.T) {
273+
t.Run("collect reward for position 01, same block", func(t *testing.T) {
274+
std.TestSetRealm(adminRealm)
275+
276+
CollectReward(1, false)
277+
})
278+
}
279+
280+
func testCollectAfterSingleBlock(t *testing.T) {
281+
t.Run("collect reward for position 01, after single block", func(t *testing.T) {
282+
std.TestSkipHeights(1)
283+
284+
std.TestSetRealm(adminRealm)
285+
286+
// FIXME @mconcat
287+
// panic: slice index out of bounds: 0 (len=0)
288+
CollectReward(1, false)
289+
})
290+
}

staker/staker.gno

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ func CollectReward(tokenId uint64, unwrapResult bool) string {
382382
// internal penalty to community pool
383383
gns.Transfer(a2u(consts.COMMUNITY_POOL_ADDR), reward.InternalPenalty)
384384

385+
// FIXME @mconcat
386+
// panic: slice index out of bounds: 0 (len=0)
385387
unclaimableInternal, unclaimableExternal := ProcessUnclaimableReward(deposit.targetPoolPath, uint64(std.GetHeight()))
386388

387389
// internal unclaimable to community pool

0 commit comments

Comments
 (0)