Skip to content

Commit

Permalink
Merge pull request #77 from gnoswap-labs/GSW-431-feat-return-pool-det…
Browse files Browse the repository at this point in the history
…ail-in-find-best-pool

GSW-431 feat: return pool detail in `FindBestPool()`
  • Loading branch information
r3v4s authored Oct 16, 2023
2 parents 7b8d21d + b7f37b8 commit 5168cdb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
21 changes: 13 additions & 8 deletions pool/_TEST_pool_router_test.gnoa
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pool

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

Expand Down Expand Up @@ -90,43 +91,47 @@ func TestPositionMint3000(t *testing.T) {
}

func TestFindBestPoolTruePositive(t *testing.T) {
bestPath := FindBestPool(
bestPoolPathDetail := FindBestPool(
"foo", // tokenA
"bar", // tokenB
true, // zeroForOne
500, // amountSpecified
)
shouldEQ(t, bestPath, "bar_foo_500")
jsonStr := gjson.Parse(bestPoolPathDetail)
shouldEQ(t, jsonStr.Get("response.data.pool_path").String(), "bar_foo_500")
}

func TestFindBestPoolTrueNegative(t *testing.T) {
bestPath := FindBestPool(
bestPoolPathDetail := FindBestPool(
"foo", // tokenA
"bar", // tokenB
true, // zeroForOne
-5000, // amountSpecified
)
shouldEQ(t, bestPath, "bar_foo_500")
jsonStr := gjson.Parse(bestPoolPathDetail)
shouldEQ(t, jsonStr.Get("response.data.pool_path").String(), "bar_foo_500")
}

func TestFindBestPoolFalsePositive(t *testing.T) {
bestPath := FindBestPool(
bestPoolPathDetail := FindBestPool(
"foo", // tokenA
"bar", // tokenB
false, // zeroForOne
50, // amountSpecified
)
shouldEQ(t, bestPath, "bar_foo_3000")
jsonStr := gjson.Parse(bestPoolPathDetail)
shouldEQ(t, jsonStr.Get("response.data.pool_path").String(), "bar_foo_3000")
}

func TestFindBestPoolFalseNegative(t *testing.T) {
bestPath := FindBestPool(
bestPoolPathDetail := FindBestPool(
"foo", // tokenA
"bar", // tokenB
false, // zeroForOne
-1234, // amountSpecified
)
shouldEQ(t, bestPath, "bar_foo_500")
jsonStr := gjson.Parse(bestPoolPathDetail)
shouldEQ(t, jsonStr.Get("response.data.pool_path").String(), "bar_foo_500")
}

func TestFindBestPoolWrong(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pool/getter_api.gno
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type ApiPositionInfo struct {
}

type SinglePool struct {
PoolPath string `json:"pool_path"`
T0Balance bigint `json:"token0_balance"`
T1Balance bigint `json:"token1_balance"`
TickSpacing int32 `json:"tick_spacing"`
Expand Down Expand Up @@ -131,6 +132,7 @@ func ApiGetPool(key string) string {
Data SinglePool `json:"data"`
}{
Data: SinglePool{
PoolPath: key,
T0Balance: pool.balances.token0,
T1Balance: pool.balances.token1,
TickSpacing: pool.tickSpacing,
Expand Down
4 changes: 2 additions & 2 deletions pool/pool_router.gno
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ func FindBestPool(
}

if zeroForOne == true { // if token0 is being sold to buy token1, then we want to find the pool with the largest tick (more token1 can be bought)
return poolWithTick[maxTick]
return ApiGetPool(poolWithTick[maxTick])

} else { // if token1 is being sold to buy token0, then we want to find the pool with the smallest tick (more token0 can be bought)
return poolWithTick[minTick]
return ApiGetPool(poolWithTick[minTick])
}
}

Expand Down

0 comments on commit 5168cdb

Please sign in to comment.