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

GSW-1070 feat increase liquidity on a pool containing gnot tokens #219

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions _deploy/r/demo/gnoswap/consts/consts.gno
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const (

GNFT_PATH string = "gno.land/r/demo/gnft"
GNFT_ADDR std.Address = std.DerivePkgAddr(GNFT_PATH)

WUGNOT_PATH string = "gno.land/r/demo/wugnot"
WUGNOT_ADDR std.Address = std.DerivePkgAddr(WUGNOT_PATH)
)

// NUMBER
Expand Down
97 changes: 0 additions & 97 deletions gov/z_proposal_filetest.gno

This file was deleted.

227 changes: 227 additions & 0 deletions position/_TEST_/_TEST_position_ZZ_increase_decrease_native_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package position

import (
"std"
"testing"

"gno.land/r/demo/gnoswap/common"
"gno.land/r/demo/gnoswap/consts"

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

pl "gno.land/r/demo/pool"
)

// 1. Create Pool
func TestPoolInitCreatePool(t *testing.T) {
std.TestSetPrevAddr(gsa)

gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE)
pl.CreatePool(consts.GNS_PATH, consts.GNOT, fee500, common.TickMathGetSqrtRatioAtTick(10000).ToString()) // x2.71814592682522526700950038502924144268035888671875
// event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:500}]}
}

func TestMintPosition(t *testing.T) {
std.TestSetPrevAddr(gsa)

gns.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)
wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)

wugnot.Approve(a2u(consts.POSITION_ADDR), consts.UINT64_MAX) // WRAP

// prepare 50000005ugnot (5 for refund test)
testBanker := std.GetBanker(std.BankerTypeRealmIssue)
testBanker.IssueCoin(gsa, "ugnot", 50000005)

// simulate transfer & decrase
gsaNativeBalance := ugnotBalanceOf(gsa)
shouldEQ(t, gsaNativeBalance, 50000005)

std.TestSetOrigSend(std.Coins{{"ugnot", 50000005}}, nil)
testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", -50000005)

gsaNativeBalance = ugnotBalanceOf(gsa)
shouldEQ(t, gsaNativeBalance, 0)

gsaOldWugnotBalance := wugnot.BalanceOf(a2u(gsa))
shouldEQ(t, gsaOldWugnotBalance, 0)

tokenId, liquidity, amount0, amount1 := Mint(
consts.GNS_PATH,
consts.GNOT,
fee500,
8000,
12000,
"50000000",
"50000000",
"0",
"0",
max_timeout,
gsa.String(),
)
// event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:500} {p_tickLower 8000} {p_tickUpper 12000} {tokenId 1} {liquidity 318704392} {amount0 18394892} {amount1 50000000}]}

shouldEQ(t, tokenId, 1)
shouldEQ(t, getNextId(), 2)
shouldEQ(t, amount0, "18394892")
shouldEQ(t, amount1, "50000000")

position := positions[tokenId]
shouldEQ(t, position.poolKey, "gno.land/r/demo/gns:gno.land/r/demo/wugnot:500")

// SPEND ALL WUGNOT
newOldWugnotBalance := wugnot.BalanceOf(a2u(gsa))
shouldEQ(t, gsaOldWugnotBalance, newOldWugnotBalance)

gsaNativeBalance = ugnotBalanceOf(gsa)
shouldEQ(t, gsaNativeBalance, 5)
// 1. 50000005 ugnot sent
// 2. 50000005 ugnot wrapped to wugnot
// 3. 50000000 wugnot spent to mint (amount1)
// 4. refund 50000005 - 50000000 = 5

}

func TestIncreaseLiquidity(t *testing.T) {
std.TestSetPrevAddr(gsa)

gns.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)
wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)

wugnot.Approve(a2u(consts.POSITION_ADDR), consts.UINT64_MAX) // WRAP

pool := getPoolFromLpTokenId(uint64(1))
oldLiquidity := pool.PoolGetLiquidity()

// prepare 10000005ugnot (5 for refund test)
testBanker := std.GetBanker(std.BankerTypeRealmIssue)
testBanker.IssueCoin(gsa, "ugnot", 10000005)

// simulate transfer & decrase
gsaNativeBalance := ugnotBalanceOf(gsa)
shouldEQ(t, gsaNativeBalance, 10000010)

std.TestSetOrigSend(std.Coins{{"ugnot", 10000005}}, nil)
testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", -10000005)

gsaNativeBalance = ugnotBalanceOf(gsa)
shouldEQ(t, gsaNativeBalance, 5)

gsaOldWugnotBalance := wugnot.BalanceOf(a2u(gsa))
shouldEQ(t, gsaOldWugnotBalance, 0)

_, _, m0, m1, _ := IncreaseLiquidity( // tokenId, liq, a0, a1, poolPath
uint64(1), // tokenId
"10000000", // amount0Desired
"10000000", // amount1Desired
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
)
// event: {GNOSWAP gno.land/r/demo/position IncreaseLiquidity [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:500} {liquidity 63740878} {amount0 3678979} {amount1 10000000}]}
shouldEQ(t, m0, "3678979")
shouldEQ(t, m1, "10000000")

