cyclos-core 0.1.6

Anchor client and source for Cykura (formerly Cyclos) concentrated liquidity AMM
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
///! Helper functions to find price changes for change in token supply and vice versa
use super::big_num::U128;
use super::fixed_point_32;
use super::full_math::MulDiv;
use super::unsafe_math::UnsafeMathTrait;

/// Gets the next sqrt price √P' given a delta of token_0
///
/// Always round up because
/// 1. In the exact output case, token 0 supply decreases leading to price increase.
/// Move price up so that exact output is met.
/// 2. In the exact input case, token 0 supply increases leading to price decrease.
/// Do not round down to minimize price impact. We only need to meet input
/// change and not guarantee exact output.
///
/// Use function for exact input or exact output swaps for token 0
///
/// # Formula
///
/// * `√P' = √P * L / (L + Δx * √P)`
/// * If Δx * √P overflows, use alternate form `√P' = L / (L/√P + Δx)`
///
/// # Proof
///
/// For constant y,
/// √P * L = y
/// √P' * L' = √P * L
/// √P' = √P * L / L'
/// √P' = √P * L / L'
/// √P' = √P * L / (L + Δx*√P)
///
/// # Arguments
///
/// * `sqrt_p_x32` - The starting price `√P`, i.e., before accounting for the token_1 delta,
/// where P is `token_1_supply / token_0_supply`
/// * `liquidity` - The amount of usable liquidity L
/// * `amount` - Delta of token 0 (Δx) to add or remove from virtual reserves
/// * `add` - Whether to add or remove the amount of token_0
///
pub fn get_next_sqrt_price_from_amount_0_rounding_up(
    sqrt_p_x32: u64,
    liquidity: u64,
    amount: u64,
    add: bool,
) -> u64 {
    // we short circuit amount == 0 because the result is otherwise not
    // guaranteed to equal the input price
    if amount == 0 {
        return sqrt_p_x32;
    };
    let numerator_1 = (U128::from(liquidity)) << fixed_point_32::RESOLUTION; // U32.32

    if add {
        // Used native overflow check instead of the `a * b / b == a` Solidity method
        // https://stackoverflow.com/q/70143451/7721443

        if let Some(product) = amount.checked_mul(sqrt_p_x32) {
            let denominator = numerator_1 + U128::from(product);
            if denominator >= numerator_1 {
                return numerator_1
                    .mul_div_ceil(U128::from(sqrt_p_x32), denominator)
                    .unwrap()
                    .as_u64();
            };
        }
        // Alternate form if overflow - `√P' = L / (L/√P + Δx)`

        U128::div_rounding_up(
            numerator_1,
            (numerator_1 / U128::from(sqrt_p_x32))
                .checked_add(U128::from(amount))
                .unwrap(),
        )
        .as_u64()
    } else {
        // if the product overflows, we know the denominator underflows
        // in addition, we must check that the denominator does not underflow
        // assert!(product / amount == sqrt_p_x32 && numerator_1 > product);
        let product = U128::from(amount.checked_mul(sqrt_p_x32).unwrap());
        assert!(numerator_1 > product);

        let denominator = numerator_1 - product;
        numerator_1
            .mul_div_ceil(U128::from(sqrt_p_x32), denominator)
            .unwrap()
            .as_u64()
    }
}

