Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(position): Position #445

Closed
wants to merge 73 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
609d58a
remove function prefix from error messages
notJoon Dec 18, 2024
2bbdfe3
temporally disable RegisterGRC20Internface in test helper
notJoon Dec 18, 2024
6772b21
mv helper -> utils
notJoon Dec 18, 2024
d6346e1
fix
notJoon Dec 18, 2024
a081d2e
missing import
notJoon Dec 18, 2024
7e6952f
remove function prefix from error messages
notJoon Dec 18, 2024
db157c5
temporally disable RegisterGRC20Internface in test helper
notJoon Dec 18, 2024
997e0c7
mv helper -> utils
notJoon Dec 18, 2024
06e952f
fix
notJoon Dec 18, 2024
3c63a54
missing import
notJoon Dec 18, 2024
7f13007
fix: test code run fail
onlyhyde Dec 18, 2024
aa66d13
Merge branch 'refactor-position' of https://github.com/gnoswap-labs/g…
notJoon Dec 18, 2024
373a410
refactor: early return for `handleNativeToken`, `handleLeftoverNative…
notJoon Dec 18, 2024
d05bbb3
test: update test helper
onlyhyde Dec 18, 2024
188179c
refactor: returning error in `wrap`
notJoon Dec 18, 2024
f038818
test: splitOf test case added
onlyhyde Dec 18, 2024
139dbf1
Merge branch 'refactor-position' of https://github.com/gnoswap-labs/g…
onlyhyde Dec 18, 2024
039399f
refactor: `Mint`
notJoon Dec 18, 2024
5dad2b6
refactor: return error from `addLiquidity`
notJoon Dec 18, 2024
a4d96a7
refactor: `IncreaseLiquidity`
notJoon Dec 18, 2024
689a21d
fix
notJoon Dec 18, 2024
9d4992e
mv: assert functions
notJoon Dec 18, 2024
7fc2ed3
refactor: CollectFee
notJoon Dec 19, 2024
b924b82
remove function prefix from error messages
notJoon Dec 18, 2024
eec08f5
temporally disable RegisterGRC20Internface in test helper
notJoon Dec 18, 2024
400491f
mv helper -> utils
notJoon Dec 18, 2024
43dd70f
fix
notJoon Dec 18, 2024
6882f35
missing import
notJoon Dec 18, 2024
6eddfa3
fix: test code run fail
onlyhyde Dec 18, 2024
03d1e4d
refactor: early return for `handleNativeToken`, `handleLeftoverNative…
notJoon Dec 18, 2024
78b921a
test: update test helper
onlyhyde Dec 18, 2024
ebc3c82
test: splitOf test case added
onlyhyde Dec 18, 2024
3f24706
refactor: returning error in `wrap`
notJoon Dec 18, 2024
dce1f4f
refactor: `Mint`
notJoon Dec 18, 2024
8b6b964
refactor: return error from `addLiquidity`
notJoon Dec 18, 2024
dad1f9c
refactor: `IncreaseLiquidity`
notJoon Dec 18, 2024
0cfccbe
fix
notJoon Dec 18, 2024
3f03870
mv: assert functions
notJoon Dec 18, 2024
d10b71a
refactor: CollectFee
notJoon Dec 19, 2024
aeae420
fix : sync test helper code of position with pool
onlyhyde Dec 19, 2024
7c1ad77
refactor: Add prerequisite checks to perform mint
onlyhyde Dec 19, 2024
65e74ab
Merge branch 'refactor-position' of https://github.com/gnoswap-labs/g…
notJoon Dec 19, 2024
5aa7bf7
save: rewrited version
notJoon Dec 19, 2024
c0b3fb8
update docs
notJoon Dec 19, 2024
03f07f0
refactor: position mint
onlyhyde Dec 20, 2024
808b22a
GSW-1839 refactor: use avl.Tree in position (#440)
r3v4s Dec 20, 2024
264669c
chore: update comments for wrap function
onlyhyde Dec 20, 2024
9a1b381
remove function prefix from error messages
notJoon Dec 18, 2024
f4375f6
mv helper -> utils
notJoon Dec 18, 2024
82506da
fix
notJoon Dec 18, 2024
f442bce
missing import
notJoon Dec 18, 2024
a7252a5
mv helper -> utils
notJoon Dec 18, 2024
cdbc78b
fix: test code run fail
onlyhyde Dec 18, 2024
f299348
refactor: early return for `handleNativeToken`, `handleLeftoverNative…
notJoon Dec 18, 2024
ee552b4
test: update test helper
onlyhyde Dec 18, 2024
a661b0e
test: splitOf test case added
onlyhyde Dec 18, 2024
a5a404f
refactor: returning error in `wrap`
notJoon Dec 18, 2024
5bf71cc
refactor: `Mint`
notJoon Dec 18, 2024
d54e717
refactor: return error from `addLiquidity`
notJoon Dec 18, 2024
c7a0ce5
refactor: `IncreaseLiquidity`
notJoon Dec 18, 2024
0ee46d4
fix
notJoon Dec 18, 2024
75b7af2
mv: assert functions
notJoon Dec 18, 2024
9ab5531
refactor: CollectFee
notJoon Dec 19, 2024
fbc8c94
refactor: `IncreaseLiquidity`
notJoon Dec 18, 2024
3414720
fix : sync test helper code of position with pool
onlyhyde Dec 19, 2024
2b9d052
refactor: Add prerequisite checks to perform mint
onlyhyde Dec 19, 2024
024baf4
save: rewrited version
notJoon Dec 19, 2024
04fa34c
update docs
notJoon Dec 19, 2024
8964d6e
refactor: position mint
onlyhyde Dec 20, 2024
0d5e300
chore: update comments for wrap function
onlyhyde Dec 20, 2024
d958ec5
Merge remote-tracking branch 'origin/refactor-position' into refactor…
onlyhyde Dec 20, 2024
86fc641
fix : run test code fail
onlyhyde Dec 20, 2024
c98ba94
refactor: rename addDetailToError to newErrorWithDetail
onlyhyde Dec 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _deploy/r/gnoswap/consts/consts.gno
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
// WRAP & UNWRAP
const (
GNOT string = "gnot"
UGNOT string = "ugnot"
WRAPPED_WUGNOT string = "gno.land/r/demo/wugnot"

// defined in https://github.com/gnolang/gno/blob/81a88a2976ba9f2f9127ebbe7fb7d1e1f7fa4bd4/examples/gno.land/r/demo/wugnot/wugnot.gno#L19
Expand Down
98 changes: 91 additions & 7 deletions pool/_helper_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ const (
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

fee100 uint32 = 100
fee500 uint32 = 500
fee3000 uint32 = 3000
fee10000 uint32 = 10000
maxApprove uint64 = 18446744073709551615
max_timeout int64 = 9999999999
maxSqrtPriceLimitX96 string = "1461446703485210103287273052203988822378723970341"

TIER_1 uint64 = 1
TIER_2 uint64 = 2
TIER_3 uint64 = 3
)

const (
Expand All @@ -52,6 +56,7 @@ var (
admin = pusers.AddressOrName(consts.ADMIN)
alice = pusers.AddressOrName(testutils.TestAddress("alice"))
pool = pusers.AddressOrName(consts.POOL_ADDR)
bob = pusers.AddressOrName(testutils.TestAddress("bob"))
protocolFee = pusers.AddressOrName(consts.PROTOCOL_FEE_ADDR)
router = pusers.AddressOrName(consts.ROUTER_ADDR)
adminRealm = std.NewUserRealm(users.Resolve(admin))
Expand Down Expand Up @@ -187,6 +192,22 @@ func TokenApprove(t *testing.T, tokenPath string, owner, spender pusers.AddressO
}
}

func CreatePool(t *testing.T,
token0 string,
token1 string,
fee uint32,
sqrtPriceX96 string,
caller std.Address) {
t.Helper()

std.TestSetRealm(std.NewUserRealm(caller))
poolPath := pl.GetPoolPath(token0, token1, fee)
if !pl.DoesPoolPathExist(poolPath) {
pl.CreatePool(token0, token1, fee, sqrtPriceX96)
sr.SetPoolTierByAdmin(poolPath, TIER_1)
}
}

func MintPosition(t *testing.T,
token0 string,
token1 string,
Expand Down Expand Up @@ -373,6 +394,54 @@ func MintPositionAll(t *testing.T, caller std.Address) {

}

func MakeMintPositionWithoutFee(t *testing.T) (uint64, string, string, string) {
t.Helper()

// make actual data to test resetting not only position's state but also pool's state
std.TestSetRealm(adminRealm)

// set pool create fee to 0 for testing
pl.SetPoolCreationFeeByAdmin(0)
CreatePool(t, barPath, fooPath, fee500, common.TickMathGetSqrtRatioAtTick(0).ToString(), users.Resolve(admin))

TokenApprove(t, barPath, admin, pool, consts.UINT64_MAX)
TokenApprove(t, fooPath, admin, pool, consts.UINT64_MAX)

// mint position
return Mint(
barPath,
fooPath,
fee500,
-887270,
887270,
"50000",
"50000",
"0",
"0",
max_timeout,
users.Resolve(admin),
users.Resolve(admin),
)
}

func LPTokenApprove(t *testing.T, owner, operator pusers.AddressOrName, tokenId uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(users.Resolve(owner)))
gnft.Approve(operator, tokenIdFrom(tokenId))
}

func LPTokenStake(t *testing.T, owner pusers.AddressOrName, tokenId uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(users.Resolve(owner)))
sr.StakeToken(tokenId)
}

func LPTokenUnStake(t *testing.T, owner pusers.AddressOrName, tokenId uint64, unwrap bool) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(users.Resolve(owner)))
sr.UnstakeToken(tokenId, unwrap)
}

func wugnotApprove(t *testing.T, owner, spender pusers.AddressOrName, amount uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(users.Resolve(owner)))
Expand Down Expand Up @@ -569,7 +638,17 @@ func burnUsdc(addr pusers.AddressOrName) {
usdc.Burn(addr, usdc.BalanceOf(addr))
}

func TestBeforeResetObject(t *testing.T) {
// burnAllNFT burns all NFTs
func burnAllNFT(t *testing.T) {
t.Helper()

std.TestSetRealm(std.NewCodeRealm(consts.POSITION_PATH))
for i := uint64(1); i <= gnft.TotalSupply(); i++ {
gnft.Burn(tokenIdFrom(i))
}
}

func TestBeforeResetPoolObject(t *testing.T) {
// make some data
pools = avl.NewTree()
pools.Set("gno.land/r/gnoswap/v1/gns:gno.land/r/onbloc/usdc", &Pool{
Expand Down Expand Up @@ -604,3 +683,8 @@ func TestBurnTokens(t *testing.T) {
uassert.Equal(t, foo.BalanceOf(a2u(addr01)), uint64(0)) // 100_000_000 -> 0
uassert.Equal(t, bar.BalanceOf(a2u(addr01)), uint64(0)) // 100_000_000 -> 0
}

func TestBurnAllNFT(t *testing.T) {
burnAllNFT(t)
uassert.Equal(t, gnft.TotalSupply(), uint64(0), "gnft total supply should be 0")
}
2 changes: 1 addition & 1 deletion pool/position.gno
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func positionUpdate(
if position.liquidity.IsZero() {
panic(addDetailToError(
errZeroLiquidity,
"position.gno__positionUpdate() || both liquidityDelta and current position's liquidity are zero",
"both liquidityDelta and current position's liquidity are zero",
))
}

Expand Down
4 changes: 2 additions & 2 deletions pool/position_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestPositionUpdateWithKey(t *testing.T) {
panicMsg string
expectedLiquidity string
}{
{i256.MustFromDecimal("0"), u256.Zero(), u256.Zero(), true, `[GNOSWAP-POOL-010] zero liquidity || position.gno__positionUpdate() || both liquidityDelta and current position's liquidity are zero`, ""},
{i256.MustFromDecimal("0"), u256.Zero(), u256.Zero(), true, `[GNOSWAP-POOL-010] zero liquidity || both liquidityDelta and current position's liquidity are zero`, ""},
{i256.MustFromDecimal("100000"), u256.Zero(), u256.Zero(), false, "", "100000"},
}

