-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: calculateSqrtPriceLimitForSwap
- Loading branch information
Showing
5 changed files
with
105 additions
and
11 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
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,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(), | ||
) | ||
} | ||
}) | ||
} | ||
} |
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