/// Gets the next sqrt price given a delta of token_1
///
/// Always round down because
/// 1. In the exact output case, token 1 supply decreases leading to price decrease.
/// Move price down by rounding down so that exact output of token 0 is met.
/// 2. In the exact input case, token 1 supply increases leading to price increase.
/// Do not round down to minimize price impact. We only need to meet input
/// change and not gurantee exact output for token 0.
///
///
/// # Formula
///
/// * `√P' = √P + Δy / L`
///
/// # Arguments
///
/// * `sqrt_p_x32` - The starting price `√P`, i.e., before accounting for the token_1 delta
/// * `liquidity` - The amount of usable liquidity L
/// * `amount` - Delta of token 1 (Δy) to add or remove from virtual reserves
/// * `add` - Whether to add or remove the amount of token_1
///
pub fn get_next_sqrt_price_from_amount_1_rounding_down(
    sqrt_p_x32: u64,
    liquidity: u64,
    amount: u64,
    add: bool,
) -> u64 {
    // if we are adding (subtracting), rounding down requires rounding the quotient down (up)
    // in both cases, avoid a mul_div for most inputs to save gas
    // if amount <= u32::MAX, overflows do not happen
    if add {
        // quotient - `Δy / L` as U32.32
        let quotient = if amount <= (u32::MAX as u64) {
            // u32::MAX or below so that amount x 2^32 does not overflow
            (amount << fixed_point_32::RESOLUTION) / liquidity
        } else {
            amount
                .mul_div_floor(fixed_point_32::Q32, liquidity as u64)
                .unwrap()
        };

        sqrt_p_x32.checked_add(quotient).unwrap()
    } else {
        let quotient = if amount <= (u32::MAX as u64) {
            u64::div_rounding_up(amount << fixed_point_32::RESOLUTION, liquidity)
        } else {
            amount.mul_div_ceil(fixed_point_32::Q32, liquidity).unwrap()
        };

        assert!(sqrt_p_x32 > quotient);
        sqrt_p_x32 - quotient
    }
}

/// Gets the next sqrt price given an input amount of token_0 or token_1
/// Throws if price or liquidity are 0, or if the next price is out of bounds
///
/// # Arguments
///
/// * `sqrt_p_x32` - The starting price `√P`, i.e., before accounting for the input amount
/// * `liquidity` - The amount of usable liquidity
/// * `amount_in` - How much of token_0, or token_1, is being swapped in
/// * `zero_for_one` - Whether the amount in is token_0 or token_1
///
pub fn get_next_sqrt_price_from_input(
    sqrt_p_x32: u64,
    liquidity: u64,
    amount_in: u64,
    zero_for_one: bool,
) -> u64 {
    assert!(sqrt_p_x32 > 0);
    assert!(liquidity > 0);

    // round to make sure that we don't pass the target price
    if zero_for_one {
        get_next_sqrt_price_from_amount_0_rounding_up(sqrt_p_x32, liquidity, amount_in, true)
    } else {
        get_next_sqrt_price_from_amount_1_rounding_down(sqrt_p_x32, liquidity, amount_in, true)
    }
}

/// Gets the next sqrt price given an output amount of token0 or token1
///
/// Throws if price or liquidity are 0 or the next price is out of bounds
///
/// # Arguments
///
/// * `sqrt_p_x32` - The starting price `√P`, i.e., before accounting for the output amount
/// * `liquidity` - The amount of usable liquidity
/// * `amount_out` - How much of token_0, or token_1, is being swapped out
/// * `zero_for_one` - Whether the amount out is token_0 or token_1
///
pub fn get_next_sqrt_price_from_output(
    sqrt_p_x32: u64,
    liquidity: u64,
    amount_out: u64,
    zero_for_one: bool,
) -> u64 {
    assert!(sqrt_p_x32 > 0);
    assert!(liquidity > 0);

    if zero_for_one {
        get_next_sqrt_price_from_amount_1_rounding_down(sqrt_p_x32, liquidity, amount_out, false)
    } else {
        get_next_sqrt_price_from_amount_0_rounding_up(sqrt_p_x32, liquidity, amount_out, false)
    }
}

