Skip to content

Commit

Permalink
Merge pull request #139 from gnoswap-labs/GSW-719-fix-tick-rounding-e…
Browse files Browse the repository at this point in the history
…rror

GSW-719 fix: tick rouding error
  • Loading branch information
notJoon authored Dec 18, 2023
2 parents 7279365 + 1a205b7 commit 66c42b1
Show file tree
Hide file tree
Showing 6 changed files with 1,598 additions and 99 deletions.
97 changes: 97 additions & 0 deletions _test/_TEST_helper_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package swap_scneraio

import (
"std"
"strconv"
"testing"

"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/grc/grc721"

"gno.land/r/demo/users"

"gno.land/r/demo/wugnot"
)

func balanceOf(token *grc20.AdminToken, addr std.Address) uint64 {
balance, err := token.BalanceOf(addr)
if err != nil {
panic(err)
}

return balance
}

func a2u(addr std.Address) users.AddressOrName {
return users.AddressOrName(addr)
}

func tid(tokenId interface{}) grc721.TokenID {
if tokenId == nil {
panic("tid() || tokenId is nil")
}

switch tokenId.(type) {
case bigint:
return grc721.TokenID(string(tokenId.(bigint)))
case string:
return grc721.TokenID(tokenId.(string))
case int:
return grc721.TokenID(strconv.Itoa(tokenId.(int)))
case uint64:
return grc721.TokenID(strconv.Itoa(int(tokenId.(uint64))))
case grc721.TokenID:
return tokenId.(grc721.TokenID)
default:
panic("[STAKER] utils.gno__tid() || unsupported tokenId type")
}
}

func ugnotBalanceOf(addr std.Address) uint64 {
testBanker := std.GetBanker(std.BankerTypeRealmIssue)

coins := testBanker.GetCoins(addr)
if len(coins) == 0 {
return 0
}

return uint64(testBanker.GetCoins(addr)[0].Amount)
}

func wugnotBalanceOf(addr std.Address) uint64 {
return wugnot.BalanceOf(a2u(addr))
}

/* HELPER */
func shouldEQ(t *testing.T, got, expected interface{}) {
if got != expected {
t.Errorf("got %v, expected %v", got, expected)
}
}

func shouldNEQ(t *testing.T, got, expected interface{}) {
if got == expected {
t.Errorf("got %v, didn't expected %v", got, expected)
}
}

func shouldGT(t *testing.T, l, r interface{}) {
if !(l < r) {
t.Errorf("expected %v < %v", l, r)
}
}

func shouldLT(t *testing.T, l, r interface{}) {
if !(l > r) {
t.Errorf("expected %v > %v", l, r)
}
}

func shouldPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic")
}
}()
f()
}
111 changes: 12 additions & 99 deletions _test/_TEST_scenario_01_test.gno
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
// single lp, single swap, normal scenario
package swap_scneraio

import (
"encoding/gjson"
"std"
"strconv"
"testing"

"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/grc/grc721"
"gno.land/p/demo/testutils"

"gno.land/r/demo/users"

_ "gno.land/r/demo/grc20_wrapper"

pl "gno.land/r/demo/pool"
Expand Down Expand Up @@ -155,9 +151,9 @@ func TestPositionMintBarBazInRange(t *testing.T) {
)

shouldEQ(t, tokenId, bigint(1))
shouldEQ(t, liquidity, bigint(4473213900))
shouldEQ(t, liquidity, bigint(4473213901))
shouldEQ(t, amount0, bigint(499941))
shouldEQ(t, amoutn1, bigint(9999999))
shouldEQ(t, amoutn1, bigint(10000000))

std.TestSkipHeights(3)
}
Expand All @@ -184,9 +180,9 @@ func TestPositionMintBarBazLowerRange(t *testing.T) {
)

shouldEQ(t, tokenId, bigint(2))
shouldEQ(t, liquidity, bigint(4484410364))
shouldEQ(t, liquidity, bigint(4484410365))
shouldEQ(t, amount0, bigint(0))
shouldEQ(t, amoutn1, bigint(9999999))
shouldEQ(t, amoutn1, bigint(10000000))

std.TestSkipHeights(3)
}
Expand All @@ -213,8 +209,8 @@ func TestPositionMintBarBazUpperRange(t *testing.T) {
)

