Skip to content

Commit 71823f1

Browse files
author
0xTopaz
committed
Merge branch 'dry-swap' of https://github.com/gnoswap-labs/gnoswap into dry-swap
2 parents 906ab51 + 0686a75 commit 71823f1

File tree

11 files changed

+413
-290
lines changed

11 files changed

+413
-290
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,7 @@ pyrightconfig.json
348348
.history
349349
.ionide
350350

351-
# End of https://www.toptal.com/developers/gitignore/api/macos,go,goland+all,visualstudiocode,dotenv,python,jupyternotebooks
351+
# End of https://www.toptal.com/developers/gitignore/api/macos,go,goland+all,visualstudiocode,dotenv,python,jupyternotebooks
352+
353+
.scannerwork
354+

_deploy/p/gnoswap/pool/__TEST_swap_math_test.gnoA

Lines changed: 0 additions & 202 deletions
This file was deleted.

_deploy/p/gnoswap/pool/swap_math.gno

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,30 @@ import (
88
// SwapMathComputeSwapStepStr computes the next sqrt price, amount in, amount out, and fee amount
99
// Computes the result of swapping some amount in, or amount out, given the parameters of the swap
1010
// The fee, plus the amount in, will never exceed the amount remaining if the swap's `amountSpecified` is positive
11+
//
1112
// input:
12-
// sqrtRatioCurrentX96: the current sqrt price of the pool
13-
// sqrtRatioTargetX96: The price that cannot be exceeded, from which the direction of the swap is inferred
14-
// liquidity: The usable liquidity of the pool
15-
// amountRemaining: How much input or output amount is remaining to be swapped in/out
16-
// feePips: The fee taken from the input amount, expressed in hundredths of a bip
13+
// - sqrtRatioCurrentX96: the current sqrt price of the pool
14+
// - sqrtRatioTargetX96: The price that cannot be exceeded, from which the direction of the swap is inferred
15+
// - liquidity: The usable liquidity of the pool
16+
// - amountRemaining: How much input or output amount is remaining to be swapped in/out
17+
// - feePips: The fee taken from the input amount, expressed in hundredths of a bip
18+
//
1719
// output:
18-
// sqrtRatioNextX96: The price after swapping the amount in/out, not to exceed the price target
19-
// amountIn: The amount to be swapped in, of either token0 or token1, based on the direction of the swap
20-
// amountOut: The amount to be received, of either token0 or token1, based on the direction of the swap
21-
// feeAmount: The amount of input that will be taken as a fee
20+
// - sqrtRatioNextX96: The price after swapping the amount in/out, not to exceed the price target
21+
// - amountIn: The amount to be swapped in, of either token0 or token1, based on the direction of the swap
22+
// - amountOut: The amount to be received, of either token0 or token1, based on the direction of the swap
23+
// - feeAmount: The amount of input that will be taken as a fee
2224
func SwapMathComputeSwapStepStr(
23-
sqrtRatioCurrentX96 *u256.Uint, // uint160
24-
sqrtRatioTargetX96 *u256.Uint, // uint160
25-
liquidity *u256.Uint, // uint128
26-
amountRemaining *i256.Int, // int256
25+
sqrtRatioCurrentX96 *u256.Uint,
26+
sqrtRatioTargetX96 *u256.Uint,
27+
liquidity *u256.Uint,
28+
amountRemaining *i256.Int,
2729
feePips uint64,
28-
) (string, string, string, string) { // (sqrtRatioNextX96, amountIn, amountOut, feeAmount *u256.Uint)
30+
) (string, string, string, string) {
31+
if sqrtRatioCurrentX96 == nil || sqrtRatioTargetX96 == nil || liquidity == nil || amountRemaining == nil {
32+
panic("SwapMathComputeSwapStepStr: invalid input")
33+
}
34+
2935
zeroForOne := sqrtRatioCurrentX96.Gte(sqrtRatioTargetX96)
3036

3137
// POSTIVIE == EXACT_IN => Estimated AmountOut
@@ -39,36 +45,51 @@ func SwapMathComputeSwapStepStr(
3945

4046
if exactIn {
4147
amountRemainingLessFee := u256.MulDiv(amountRemaining.Abs(), u256.NewUint(1000000-feePips), u256.NewUint(1000000))
42-
4348
if zeroForOne {
44-
amountIn = sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, true)
49+
amountIn = sqrtPriceMathGetAmount0DeltaHelper(
50+
sqrtRatioTargetX96.Clone(),
51+
sqrtRatioCurrentX96.Clone(),
52+
liquidity.Clone(),
53+
true)
4554
} else {
46-
amountIn = sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, true)
55+
amountIn = sqrtPriceMathGetAmount1DeltaHelper(
56+
sqrtRatioCurrentX96.Clone(),
57+
sqrtRatioTargetX96.Clone(),
58+
liquidity.Clone(),
59+
true)
4760
}
4861

4962
if amountRemainingLessFee.Gte(amountIn) {
5063
sqrtRatioNextX96 = sqrtRatioTargetX96.Clone()
5164
} else {
5265
sqrtRatioNextX96 = sqrtPriceMathGetNextSqrtPriceFromInput(
53-
sqrtRatioCurrentX96,
54-
liquidity,
55-
amountRemainingLessFee,
66+
sqrtRatioCurrentX96.Clone(),
67+
liquidity.Clone(),
68+
amountRemainingLessFee.Clone(),
5669
zeroForOne,
5770
)
5871
}
5972
} else {
6073
if zeroForOne {
61-
amountOut = sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, false)
74+
amountOut = sqrtPriceMathGetAmount1DeltaHelper(
75+
sqrtRatioTargetX96.Clone(),
76+
sqrtRatioCurrentX96.Clone(),
77+
liquidity.Clone(),
78+
false)
6279
} else {
63-
amountOut = sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, false)
80+
amountOut = sqrtPriceMathGetAmount0DeltaHelper(
81+
sqrtRatioCurrentX96.Clone(),
82+
sqrtRatioTargetX96.Clone(),
83+
liquidity.Clone(),
84+
false)
6485
}
6586