/// Gets the amount_0 delta between two prices, for given amount of liquidity (formula 6.30)
///
/// # Formula
///
/// * `Δx = L * (1 / √P_lower - 1 / √P_upper)`
/// * i.e. `L * (√P_upper - √P_lower) / (√P_upper * √P_lower)`
///
/// # Arguments
///
/// * `sqrt_ratio_a_x32` - A sqrt price
/// * `sqrt_ratio_b_x32` - Another sqrt price
/// * `liquidity` - The amount of usable liquidity
/// * `round_up`- Whether to round the amount up or down
///
pub fn get_amount_0_delta_unsigned(
    mut sqrt_ratio_a_x32: u64,
    mut sqrt_ratio_b_x32: u64,
    liquidity: u64,
    round_up: bool,
) -> u64 {
    // sqrt_ratio_a_x32 should hold the smaller value
    if sqrt_ratio_a_x32 > sqrt_ratio_b_x32 {
        std::mem::swap(&mut sqrt_ratio_a_x32, &mut sqrt_ratio_b_x32);
    };

    let numerator_1 = U128::from(liquidity) << fixed_point_32::RESOLUTION;
    let numerator_2 = U128::from(sqrt_ratio_b_x32 - sqrt_ratio_a_x32);

    assert!(sqrt_ratio_a_x32 > 0);

    if round_up {
        U128::div_rounding_up(
            numerator_1
                .mul_div_ceil(numerator_2, U128::from(sqrt_ratio_b_x32))
                .unwrap(),
            U128::from(sqrt_ratio_a_x32),
        )
        .as_u64()
    } else {
        (numerator_1
            .mul_div_floor(numerator_2, U128::from(sqrt_ratio_b_x32))
            .unwrap()
            / U128::from(sqrt_ratio_a_x32))
        .as_u64()
    }
}

/// Gets the amount_1 delta between two prices, for given amount of liquidity (formula 6.30)
///
/// # Formula
///
/// * `Δy = L (√P_upper - √P_lower)`
///
/// # Arguments
///
/// * `sqrt_ratio_a_x32` - A sqrt price
/// * `sqrt_ratio_b_x32` - Another sqrt price
/// * `liquidity` - The amount of usable liquidity
/// * `round_up`- Whether to round the amount up or down
///
pub fn get_amount_1_delta_unsigned(
    mut sqrt_ratio_a_x32: u64,
    mut sqrt_ratio_b_x32: u64,
    liquidity: u64,
    round_up: bool,
) -> u64 {
    // sqrt_ratio_a_x32 should hold the smaller value
    if sqrt_ratio_a_x32 > sqrt_ratio_b_x32 {
        std::mem::swap(&mut sqrt_ratio_a_x32, &mut sqrt_ratio_b_x32);
    };

    if round_up {
        liquidity.mul_div_ceil(sqrt_ratio_b_x32 - sqrt_ratio_a_x32, fixed_point_32::Q32)
    } else {
        liquidity.mul_div_floor(sqrt_ratio_b_x32 - sqrt_ratio_a_x32, fixed_point_32::Q32)
    }
    .unwrap()
}

/// Helper function to get signed token_0 delta between two prices,
/// for the given change in liquidity
///
/// # Arguments
///
/// * `sqrt_ratio_a_x32` - A sqrt price
/// * `sqrt_ratio_b_x32` - Another sqrt price
/// * `liquidity` - The change in liquidity for which to compute amount_0 delta
///
pub fn get_amount_0_delta_signed(
    sqrt_ratio_a_x32: u64,
    sqrt_ratio_b_x32: u64,
    liquidity: i64,
) -> i64 {
    if liquidity < 0 {
        -(get_amount_0_delta_unsigned(sqrt_ratio_a_x32, sqrt_ratio_b_x32, -liquidity as u64, false)
            as i64)
    } else {
        // TODO check overflow, since i64::MAX < u64::MAX
        get_amount_0_delta_unsigned(sqrt_ratio_a_x32, sqrt_ratio_b_x32, liquidity as u64, true)
            as i64
    }
}

