Skip to content

Commit

Permalink
test: calculateSqrtPriceLimitForSwap
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Dec 11, 2024
1 parent 702bc8a commit f3c9ca1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 11 deletions.
31 changes: 30 additions & 1 deletion router/router_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package router

import (
"std"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -168,7 +169,7 @@ func TestProcessResult(t *testing.T) {
expectError: false,
},
{
name: "ExactIn amount mismatch",
name: "ExactIn amount mismatt.",
swapType: ExactIn,
resultAmountIn: u256.MustFromDecimal("90"),
resultAmountOut: u256.MustFromDecimal("85"),
Expand Down Expand Up @@ -366,6 +367,7 @@ type MockGRC20 struct {
TransferFromFn func(from, to pusers.AddressOrName, amount uint64)
BalanceOfFn func(owner pusers.AddressOrName) uint64
ApproveFn func(spender pusers.AddressOrName, amount uint64)
AllowanceFn func(owner, spender pusers.AddressOrName) uint64
}

func (m MockGRC20) Transfer() func(to pusers.AddressOrName, amount uint64) {
Expand All @@ -383,3 +385,30 @@ func (m MockGRC20) BalanceOf() func(owner pusers.AddressOrName) uint64 {
func (m MockGRC20) Approve() func(spender pusers.AddressOrName, amount uint64) {
return m.ApproveFn
}

func (m MockGRC20) Allowance() func(owner, spender pusers.AddressOrName) uint64 {
if m.AllowanceFn != nil {
return m.AllowanceFn
}
return func(owner, spender pusers.AddressOrName) uint64 {
return 1000000000000
}
}

func setupTestPool(
t *testing.T,
token0Path, token1Path string,
fee uint32,
sqrtPriceX96 string,
) {
t.Helper()

std.TestSetRealm(std.NewUserRealm(consts.ADMIN))
pl.SetPoolCreationFeeByAdmin(1)

if token0Path > token1Path {
t.Fatalf("tokens are not sorted: %s > %s", token0Path, token1Path)
}

pl.CreatePool(token0Path, token1Path, fee, sqrtPriceX96)
}
8 changes: 4 additions & 4 deletions router/swap_inner.gno
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func calculateSqrtPriceLimitForSwap(zeroForOne bool, fee uint32, sqrtPriceLimitX
return sqrtPriceLimitX96.Sub(sqrtPriceLimitX96, u256.One())
}

func _swap(
func swapInner(
amountSpecified *i256.Int,
recipient std.Address,
sqrtPriceLimitX96 *u256.Uint,
Expand Down Expand Up @@ -66,7 +66,7 @@ func _swap(
return poolRecv.Abs(), poolOut.Abs()
}

func _swapDry(
func swapDryInner(
amountSpecified *i256.Int,
sqrtPriceLimitX96 *u256.Uint,
data SwapCallbackData,
Expand Down Expand Up @@ -117,7 +117,7 @@ func getMinTick(fee uint32) int32 {
default:
panic(addDetailToError(
errInvalidPoolFeeTier,
ufmt.Sprintf("swap_inner.gno__getMaxTick() || unknown fee(%d)", fee),
ufmt.Sprintf("swapInner.gno__getMaxTick() || unknown fee(%d)", fee),
))
}
}
Expand All @@ -135,7 +135,7 @@ func getMaxTick(fee uint32) int32 {
default:
panic(addDetailToError(
errInvalidPoolFeeTier,
ufmt.Sprintf("swap_inner.gno__getMaxTick() || unknown fee(%d)", fee),
ufmt.Sprintf("swapInner.gno__getMaxTick() || unknown fee(%d)", fee),
))
}
}
65 changes: 65 additions & 0 deletions router/swap_inner_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package router

import (
"testing"

"gno.land/r/gnoswap/v1/common"
u256 "gno.land/p/gnoswap/uint256"
)

func TestCalculateSqrtPriceLimitForSwap(t *testing.T) {
tests := []struct {
name string
zeroForOne bool
fee uint32
sqrtPriceLimitX96 *u256.Uint
expected *u256.Uint
}{
{
name: "already set sqrtPriceLimit",
zeroForOne: true,
fee: 500,
sqrtPriceLimitX96: u256.NewUint(1000),
expected: u256.NewUint(1000),
},
{
name: "when zeroForOne is true, calculate min tick",
zeroForOne: true,
fee: 500,
sqrtPriceLimitX96: u256.Zero(),
expected: common.TickMathGetSqrtRatioAtTick(getMinTick(500)).Add(
common.TickMathGetSqrtRatioAtTick(getMinTick(500)),
u256.One(),
),
},
{
name: "when zeroForOne is false, calculate max tick",
zeroForOne: false,
fee: 500,
sqrtPriceLimitX96: u256.Zero(),
expected: common.TickMathGetSqrtRatioAtTick(getMaxTick(500)).Sub(
common.TickMathGetSqrtRatioAtTick(getMaxTick(500)),
u256.One(),
),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := calculateSqrtPriceLimitForSwap(
tt.zeroForOne,
tt.fee,
tt.sqrtPriceLimitX96,
)

if !result.Eq(tt.expected) {
t.Errorf(
"case '%s': expected %s, actual %s",
tt.name,
tt.expected.ToString(),
result.ToString(),
)
}
})
}
}
8 changes: 4 additions & 4 deletions router/swap_multi.gno
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func multiSwap(params SwapParams, currentPoolIndex, numPools int, swapPath strin
recipient = params.recipient
}

amountIn, amountOut := _swap(
amountIn, amountOut := swapInner(
params.amountSpecified,
recipient,
u256.Zero(),
Expand Down Expand Up @@ -109,7 +109,7 @@ func multiSwapNegative(params SwapParams, numPools int, swapPath string) (*u256.
recipient = consts.ROUTER_ADDR
}

amountIn, amountOut := _swap(
amountIn, amountOut := swapInner(
swapInfo[currentPoolIndex].amountSpecified,
recipient,
u256.Zero(),
Expand Down Expand Up @@ -143,7 +143,7 @@ func multiSwapDry(params SwapParams, currentPoolIndex, numPool int, swapPath str
for {
currentPoolIndex++

amountIn, amountOut := _swapDry(
amountIn, amountOut := swapDryInner(
params.amountSpecified,
u256.Zero(),
SwapCallbackData{
Expand Down Expand Up @@ -178,7 +178,7 @@ func multiSwapNegativeDry(params SwapParams, currentPoolIndex int, swapPath stri
payer := consts.ROUTER_ADDR

for {
amountIn, amountOut := _swapDry(
amountIn, amountOut := swapDryInner(
params.amountSpecified,
u256.Zero(),
SwapCallbackData{
Expand Down
4 changes: 2 additions & 2 deletions router/swap_single.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func singleSwap(params SingleSwapParams) (*u256.Uint, *u256.Uint) { // amountIn, amountOut
amountIn, amountOut := _swap(
amountIn, amountOut := swapInner(
params.amountSpecified,
std.PrevRealm().Addr(), // if single swap => user will recieve
u256.Zero(), // sqrtPriceLimitX96
Expand All @@ -23,7 +23,7 @@ func singleSwap(params SingleSwapParams) (*u256.Uint, *u256.Uint) { // amountIn,
}

func singleSwapDry(params SingleSwapParams) (*u256.Uint, *u256.Uint) { // amountIn, amountOut
amountIn, amountOut := _swapDry(
amountIn, amountOut := swapDryInner(
params.amountSpecified,
u256.Zero(), // sqrtPriceLimitX96
SwapCallbackData{
Expand Down

0 comments on commit f3c9ca1

Please sign in to comment.