Skip to content

Commit 3bac6b1

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/mconcat/refactor-emission-part-1' into mconcat/refactor-emission-part-1
2 parents 94a34f3 + 60fdddc commit 3bac6b1

6 files changed

+167
-71
lines changed

staker/reward_calculation_canonical_test.gno

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ func TestWarmup_1(t *testing.T) {
792792
canonical.NextBlock()
793793
canonical.AssertEmulatedRewardOf(0, expected * 10)
794794
}
795-
/*
795+
796796
func TestWarmup_2(t *testing.T) {
797797
modifyWarmup(0, 10)
798798
modifyWarmup(1, 10)
@@ -842,8 +842,6 @@ func TestWarmup_2(t *testing.T) {
842842
canonical.NextBlock()
843843
canonical.NextBlock()
844844

845-
canonical.AssertEmulatedRewardOf(0, expected0*10+expected1*10)
846-
847845
expected2 := canonical.PerBlockEmission * 70 / 100
848846

849847
canonical.NextBlock()
@@ -870,9 +868,9 @@ func TestWarmup_2(t *testing.T) {
870868
canonical.NextBlock()
871869
canonical.NextBlock()
872870

871+
canonical.AssertEmulatedRewardOf(0, expected0*10+expected1*10+expected2*10+expected3*10)
873872

874873
}
875-
*/
876874
// ================
877875
// randomized
878876

staker/reward_calculation_pool.gno

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"gno.land/p/demo/avl"
99