/// Helper function to get signed token_1 delta between two prices,
/// for the given change in liquidity
///
/// # Arguments
///
/// * `sqrt_ratio_a_x32` - A sqrt price
/// * `sqrt_ratio_b_x32` - Another sqrt price
/// * `liquidity` - The change in liquidity for which to compute amount_1 delta
///
pub fn get_amount_1_delta_signed(
    sqrt_ratio_a_x32: u64,
    sqrt_ratio_b_x32: u64,
    liquidity: i64,
) -> i64 {
    if liquidity < 0 {
        -(get_amount_1_delta_unsigned(sqrt_ratio_a_x32, sqrt_ratio_b_x32, -liquidity as u64, false)
            as i64)
    } else {
        // TODO check overflow, since i64::MAX < u64::MAX
        get_amount_1_delta_unsigned(sqrt_ratio_a_x32, sqrt_ratio_b_x32, liquidity as u64, true)
            as i64
    }
}

#[cfg(test)]
mod sqrt_math {
    use super::*;
    use crate::libraries::test_utils::*;

    // ---------------------------------------------------------------------
    // 1. get_next_sqrt_price_from_input()

    mod get_next_sqrt_price_from_input {
        use super::*;

        #[test]
        #[should_panic]
        fn fails_if_price_is_zero() {
            get_next_sqrt_price_from_input(0, 0, u64::pow(10, 17), false);
        }

        #[test]
        #[should_panic]
        fn fails_if_liquidity_is_zero() {
            get_next_sqrt_price_from_input(1, 0, u64::pow(10, 8), true);
        }

        #[test]
        #[should_panic]
        fn fails_if_input_amount_overflows_the_price() {
            let sqrt_p_x32 = u64::MAX;
            let liquidity: u64 = 1024;
            let amount_in: u64 = 1024;

            // sqrt_p_x32.checked_add() should fail
            get_next_sqrt_price_from_input(sqrt_p_x32, liquidity, amount_in, false);
        }

        #[test]
        fn any_input_amount_cannot_underflow_the_price() {
            let sqrt_p_x32 = 1;
            let liquidity = 1;
            let amount_in = u64::pow(2, 63);

            assert_eq!(
                get_next_sqrt_price_from_input(sqrt_p_x32, liquidity, amount_in, true),
                1
            );
        }

        #[test]
        fn returns_input_price_if_amount_in_is_zero_and_zero_for_one_is_true() {
            let sqrt_p_x32 = 1 * fixed_point_32::Q32;
            assert_eq!(
                get_next_sqrt_price_from_input(sqrt_p_x32, u64::pow(10, 8), 0, true),
                sqrt_p_x32
            );
        }

        #[test]
        fn returns_input_price_if_amount_in_is_zero_and_zero_for_one_is_false() {
            let sqrt_p_x32 = 1 * fixed_point_32::Q32;
            assert_eq!(
                get_next_sqrt_price_from_input(sqrt_p_x32, u64::pow(10, 8), 0, false),
                sqrt_p_x32
            );
        }

        #[test]
        fn returns_the_minimum_price_for_max_inputs() {
            let sqrt_p_x32 = u64::MAX - 1;
            let liquidity = u32::MAX as u64;
            let max_amount_no_overflow =
                u64::MAX - ((liquidity << fixed_point_32::RESOLUTION) / sqrt_p_x32);

            assert_eq!(
                get_next_sqrt_price_from_input(sqrt_p_x32, liquidity, max_amount_no_overflow, true),
                1
            );
        }

        #[test]
        fn input_amount_of_01_token_1() {
            // price of token 0 wrt token 1 increases as token_1 supply increases
            let sqrt_p_x32 = 1 * fixed_point_32::Q32;
            let liquidity = u64::pow(10, 8);
            let amount_0_in = u64::pow(10, 7); // 10^7 / 10^8 = 0.1
            assert_eq!(
                get_next_sqrt_price_from_input(sqrt_p_x32, liquidity, amount_0_in, false),
                4724464025 // `√P' = √P + Δy / L`, rounded down
                           // https://www.wolframalpha.com/input/?i=floor%282%5E32+*+%281+%2B+0.1%29%29
            );
        }

