@@ -8,24 +8,30 @@ import (
8
8
// SwapMathComputeSwapStepStr computes the next sqrt price, amount in, amount out, and fee amount
9
9
// Computes the result of swapping some amount in, or amount out, given the parameters of the swap
10
10
// The fee, plus the amount in, will never exceed the amount remaining if the swap's `amountSpecified` is positive
11
+ //
11
12
// 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
+ //
17
19
// 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
22
24
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,
27
29
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
+
29
35
zeroForOne := sqrtRatioCurrentX96.Gte(sqrtRatioTargetX96)
30
36
31
37
// POSTIVIE == EXACT_IN => Estimated AmountOut
@@ -39,36 +45,51 @@ func SwapMathComputeSwapStepStr(
39
45
40
46
if exactIn {
41
47
amountRemainingLessFee := u256.MulDiv(amountRemaining.Abs(), u256.NewUint(1000000-feePips), u256.NewUint(1000000))
42
-
43
48
if zeroForOne {
44
- amountIn = sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, true)
49
+ amountIn = sqrtPriceMathGetAmount0DeltaHelper(
50
+ sqrtRatioTargetX96.Clone(),
51
+ sqrtRatioCurrentX96.Clone(),
52
+ liquidity.Clone(),
53
+ true)
45
54
} else {
46
- amountIn = sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, true)
55
+ amountIn = sqrtPriceMathGetAmount1DeltaHelper(
56
+ sqrtRatioCurrentX96.Clone(),
57
+ sqrtRatioTargetX96.Clone(),
58
+ liquidity.Clone(),
59
+ true)
47
60
}
48
61
49
62
if amountRemainingLessFee.Gte(amountIn) {
50
63
sqrtRatioNextX96 = sqrtRatioTargetX96.Clone()
51
64
} else {
52
65
sqrtRatioNextX96 = sqrtPriceMathGetNextSqrtPriceFromInput(
53
- sqrtRatioCurrentX96,
54
- liquidity,
55
- amountRemainingLessFee,
66
+ sqrtRatioCurrentX96.Clone() ,
67
+ liquidity.Clone() ,
68
+ amountRemainingLessFee.Clone() ,
56
69
zeroForOne,
57
70
)
58
71
}
59
72
} else {
60
73
if zeroForOne {
61
- amountOut = sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, false)
74
+ amountOut = sqrtPriceMathGetAmount1DeltaHelper(
75
+ sqrtRatioTargetX96.Clone(),
76
+ sqrtRatioCurrentX96.Clone(),
77
+ liquidity.Clone(),
78
+ false)
62
79
} else {
63
- amountOut = sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, false)
80
+ amountOut = sqrtPriceMathGetAmount0DeltaHelper(
81
+ sqrtRatioCurrentX96.Clone(),
82
+ sqrtRatioTargetX96.Clone(),
83
+ liquidity.Clone(),
84
+ false)
64
85
}
65
86
66
87
if amountRemaining.Abs().Gte(amountOut) {
67
88
sqrtRatioNextX96 = sqrtRatioTargetX96.Clone()
68
89
} else {
69
90
sqrtRatioNextX96 = sqrtPriceMathGetNextSqrtPriceFromOutput(
70
- sqrtRatioCurrentX96,
71
- liquidity,
91
+ sqrtRatioCurrentX96.Clone() ,
92
+ liquidity.Clone() ,
72
93
amountRemaining.Abs(),
73
94
zeroForOne,
74
95
)
@@ -79,26 +100,39 @@ func SwapMathComputeSwapStepStr(
79
100
80
101
if zeroForOne {
81
102
if !(isMax && exactIn) {
82
- amountIn = sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioNextX96, sqrtRatioCurrentX96, liquidity, true)
103
+ amountIn = sqrtPriceMathGetAmount0DeltaHelper(
104
+ sqrtRatioNextX96.Clone(),
105
+ sqrtRatioCurrentX96.Clone(),
106
+ liquidity.Clone(),
107
+ true)
83
108
}
84
-
85
109
if !(isMax && !exactIn) {
86
- amountOut = sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioNextX96, sqrtRatioCurrentX96, liquidity, false)
110
+ amountOut = sqrtPriceMathGetAmount1DeltaHelper(
111
+ sqrtRatioNextX96.Clone(),
112
+ sqrtRatioCurrentX96.Clone(),
113
+ liquidity.Clone(),
114
+ false)
87
115
}
88
116
} else {
89
117
if !(isMax && exactIn) {
90
- amountIn = sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioCurrentX96, sqrtRatioNextX96, liquidity, true)
118
+ amountIn = sqrtPriceMathGetAmount1DeltaHelper(
119
+ sqrtRatioCurrentX96.Clone(),
120
+ sqrtRatioNextX96.Clone(),
121
+ liquidity.Clone(),
122
+ true)
91
123
}
92
-
93
124
if !(isMax && !exactIn) {
94
- amountOut = sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioCurrentX96, sqrtRatioNextX96, liquidity, false)
125
+ amountOut = sqrtPriceMathGetAmount0DeltaHelper(
126
+ sqrtRatioCurrentX96.Clone(),
127
+ sqrtRatioNextX96.Clone(),
128
+ liquidity.Clone(),
129
+ false)
95
130
}
96
131
}
97
132
98
133
if !exactIn && amountOut.Gt(amountRemaining.Abs()) {
99
134
amountOut = amountRemaining.Abs()
100
135
}
101
-
102
136
if exactIn && !(sqrtRatioNextX96.Eq(sqrtRatioTargetX96)) {
103
137
feeAmount = new(u256.Uint).Sub(amountRemaining.Abs(), amountIn)
104
138
} else {
0 commit comments