Expand Down Expand Up @@ -109,7 +109,7 @@ func TestPositionUpdate(t *testing.T) {
feeGrowthInside0X128: u256.Zero(),
feeGrowthInside1X128: u256.Zero(),
shouldPanic: true,
panicMsg: `[GNOSWAP-POOL-010] zero liquidity || position.gno__positionUpdate() || both liquidityDelta and current position's liquidity are zero`,
panicMsg: `[GNOSWAP-POOL-010] zero liquidity || both liquidityDelta and current position's liquidity are zero`,
},
{
initialLiquidity: u256.Zero(),
Expand Down
2 changes: 1 addition & 1 deletion pool/tests/__TEST_pool_spec_#6_test.gnoA
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestPokeIsNotAllowedOnUninitilaizedPosition(t *testing.T) {
std.TestSetRealm(posRealm)
uassert.PanicsWithMessage(
t,
`[GNOSWAP-POOL-010] zero liquidity || position.gno__positionUpdate() || both liquidityDelta and (self)liquidity are zero`,
`[GNOSWAP-POOL-010] zero liquidity || both liquidityDelta and (self)liquidity are zero`,
func() {
Burn(
barPath,
Expand Down
35 changes: 23 additions & 12 deletions position/_GET_no_receiver.gno
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,62 @@ import (

// type Position
func PositionGetPosition(tokenId uint64) Position {
return positions[tokenId]
position, _ := GetPosition(tokenId)
return position
}

func PositionGetPositionNonce(tokenId uint64) *u256.Uint {
return positions[tokenId].nonce
position := MustGetPosition(tokenId)
return position.nonce
}

func PositionGetPositionOperator(tokenId uint64) std.Address {
return positions[tokenId].operator
position := MustGetPosition(tokenId)
return position.operator
}

func PositionGetPositionPoolKey(tokenId uint64) string {
return positions[tokenId].poolKey
position := MustGetPosition(tokenId)
return position.poolKey
}

func PositionGetPositionTickLower(tokenId uint64) int32 {
return positions[tokenId].tickLower
position := MustGetPosition(tokenId)
return position.tickLower
}

func PositionGetPositionTickUpper(tokenId uint64) int32 {
return positions[tokenId].tickUpper
position := MustGetPosition(tokenId)
return position.tickUpper
}

func PositionGetPositionLiquidity(tokenId uint64) *u256.Uint {
return positions[tokenId].liquidity
position := MustGetPosition(tokenId)
return position.liquidity
}

func PositionGetPositionFeeGrowthInside0LastX128(tokenId uint64) *u256.Uint {
return positions[tokenId].feeGrowthInside0LastX128
position := MustGetPosition(tokenId)
return position.feeGrowthInside0LastX128
}

func PositionGetPositionFeeGrowthInside1LastX128(tokenId uint64) *u256.Uint {
return positions[tokenId].feeGrowthInside1LastX128
position := MustGetPosition(tokenId)
return position.feeGrowthInside1LastX128
}

func PositionGetPositionTokensOwed0(tokenId uint64) *u256.Uint {
return positions[tokenId].tokensOwed0
position := MustGetPosition(tokenId)
return position.tokensOwed0
}

func PositionGetPositionTokensOwed1(tokenId uint64) *u256.Uint {
return positions[tokenId].tokensOwed1
position := MustGetPosition(tokenId)
return position.tokensOwed1
}

func PositionIsInRange(tokenId uint64) bool {
position := positions[tokenId]
position := MustGetPosition(tokenId)
poolPath := position.poolKey
poolCurrentTick := pl.PoolGetSlot0Tick(poolPath)

Expand Down
12 changes: 6 additions & 6 deletions position/_GET_no_receiver_string.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ package position

// type Position
func PositionGetPositionNonceStr(tokenId uint64) string {
return positions[tokenId].nonce.ToString()
return MustGetPosition(tokenId).nonce.ToString()
}

func PositionGetPositionLiquidityStr(tokenId uint64) string {
return positions[tokenId].liquidity.ToString()
return MustGetPosition(tokenId).liquidity.ToString()
}

func PositionGetPositionFeeGrowthInside0LastX128Str(tokenId uint64) string {
return positions[tokenId].feeGrowthInside0LastX128.ToString()
return MustGetPosition(tokenId).feeGrowthInside0LastX128.ToString()
}

func PositionGetPositionFeeGrowthInside1LastX128Str(tokenId uint64) string {
return positions[tokenId].feeGrowthInside1LastX128.ToString()
return MustGetPosition(tokenId).feeGrowthInside1LastX128.ToString()
}

func PositionGetPositionTokensOwed0Str(tokenId uint64) string {
return positions[tokenId].tokensOwed0.ToString()
return MustGetPosition(tokenId).tokensOwed0.ToString()
}

func PositionGetPositionTokensOwed1Str(tokenId uint64) string {
return positions[tokenId].tokensOwed1.ToString()
return MustGetPosition(tokenId).tokensOwed1.ToString()
}
Loading
Loading