        #[test]
        fn input_amount_of_01_token_0() {
            // price of token_0 wrt token_1 decreases as token_0 supply increases
            let sqrt_p_x32 = 1 * fixed_point_32::Q32;
            let liquidity = u64::pow(10, 8);
            let amount_0_in = u64::pow(10, 7); // 10^7 / 10^8 = 0.1
            assert_eq!(
                get_next_sqrt_price_from_input(sqrt_p_x32, liquidity, amount_0_in, true),
                3904515724 // `√P' = √P * L / (L + Δx * √P)`, rounded up
                           // https://www.wolframalpha.com/input/?i=ceil%282%5E32+*+%281+%2F+%281+%2B+0.1%29%29%29
            );
        }

        #[test]
        fn amount_in_is_greater_than_u32_max_and_zero_for_one_is_true() {
            let sqrt_p_x32 = 1 * fixed_point_32::Q32;
            let liquidity = u64::pow(10, 8);
            let amount_0_in = u64::pow(10, 12); // 10^12 / 10^8 = 10^4
            assert_eq!(
                get_next_sqrt_price_from_input(sqrt_p_x32, liquidity, amount_0_in, true),
                429454 // `√P' = √P * L / (L + Δx * √P)`, rounded up
                       // https://www.wolframalpha.com/input/?i=ceil%282%5E32+*+%281+%2F+%281+%2B+10%5E4%29%29%29
            );
        }

        #[test]
        fn can_return_1_with_enough_amount_in_and_zero_for_one_is_true() {
            assert_eq!(
                get_next_sqrt_price_from_input(encode_price_sqrt_x32(1, 1), 1, u64::MAX / 2, true),
                1 // `√P' = √P * L / (L + Δx * √P)`, rounded up = ceil(1 * 1 / (1 + (2^64 - 1)/2))
                  // https://www.wolframalpha.com/input/?i=ceil%281+*+1+%2F+%281+%2B+%282%5E64+-+1%29%2F2%29%29
            );
        }
    }

    // ---------------------------------------------------------------------
    // 2. get_next_sqrt_price_from_input()

    mod get_next_sqrt_price_from_output {
        use super::*;

        #[test]
        #[should_panic]
        fn fails_if_price_is_zero() {
            get_next_sqrt_price_from_output(0, 0, u64::pow(10, 17), false);
        }

        #[test]
        #[should_panic]
        fn fails_if_liquidity_is_zero() {
            get_next_sqrt_price_from_output(1, 0, u64::pow(10, 17), true);
        }

        /// Output amount should be less than virtual reserves in the pool,
        /// otherwise the function fails
        ///
        #[test]
        #[should_panic]
        fn fails_if_output_amount_is_exactly_the_virtual_reserves_of_token_0() {
            let reserve_0: u64 = 4;
            let reserve_1 = 262144;
            let sqrt_p_x32 = encode_price_sqrt_x32(reserve_1, reserve_0); // 4194304
            let liquidity = encode_liquidity(reserve_1, reserve_0); // 1024
            get_next_sqrt_price_from_output(sqrt_p_x32, liquidity, reserve_0, false);
        }

        /// Output amount should be less than virtual reserves in the pool,
        /// otherwise the function fails
        ///
        #[test]
        #[should_panic]
        fn fails_if_output_amount_is_greater_than_virtual_reserves_of_token_0() {
            let reserve_0: u64 = 4;
            let reserve_1 = 262144;
            let sqrt_p_x32 = encode_price_sqrt_x32(reserve_1, reserve_0); // 4194304
            let liquidity = encode_liquidity(reserve_1, reserve_0); // 1024
            get_next_sqrt_price_from_output(sqrt_p_x32, liquidity, reserve_0 + 1, false);
        }

        /// Output amount should be less than virtual reserves in the pool, otherwise the function fails
        #[test]
        #[should_panic]
        fn fails_if_output_amount_is_greater_than_virtual_reserves_of_token_1() {
            let reserve_0: u64 = 4;
            let reserve_1 = 262144;
            let sqrt_p_x32 = encode_price_sqrt_x32(reserve_1, reserve_0); // 4194304
            let liquidity = encode_liquidity(reserve_1, reserve_0); // 1024
            get_next_sqrt_price_from_output(sqrt_p_x32, liquidity, reserve_1 + 1, true);
        }