10+
ufmt "gno.land/p/demo/ufmt"
11+
1012
i256 "gno.land/p/gnoswap/int256"
1113
u256 "gno.land/p/gnoswap/uint256"
1214
)
@@ -235,7 +237,6 @@ func (self *ExternalRewardState) AccumulateReward(startHeight, endHeight uint64)
235237
for incentiveId, rewardRatio := range poolRewardRatios {
236238
positionReward := u256.Zero().Mul(self.deposit.liquidity, rewardRatio)
237239
positionReward = u256.Zero().Mul(positionReward, u256.NewUint(blockNumber))
238-
println("AccumulateReward", blockNumber, incentiveId, rewardRatio.ToString(), positionReward.ToString())
239240
acc, ok := self.rewards[self.currentWarmup.Index][incentiveId]
240241
if !ok {
241242
acc = u256.Zero()
@@ -278,7 +279,7 @@ func (self *ExternalRewardState) TickCrossesToExternalReward(startHeight, endHei
278279

279280
if endHeight < warmup.NextWarmupHeight {
280281
// fully submerged in the current warmup
281-
tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
282+
currentlyInRange, tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
282283
startHeight,
283284
endHeight,
284285
currentlyInRange,
@@ -292,7 +293,7 @@ func (self *ExternalRewardState) TickCrossesToExternalReward(startHeight, endHei
292293
}
293294

294295
// partially included in the current warmup
295-
tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
296+
currentlyInRange, tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
296297
startHeight,
297298
warmup.NextWarmupHeight,
298299
currentlyInRange,
@@ -377,6 +378,7 @@ func (self *InternalRewardState) ApplyWarmup() {
377378
}
378379

379380
func (self *InternalRewardState) TickCrossesToInternalReward(startHeight, endHeight int64, currentlyInRange bool, tickUpperCrosses []int64, tickLowerCrosses []int64) {
381+
380382
for _, warmup := range self.deposit.warmups {
381383
self.currentWarmup = warmup
382384

@@ -387,7 +389,7 @@ func (self *InternalRewardState) TickCrossesToInternalReward(startHeight, endHei
387389

388390
if endHeight < warmup.NextWarmupHeight {
389391
// fully submerged in the current warmup
390-
tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
392+
currentlyInRange, tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
391393
startHeight,
392394
endHeight,
393395
currentlyInRange,
@@ -401,7 +403,7 @@ func (self *InternalRewardState) TickCrossesToInternalReward(startHeight, endHei
401403
}
402404

403405
// partially included in the current warmup
404-
tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
406+
currentlyInRange, tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
405407
startHeight,
406408
warmup.NextWarmupHeight,
407409
currentlyInRange,

staker/reward_calculation_tick.gno

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ type PerIntervalFunc = func(startHeight, endHeight uint64)
165165

166166
// Gas optimization: encoding { height: int64, inRange: bool } as int64.
167167
// height will be negative if inRange is false.
168-
func ForEachEligibleInterval(startHeight, endHeight int64, currentInRange bool, tickUpperCross []int64, tickLowerCross []int64, f PerIntervalFunc) ([]int64, []int64) {
168+
func ForEachEligibleInterval(startHeight, endHeight int64, currentInRange bool, tickUpperCross []int64, tickLowerCross []int64, f PerIntervalFunc) (bool, []int64, []int64) {
169169
tickUpperCrossI := 0
170170
tickLowerCrossI := 0
171171
tickUpperCrossLen := len(tickUpperCross)
@@ -317,12 +317,10 @@ func ForEachEligibleInterval(startHeight, endHeight int64, currentInRange bool,
317317
}
318318

319319
if currentInRange {
320-
// println("residual currentInRange")
321-
// println(ufmt.Sprintf("startHeight: %d, endHeight: %d", startHeight, endHeight))
322320
f(uint64(startHeight), uint64(endHeight))
323321
}
324322

325-
return tickUpperCross[tickUpperCrossI:], tickLowerCross[tickLowerCrossI:]
323+
return currentInRange, tickUpperCross[tickUpperCrossI:], tickLowerCross[tickLowerCrossI:]
326324
}
327325

328326
func TickCrossHook(pools *Pools, height func() int64) func(poolPath string, tickId int32, zeroForOne bool) {

staker/tests/__TEST_staker_NFT_transfer_01_test.gnoA

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
// User 'A' can not transfer NFT to 'B'
44
// user 'A' can collect reward
55

6-
package staker
6+
package tests
77

88
import (
9-
"std"
10-
"testing"
11-
"time"
12-
139
"gno.land/p/demo/testutils"
1410
"gno.land/p/demo/uassert"
11+
"std"
12+
"testing"
1513

1614
"gno.land/r/gnoswap/v1/consts"
1715

@@ -20,10 +18,48 @@ import (
2018

2119
"gno.land/r/gnoswap/v1/gnft"
2220

21+
pusers "gno.land/p/demo/users"
22+
"gno.land/r/demo/users"
2323
"gno.land/r/onbloc/bar"
2424
"gno.land/r/onbloc/qux"
2525
)
2626

27+
const (
28+
ugnotDenom string = "ugnot"
29+
ugnotPath string = "gno.land/r/gnoswap/v1/pool:ugnot"
30+
wugnotPath string = "gno.land/r/demo/wugnot"
31+
gnsPath string = "gno.land/r/gnoswap/v1/gns"
32+
barPath string = "gno.land/r/onbloc/bar"
33+
bazPath string = "gno.land/r/onbloc/baz"
34+
fooPath string = "gno.land/r/onbloc/foo"
35+
oblPath string = "gno.land/r/onbloc/obl"
36+
quxPath string = "gno.land/r/onbloc/qux"
37+
38+
fee100 uint32 = 100
39+
fee500 uint32 = 500
40+
fee3000 uint32 = 3000
41+
maxApprove uint64 = 18446744073709551615
42+
max_timeout int64 = 9999999999
43+
)
44+
45+
const (
46+
// define addresses to use in tests
47+
addr01 = testutils.TestAddress("addr01")
48+
addr02 = testutils.TestAddress("addr02")
49+
)
50+
51+
var (
52+
admin = pusers.AddressOrName(consts.ADMIN)
53+
alice = pusers.AddressOrName(testutils.TestAddress("alice"))
54+
pool = pusers.AddressOrName(consts.POOL_ADDR)
55+
protocolFee = pusers.AddressOrName(consts.PROTOCOL_FEE_ADDR)
56+
adminRealm = std.NewUserRealm(users.Resolve(admin))
57+
posRealm = std.NewCodeRealm(consts.POSITION_PATH)
58+
59+
// addresses used in tests
60+
addrUsedInTest = []std.Address{addr01, addr02}
61+
)
62+
2763
func TestNftTransfer01(t *testing.T) {
2864
testInit(t)
2965
testPoolCreatePool(t)
@@ -38,20 +74,15 @@ func testInit(t *testing.T) {
3874
// set pool create fee to 0 for testing
3975
std.TestSetRealm(adminRealm)
4076
pl.SetPoolCreationFeeByAdmin(0)
41-
42-
// init pool tiers
43-
// tier 1
44-
poolTiers["gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:500"] = InternalTier{
45-
tier: 1,
46-
startTimestamp: time.Now().Unix(),
47-
}
4877
})
4978
}
5079

5180
func testPoolCreatePool(t *testing.T) {
5281
t.Run("create pool", func(t *testing.T) {
5382
std.TestSetRealm(adminRealm)
5483
pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7
84+
// tier 1
85+
addPoolTier(t, `gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:500`, 1)
5586
std.TestSkipHeights(1)
5687
})
5788
}
@@ -74,14 +105,15 @@ func testPositionMint01(t *testing.T) {
74105
"1", // amount0Min
75106
"1", // amount1Min
76107
max_timeout, // deadline
77-
admin,
78-
admin,
108+
users.Resolve(admin),
109+
users.Resolve(admin),
79110
)
80111

81112
std.TestSkipHeights(1)
82113

83114
uassert.Equal(t, lpTokenId, uint64(1))
84-
uassert.Equal(t, gnft.OwnerOf(tid(lpTokenId)), admin)
115+
owner, _ := gnft.OwnerOf(tid(lpTokenId))
116+
uassert.Equal(t, owner, users.Resolve(admin))
85117
uassert.Equal(t, amount0, "368")
86118
uassert.Equal(t, amount1, "1000")
87119
})
@@ -92,15 +124,15 @@ func testStakeToken01(t *testing.T) {
92124
std.TestSetRealm(adminRealm)
93125

94126
// approve nft to staker
95-
gnft.Approve(a2u(consts.STAKER_ADDR), tid(uint64(1)))
127+
gnft.Approve(consts.STAKER_ADDR, tid(uint64(1)))
96128
std.TestSkipHeights(1)
97129

98-
StakeToken(1) // GNFT tokenId
99-
130+
StakeToken(uint64(1)) // GNFT tokenId
100131
std.TestSkipHeights(1)
101132

102-
uassert.Equal(t, gnft.OwnerOf(tid(1)), consts.STAKER_ADDR)
103-
uassert.Equal(t, len(deposits), 1)
133+
owner, _ := gnft.OwnerOf(tid(1))
134+
uassert.Equal(t, owner, consts.STAKER_ADDR)
135+
uassert.Equal(t, deposits.Size(), 1)
104136
})
105137
}
106138

@@ -111,26 +143,26 @@ func testTransferNft(t *testing.T) {
111143
t.Run("caller is not a owner (caller is same as spender)", func(t *testing.T) {
112144
uassert.PanicsWithMessage(
113145
t,
114-
`caller is not token owner or approved`,
146+
`[GNOSWAP-GNFT-001] caller has no permission || caller (g1v36k6mteta047h6lta047h6lta047h6lz7gmv8) is not the owner or operator of token (1)`,
115147
func() {
116-
std.TestSetRealm(adminRealm)
117-
gnft.TransferFrom(a2u(admin), a2u(dummyAddr), tid(uint64(1)))
148+
std.TestSetRealm(std.NewUserRealm(dummyAddr))
149+
gnft.TransferFrom(users.Resolve(admin), dummyAddr, tid(uint64(1)))
118150
},
119151
)
120152
})
121153

122154
t.Run("caller is not a owner (caller is different from spender)", func(t *testing.T) {
123155
uassert.PanicsWithMessage(
124156
t,
125-
`caller is not token owner or approved`,
157+
`[GNOSWAP-GNFT-001] caller has no permission || caller (g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d) is not the owner or operator of token (1)`,
126158
func() {
127159
std.TestSetRealm(adminRealm)
128-
gnft.TransferFrom(a2u(consts.STAKER_ADDR), a2u(dummyAddr), tid(uint64(1)))
160+
gnft.TransferFrom(consts.STAKER_ADDR, dummyAddr, tid(uint64(1)))
129161
},
130162
)
131163
})
132164

133-
gnft.TransferFrom(a2u(consts.STAKER_ADDR), a2u(dummyAddr), tid(uint64(1)))
165+
gnft.TransferFrom(consts.STAKER_ADDR, dummyAddr, tid(uint64(1)))
134166
})
135167
}
136168

staker/tests/__TEST_staker_NFT_transfer_02_test.gnoA

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,59 @@
11
// User 'A' mints and stake NFT (with one click staking)
22
// user 'A' can collect reward
33

4-
package staker
4+
package tests
55

66
import (
7+
"gno.land/p/demo/uassert"
78
"std"
89
"testing"
9-
"time"
10-
11-
"gno.land/p/demo/uassert"
1210

1311
"gno.land/r/gnoswap/v1/consts"
1412

1513
pl "gno.land/r/gnoswap/v1/pool"
1614

1715
"gno.land/r/onbloc/bar"
1816
"gno.land/r/onbloc/qux"
17+
18+
"gno.land/p/demo/testutils"
19+
pusers "gno.land/p/demo/users"
20+
"gno.land/r/demo/users"
21+
)
22+
23+
const (
24+
ugnotDenom string = "ugnot"
25+
ugnotPath string = "gno.land/r/gnoswap/v1/pool:ugnot"
26+
wugnotPath string = "gno.land/r/demo/wugnot"
27+
gnsPath string = "gno.land/r/gnoswap/v1/gns"
28+
barPath string = "gno.land/r/onbloc/bar"
29+
bazPath string = "gno.land/r/onbloc/baz"
30+
fooPath string = "gno.land/r/onbloc/foo"
31+
oblPath string = "gno.land/r/onbloc/obl"
32+
quxPath string = "gno.land/r/onbloc/qux"
33+
34+
fee100 uint32 = 100
35+
fee500 uint32 = 500
36+
fee3000 uint32 = 3000
37+
maxApprove uint64 = 18446744073709551615
38+
max_timeout int64 = 9999999999
39+
)
40+
41+
const (
42+
// define addresses to use in tests
43+
addr01 = testutils.TestAddress("addr01")
44+
addr02 = testutils.TestAddress("addr02")
45+
)
46+
47+
var (
48+
admin = pusers.AddressOrName(consts.ADMIN)
49+
alice = pusers.AddressOrName(testutils.TestAddress("alice"))
50+
pool = pusers.AddressOrName(consts.POOL_ADDR)
51+
protocolFee = pusers.AddressOrName(consts.PROTOCOL_FEE_ADDR)
52+
adminRealm = std.NewUserRealm(users.Resolve(admin))
53+
posRealm = std.NewCodeRealm(consts.POSITION_PATH)
54+
55+
// addresses used in tests
56+
addrUsedInTest = []std.Address{addr01, addr02}
1957
)
2058

2159
func TestNftTransfer02(t *testing.T) {
@@ -30,20 +68,15 @@ func testInit(t *testing.T) {
3068
// set pool create fee to 0 for testing
3169
std.TestSetRealm(adminRealm)
3270
pl.SetPoolCreationFeeByAdmin(0)
33-
34-
// init pool tiers
35-
// tier 1
36-
poolTiers["gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:500"] = InternalTier{
37-
tier: 1,
38-
startTimestamp: time.Now().Unix(),
39-
}
4071
})
4172
}
4273

4374
func testPoolCreatePool(t *testing.T) {
4475
t.Run("create pool", func(t *testing.T) {
4576
std.TestSetRealm(adminRealm)
4677
pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7
78+
// tier 1
79+
addPoolTier(t, `gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:500`, 1)
4780
std.TestSkipHeights(1)
4881
})
4982
}
@@ -55,7 +88,7 @@ func testStakerMintAndStake(t *testing.T) {
5588
qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)
5689
std.TestSkipHeights(2)
5790

58-
MintAndStake(
91+
tokenId, _, _, _, _ := MintAndStake(
5992
barPath, // token0
6093
quxPath, // token1
6194
500, // fee

0 commit comments

Comments
 (0)