Skip to content

Commit

Permalink
Merge branch 'main' into mconcat/refactor-emission-part-1
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyhyde committed Jan 7, 2025
2 parents 1ae4f49 + 2aff909 commit 0d789a5
Show file tree
Hide file tree
Showing 61 changed files with 1,195 additions and 377 deletions.
4 changes: 4 additions & 0 deletions _deploy/p/gnoswap/uint256/arithmetic.gno
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ func (z *Uint) isBitSet(n uint) bool {
return (z.arr[n/64] & (1 << (n % 64))) != 0
}

func (z *Uint) IsOverflow() bool {
return z.isBitSet(255)
}

// addTo computes x += y.
// Requires len(x) >= len(y).
func addTo(x, y []uint64) uint64 {
Expand Down
43 changes: 43 additions & 0 deletions _deploy/p/gnoswap/uint256/arithmetic_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,49 @@ type binOp2Test struct {
x, y, want string
}

func TestIsOverflow(t *testing.T) {
tests := []struct {
name string
input *Uint
expected bool
}{
{
name: "Number greater than max value",
input: &Uint{arr: [4]uint64{
^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0),
}},
expected: true,
},
{
name: "Max value",
input: &Uint{arr: [4]uint64{
^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0) >> 1,
}},
expected: false,
},
{
name: "0",
input: &Uint{arr: [4]uint64{0, 0, 0, 0}},
expected: false,
},
{
name: "Only 255th bit set",
input: &Uint{arr: [4]uint64{
0, 0, 0, uint64(1) << 63,
}},
expected: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.input.IsOverflow(); got != tt.expected {
t.Errorf("IsOverflow() = %v, expected %v", got, tt.expected)
}
})
}
}