shouldEQ(t, tokenId, bigint(3))
shouldEQ(t, liquidity, bigint(470937834))
shouldEQ(t, amount0, bigint(9999999))
shouldEQ(t, liquidity, bigint(470937835))
shouldEQ(t, amount0, bigint(10000000))
shouldEQ(t, amoutn1, bigint(0))

std.TestSkipHeights(3)
Expand All @@ -241,8 +237,8 @@ func TestPositionMintBazFooInRange(t *testing.T) {
)

shouldEQ(t, tokenId, bigint(4))
shouldEQ(t, liquidity, bigint(3163542978))
shouldEQ(t, amount0, bigint(9999999))
shouldEQ(t, liquidity, bigint(3163542979))
shouldEQ(t, amount0, bigint(10000000))
shouldEQ(t, amoutn1, bigint(999700))

std.TestSkipHeights(3)
Expand Down Expand Up @@ -278,8 +274,8 @@ func TestPositionMintFooGnotInRange(t *testing.T) {
)

shouldEQ(t, tokenId, bigint(5))
shouldEQ(t, liquidity, bigint(45848241))
shouldEQ(t, amount0, bigint(9999999))
shouldEQ(t, liquidity, bigint(45848242))
shouldEQ(t, amount0, bigint(10000000))
shouldEQ(t, amoutn1, bigint(499941))

std.TestSkipHeights(4)
Expand Down Expand Up @@ -589,7 +585,7 @@ func TestStakerUnstake(t *testing.T) {
func TestPositionBurnUnstakedPosition(t *testing.T) {
tokenId, liquidity, amount0, amount1, poolPath := pos.Burn(1)
shouldEQ(t, tokenId, uint64(1))
shouldEQ(t, liquidity, bigint(4473213900))
shouldEQ(t, liquidity, bigint(4473213901))
shouldEQ(t, amount0, bigint(604921))
shouldEQ(t, amount1, bigint(7900364))
shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/baz:100")
Expand All @@ -614,86 +610,3 @@ func TestStakerEndExternalIncentive(t *testing.T) {

shouldEQ(t, balanceOf(oblObj, test1), 999999506)
}

func balanceOf(token *grc20.AdminToken, addr std.Address) uint64 {
balance, err := token.BalanceOf(addr)
if err != nil {
panic(err)
}

return balance
}

func a2u(addr std.Address) users.AddressOrName {
return users.AddressOrName(addr)
}

func tid(tokenId interface{}) grc721.TokenID {
if tokenId == nil {
panic("tid() || tokenId is nil")
}

switch tokenId.(type) {
case bigint:
return grc721.TokenID(string(tokenId.(bigint)))
case string:
return grc721.TokenID(tokenId.(string))
case int:
return grc721.TokenID(strconv.Itoa(tokenId.(int)))
case uint64:
return grc721.TokenID(strconv.Itoa(int(tokenId.(uint64))))
case grc721.TokenID:
return tokenId.(grc721.TokenID)
default:
panic("[STAKER] utils.gno__tid() || unsupported tokenId type")
}
}

func ugnotBalanceOf(addr std.Address) uint64 {
testBanker := std.GetBanker(std.BankerTypeRealmIssue)

coins := testBanker.GetCoins(addr)
if len(coins) == 0 {
return 0
}

return uint64(testBanker.GetCoins(addr)[0].Amount)
}

func wugnotBalanceOf(addr std.Address) uint64 {
return wugnot.BalanceOf(a2u(addr))
}

/* HELPER */
func shouldEQ(t *testing.T, got, expected interface{}) {
if got != expected {
t.Errorf("got %v, expected %v", got, expected)
}
}

func shouldNEQ(t *testing.T, got, expected interface{}) {
if got == expected {
t.Errorf("got %v, didn't expected %v", got, expected)
}
}

func shouldGT(t *testing.T, l, r interface{}) {
if !(l < r) {
t.Errorf("expected %v < %v", l, r)
}
}

func shouldLT(t *testing.T, l, r interface{}) {
if !(l > r) {
t.Errorf("expected %v > %v", l, r)
}
}

func shouldPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic")
}
}()
f()
}
Loading

0 comments on commit 66c42b1

Please sign in to comment.