nautilus_model/defi/pool_analysis/
swap_math.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use alloy_primitives::{I256, U160, U256};
17
18use crate::defi::tick_map::{
19    full_math::FullMath,
20    sqrt_price_math::{
21        get_amount0_delta, get_amount1_delta, get_next_sqrt_price_from_input,
22        get_next_sqrt_price_from_output,
23    },
24};
25
26#[derive(Debug, Clone)]
27pub struct SwapStepResult {
28    pub sqrt_ratio_next_x96: U160,
29    pub amount_in: U256,
30    pub amount_out: U256,
31    pub fee_amount: U256,
32}
33
34pub const MAX_FEE: U256 = U256::from_limbs([1000000, 0, 0, 0]);
35
36/// Computes the result of swapping some amount in, or amount out, given the parameters of the swap.
37///
38/// # Arguments
39///
40/// - `sqrt_ratio_current_x96` - The current sqrt price of the pool
41/// - `sqrt_ratio_target_x96` - The price that cannot be exceeded, from which the direction of the swap is inferred
42/// - `liquidity` - The usable liquidity
43/// - `amount_remaining` - How much input or output amount is remaining to be swapped in/out
44/// - `fee_pips` - The fee taken from the input amount, expressed in hundredths of a bip
45///
46/// # Errors
47///
48/// This function returns an error if:
49/// - Fee adjustment calculations overflow or encounter division by zero.
50/// - Amount or price delta calculations overflow the supported numeric range.
51pub fn compute_swap_step(
52    sqrt_ratio_current_x96: U160,
53    sqrt_ratio_target_x96: U160,
54    liquidity: u128,
55    amount_remaining: I256,
56    fee_pips: u32,
57) -> anyhow::Result<SwapStepResult> {
58    let fee_pips = U256::from(fee_pips);
59    let fee_complement = MAX_FEE - fee_pips;
60
61    // Represent a direction of the swap, should we move price down (swap token0 for token1)
62    // or price up (swap token1 for token0)
63    let zero_for_one = sqrt_ratio_current_x96 >= sqrt_ratio_target_x96;
64
65    // true = exact input swap (know input amount, calculate output)
66    // false = exact output swap (know desired output, calculate required input)
67    let exact_in = amount_remaining.is_positive() || amount_remaining.is_zero();
68
69    let sqrt_ratio_next_x96: U160;
70    let mut amount_in: U256 = U256::ZERO;
71    let mut amount_out: U256 = U256::ZERO;
72
73    if exact_in {
74        // Calculate how much input is needed to reach target, considering fees
75        let amount_remaining_less_fee =
76            FullMath::mul_div(amount_remaining.into_raw(), fee_complement, MAX_FEE)?;
77
78        amount_in = if zero_for_one {
79            get_amount0_delta(
80                sqrt_ratio_target_x96,
81                sqrt_ratio_current_x96,
82                liquidity,
83                true,
84            )
85        } else {
86            get_amount1_delta(
87                sqrt_ratio_current_x96,
88                sqrt_ratio_target_x96,
89                liquidity,
90                true,
91            )
92        };
93
94        if amount_remaining_less_fee >= amount_in {
95            sqrt_ratio_next_x96 = sqrt_ratio_target_x96;
96        } else {
97            sqrt_ratio_next_x96 = get_next_sqrt_price_from_input(
98                sqrt_ratio_current_x96,
99                liquidity,
100                amount_remaining_less_fee,
101                zero_for_one,
102            );
103        }
104    } else {
105        // Calculate how much output can be obtained to reach target
106        amount_out = if zero_for_one {
107            get_amount1_delta(
108                sqrt_ratio_target_x96,
109                sqrt_ratio_current_x96,
110                liquidity,
111                false,
112            )
113        } else {
114            get_amount0_delta(
115                sqrt_ratio_current_x96,
116                sqrt_ratio_target_x96,
117                liquidity,
118                false,
119            )
120        };
121
122        if U256::from(amount_remaining.unsigned_abs()) >= amount_out {
123            sqrt_ratio_next_x96 = sqrt_ratio_target_x96;
124        } else {
125            sqrt_ratio_next_x96 = get_next_sqrt_price_from_output(
126                sqrt_ratio_current_x96,
127                liquidity,
128                U256::from(amount_remaining.unsigned_abs()),
129                zero_for_one,
130            );
131        }
132    }
133
134    let max = sqrt_ratio_target_x96 == sqrt_ratio_next_x96;
135
136    // get the input/output amounts
137    if zero_for_one {
138        amount_in = if max && exact_in {
139            amount_in
140        } else {
141            get_amount0_delta(sqrt_ratio_next_x96, sqrt_ratio_current_x96, liquidity, true)
142        };
143        amount_out = if max && !exact_in {
144            amount_out
145        } else {
146            get_amount1_delta(
147                sqrt_ratio_next_x96,
148                sqrt_ratio_current_x96,
149                liquidity,
150                false,
151            )
152        };
153    } else {
154        amount_in = if max && exact_in {
155            amount_in
156        } else {
157            get_amount1_delta(sqrt_ratio_current_x96, sqrt_ratio_next_x96, liquidity, true)
158        };
159        amount_out = if max && !exact_in {
160            amount_out
161        } else {
162            get_amount0_delta(
163                sqrt_ratio_current_x96,
164                sqrt_ratio_next_x96,
165                liquidity,
166                false,
167            )
168        };
169    }
170
171    // cap the output amount to not exceed the remaining output amount
172    if !exact_in && amount_out > U256::from(amount_remaining.unsigned_abs()) {
173        amount_out = U256::from(amount_remaining.unsigned_abs());
174    }
175
176    let fee_amount: U256 = if exact_in && sqrt_ratio_next_x96 != sqrt_ratio_target_x96 {
177        // we didn't reach the target, so take the remainder of the maximum input as fee
178        U256::from(amount_remaining.unsigned_abs()) - amount_in
179    } else {
180        FullMath::mul_div_rounding_up(amount_in, fee_pips, fee_complement)?
181    };
182
183    Ok(SwapStepResult {
184        sqrt_ratio_next_x96,
185        amount_in,
186        amount_out,
187        fee_amount,
188    })
189}
190
191#[cfg(test)]
192mod tests {
193    // Most of the tests are from https://github.com/Uniswap/v3-core/blob/main/test/SwapMath.spec.ts
194    use std::str::FromStr;
195
196    use rstest::rstest;
197
198    use super::*;
199    use crate::defi::tick_map::sqrt_price_math::{encode_sqrt_ratio_x96, expand_to_18_decimals};
200
201    #[rstest]
202    fn test_exact_amount_in_that_gets_capped_at_price_target_in_one_for_zero() {
203        let price = encode_sqrt_ratio_x96(1, 1);
204        let price_target = encode_sqrt_ratio_x96(101, 100);
205        let liquidity = expand_to_18_decimals(2);
206        let amount = expand_to_18_decimals(1);
207        let fee = 600;
208
209        let result = compute_swap_step(
210            price,
211            price_target,
212            liquidity,
213            I256::from_str(&amount.to_string()).unwrap(),
214            fee,
215        )
216        .unwrap();
217
218        assert_eq!(
219            result.amount_in,
220            U256::from_str("9975124224178055").unwrap()
221        );
222        assert_eq!(
223            result.amount_out,
224            U256::from_str("9925619580021728").unwrap()
225        );
226        assert_eq!(result.fee_amount, U256::from_str("5988667735148").unwrap());
227        assert_eq!(result.sqrt_ratio_next_x96, price_target);
228
229        // entire amount is not used
230        assert!(result.amount_in + result.fee_amount < U256::from(amount));
231
232        let price_after_whole_input_amount = get_next_sqrt_price_from_input(
233            price,
234            liquidity,
235            U256::from(amount),
236            false, // zero_for_one = false
237        );
238
239        // price is capped at price target
240        assert_eq!(result.sqrt_ratio_next_x96, price_target);
241        // price is less than price after whole input amount
242        assert!(result.sqrt_ratio_next_x96 < price_after_whole_input_amount);
243    }
244
245    #[rstest]
246    fn test_exact_amount_in_that_is_fully_spent_in_one_for_zero() {
247        let price = encode_sqrt_ratio_x96(1, 1);
248        let price_target = encode_sqrt_ratio_x96(1000, 100);
249        let liquidity = expand_to_18_decimals(2);
250        let amount = expand_to_18_decimals(1);
251        let fee = 600;
252
253        let result = compute_swap_step(
254            price,
255            price_target,
256            liquidity,
257            I256::from_str(&amount.to_string()).unwrap(),
258            fee,
259        )
260        .unwrap();
261
262        assert_eq!(
263            result.amount_in,
264            U256::from_str("999400000000000000").unwrap()
265        );
266        assert_eq!(
267            result.fee_amount,
268            U256::from_str("600000000000000").unwrap()
269        );
270        assert_eq!(
271            result.amount_out,
272            U256::from_str("666399946655997866").unwrap()
273        );
274
275        // entire amount is used
276        assert_eq!(result.amount_in + result.fee_amount, U256::from(amount));
277
278        let price_after_whole_input_amount_less_fee = get_next_sqrt_price_from_input(
279            price,
280            liquidity,
281            U256::from(amount) - result.fee_amount,
282            false, // zero_for_one = false
283        );
284
285        // price does not reach price target
286        assert!(result.sqrt_ratio_next_x96 < price_target);
287        // price is equal to price after whole input amount
288        assert_eq!(
289            result.sqrt_ratio_next_x96,
290            price_after_whole_input_amount_less_fee
291        );
292    }
293
294    #[rstest]
295    fn test_exact_amount_out_that_is_fully_received_in_one_for_zero() {
296        let price = encode_sqrt_ratio_x96(1, 1);
297        let price_target = encode_sqrt_ratio_x96(10000, 100);
298        let liquidity = expand_to_18_decimals(2);
299        let amount = expand_to_18_decimals(1);
300        let fee = 600;
301
302        // Negative amount for exact output
303        let amount_negative = -I256::from_str(&amount.to_string()).unwrap();
304
305        let result =
306            compute_swap_step(price, price_target, liquidity, amount_negative, fee).unwrap();
307
308        assert_eq!(
309            result.amount_in,
310            U256::from_str("2000000000000000000").unwrap()
311        );
312        assert_eq!(
313            result.fee_amount,
314            U256::from_str("1200720432259356").unwrap()
315        );
316        assert_eq!(result.amount_out, U256::from(amount));
317
318        let price_after_whole_output_amount = get_next_sqrt_price_from_output(
319            price,
320            liquidity,
321            U256::from(amount),
322            false, // zero_for_one = false
323        );
324
325        // price does not reach price target
326        assert!(result.sqrt_ratio_next_x96 < price_target);
327        // price is equal to price after whole output amount
328        assert_eq!(result.sqrt_ratio_next_x96, price_after_whole_output_amount);
329    }
330
331    #[rstest]
332    fn test_amount_out_is_capped_at_the_desired_amount_out() {
333        let result = compute_swap_step(
334            U160::from_str("417332158212080721273783715441582").unwrap(),
335            U160::from_str("1452870262520218020823638996").unwrap(),
336            159344665391607089467575320103,
337            I256::from_str("-1").unwrap(),
338            1,
339        )
340        .unwrap();
341
342        assert_eq!(result.amount_in, U256::from(1));
343        assert_eq!(result.fee_amount, U256::from(1));
344        assert_eq!(result.amount_out, U256::from(1)); // would be 2 if not capped
345        assert_eq!(
346            result.sqrt_ratio_next_x96,
347            U160::from_str("417332158212080721273783715441581").unwrap()
348        );
349    }
350
351    #[rstest]
352    fn test_entire_input_amount_taken_as_fee() {
353        let result = compute_swap_step(
354            U160::from_str("2413").unwrap(),
355            U160::from_str("79887613182836312").unwrap(),
356            1985041575832132834610021537970,
357            I256::from_str("10").unwrap(),
358            1872,
359        )
360        .unwrap();
361
362        assert_eq!(result.amount_in, U256::ZERO);
363        assert_eq!(result.fee_amount, U256::from(10));
364        assert_eq!(result.amount_out, U256::ZERO);
365        assert_eq!(result.sqrt_ratio_next_x96, U160::from_str("2413").unwrap());
366    }
367
368    #[rstest]
369    fn test_handles_intermediate_insufficient_liquidity_in_zero_for_one_exact_output_case() {
370        let sqrt_p = U160::from_str("20282409603651670423947251286016").unwrap();
371        // sqrtPTarget = sqrtP * 11 / 10
372        let sqrt_p_target = U160::from(U256::from(sqrt_p) * U256::from(11) / U256::from(10));
373        let liquidity = 1024;
374        // virtual reserves of one are only 4
375        let amount_remaining = I256::from_str("-4").unwrap();
376        let fee_pips = 3000;
377
378        let result =
379            compute_swap_step(sqrt_p, sqrt_p_target, liquidity, amount_remaining, fee_pips)
380                .unwrap();
381
382        assert_eq!(result.amount_out, U256::ZERO);
383        assert_eq!(result.sqrt_ratio_next_x96, sqrt_p_target);
384        assert_eq!(result.amount_in, U256::from(26215));
385        assert_eq!(result.fee_amount, U256::from(79));
386    }
387
388    #[rstest]
389    fn test_handles_intermediate_insufficient_liquidity_in_one_for_zero_exact_output_case() {
390        let sqrt_p = U160::from_str("20282409603651670423947251286016").unwrap();
391        // sqrtPTarget = sqrtP * 9 / 10
392        let sqrt_p_target = U160::from(U256::from(sqrt_p) * U256::from(9) / U256::from(10));
393        let liquidity = 1024;
394        // virtual reserves of zero are only 262144
395        let amount_remaining = I256::from_str("-263000").unwrap();
396        let fee_pips = 3000;
397
398        let result =
399            compute_swap_step(sqrt_p, sqrt_p_target, liquidity, amount_remaining, fee_pips)
400                .unwrap();
401
402        assert_eq!(result.amount_out, U256::from(26214));
403        assert_eq!(result.sqrt_ratio_next_x96, sqrt_p_target);
404        assert_eq!(result.amount_in, U256::from(1));
405        assert_eq!(result.fee_amount, U256::from(1));
406    }
407}