-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
GSW-636 fix wrap unwrap
- Loading branch information
Showing
8 changed files
with
507 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package tc | ||
|
||
import ( | ||
"encoding/gjson" | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/testutils" | ||
|
||
_ "gno.land/r/grc20_wrapper" | ||
pl "gno.land/r/pool" | ||
pos "gno.land/r/position" | ||
rou "gno.land/r/router" | ||
) | ||
|
||
var ( | ||
pc01 = testutils.TestAddress("pc01") // Pool Creator 01 | ||
lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 | ||
tr01 = testutils.TestAddress("tr01") // Trader 01 | ||
|
||
poolAddr = std.DerivePkgAddr("gno.land/r/pool") | ||
posAddr = std.DerivePkgAddr("gno.land/r/position") | ||
routerAddr = std.DerivePkgAddr("gno.land/r/router") | ||
) | ||
|
||
var ( | ||
// Common | ||
barPath = "gno.land/r/bar" | ||
gnsPath = "gno.land/r/gns" | ||
gnotPath = "gnot" | ||
|
||
MAX_TIMEOUT bigint = 9999999999 | ||
) | ||
|
||
// debug addr | ||
func init() { | ||
println(pc01, "// pc01") | ||
println(lp01, "// lp01") | ||
println(tr01, "// tr01") | ||
println(poolAddr, "// pool") | ||
println(posAddr, "// pos") | ||
println(routerAddr, "// router") | ||
} | ||
|
||
func TestInitManual(t *testing.T) { | ||
std.TestSetOrigCaller(pc01) | ||
pl.InitManual() | ||
std.TestSkipHeights(1) | ||
} | ||
|
||
func TestCreatePool(t *testing.T) { | ||
std.TestSetOrigCaller(pc01) | ||
|
||
// gns > bar | ||
// gns:gnot | ||
// gnot:bar | ||
|
||
pl.CreatePool(gnsPath, gnotPath, uint16(100), 130621891405341611593710811006) // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 | ||
pl.CreatePool(gnotPath, barPath, uint16(100), 130621891405341611593710811006) // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 | ||
// 1 gns ≈ 7.29 bar | ||
|
||
jsonOutput := pl.ApiGetPools() | ||
jsonStr := gjson.Parse(jsonOutput) | ||
shouldEQ(t, len(jsonStr.Get("response.data").Array()), 2) | ||
} | ||
|
||
func TestPositionMintGnsGnot(t *testing.T) { | ||
std.TestSetOrigCaller(lp01) | ||
|
||
// prepare ugnot | ||
testBanker := std.GetBanker(std.BankerTypeRealmIssue) | ||
testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 99999) | ||
|
||
// send | ||
std.TestSetOrigSend(std.Coins{{"ugnot", 99999}}, nil) | ||
testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 99999) | ||
|
||
tokenId, liquidity, amount0, amount1 := pos.Mint(gnsPath, gnotPath, uint16(100), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) | ||
shouldEQ(t, tokenId, uint64(1)) | ||
shouldEQ(t, amount0 > 0, true) // 36789 | ||
shouldEQ(t, amount1 > 0, true) // 99999 | ||
} | ||
|
||
func TestPositionMintGnotBar(t *testing.T) { | ||
std.TestSetOrigCaller(lp01) | ||
|
||
// prepare ugnot | ||
testBanker := std.GetBanker(std.BankerTypeRealmIssue) | ||
testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 99999) | ||
|
||
// send | ||
std.TestSetOrigSend(std.Coins{{"ugnot", 99999}}, nil) | ||
testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 99999) | ||
|
||
tokenId, liquidity, amount0, amount1 := pos.Mint(gnotPath, barPath, uint16(100), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) | ||
shouldEQ(t, tokenId, uint64(2)) | ||
shouldEQ(t, amount0 > 0, true) // 99999 | ||
shouldEQ(t, amount1 > 0, true) // 36865 | ||
} | ||
|
||
func TestDrySwapRouteGnsBarExactIn(t *testing.T) { | ||
std.TestSetOrigCaller(lp01) | ||
|
||
dryResult := rou.DrySwapRoute( | ||
gnsPath, // inputToken | ||
barPath, // outputToken | ||
bigint(1000), // amountSpecified | ||
"EXACT_IN", // swapType | ||
"gno.land/r/gns:gnot:100*POOL*gnot:gno.land/r/bar:100", // strRouteArr | ||
"100", // quoteArr | ||
) | ||
shouldEQ(t, dryResult, bigint(7339)) | ||
} | ||
|
||
func TestSwapRouteGnsBarExactIn(t *testing.T) { | ||
std.TestSetOrigCaller(lp01) | ||
|
||
std.TestSetOrigSend(std.Coins{{"", 0}}, nil) | ||
swapResult := rou.SwapRoute( | ||
gnsPath, // inputToken | ||
barPath, // outputToken | ||
bigint(1000), // amountSpecified | ||
"EXACT_IN", // swapType | ||
"gno.land/r/gns:gnot:100*POOL*gnot:gno.land/r/bar:100", // strRouteArr | ||
"100", // quoteArr | ||
"0", // tokenAmountLimit | ||
) | ||
shouldEQ(t, swapResult, bigint(7339)) | ||
} | ||
|
||
/* HELPER */ | ||
func shouldEQ(t *testing.T, got, expected interface{}) { | ||
if got != expected { | ||
t.Errorf("got %v, expected %v", got, expected) | ||
} | ||
} | ||
|
||
func ugnotBalance(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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package tc | ||
|
||
import ( | ||
"encoding/gjson" | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/testutils" | ||
|
||
_ "gno.land/r/grc20_wrapper" | ||
pl "gno.land/r/pool" | ||
pos "gno.land/r/position" | ||
rou "gno.land/r/router" | ||
) | ||
|
||
var ( | ||
pc01 = testutils.TestAddress("pc01") // Pool Creator 01 | ||
lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 | ||
tr01 = testutils.TestAddress("tr01") // Trader 01 | ||
|
||
poolAddr = std.DerivePkgAddr("gno.land/r/pool") | ||
posAddr = std.DerivePkgAddr("gno.land/r/position") | ||
routerAddr = std.DerivePkgAddr("gno.land/r/router") | ||
) | ||
|
||
var ( | ||
// Common | ||
fooPath = "gno.land/r/foo" | ||
barPath = "gno.land/r/bar" | ||
gnsPath = "gno.land/r/gns" | ||
quxPath = "gno.land/r/qux" | ||
gnotPath = "gnot" | ||
|
||
MAX_TIMEOUT bigint = 9999999999 | ||
) | ||
|
||
// // debug addr | ||
// func init() { | ||
// println(pc01, "// pc01") | ||
// println(lp01, "// lp01") | ||
// println(tr01, "// tr01") | ||
// println(poolAddr, "// pool") | ||
// println(posAddr, "// pos") | ||
// println(routerAddr, "// router") | ||
// } | ||
|
||
func TestInitManual(t *testing.T) { | ||
std.TestSetOrigCaller(pc01) | ||
pl.InitManual() | ||
std.TestSkipHeights(1) | ||
} | ||
|
||
func TestCreatePool(t *testing.T) { | ||
std.TestSetOrigCaller(pc01) | ||
|
||
// WRAP | ||
// gns > bar | ||
// gns:gnot | ||
// gnot:bar | ||
pl.CreatePool(gnsPath, gnotPath, uint16(100), 130621891405341611593710811006) // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 | ||
pl.CreatePool(gnotPath, barPath, uint16(100), 130621891405341611593710811006) // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 | ||
|
||
// UNWRAP | ||
// foo > qux | ||
// qux > ugnot | ||
pl.CreatePool(fooPath, quxPath, uint16(100), 130621891405341611593710811006) // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 | ||
pl.CreatePool(quxPath, gnotPath, uint16(100), 130621891405341611593710811006) // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 | ||
// 1 foo ≈ 7.29 gnot | ||
|
||
jsonOutput := pl.ApiGetPools() | ||
jsonStr := gjson.Parse(jsonOutput) | ||
shouldEQ(t, len(jsonStr.Get("response.data").Array()), 4) | ||
} | ||
|
||
func TestPositionMintGnsGnot(t *testing.T) { | ||
std.TestSetOrigCaller(lp01) | ||
|
||
// prepare ugnot | ||
testBanker := std.GetBanker(std.BankerTypeRealmIssue) | ||
testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 99999) | ||
|
||
// send | ||
std.TestSetOrigSend(std.Coins{{"ugnot", 99999}}, nil) | ||
testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 99999) | ||
|
||
tokenId, liquidity, amount0, amount1 := pos.Mint(gnsPath, gnotPath, uint16(100), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) | ||
shouldEQ(t, tokenId, uint64(1)) | ||
shouldEQ(t, amount0 > 0, true) // 36789 | ||
shouldEQ(t, amount1 > 0, true) // 99999 | ||
} | ||
|
||
func TestPositionMintGnotBar(t *testing.T) { | ||
std.TestSetOrigCaller(lp01) | ||
|
||
// prepare ugnot | ||
testBanker := std.GetBanker(std.BankerTypeRealmIssue) | ||
testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 99999) | ||
|
||
// send | ||
std.TestSetOrigSend(std.Coins{{"ugnot", 99999}}, nil) | ||
testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 99999) | ||
|
||
tokenId, liquidity, amount0, amount1 := pos.Mint(gnotPath, barPath, uint16(100), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) | ||
shouldEQ(t, tokenId, uint64(2)) | ||
shouldEQ(t, amount0 > 0, true) // 99999 | ||
shouldEQ(t, amount1 > 0, true) // 36865 | ||
} | ||
|
||
func TestPositionMintFooQux(t *testing.T) { | ||
std.TestSetOrigCaller(lp01) | ||
|
||
tokenId, liquidity, amount0, amount1 := pos.Mint(fooPath, quxPath, uint16(100), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) | ||
shouldEQ(t, tokenId, uint64(3)) | ||
shouldEQ(t, amount0 > 0, true) // 99999 | ||
shouldEQ(t, amount1 > 0, true) // 36865 | ||
} | ||
|
||
func TestPositionMintQuxGnot(t *testing.T) { | ||
std.TestSetOrigCaller(lp01) | ||
|
||
// prepare ugnot | ||
testBanker := std.GetBanker(std.BankerTypeRealmIssue) | ||
testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 99999) | ||
|
||
// send | ||
std.TestSetOrigSend(std.Coins{{"ugnot", 99999}}, nil) | ||
testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 99999) | ||
|
||
tokenId, liquidity, amount0, amount1 := pos.Mint(quxPath, gnotPath, uint16(100), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) | ||
shouldEQ(t, tokenId, uint64(4)) | ||
shouldEQ(t, amount0 > 0, true) // 99999 | ||
shouldEQ(t, amount1 > 0, true) // 36865 | ||
} | ||
|
||
// Wrap TEST | ||
func TestSwapRouteGnsBarExactIn(t *testing.T) { | ||
std.TestSetOrigCaller(tr01) | ||
|
||
trOldUgnot := ugnotBalance(tr01) | ||
shouldEQ(t, trOldUgnot, uint64(0)) | ||
|
||
std.TestSetOrigSend(std.Coins{{"", 0}}, nil) | ||
swapResult := rou.SwapRoute( | ||
gnsPath, // inputToken | ||
barPath, // outputToken | ||
bigint(1000), // amountSpecified | ||
"EXACT_IN", // swapType | ||
"gno.land/r/gns:gnot:100*POOL*gnot:gno.land/r/bar:100", // strRouteArr | ||
"100", // quoteArr | ||
"0", // tokenAmountLimit | ||
) | ||
|
||
trNewUgnot := ugnotBalance(tr01) | ||
shouldEQ(t, trNewUgnot, uint64(0)) | ||
|
||
shouldEQ(t, swapResult, bigint(7339)) | ||
} | ||
|
||
// UnWrap TEST | ||
func TestSwapRouteFooGnotExactIn(t *testing.T) { | ||
std.TestSetOrigCaller(tr01) | ||
|
||
trOldUgnot := ugnotBalance(tr01) | ||
shouldEQ(t, trOldUgnot, uint64(0)) | ||
|
||
std.TestSetOrigSend(std.Coins{{"", 0}}, nil) | ||
swapResult := rou.SwapRoute( | ||
fooPath, // inputToken | ||
gnotPath, // outputToken | ||
bigint(1000), // amountSpecified | ||
"EXACT_IN", // swapType | ||
"gno.land/r/foo:gno.land/r/qux:100*POOL*gno.land/r/qux:gnot:100", // strRouteArr | ||
"100", // quoteArr | ||
"0", // tokenAmountLimit | ||
) | ||
shouldEQ(t, swapResult, bigint(7339)) | ||
|
||
trNewUgnot := ugnotBalance(tr01) | ||
shouldEQ(t, trNewUgnot, uint64(7339)) | ||
} | ||
|
||
/* HELPER */ | ||
func shouldEQ(t *testing.T, got, expected interface{}) { | ||
if got != expected { | ||
t.Errorf("got %v, expected %v", got, expected) | ||
} | ||
} | ||
|
||
func ugnotBalance(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) | ||
} |
Oops, something went wrong.