newLiquidity := pool.PoolGetLiquidity()

shouldEQ(t, newLiquidity.Gt(oldLiquidity), true)

// SPEND ALL WUGNOT
newOldWugnotBalance := wugnot.BalanceOf(a2u(gsa))
shouldEQ(t, gsaOldWugnotBalance, newOldWugnotBalance)

gsaNativeBalance = ugnotBalanceOf(gsa)
shouldEQ(t, gsaNativeBalance, 10)
// 1. 10000005 ugnot sent
// 2. 10000005 ugnot wrapped to wugnot
// 3. 10000000 wugnot spent to mint (amount1)
// 4. refund 10000005 - 10000000 = 5
// 5. user already had 5 ugnot = 5 + 5
}

func TestDecreaseLiquidityWrapped(t *testing.T) {
std.TestSetPrevRealm("")
std.TestSetOrigCaller(gsa)

oldLiquidity := getPoolFromLpTokenId(uint64(1)).PoolGetLiquidity()

userWugnotBalance := wugnot.BalanceOf(a2u(gsa))
shouldEQ(t, userWugnotBalance, 0)

userUgnotBalance := ugnotBalanceOf(gsa)
shouldEQ(t, userUgnotBalance, 10)

_, _, _, _, a0, a1, _ := DecreaseLiquidity( // tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath
uint64(1), // tokenId
20, // liquidityRatio
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
false, // unwrapResult
)
// --- event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/gns} {p_token1Path gno.land/r/demo/wugnot} {fee0Amount 0} {fee1Amount 0}]}
// --- event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:500}]}
// --- event: {GNOSWAP gno.land/r/demo/position DecreaseLiquidity [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {p_liquidityRatio 20} {poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:500} {liquidity 76489054} {fee0 0} {fee1 0} {amount0 4414773} {amount1 11999999}]}

userWugnotBalance = wugnot.BalanceOf(a2u(gsa)) // wrapped result, so wunogt increased
shouldEQ(t, userWugnotBalance, 11999999)

userUgnotBalance = ugnotBalanceOf(gsa) // wrapped result, so ugnot didn't change
shouldEQ(t, userUgnotBalance, 10)

newLiquidity := getPoolFromLpTokenId(uint64(1)).PoolGetLiquidity()
shouldEQ(t, true, newLiquidity.Lt(oldLiquidity))

// check fee left
tokenId, fee0, fee1, poolPath := CollectFee(1)
// --- event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/gns} {p_token1Path gno.land/r/demo/wugnot} {fee0Amount 0} {fee1Amount 0}]}
// --- event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:500}]}

shouldEQ(t, tokenId, uint64(1))
shouldEQ(t, fee0, "0")
shouldEQ(t, fee1, "0")
}

func TestDecreaseLiquidityUnwrapped(t *testing.T) {
std.TestSetPrevRealm("")
std.TestSetOrigCaller(gsa)

oldLiquidity := getPoolFromLpTokenId(uint64(1)).PoolGetLiquidity()

userWugnotBalance := wugnot.BalanceOf(a2u(gsa))
shouldEQ(t, userWugnotBalance, 11999999)

userUgnotBalance := ugnotBalanceOf(gsa)
shouldEQ(t, userUgnotBalance, 10)

_, _, _, _, a0, a1, _ := DecreaseLiquidity( // tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath
uint64(1), // tokenId
50, // liquidityRatio
"0", // amount0Min
"0", // amount1Min
max_timeout, // deadline
true, // unwrapResult
)
// --- event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/gns} {p_token1Path gno.land/r/demo/wugnot} {fee0Amount 0} {fee1Amount 0}]}
// --- event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:500}]}
// --- event: {GNOSWAP gno.land/r/demo/position DecreaseLiquidity [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {p_liquidityRatio 20} {poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:500} {liquidity 76489054} {fee0 0} {fee1 0} {amount0 4414773} {amount1 11999999}]}

userWugnotBalance = wugnot.BalanceOf(a2u(gsa)) // unwrapped result, so wugnot didn't change
shouldEQ(t, userWugnotBalance, 11999999)

userUgnotBalance = ugnotBalanceOf(gsa) // unwrapped result, so ugnot decreased
shouldEQ(t, userUgnotBalance, 24000009)

newLiquidity := getPoolFromLpTokenId(uint64(1)).PoolGetLiquidity()
shouldEQ(t, true, newLiquidity.Lt(oldLiquidity))

// check fee left
tokenId, fee0, fee1, poolPath := CollectFee(1)
// --- event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/gns} {p_token1Path gno.land/r/demo/wugnot} {fee0Amount 0} {fee1Amount 0}]}
// --- event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:500}]}

shouldEQ(t, tokenId, uint64(1))
shouldEQ(t, fee0, "0")
shouldEQ(t, fee1, "0")
}
Loading
Loading