        /// Output amount should be less than virtual reserves in the pool, otherwise the function fails
        #[test]
        #[should_panic]
        fn fails_if_output_amount_is_exactly_the_virtual_reserves_of_token_1() {
            let reserve_0: u64 = 4;
            let reserve_1 = 262144;
            let sqrt_p_x32 = encode_price_sqrt_x32(reserve_1, reserve_0); // 4194304
            let liquidity = encode_liquidity(reserve_1, reserve_0); // 1024
            get_next_sqrt_price_from_output(sqrt_p_x32, liquidity, reserve_1, true);
        }

        #[test]
        fn succeeds_if_output_amount_is_less_than_virtual_reserves_of_token_1() {
            let reserve_0: u64 = 4;
            let reserve_1 = 262144;
            let sqrt_p_x32 = encode_price_sqrt_x32(reserve_1, reserve_0); // 4194304
            println!("Sqrt p {}", sqrt_p_x32);
            let liquidity = encode_liquidity(reserve_1, reserve_0); // 1024

            assert_eq!(
                get_next_sqrt_price_from_output(sqrt_p_x32, liquidity, reserve_1 - 1, true),
                4194304 // √P' = √P + Δy / L, rounding down = 4194304 - floor(262143 / (1024 * 2^32))
                        // https://www.wolframalpha.com/input/?i=4194304+-+floor%28262143+%2F+%281024+*+2%5E32%29%29
            );
        }

        /// If input amount is zero, there is no price movement. The input price
        /// is returned
        ///
        #[test]
        fn returns_input_price_if_amount_in_is_zero_and_zero_for_one_is_true() {
            let sqrt_p_x32 = encode_price_sqrt_x32(1, 1); // 4294967296
            assert_eq!(
                get_next_sqrt_price_from_output(sqrt_p_x32, u64::pow(10, 8), 0, true),
                sqrt_p_x32
            );
        }

        /// If input amount is zero, there is no price movement. The input price
        /// is returned
        ///
        #[test]
        fn returns_input_price_if_amount_in_is_zero_and_zero_for_one_is_false() {
            let sqrt_p_x32 = encode_price_sqrt_x32(1, 1); // 4294967296
            assert_eq!(
                get_next_sqrt_price_from_output(sqrt_p_x32, u64::pow(10, 8), 0, false),
                sqrt_p_x32
            );
        }

        #[test]
        fn output_amount_of_01_token_1_when_zero_for_one_is_false() {
            let reserve_0 = u64::pow(10, 8);
            let reserve_1 = u64::pow(10, 8);

            let sqrt_p_x32 = encode_price_sqrt_x32(reserve_1, reserve_0); // 2^32 = 4294967296
            let liquidity = encode_liquidity(reserve_1, reserve_0); // 1

            let amount_0_out = reserve_1 / 10;
            assert_eq!(
                get_next_sqrt_price_from_output(sqrt_p_x32, liquidity, amount_0_out, false),
                4772185885 // `√P' = √P * L / (L + Δx * √P)`, rounded up
                           //   = ceil(2^32 * 1 / (1 - 0.1 * 2^32 / 2^32))
                           // https://www.wolframalpha.com/input/?i=ceil%282%5E32+*+1+%2F+%281+-+0.1+*+2%5E32+%2F+2%5E32%29%29
            );
        }

        #[test]
        fn output_amount_of_01_token_1_when_zero_for_one_is_true() {
            let reserve_0 = u64::pow(10, 8);
            let reserve_1 = u64::pow(10, 8);

            let sqrt_p_x32 = encode_price_sqrt_x32(reserve_1, reserve_0); // 2^32 = 4294967296
            let liquidity = encode_liquidity(reserve_1, reserve_0); // 1

            let amount_1_out = reserve_1 / 10;

            assert_eq!(
                get_next_sqrt_price_from_output(sqrt_p_x32, liquidity, amount_1_out, true),
                3865470566 // `√P' = √P + Δy / L`, rounded down = floor(2^32 - (0.1 / 1)*2^32)
                           // https://www.wolframalpha.com/input/?i=floor%282%5E32+-+%280.1+%2F+1%29*2%5E32%29
            );
        }

