Skip to content

Commit

Permalink
fix???
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Feb 19, 2024
1 parent e7a4d13 commit a1f3a33
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 33 deletions.
66 changes: 33 additions & 33 deletions pool/sqrt_price_math.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ import (
)

func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(
sqrtPX96 bigint,
liquidity bigint,
amount bigint,
add bool,
sqrtPX96 bigint,
liquidity bigint,
amount bigint,
add bool,
) bigint {
requireUnsigned(
sqrtPX96,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected sqrtPX96 must be unsigned, got: %d",
sqrtPX96,
))
))

requireUnsigned(
liquidity,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected liquidity must be unsigned, got: %d",
liquidity,
))
))

requireUnsigned(
amount,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected amount must be unsigned, got: %d",
amount,
))
))

if amount == 0 {
return sqrtPX96
Expand All @@ -47,7 +47,7 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected denominator != 0, got: %d",
denominator,
))
))

return numerator1 * sqrtPX96 / denominator
}
Expand All @@ -59,7 +59,7 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp() || expected denominator >= 0, got: %d",
denominator,
))
))

return numerator1 / ((numerator1 / sqrtPX96) + amount)
}
Expand All @@ -75,21 +75,21 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || expected sqrtPX96 must be unsigned, got: %d",
sqrtPX96,
))
))

requireUnsigned(
amount,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || expected amount must be unsigned, got: %d",
amount,
))
))

requireUnsigned(
liquidity,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown() || expected liquidity must be unsigned, got: %d",
liquidity,
))
))

var quotient bigint
if amount <= consts.MAX_UINT160 {
Expand Down Expand Up @@ -126,7 +126,7 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(
)

return result
}
}

func sqrtPriceMathGetNextSqrtPriceFromInput(
sqrtPX96 bigint,
Expand All @@ -146,15 +146,14 @@ func sqrtPriceMathGetNextSqrtPriceFromInput(
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || expected liquidity must be unsigned, got: %d",
liquidity,
))
))

requireUnsigned(
amountIn,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromInput() || expected amountIn must be unsigned, got: %d",
amountIn,
))

))

if zeroForOne {
amount0Result := sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true)
Expand Down Expand Up @@ -187,14 +186,14 @@ func sqrtPriceMathGetNextSqrtPriceFromOutput(
requireUnsigned(
sqrtPX96,
ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || expected sqrtPX96 >= 0, got: %s",
sqrtPX96,
))
sqrtPX96,
))

requireUnsigned(
liquidity,
ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetNextSqrtPriceFromOutput() || expected liquidity >= 0, got: %d",
liquidity,
))
liquidity,
))

if zeroForOne {
amount1Result := sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false)
Expand Down Expand Up @@ -225,21 +224,21 @@ func sqrtPriceMathGetAmount0DeltaHelper(
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected sqrtRatioAX96 must be unsigned, got: %d",
sqrtRatioAX96,
))
))

requireUnsigned(
sqrtRatioBX96,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected sqrtRatioBX96 must be unsigned, got: %d",
sqrtRatioBX96,
))
))

requireUnsigned(
liquidity,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0DeltaHelper() || expected liquidity must be unsigned, got: %d",
liquidity,
))
))

if sqrtRatioAX96 > sqrtRatioBX96 {
sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96
Expand All @@ -259,22 +258,22 @@ func sqrtPriceMathGetAmount1DeltaHelper(
requireUnsigned(
sqrtRatioAX96,
ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1DeltaHelper() || expected sqrtRatioAX96 must be unsigned, got: %d",
sqrtRatioAX96,
))
sqrtRatioAX96,
))

requireUnsigned(
sqrtRatioBX96,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1DeltaHelper() || expected sqrtRatioBX96 must be unsigned, got: %d",
sqrtRatioBX96,
))
))

requireUnsigned(
liquidity,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1DeltaHelper() || expected liquidity must be unsigned, got: %d",
liquidity,
))
))

if sqrtRatioAX96 > sqrtRatioBX96 {
sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96
Expand All @@ -293,19 +292,20 @@ func sqrtPriceMathGetAmount0Delta(
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0Delta() || expected sqrtRatioAX96 must be unsigned, got: %d",
sqrtRatioAX96,
))
))

requireUnsigned(
sqrtRatioBX96,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount0Delta() || expected sqrtRatioBX96 must be unsigned, got: %d",
sqrtRatioBX96,
))
))

if liquidity < 0 {
return -sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, absBigint(-liquidity))
return -sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, liquidity)
}

println("THIS")
return sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, liquidity)
}

Expand All @@ -317,15 +317,15 @@ func sqrtPriceMathGetAmount1Delta(
requireUnsigned(
sqrtRatioAX96,
ufmt.Sprintf("[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1Delta() || expected sqrtRatioAX96 must be unsigned, got: %d",
sqrtRatioAX96,
))
sqrtRatioAX96,
))

requireUnsigned(
sqrtRatioBX96,
ufmt.Sprintf(
"[POOL] sqrt_price_math.gno__sqrtPriceMathGetAmount1Delta() || expected sqrtRatioBX96 must be unsigned, got: %d",
sqrtRatioBX96,
))
))

if liquidity < 0 {
return -sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, absBigint(-liquidity))
Expand Down
38 changes: 38 additions & 0 deletions router/_TEST_RATIO_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package router

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

pl "gno.land/r/demo/pool"
)

func TestInitManual(t *testing.T) {
std.TestSetOrigCaller(test1)
pl.InitManual()
std.TestSkipHeights(1)
}

func TestCreatePool(t *testing.T) {
std.TestSetOrigCaller(test1)

pl.CreatePool(wgnotPath, barPath, fee100, common.TickMathGetSqrtRatioAtTick(1000)) // 1.1051 BAR

jsonOutput := pl.ApiGetPools()
jsonStr := gjson.Parse(jsonOutput)
shouldEQ(t, len(jsonStr.Get("response").Array()), 1)
}

func TestApiGetRatiosFromBase(t *testing.T) {
jsonOutput := ApiGetRatiosFromBase()
jsonStr := gjson.Parse(jsonOutput)
jsonArr := jsonStr.Get("response").Array()
shouldEQ(t, len(jsonArr), 4)
println(jsonArr[0].String())

// shouldEQ(t, jsonArr[0].String(), "{\"path\":\"gno.land/r/demo/wugnot\",\"price\":79228162514264337593543950336}") // 1
// shouldEQ(t, jsonArr[1].String(), "{\"path\":\"gno.land/r/demo/bar\",\"price\":29147869410676662479573841823}") // 0.3678978344
// shouldEQ(t, jsonArr[2].String(), "{\"path\":\"gno.land/r/demo/qux\",\"price\":10723438032895153248244559718}") // 0.1353488165
// shouldEQ(t, jsonArr[3].String(), "{\"path\":\"gno.land/r/demo/baz\",\"price\":1451404646985709758556457134}") // 0.0183193021
}

0 comments on commit a1f3a33

Please sign in to comment.