Skip to content

Commit 099f1cb

Browse files
committed
test: define test helper function
1 parent 1f5a147 commit 099f1cb

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

_deploy/p/gnoswap/pool/swap_math_test.gno

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ func TestSwapMathComputeSwapStepStr(t *testing.T) {
2222
}{
2323
{
2424
name: "exact amount in that gets capped at price target in one for zero",
25-
currentX96: encodePriceSqrt("1", "1"),
26-
targetX96: encodePriceSqrt("101", "100"),
25+
currentX96: encodePriceSqrt(t, "1", "1"),
26+
targetX96: encodePriceSqrt(t, "101", "100"),
2727
liquidity: u256.MustFromDecimal("2000000000000000000"),
2828
amountRemaining: i256.MustFromDecimal("1000000000000000000"),
2929
feePips: 600,
30-
sqrtNextX96: encodePriceSqrt("101", "100"),
30+
sqrtNextX96: encodePriceSqrt(t, "101", "100"),
3131
chkSqrtNextX96: func(sqrtRatioNextX96, priceTarget *u256.Uint) {
3232
uassert.True(t, sqrtRatioNextX96.Eq(priceTarget))
3333
},
@@ -37,12 +37,12 @@ func TestSwapMathComputeSwapStepStr(t *testing.T) {
3737
},
3838
{
3939
name: "exact amount out that gets capped at price target in one for zero",
40-
currentX96: encodePriceSqrt("1", "1"),
41-
targetX96: encodePriceSqrt("101", "100"),
40+
currentX96: encodePriceSqrt(t, "1", "1"),
41+
targetX96: encodePriceSqrt(t, "101", "100"),
4242
liquidity: u256.MustFromDecimal("2000000000000000000"),
4343
amountRemaining: i256.MustFromDecimal("-1000000000000000000"),
4444
feePips: 600,
45-
sqrtNextX96: encodePriceSqrt("101", "100"),
45+
sqrtNextX96: encodePriceSqrt(t, "101", "100"),
4646
chkSqrtNextX96: func(sqrtRatioNextX96, priceTarget *u256.Uint) {
4747
uassert.True(t, sqrtRatioNextX96.Eq(priceTarget))
4848
},
@@ -52,11 +52,11 @@ func TestSwapMathComputeSwapStepStr(t *testing.T) {
5252
},
5353
{
5454
name: "exact amount in that is fully spent in one for zero",
55-
currentX96: encodePriceSqrt("1", "1"),
56-
targetX96: encodePriceSqrt("1000", "100"),
55+
currentX96: encodePriceSqrt(t, "1", "1"),
56+
targetX96: encodePriceSqrt(t, "1000", "100"),
5757
liquidity: u256.MustFromDecimal("2000000000000000000"),
5858
amountRemaining: i256.MustFromDecimal("1000000000000000000"),
59-
sqrtNextX96: encodePriceSqrt("1000", "100"),
59+
sqrtNextX96: encodePriceSqrt(t, "1000", "100"),
6060
feePips: 600,
6161
chkSqrtNextX96: func(sqrtRatioNextX96, priceTarget *u256.Uint) {
6262
uassert.True(t, sqrtRatioNextX96.Lte(priceTarget))
@@ -67,12 +67,12 @@ func TestSwapMathComputeSwapStepStr(t *testing.T) {
6767
},
6868
{
6969
name: "exact amount out that is fully received in one for zero",
70-
currentX96: encodePriceSqrt("1", "1"),
71-
targetX96: encodePriceSqrt("1000", "100"),
70+
currentX96: encodePriceSqrt(t, "1", "1"),
71+
targetX96: encodePriceSqrt(t, "1000", "100"),
7272
liquidity: u256.MustFromDecimal("2000000000000000000"),
7373
amountRemaining: i256.MustFromDecimal("-1000000000000000000"),
7474
feePips: 600,
75-
sqrtNextX96: encodePriceSqrt("1000", "100"),
75+
sqrtNextX96: encodePriceSqrt(t, "1000", "100"),
7676
chkSqrtNextX96: func(sqrtRatioNextX96, priceTarget *u256.Uint) {
7777
uassert.True(t, sqrtRatioNextX96.Lt(priceTarget))
7878
},
@@ -169,7 +169,9 @@ func TestSwapMathComputeSwapStepStr(t *testing.T) {
169169
}
170170

171171
// encodePriceSqrt calculates the sqrt((reserve1 << 192) / reserve0)
172-
func encodePriceSqrt(reserve1, reserve0 string) *u256.Uint {
172+
func encodePriceSqrt(t *testing.T, reserve1, reserve0 string) *u256.Uint {
173+
t.Helper()
174+
173175
reserve1Uint := u256.MustFromDecimal(reserve1)
174176
reserve0Uint := u256.MustFromDecimal(reserve0)
175177

@@ -185,11 +187,13 @@ func encodePriceSqrt(reserve1, reserve0 string) *u256.Uint {
185187
ratioX192 := new(u256.Uint).Div(numerator, reserve0Uint)
186188

187189
// Return sqrt(ratioX192)
188-
return sqrt(ratioX192)
190+
return sqrt(t, ratioX192)
189191
}
190192

191193
// sqrt computes the integer square root of a u256.Uint
192-
func sqrt(x *u256.Uint) *u256.Uint {
194+
func sqrt(t *testing.T, x *u256.Uint) *u256.Uint {
195+
t.Helper()
196+
193197
if x.IsZero() {
194198
return u256.NewUint(0)
195199
}

0 commit comments

Comments
 (0)