        /// Output amount should be less than virtual reserves in the pool,
        /// otherwise the function fails. Here the exact output amount is greater
        /// than liquidity available in the pool
        ///
        #[test]
        #[should_panic]
        fn reverts_if_amount_out_is_impossible_in_zero_for_one_direction() {
            let sqrt_p_x32 = encode_price_sqrt_x32(1, 1);
            let liquidity = encode_liquidity(1, 1);
            get_next_sqrt_price_from_output(sqrt_p_x32, liquidity, u64::MAX, true);
        }

        /// Output amount should be less than virtual reserves in the pool,
        /// otherwise the function fails. Here the exact output amount is greater
        /// than liquidity available in the pool
        ///
        #[test]
        #[should_panic]
        fn reverts_if_amount_out_is_impossible_in_one_for_zero_direction() {
            let sqrt_p_x32 = encode_price_sqrt_x32(1, 1);
            let liquidity = encode_liquidity(1, 1);
            get_next_sqrt_price_from_output(sqrt_p_x32, liquidity, u64::MAX, false);
        }
    }

    mod get_amount_0_delta {
        use super::*;

        /// If liquidity is 0, virtual reserves are absent
        ///
        #[test]
        fn returns_0_if_liquidity_is_0() {
            assert_eq!(
                get_amount_0_delta_unsigned(
                    encode_price_sqrt_x32(1, 1),
                    encode_price_sqrt_x32(2, 1),
                    0,
                    true
                ),
                0
            )
        }

        /// Virtual reserves at a single price are constant. Price change
        /// is needed to observe a delta in reserves.
        ///
        #[test]
        fn returns_0_if_prices_are_equal() {
            assert_eq!(
                get_amount_0_delta_unsigned(
                    encode_price_sqrt_x32(100, 100),
                    encode_price_sqrt_x32(100, 100),
                    encode_liquidity(100, 100),
                    true
                ),
                0
            )
        }

        /// Returns 0.1 of amount_0 for price of 1 to 1.21
        ///
        #[test]
        fn returns_one_eleventh_of_amount_0_for_price_change_from_1_to_1_point_21() {
            let amount_0 = get_amount_0_delta_unsigned(
                encode_price_sqrt_x32(100, 100), // 2^32
                encode_price_sqrt_x32(121, 100), // 1.1 * 2^32 = 4724464026
                u64::pow(10, 8),
                true,
            );
            // Δx = L * (1 / √P_lower - 1 / √P_upper)
            //      = 10^8 (1/1 - 1/sqrt(1.21)) = 10^8 (1/1 - 1/1.1) = 10^8 * (0.1/1.1) = 10^8 / 11
            assert_eq!(amount_0, 9090910); // ceil(10^8 / 11)

            let amount_0_rounded_down = get_amount_0_delta_unsigned(
                encode_price_sqrt_x32(1, 1),
                encode_price_sqrt_x32(121, 100),
                u64::pow(10, 8),
                false,
            );
            assert_eq!(amount_0_rounded_down, amount_0 - 1); // floor(10^8 / 11)
        }

