Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/mconcat/refactor-em…
Browse files Browse the repository at this point in the history
…ission-part-1' into mconcat/refactor-emission-part-1
  • Loading branch information
r3v4s committed Dec 30, 2024
2 parents 94a34f3 + 60fdddc commit 3bac6b1
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 71 deletions.
6 changes: 2 additions & 4 deletions staker/reward_calculation_canonical_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ func TestWarmup_1(t *testing.T) {
canonical.NextBlock()
canonical.AssertEmulatedRewardOf(0, expected * 10)
}
/*

func TestWarmup_2(t *testing.T) {
modifyWarmup(0, 10)
modifyWarmup(1, 10)
Expand Down Expand Up @@ -842,8 +842,6 @@ func TestWarmup_2(t *testing.T) {
canonical.NextBlock()
canonical.NextBlock()

canonical.AssertEmulatedRewardOf(0, expected0*10+expected1*10)

expected2 := canonical.PerBlockEmission * 70 / 100

canonical.NextBlock()
Expand All @@ -870,9 +868,9 @@ func TestWarmup_2(t *testing.T) {
canonical.NextBlock()
canonical.NextBlock()

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

}
*/
// ================
// randomized

Expand Down
12 changes: 7 additions & 5 deletions staker/reward_calculation_pool.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"gno.land/p/demo/avl"

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