6687
if amountRemaining.Abs().Gte(amountOut) {
6788
sqrtRatioNextX96 = sqrtRatioTargetX96.Clone()
6889
} else {
6990
sqrtRatioNextX96 = sqrtPriceMathGetNextSqrtPriceFromOutput(
70-
sqrtRatioCurrentX96,
71-
liquidity,
91+
sqrtRatioCurrentX96.Clone(),
92+
liquidity.Clone(),
7293
amountRemaining.Abs(),
7394
zeroForOne,
7495
)
@@ -79,26 +100,39 @@ func SwapMathComputeSwapStepStr(
79100

80101
if zeroForOne {
81102
if !(isMax && exactIn) {
82-
amountIn = sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioNextX96, sqrtRatioCurrentX96, liquidity, true)
103+
amountIn = sqrtPriceMathGetAmount0DeltaHelper(
104+
sqrtRatioNextX96.Clone(),
105+
sqrtRatioCurrentX96.Clone(),
106+
liquidity.Clone(),
107+
true)
83108
}
84-
85109
if !(isMax && !exactIn) {
86-
amountOut = sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioNextX96, sqrtRatioCurrentX96, liquidity, false)
110+
amountOut = sqrtPriceMathGetAmount1DeltaHelper(
111+
sqrtRatioNextX96.Clone(),
112+
sqrtRatioCurrentX96.Clone(),
113+
liquidity.Clone(),
114+
false)
87115
}
88116
} else {
89117
if !(isMax && exactIn) {
90-
amountIn = sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioCurrentX96, sqrtRatioNextX96, liquidity, true)
118+
amountIn = sqrtPriceMathGetAmount1DeltaHelper(
119+
sqrtRatioCurrentX96.Clone(),
120+
sqrtRatioNextX96.Clone(),
121+
liquidity.Clone(),
122+
true)
91123
}
92-
93124
if !(isMax && !exactIn) {
94-
amountOut = sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioCurrentX96, sqrtRatioNextX96, liquidity, false)
125+
amountOut = sqrtPriceMathGetAmount0DeltaHelper(
126+
sqrtRatioCurrentX96.Clone(),
127+
sqrtRatioNextX96.Clone(),
128+
liquidity.Clone(),
129+
false)
95130
}
96131
}
97132

98133
if !exactIn && amountOut.Gt(amountRemaining.Abs()) {
99134
amountOut = amountRemaining.Abs()
100135
}
101-
102136
if exactIn && !(sqrtRatioNextX96.Eq(sqrtRatioTargetX96)) {
103137
feeAmount = new(u256.Uint).Sub(amountRemaining.Abs(), amountIn)
104138
} else {

0 commit comments

Comments
 (0)