        /// Functions should handle overflow  of intermediary values without loss of precision
        ///
        /// This is ensured by the fullmath library
        ///
        /// Since `price: u64 = sqrt(reserve_1 / reserve_0) * 2^32`, it will overflow when
        /// reserve_1 > 2^32
        #[test]
        fn works_for_prices_that_overflow() {
            // Δx = L * (1 / √P_lower - 1 / √P_upper) = L (√P_upper - √P_lower)/(√P_upper * √P_lower)
            // Intermdiary value L (√P_upper - √P_lower) = 10^8 * (2^50 - 2^48) > u64::MAX, i.e overflow
            let amount_0_up = get_amount_0_delta_unsigned(
                encode_price_sqrt_x32(u64::pow(2, 50), 1), // 2^55
                encode_price_sqrt_x32(u64::pow(2, 48), 1), // 2^53
                u64::pow(10, 8),
                true,
            );
            // Δx = L * (1 / √P_lower - 1 / √P_upper) = 10^8 (1/2^24 - 1/2^25) = 2.98
            assert_eq!(amount_0_up, 3); // ceil(2.98)

            let amount_0_down = get_amount_0_delta_unsigned(
                encode_price_sqrt_x32(u64::pow(2, 50), 1), //encodePriceSqrt(BigNumber.from(2).pow(90), 1)
                encode_price_sqrt_x32(u64::pow(2, 48), 1), //encodePriceSqrt(BigNumber.from(2).pow(96), 1)
                u64::pow(10, 8),
                false,
            );

            assert_eq!(amount_0_down, 2); // floor(2.98)
        }
    }

    mod get_amount_1_delta {
        use super::*;

        /// If liquidity is 0, virtual reserves are absent
        ///
        #[test]
        fn returns_0_if_liquidity_is_0() {
            assert_eq!(
                get_amount_1_delta_unsigned(
                    encode_price_sqrt_x32(1, 1),
                    encode_price_sqrt_x32(2, 1),
                    0,
                    true
                ),
                0
            )
        }

        /// Virtual reserves at a single price are constant. Price change
        /// is needed to observe a delta in reserves.
        ///
        #[test]
        fn returns_0_if_prices_are_equal() {
            assert_eq!(
                get_amount_1_delta_unsigned(
                    encode_price_sqrt_x32(100, 100),
                    encode_price_sqrt_x32(100, 100),
                    encode_liquidity(100, 100),
                    true
                ),
                0
            )
        }

        /// Returns 0.1 of amount_0 for price of 1 to 1.21
        ///
        #[test]
        fn returns_one_tenth_of_amount_1_for_price_change_from_1_to_1_point_21() {
            let amount_1 = get_amount_1_delta_unsigned(
                encode_price_sqrt_x32(100, 100), // 2^32
                encode_price_sqrt_x32(121, 100), // 1.1 * 2^32 = 4724464026
                u64::pow(10, 8),
                true,
            );
            // Δy = L * (√P_upper - √P_lower)
            //      = 10^8 (sqrt(1.21) - 1) = 10^8 (1.1 - 1) = 10^8 * 0.1
            assert_eq!(amount_1, u64::pow(10, 7) + 1); // ceil

            let amount_1_rounded_down = get_amount_1_delta_unsigned(
                encode_price_sqrt_x32(1, 1),
                encode_price_sqrt_x32(121, 100),
                u64::pow(10, 8),
                false,
            );
            assert_eq!(amount_1_rounded_down, u64::pow(10, 7)); // floor
        }
    }

    mod swap_computation {
        use super::*;

        /// Overflow of the product `√P_upper * √P_lower` should not lead to loss
        /// of precision when finding the value of Δx
        #[test]
        fn sqrt_p_by_sqrt_q_overflows() {
            // Δx = L * (1 / √P_lower - 1 / √P_upper) = L (√P_upper - √P_lower)/(√P_upper * √P_lower)
            // √P_upper * √P_lower should be handled without loss of precision
            let amount_0_up =
                get_amount_0_delta_unsigned(u64::MAX, u64::MAX - 1, u64::pow(10, 8), true);
            // Δx = L * (1 / √P_lower - 1 / √P_upper) = 10^8 (1/2^32 - 1/(2^32 - 1) = 0...
            assert_eq!(amount_0_up, 1); // ceil

            let amount_0_down =
                get_amount_0_delta_unsigned(u64::MAX, u64::MAX - 1, u64::pow(10, 8), false);

            assert_eq!(amount_0_down, 0); // floor
        }
    }
}