i256 "gno.land/p/gnoswap/int256"
u256 "gno.land/p/gnoswap/uint256"
)
Expand Down Expand Up @@ -235,7 +237,6 @@ func (self *ExternalRewardState) AccumulateReward(startHeight, endHeight uint64)
for incentiveId, rewardRatio := range poolRewardRatios {
positionReward := u256.Zero().Mul(self.deposit.liquidity, rewardRatio)
positionReward = u256.Zero().Mul(positionReward, u256.NewUint(blockNumber))
println("AccumulateReward", blockNumber, incentiveId, rewardRatio.ToString(), positionReward.ToString())
acc, ok := self.rewards[self.currentWarmup.Index][incentiveId]
if !ok {
acc = u256.Zero()
Expand Down Expand Up @@ -278,7 +279,7 @@ func (self *ExternalRewardState) TickCrossesToExternalReward(startHeight, endHei

if endHeight < warmup.NextWarmupHeight {
// fully submerged in the current warmup
tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
currentlyInRange, tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
startHeight,
endHeight,
currentlyInRange,
Expand All @@ -292,7 +293,7 @@ func (self *ExternalRewardState) TickCrossesToExternalReward(startHeight, endHei
}

// partially included in the current warmup
tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
currentlyInRange, tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
startHeight,
warmup.NextWarmupHeight,
currentlyInRange,
Expand Down Expand Up @@ -377,6 +378,7 @@ func (self *InternalRewardState) ApplyWarmup() {
}

func (self *InternalRewardState) TickCrossesToInternalReward(startHeight, endHeight int64, currentlyInRange bool, tickUpperCrosses []int64, tickLowerCrosses []int64) {

for _, warmup := range self.deposit.warmups {
self.currentWarmup = warmup

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

if endHeight < warmup.NextWarmupHeight {
// fully submerged in the current warmup
tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
currentlyInRange, tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
startHeight,
endHeight,
currentlyInRange,
Expand All @@ -401,7 +403,7 @@ func (self *InternalRewardState) TickCrossesToInternalReward(startHeight, endHei
}

// partially included in the current warmup
tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
currentlyInRange, tickUpperCrosses, tickLowerCrosses = ForEachEligibleInterval(
startHeight,
warmup.NextWarmupHeight,
currentlyInRange,
Expand Down
6 changes: 2 additions & 4 deletions staker/reward_calculation_tick.gno
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ type PerIntervalFunc = func(startHeight, endHeight uint64)

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

if currentInRange {
// println("residual currentInRange")
// println(ufmt.Sprintf("startHeight: %d, endHeight: %d", startHeight, endHeight))
f(uint64(startHeight), uint64(endHeight))
}

return tickUpperCross[tickUpperCrossI:], tickLowerCross[tickLowerCrossI:]
return currentInRange, tickUpperCross[tickUpperCrossI:], tickLowerCross[tickLowerCrossI:]
}

func TickCrossHook(pools *Pools, height func() int64) func(poolPath string, tickId int32, zeroForOne bool) {
Expand Down
84 changes: 58 additions & 26 deletions staker/tests/__TEST_staker_NFT_transfer_01_test.gnoA
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
// User 'A' can not transfer NFT to 'B'
// user 'A' can collect reward

package staker
package tests

import (
"std"
"testing"
"time"

"gno.land/p/demo/testutils"
"gno.land/p/demo/uassert"
"std"
"testing"

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

Expand All @@ -20,10 +18,48 @@ import (

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

pusers "gno.land/p/demo/users"
"gno.land/r/demo/users"
"gno.land/r/onbloc/bar"
"gno.land/r/onbloc/qux"
)

const (
ugnotDenom string = "ugnot"
ugnotPath string = "gno.land/r/gnoswap/v1/pool:ugnot"
wugnotPath string = "gno.land/r/demo/wugnot"
gnsPath string = "gno.land/r/gnoswap/v1/gns"
barPath string = "gno.land/r/onbloc/bar"
bazPath string = "gno.land/r/onbloc/baz"
fooPath string = "gno.land/r/onbloc/foo"
oblPath string = "gno.land/r/onbloc/obl"
quxPath string = "gno.land/r/onbloc/qux"

fee100 uint32 = 100
fee500 uint32 = 500
fee3000 uint32 = 3000
maxApprove uint64 = 18446744073709551615
max_timeout int64 = 9999999999
)

const (
// define addresses to use in tests
addr01 = testutils.TestAddress("addr01")
addr02 = testutils.TestAddress("addr02")
)

var (
admin = pusers.AddressOrName(consts.ADMIN)
alice = pusers.AddressOrName(testutils.TestAddress("alice"))
pool = pusers.AddressOrName(consts.POOL_ADDR)
protocolFee = pusers.AddressOrName(consts.PROTOCOL_FEE_ADDR)
adminRealm = std.NewUserRealm(users.Resolve(admin))
posRealm = std.NewCodeRealm(consts.POSITION_PATH)

// addresses used in tests
addrUsedInTest = []std.Address{addr01, addr02}
)

func TestNftTransfer01(t *testing.T) {
testInit(t)
testPoolCreatePool(t)
Expand All @@ -38,20 +74,15 @@ func testInit(t *testing.T) {
// set pool create fee to 0 for testing
std.TestSetRealm(adminRealm)
pl.SetPoolCreationFeeByAdmin(0)

// init pool tiers
// tier 1
poolTiers["gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:500"] = InternalTier{
tier: 1,
startTimestamp: time.Now().Unix(),
}
})
}

func testPoolCreatePool(t *testing.T) {
t.Run("create pool", func(t *testing.T) {
std.TestSetRealm(adminRealm)
pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7
// tier 1
addPoolTier(t, `gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:500`, 1)
std.TestSkipHeights(1)
})
}
Expand All @@ -74,14 +105,15 @@ func testPositionMint01(t *testing.T) {
"1", // amount0Min
"1", // amount1Min
max_timeout, // deadline
admin,
admin,
users.Resolve(admin),
users.Resolve(admin),
)

std.TestSkipHeights(1)

uassert.Equal(t, lpTokenId, uint64(1))
uassert.Equal(t, gnft.OwnerOf(tid(lpTokenId)), admin)
owner, _ := gnft.OwnerOf(tid(lpTokenId))
uassert.Equal(t, owner, users.Resolve(admin))
uassert.Equal(t, amount0, "368")
uassert.Equal(t, amount1, "1000")
})
Expand All @@ -92,15 +124,15 @@ func testStakeToken01(t *testing.T) {
std.TestSetRealm(adminRealm)

// approve nft to staker
gnft.Approve(a2u(consts.STAKER_ADDR), tid(uint64(1)))
gnft.Approve(consts.STAKER_ADDR, tid(uint64(1)))
std.TestSkipHeights(1)

StakeToken(1) // GNFT tokenId

StakeToken(uint64(1)) // GNFT tokenId
std.TestSkipHeights(1)

uassert.Equal(t, gnft.OwnerOf(tid(1)), consts.STAKER_ADDR)
uassert.Equal(t, len(deposits), 1)
owner, _ := gnft.OwnerOf(tid(1))
uassert.Equal(t, owner, consts.STAKER_ADDR)
uassert.Equal(t, deposits.Size(), 1)
})
}

Expand All @@ -111,26 +143,26 @@ func testTransferNft(t *testing.T) {
t.Run("caller is not a owner (caller is same as spender)", func(t *testing.T) {
uassert.PanicsWithMessage(
t,
`caller is not token owner or approved`,
`[GNOSWAP-GNFT-001] caller has no permission || caller (g1v36k6mteta047h6lta047h6lta047h6lz7gmv8) is not the owner or operator of token (1)`,
func() {
std.TestSetRealm(adminRealm)
gnft.TransferFrom(a2u(admin), a2u(dummyAddr), tid(uint64(1)))
std.TestSetRealm(std.NewUserRealm(dummyAddr))
gnft.TransferFrom(users.Resolve(admin), dummyAddr, tid(uint64(1)))
},
)
})

t.Run("caller is not a owner (caller is different from spender)", func(t *testing.T) {
uassert.PanicsWithMessage(
t,
`caller is not token owner or approved`,
`[GNOSWAP-GNFT-001] caller has no permission || caller (g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d) is not the owner or operator of token (1)`,
func() {
std.TestSetRealm(adminRealm)
gnft.TransferFrom(a2u(consts.STAKER_ADDR), a2u(dummyAddr), tid(uint64(1)))
gnft.TransferFrom(consts.STAKER_ADDR, dummyAddr, tid(uint64(1)))
},
)
})

gnft.TransferFrom(a2u(consts.STAKER_ADDR), a2u(dummyAddr), tid(uint64(1)))
gnft.TransferFrom(consts.STAKER_ADDR, dummyAddr, tid(uint64(1)))
})
}

Expand Down
57 changes: 45 additions & 12 deletions staker/tests/__TEST_staker_NFT_transfer_02_test.gnoA
Original file line number Diff line number Diff line change
@@ -1,21 +1,59 @@
// User 'A' mints and stake NFT (with one click staking)
// user 'A' can collect reward

package staker
package tests

import (
"gno.land/p/demo/uassert"
"std"
"testing"
"time"

"gno.land/p/demo/uassert"

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

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

"gno.land/r/onbloc/bar"
"gno.land/r/onbloc/qux"

"gno.land/p/demo/testutils"
pusers "gno.land/p/demo/users"
"gno.land/r/demo/users"
)

const (
ugnotDenom string = "ugnot"
ugnotPath string = "gno.land/r/gnoswap/v1/pool:ugnot"
wugnotPath string = "gno.land/r/demo/wugnot"
gnsPath string = "gno.land/r/gnoswap/v1/gns"
barPath string = "gno.land/r/onbloc/bar"
bazPath string = "gno.land/r/onbloc/baz"
fooPath string = "gno.land/r/onbloc/foo"
oblPath string = "gno.land/r/onbloc/obl"
quxPath string = "gno.land/r/onbloc/qux"

fee100 uint32 = 100
fee500 uint32 = 500
fee3000 uint32 = 3000
maxApprove uint64 = 18446744073709551615
max_timeout int64 = 9999999999
)

const (
// define addresses to use in tests
addr01 = testutils.TestAddress("addr01")
addr02 = testutils.TestAddress("addr02")
)

var (
admin = pusers.AddressOrName(consts.ADMIN)
alice = pusers.AddressOrName(testutils.TestAddress("alice"))
pool = pusers.AddressOrName(consts.POOL_ADDR)
protocolFee = pusers.AddressOrName(consts.PROTOCOL_FEE_ADDR)
adminRealm = std.NewUserRealm(users.Resolve(admin))
posRealm = std.NewCodeRealm(consts.POSITION_PATH)

// addresses used in tests
addrUsedInTest = []std.Address{addr01, addr02}
)

func TestNftTransfer02(t *testing.T) {
Expand All @@ -30,20 +68,15 @@ func testInit(t *testing.T) {
// set pool create fee to 0 for testing
std.TestSetRealm(adminRealm)
pl.SetPoolCreationFeeByAdmin(0)

// init pool tiers
// tier 1
poolTiers["gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:500"] = InternalTier{
tier: 1,
startTimestamp: time.Now().Unix(),
}
})
}

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

MintAndStake(
tokenId, _, _, _, _ := MintAndStake(
barPath, // token0
quxPath, // token1
500, // fee
Expand Down
Loading

0 comments on commit 3bac6b1

Please sign in to comment.