func TestAdd(t *testing.T) {
tests := []binOp2Test{
{"0", "1", "1"},
Expand Down
11 changes: 4 additions & 7 deletions position/api.gno
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@ func ApiGetPosition(lpTokenId uint64) string {
for _, position := range r.Response {
owner, err := gnft.OwnerOf(tokenIdFrom(position.LpTokenId))
if err != nil {
panic(ufmt.Sprintf("owner not found for tokenId: %d", position.LpTokenId))
owner = consts.ZERO_ADDRESS
}

positionNode := json.ObjectNode("", map[string]*json.Node{
"lpTokenId": json.NumberNode("lpTokenId", float64(position.LpTokenId)),
"burned": json.BoolNode("burned", position.Burned),
Expand Down Expand Up @@ -212,9 +211,8 @@ func ApiGetPositionsByPoolPath(poolPath string) string {
for _, position := range r.Response {
owner, err := gnft.OwnerOf(tokenIdFrom(position.LpTokenId))
if err != nil {
panic(ufmt.Sprintf("owner not found for tokenId: %d", position.LpTokenId))
owner = consts.ZERO_ADDRESS
}

positionNode := json.ObjectNode("", map[string]*json.Node{
"lpTokenId": json.NumberNode("lpTokenId", float64(position.LpTokenId)),
"burned": json.BoolNode("burned", position.Burned),
Expand Down Expand Up @@ -285,9 +283,8 @@ func ApiGetPositionsByAddress(address std.Address) string {
for _, position := range r.Response {
owner, err := gnft.OwnerOf(tokenIdFrom(position.LpTokenId))
if err != nil {
panic(ufmt.Sprintf("owner not found for tokenId: %d", position.LpTokenId))
owner = consts.ZERO_ADDRESS
}

positionNode := json.ObjectNode("", map[string]*json.Node{
"lpTokenId": json.NumberNode("lpTokenId", float64(position.LpTokenId)),
"burned": json.BoolNode("burned", position.Burned),
Expand Down Expand Up @@ -428,7 +425,7 @@ func rpcMakePosition(lpTokenId uint64) RpcPosition {

owner, err := gnft.OwnerOf(tokenIdFrom(lpTokenId))
if err != nil {
panic(ufmt.Sprintf("owner not found for tokenId: %d", lpTokenId))
owner = consts.ZERO_ADDRESS
}

return RpcPosition{
Expand Down
5 changes: 2 additions & 3 deletions position/getter.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package position
import (
"std"

"gno.land/p/demo/ufmt"

u256 "gno.land/p/gnoswap/uint256"

"gno.land/r/gnoswap/v1/gnft"
Expand Down Expand Up @@ -85,7 +83,8 @@ func PositionIsInRange(tokenId uint64) bool {
func PositionGetPositionOwner(tokenId uint64) std.Address {
owner, err := gnft.OwnerOf(tokenIdFrom(tokenId))
if err != nil {
panic(ufmt.Sprintf("owner not found for tokenId: %d", tokenId))
panic(newErrorWithDetail(
errDataNotFound, err.Error()))
}
return owner
}
Expand Down
1 change: 0 additions & 1 deletion position/position.gno
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,6 @@ func mint(params MintParams) (uint64, *u256.Uint, *u256.Uint, *u256.Uint) {
}

tokenId := getNextId()

gnft.Mint(params.mintTo, tokenIdFrom(tokenId)) // owner, tokenId

pool := pl.GetPoolFromPoolPath(poolKey)
Expand Down
2 changes: 1 addition & 1 deletion position/tests/__TEST_fee_collect_with_two_user_test.gnoA
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package position
package tests

import (
"std"
Expand Down
2 changes: 1 addition & 1 deletion position/tests/__TEST_position_api_test.gnoA
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package position
package tests

import (
"std"
Expand Down
4 changes: 2 additions & 2 deletions position/tests/__TEST_position_full_test.gnoA
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package position
package tests

import (
"std"
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestDecreaseLiquidity(t *testing.T) {

tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath := DecreaseLiquidity(
uint64(1),
uint64(50),
"50",
"0",
"0",
max_timeout,
Expand Down
4 changes: 2 additions & 2 deletions position/tests/__TEST_position_full_with_emission_test.gnoA
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package position
package tests

import (
"std"
Expand Down Expand Up @@ -115,7 +115,7 @@ func testDecreaseLiquidity(t *testing.T) {

tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath := DecreaseLiquidity(
uint64(1),
uint64(50),
"50",
"0",
"0",
max_timeout,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package position
package tests

import (
"std"
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestIncreaseLiquidity(t *testing.T) {
bar.Approve(a2u(consts.POOL_ADDR), 3678979)
foo.Approve(a2u(consts.POOL_ADDR), 10000000)

pool := getPoolFromLpTokenId(t,uint64(1))
pool := getPoolFromLpTokenId(t, uint64(1))
oldLiquidity := pool.Liquidity()

_, _, m0, m1, _ := IncreaseLiquidity(
Expand All @@ -82,18 +82,18 @@ func TestIncreaseLiquidity(t *testing.T) {

func TestDecreaseLiquidity(t *testing.T) {
std.TestSetRealm(adminRealm)
oldLiquidity := getPoolFromLpTokenId(t,uint64(1)).Liquidity()
oldLiquidity := getPoolFromLpTokenId(t, uint64(1)).Liquidity()

DecreaseLiquidity(
uint64(1), // tokenId
50, // liquidityRatio
"191222635", // liquidity
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
false, // unwrapResult
)

newLiquidity := getPoolFromLpTokenId(t,uint64(1)).Liquidity()
newLiquidity := getPoolFromLpTokenId(t, uint64(1)).Liquidity()
uassert.Equal(t, true, newLiquidity.Lt(oldLiquidity))

// check fee left
Expand All @@ -112,8 +112,8 @@ func TestDecreaseLiquidityToBurnPosition(t *testing.T) {

// burn it
DecreaseLiquidity(
uint64(1), // tokenId
100, // liquidityRatio
uint64(1), // tokenId
"191222635",
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
Expand All @@ -129,7 +129,7 @@ func TestIncreaseLiquidityBurnedPosition(t *testing.T) {
bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)
foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)

pool := getPoolFromLpTokenId(t,uint64(1))
pool := getPoolFromLpTokenId(t, uint64(1))
oldLiquidity := pool.Liquidity()

position := MustGetPosition(uint64(1))
Expand Down
18 changes: 9 additions & 9 deletions position/tests/__TEST_position_increase_decrease_test.gnoA
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package position
package tests

import (
"std"
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestIncreaseLiquidity(t *testing.T) {
bar.Approve(a2u(consts.POOL_ADDR), 3678979)
foo.Approve(a2u(consts.POOL_ADDR), 10000000)

pool := getPoolFromLpTokenId(t,uint64(1))
pool := getPoolFromLpTokenId(t, uint64(1))
oldLiquidity := pool.Liquidity()

_, _, m0, m1, _ := IncreaseLiquidity(
Expand Down Expand Up @@ -158,18 +158,18 @@ func TestSwap2(t *testing.T) {

func TestDecreaseLiquidity(t *testing.T) {
std.TestSetRealm(adminRealm)
oldLiquidity := getPoolFromLpTokenId(t,uint64(1)).Liquidity()
oldLiquidity := getPoolFromLpTokenId(t, uint64(1)).Liquidity()

DecreaseLiquidity(
uint64(1), // tokenId
50, // liquidityRatio
"191222635", // liquidity
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
false, // unwrapResult
)

newLiquidity := getPoolFromLpTokenId(t,uint64(1)).Liquidity()
newLiquidity := getPoolFromLpTokenId(t, uint64(1)).Liquidity()
uassert.Equal(t, true, newLiquidity.Lt(oldLiquidity))

// check fee left
Expand All @@ -182,18 +182,18 @@ func TestDecreaseLiquidity(t *testing.T) {

func TestDecreaseLiquidityAllThenAgainShouldPanic(t *testing.T) {
std.TestSetRealm(adminRealm)
oldLiquidity := getPoolFromLpTokenId(t,uint64(1)).Liquidity()
oldLiquidity := getPoolFromLpTokenId(t, uint64(1)).Liquidity()

DecreaseLiquidity(
uint64(1), // tokenId
100, // liquidityRatio
"191222635", // liquidity
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
false, // unwrapResult
)

newLiquidity := getPoolFromLpTokenId(t,uint64(1)).Liquidity()
newLiquidity := getPoolFromLpTokenId(t, uint64(1)).Liquidity()
uassert.Equal(t, true, newLiquidity.Lt(oldLiquidity))
uassert.Equal(t, newLiquidity.ToString(), "0")

Expand All @@ -204,7 +204,7 @@ func TestDecreaseLiquidityAllThenAgainShouldPanic(t *testing.T) {
func() {
DecreaseLiquidity(
uint64(1), // tokenId
100, // liquidityRatio
"100", // liquidity
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package position
package tests

import (
"std"
Expand Down
6 changes: 3 additions & 3 deletions position/tests/__TEST_position_mint_swap_burn_left_test.gnoA
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package position
package tests

import (
"std"
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestDecreaseAllPos01(t *testing.T) {
std.TestSetRealm(adminRealm)
tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath := DecreaseLiquidity(
uint64(1), // tokenId
100, // liquidityRatio
"318704392", // liquidity
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestDecreaseAllPos02(t *testing.T) {
std.TestSetRealm(adminRealm)
tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath := DecreaseLiquidity(
uint64(2), // tokenId
100, // liquidityRatio
"124373229", // liquidityRatio
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package position
package tests

import (
"std"
Expand Down Expand Up @@ -157,18 +157,18 @@ func testDecreaseLiquidityWrapped(t *testing.T) {

_, _, _, _, a0, a1, _ := DecreaseLiquidity( // tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath
uint64(1), // tokenId
20, // liquidityRatio
"19122263", // liquidityRatio
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
false, // unwrapResult
)

userWugnotBalance = wugnot.BalanceOf(admin) // wrapped result, so wunogt increased
uassert.Equal(t, userWugnotBalance, uint64(11999999))
uassert.Equal(t, uint64(2999999), userWugnotBalance)

userUgnotBalance = ugnotBalanceOf(t, adminAddr) // wrapped result, so ugnot didn't change
uassert.Equal(t, userUgnotBalance, uint64(10))
uassert.Equal(t, uint64(10), userUgnotBalance)

newLiquidity := getPoolFromLpTokenId(t, uint64(1)).Liquidity()
uassert.Equal(t, true, newLiquidity.Lt(oldLiquidity))
Expand All @@ -187,6 +187,7 @@ func testDecreaseLiquidityUnwrapped(t *testing.T) {
std.TestSetRealm(adminRealm)

oldLiquidity := getPoolFromLpTokenId(t, uint64(1)).Liquidity()
println("oldLiquidity ", oldLiquidity.ToString())

userWugnotBalance := wugnot.BalanceOf(admin)
uassert.Equal(t, userWugnotBalance, uint64(11999999))
Expand All @@ -196,7 +197,7 @@ func testDecreaseLiquidityUnwrapped(t *testing.T) {

_, _, _, _, a0, a1, _ := DecreaseLiquidity( // tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath
uint64(1), // tokenId
50, // liquidityRatio
"50", // liquidityRatio
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package position
package tests

import (
"std"
Expand Down Expand Up @@ -171,7 +171,7 @@ func testDecreaseWithNoUnwrap(t *testing.T) {
std.TestSetRealm(adminRealm)
DecreaseLiquidity(
uint64(1),
uint64(10),
"15164540",
"0",
"0",
int64(9999999999),
Expand All @@ -198,7 +198,7 @@ func testDecreaseWithUnwrap(t *testing.T) {
std.TestSetRealm(adminRealm)
DecreaseLiquidity(
uint64(1),
uint64(10),
"13648086",
"0",
"0",
int64(9999999999),
Expand Down
Loading

0 comments on commit 0d789a5

Please sign in to comment.