wp-solana-amm-math 0.1.1

Protocol-agnostic AMM math for Solana DEX — tick pricing, bin pricing, liquidity math, swap simulation
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
use ethnum::U256;
use rust_decimal::{prelude::*, Decimal};

use crate::AmmMathError;

/// Minimum valid tick index (matches Orca Whirlpools / Raydium CLMM).
pub const MIN_TICK: i32 = -443636;
/// Maximum valid tick index (matches Orca Whirlpools / Raydium CLMM).
pub const MAX_TICK: i32 = 443636;
/// Minimum valid sqrt price in Q64.64 (corresponds to `MIN_TICK`).
pub const MIN_SQRT_PRICE: u128 = 4295048016;
/// Maximum valid sqrt price in Q64.64 (corresponds to `MAX_TICK`).
pub const MAX_SQRT_PRICE: u128 = 79226673515401279992447579055;

/// Multiplies two u128 values and shifts right by 96, using U256 for the
/// intermediate product.
fn mul_shift_96(a: u128, b: u128) -> u128 {
    let product: U256 = U256::from(a) * U256::from(b);
    let shifted: U256 = product >> 96;
    shifted.as_u128()
}

/// Converts a tick index to a Q64.64 fixed-point sqrt price.
///
/// `sqrt_price_x64 = 2^64 * 1.0001^(tick/2)`
///
/// Implementation uses the Orca Whirlpools algorithm with precomputed
/// factors for positive and negative ticks separately.
pub fn tick_to_sqrt_price_x64(tick: i32) -> Result<u128, AmmMathError> {
    if !(MIN_TICK..=MAX_TICK).contains(&tick) {
        return Err(AmmMathError::TickOutOfRange(tick));
    }
    if tick >= 0 {
        Ok(sqrt_price_positive_tick(tick))
    } else {
        Ok(sqrt_price_negative_tick(tick))
    }
}

/// Compute sqrt_price for positive tick. Works in Q64.96, converts to
/// Q64.64 at the end via `>> 32`.
///
/// Constants represent `1.0001^(2^i / 2)` in Q64.96.
fn sqrt_price_positive_tick(tick: i32) -> u128 {
    let mut ratio: u128 = if tick & 1 != 0 {
        79232123823359799118286999567
    } else {
        79228162514264337593543950336 // 2^96 = 1.0 in Q64.96
    };

    if tick & 2 != 0 {
        ratio = mul_shift_96(ratio, 79236085330515764027303304731);
    }
    if tick & 4 != 0 {
        ratio = mul_shift_96(ratio, 79244008939048815603706035061);
    }
    if tick & 8 != 0 {
        ratio = mul_shift_96(ratio, 79259858533276714757314932305);
    }
    if tick & 16 != 0 {
        ratio = mul_shift_96(ratio, 79291567232598584799939703904);
    }
    if tick & 32 != 0 {
        ratio = mul_shift_96(ratio, 79355022692464371645785046466);
    }
    if tick & 64 != 0 {
        ratio = mul_shift_96(ratio, 79482085999252804386437311141);
    }
    if tick & 128 != 0 {
        ratio = mul_shift_96(ratio, 79736823300114093921829183326);
    }
    if tick & 256 != 0 {
        ratio = mul_shift_96(ratio, 80248749790819932309965073892);
    }
    if tick & 512 != 0 {
        ratio = mul_shift_96(ratio, 81282483887344747381513967011);
    }
    if tick & 1024 != 0 {
        ratio = mul_shift_96(ratio, 83390072131320151908154831281);
    }
    if tick & 2048 != 0 {
        ratio = mul_shift_96(ratio, 87770609709833776024991924138);
    }
    if tick & 4096 != 0 {
        ratio = mul_shift_96(ratio, 97234110755111693312479820773);
    }
    if tick & 8192 != 0 {
        ratio = mul_shift_96(ratio, 119332217159966728226237229890);
    }
    if tick & 16384 != 0 {
        ratio = mul_shift_96(ratio, 179736315981702064433883588727);
    }
    if tick & 32768 != 0 {
        ratio = mul_shift_96(ratio, 407748233172238350107850275304);
    }
    if tick & 65536 != 0 {
        ratio = mul_shift_96(ratio, 2098478828474011932436660412517);
    }
    if tick & 131072 != 0 {
        ratio = mul_shift_96(ratio, 55581415166113811149459800483533);
    }
    if tick & 262144 != 0 {
        ratio = mul_shift_96(ratio, 38992368544603139932233054999993551);
    }

    ratio >> 32
}

/// Compute sqrt_price for negative tick. Works directly in Q64.64.
///
/// Constants represent `1 / 1.0001^(2^i / 2)` in Q64.64.
fn sqrt_price_negative_tick(tick: i32) -> u128 {
    let abs_tick = tick.unsigned_abs();

    let mut ratio: u128 = if abs_tick & 1 != 0 {
        18445821805675392311
    } else {
        18446744073709551616 // 2^64 = 1.0 in Q64.64
    };

    if abs_tick & 2 != 0 {
        ratio = (ratio * 18444899583751176498) >> 64;
    }
    if abs_tick & 4 != 0 {
        ratio = (ratio * 18443055278223354162) >> 64;
    }
    if abs_tick & 8 != 0 {
        ratio = (ratio * 18439367220385604838) >> 64;
    }
    if abs_tick & 16 != 0 {
        ratio = (ratio * 18431993317065449817) >> 64;
    }
    if abs_tick & 32 != 0 {
        ratio = (ratio * 18417254355718160513) >> 64;
    }
    if abs_tick & 64 != 0 {
        ratio = (ratio * 18387811781193591352) >> 64;
    }
    if abs_tick & 128 != 0 {
        ratio = (ratio * 18329067761203520168) >> 64;
    }
    if abs_tick & 256 != 0 {
        ratio = (ratio * 18212142134806087854) >> 64;
    }
    if abs_tick & 512 != 0 {
        ratio = (ratio * 17980523815641551639) >> 64;
    }
    if abs_tick & 1024 != 0 {
        ratio = (ratio * 17526086738831147013) >> 64;
    }
    if abs_tick & 2048 != 0 {
        ratio = (ratio * 16651378430235024244) >> 64;
    }
    if abs_tick & 4096 != 0 {
        ratio = (ratio * 15030750278693429944) >> 64;
    }
    if abs_tick & 8192 != 0 {
        ratio = (ratio * 12247334978882834399) >> 64;
    }
    if abs_tick & 16384 != 0 {
        ratio = (ratio * 8131365268884726200) >> 64;
    }
    if abs_tick & 32768 != 0 {
        ratio = (ratio * 3584323654723342297) >> 64;
    }
    if abs_tick & 65536 != 0 {
        ratio = (ratio * 696457651847595233) >> 64;
    }
    if abs_tick & 131072 != 0 {
        ratio = (ratio * 26294789957452057) >> 64;
    }
    if abs_tick & 262144 != 0 {
        ratio = (ratio * 37481735321082) >> 64;
    }

    ratio
}

/// Converts a Q64.64 fixed-point sqrt price to the largest tick whose
/// sqrt_price_x64 is <= the given value.
///
/// Uses a log2-based initial estimate followed by binary search
/// refinement.
pub fn sqrt_price_x64_to_tick(sqrt_price_x64: u128) -> Result<i32, AmmMathError> {
    if !(MIN_SQRT_PRICE..=MAX_SQRT_PRICE).contains(&sqrt_price_x64) {
        return Err(AmmMathError::SqrtPriceOutOfRange(sqrt_price_x64));
    }

    // Use log2 to estimate the tick.
    // sqrt_price_x64 = 2^64 * 1.0001^(tick/2)
    // log2(sqrt_price_x64) = 64 + tick/2 * log2(1.0001)
    // tick = 2 * (log2(sqrt_price_x64) - 64) / log2(1.0001)
    // 1/log2(1.0001) ~ 6931.47
    let msb = 127 - sqrt_price_x64.leading_zeros() as i32;
    let log2_approx = msb - 64;
    let tick_estimate = ((log2_approx as f64) * 6931.47 * 2.0) as i32;

    // Binary search in a window around the estimate.
    // Use a wide initial window (14000) to cover the worst-case f64 log2
    // estimate error and avoid expensive widening loops.
    let window = 14000;
    let mut lo = (tick_estimate - window).max(MIN_TICK);
    let mut hi = (tick_estimate + window).min(MAX_TICK);

    // Safety: widen window if estimate is still off (unlikely with window=14000)
    while tick_to_sqrt_price_x64(lo)? > sqrt_price_x64 {
        lo = (lo - window).max(MIN_TICK);
    }
    while tick_to_sqrt_price_x64(hi)? < sqrt_price_x64 {
        hi = (hi + window).min(MAX_TICK);
    }

    // Binary search: find largest tick t where tick_to_sqrt_price_x64(t)
    // <= sqrt_price_x64
    while lo < hi {
        let mid = lo + (hi - lo + 1) / 2;
        let price = tick_to_sqrt_price_x64(mid)?;
        if price <= sqrt_price_x64 {
            lo = mid;
        } else {
            hi = mid - 1;
        }
    }

    Ok(lo)
}

/// Compute `10^exp` as a `Decimal` (exact for exponents up to 28).
fn decimal_pow10(exp: u32) -> Decimal {
    let mut result = Decimal::ONE;
    let ten = Decimal::from(10u64);
    for _ in 0..exp {
        result *= ten;
    }
    result
}

/// Convert a human-readable price (`Decimal`) to a Q64.64 sqrt_price.
///
/// `sqrt_price_x64 = floor(sqrt(price / 10^(decimals_a - decimals_b)) * 2^64)`
///
/// Uses `Decimal` arithmetic for the ratio and `f64` only for the final
/// sqrt + scale step, which preserves full precision for extreme prices
/// (e.g. SHIB at 0.00001 or BTC at 100 000).
pub fn decimal_price_to_sqrt_price_x64(price: Decimal, decimals_a: u8, decimals_b: u8) -> u128 {
    let decimal_diff = decimals_a as i32 - decimals_b as i32;
    // 10^|decimal_diff| as Decimal — exact for reasonable exponents (max ~18).
    let power = decimal_pow10(decimal_diff.unsigned_abs());
    let adjusted = if decimal_diff >= 0 { price / power } else { price * power };

    // Convert to f64 only for the sqrt; the Decimal division above keeps
    // extreme ratios accurate.
    let adjusted_f64 = adjusted.to_f64().unwrap_or(0.0);
    if adjusted_f64 <= 0.0 {
        return 0;
    }
    let sqrt_val = adjusted_f64.sqrt();
    let two_64 = (1u128 << 64) as f64;
    (sqrt_val * two_64).floor() as u128
}

/// Convert a human-readable price (`Decimal`) to a tick index.
///
/// Internally converts via [`decimal_price_to_sqrt_price_x64`] and then
/// [`sqrt_price_x64_to_tick`], avoiding the f64 logarithm that caused
/// ±1 tick drift at extreme prices.
pub fn price_to_tick_index(price: Decimal, decimals_a: u8, decimals_b: u8) -> i32 {
    let sqrt_price_x64 = decimal_price_to_sqrt_price_x64(price, decimals_a, decimals_b);
    sqrt_price_x64_to_tick(sqrt_price_x64).unwrap_or(if price >= Decimal::ONE {
        MAX_TICK
    } else {
        MIN_TICK
    })
}

/// Convert a tick index to a human-readable price (`Decimal`).
///
/// `price = (sqrt_price_x64 / 2^64)^2 * 10^(decimals_a - decimals_b)`
pub fn tick_index_to_price(tick_index: i32, decimals_a: u8, decimals_b: u8) -> Decimal {
    let sqrt_price_x64 = tick_to_sqrt_price_x64(tick_index).unwrap_or(if tick_index >= 0 {
        MAX_SQRT_PRICE
    } else {
        MIN_SQRT_PRICE
    });
    crate::price_math::sqrt_price_x64_to_price(sqrt_price_x64, decimals_a, decimals_b)
}

// ---------------------------------------------------------------------------
// Tick array helpers (pure math, no Solana SDK)
// ---------------------------------------------------------------------------

/// Orca Whirlpool tick array size (88 slots per array).
pub const WHIRLPOOL_TICK_ARRAY_SIZE: i32 = 88;

/// Raydium CLMM tick array size (60 slots per array).
pub const RAYDIUM_TICK_ARRAY_SIZE: i32 = 60;

/// Number of ticks per tick array for a given spacing and array size.
pub fn tick_count(tick_spacing: u16, tick_array_size: i32) -> i32 {
    tick_array_size * tick_spacing as i32
}

/// Start tick index of the tick array containing `tick_index`.
pub fn get_tick_array_start_index(tick_index: i32, tick_spacing: u16, tick_array_size: i32) -> i32 {
    let ticks_in_array = tick_count(tick_spacing, tick_array_size);
    let mut start = tick_index / ticks_in_array;
    if tick_index < 0 && tick_index % ticks_in_array != 0 {
        start -= 1;
    }
    start * ticks_in_array
}

/// Index of `tick_index` within its tick array.
///
/// Returns `None` if `tick_spacing` is zero, the tick is before
/// `start_tick_index`, or the offset exceeds the array size.
pub fn get_tick_index_in_array(
    tick_index: i32,
    start_tick_index: i32,
    tick_spacing: u16,
    tick_array_size: i32,
) -> Option<usize> {
    if tick_spacing == 0 {
        return None;
    }
    let offset = tick_index - start_tick_index;
    if offset < 0 {
        return None;
    }
    let index = offset / tick_spacing as i32;
    if index < 0 || index >= tick_array_size {
        return None;
    }
    Some(index as usize)
}

/// Snap a tick index to the nearest initializable tick.
///
/// If `round_up` is `Some(true)`, rounds up when the remainder is > 0.
/// If `round_up` is `Some(false)`, always rounds down.
/// If `round_up` is `None`, rounds to the nearest (ties round up).
pub fn get_initializable_tick_index(
    tick_index: i32,
    tick_spacing: u16,
    round_up: Option<bool>,
) -> i32 {
    let ts = tick_spacing as i32;
    let remainder = tick_index.rem_euclid(ts);
    let result = tick_index.div_euclid(ts) * ts;

    let should_round_up = if let Some(up) = round_up {
        up && remainder > 0
    } else {
        remainder >= ts / 2 && remainder > 0
    };

    if should_round_up {
        result + ts
    } else {
        result
    }
}

// ---------------------------------------------------------------------------
// Trivial utility functions
// ---------------------------------------------------------------------------

/// Check if a tick index is within the valid range [MIN_TICK, MAX_TICK].
pub fn is_tick_in_bounds(tick: i32) -> bool {
    (MIN_TICK..=MAX_TICK).contains(&tick)
}

/// Check if a tick index is initializable (aligned to tick spacing).
pub fn is_tick_initializable(tick: i32, tick_spacing: u16) -> bool {
    tick_spacing > 0 && tick % tick_spacing as i32 == 0
}

/// Negate a tick index (mirrors the price around 1.0).
pub fn invert_tick_index(tick: i32) -> i32 {
    -tick
}

/// Get the full-range tick indexes for a given tick spacing.
///
/// Returns the widest possible tick range aligned to the spacing.
pub fn get_full_range_tick_indexes(tick_spacing: u16) -> crate::liquidity_math::TickRange {
    let ts = tick_spacing as i32;
    let lower = (MIN_TICK / ts) * ts;
    let upper = (MAX_TICK / ts) * ts;
    crate::liquidity_math::TickRange { tick_lower_index: lower, tick_upper_index: upper }
}

/// Get the previous initializable tick index (floor to spacing).
pub fn get_prev_initializable_tick_index(tick: i32, tick_spacing: u16) -> i32 {
    let ts = tick_spacing as i32;
    tick.div_euclid(ts) * ts
}

/// Get the next initializable tick index (ceil to spacing).
pub fn get_next_initializable_tick_index(tick: i32, tick_spacing: u16) -> i32 {
    let ts = tick_spacing as i32;
    let floor = tick.div_euclid(ts) * ts;
    if floor == tick {
        tick
    } else {
        floor + ts
    }
}

/// Check whether a position is in range given the current sqrt_price.
pub fn is_position_in_range(
    sqrt_price_current: u128,
    tick_lower: i32,
    tick_upper: i32,
) -> Result<bool, AmmMathError> {
    let sqrt_lower = tick_to_sqrt_price_x64(tick_lower)?;
    let sqrt_upper = tick_to_sqrt_price_x64(tick_upper)?;
    Ok(sqrt_price_current >= sqrt_lower && sqrt_price_current < sqrt_upper)
}

// ---------------------------------------------------------------------------
// Deprecated f64 variants — kept for backwards compatibility
// ---------------------------------------------------------------------------

/// Convert a human-readable f64 price to a tick index.
///
/// **Deprecated**: use [`price_to_tick_index`] (accepts `Decimal`) instead.
#[deprecated(note = "Use price_to_tick_index (Decimal) for better precision at extreme prices")]
pub fn price_to_tick_index_f64(price: f64, decimals_a: u8, decimals_b: u8) -> i32 {
    let sqrt_price_x64 = price_to_sqrt_price_x64_f64(price, decimals_a, decimals_b);
    sqrt_price_x64_to_tick(sqrt_price_x64).unwrap_or(if price >= 1.0 { MAX_TICK } else { MIN_TICK })
}

/// Convert a tick index to a human-readable f64 price.
///
/// **Deprecated**: use [`tick_index_to_price`] (returns `Decimal`) instead.
#[deprecated(note = "Use tick_index_to_price (Decimal) for better precision at extreme prices")]
pub fn tick_index_to_price_f64(tick_index: i32, decimals_a: u8, decimals_b: u8) -> f64 {
    let sqrt_price_x64 = tick_to_sqrt_price_x64(tick_index).unwrap_or(if tick_index >= 0 {
        MAX_SQRT_PRICE
    } else {
        MIN_SQRT_PRICE
    });
    crate::price_math::sqrt_price_to_price(sqrt_price_x64, decimals_a, decimals_b)
}

/// Convert a human-readable f64 price to a Q64.64 sqrt_price (f64 path).
fn price_to_sqrt_price_x64_f64(price: f64, decimals_a: u8, decimals_b: u8) -> u128 {
    let power = 10f64.powi(decimals_a as i32 - decimals_b as i32);
    (f64::floor(f64::sqrt(price / power) * (1u128 << 64) as f64)) as u128
}

#[cfg(test)]
mod tests {
    use super::*;

    const Q64: u128 = 1u128 << 64;

    #[test]
    fn test_tick_zero() {
        let result = tick_to_sqrt_price_x64(0).unwrap();
        assert_eq!(result, Q64, "tick 0 should produce 2^64");
    }

    #[test]
    fn test_tick_positive_one() {
        let result = tick_to_sqrt_price_x64(1).unwrap();
        // 1.0001^(0.5) * 2^64 should be slightly above 2^64
        assert!(result > Q64);
        // Expected: ~18446744073709551616 + ~922_337_203_685 = use float
        // check
        let ratio = result as f64 / Q64 as f64;
        assert!((ratio - 1.000_05).abs() < 0.000_001, "tick 1 ratio {ratio} not close to 1.00005");
    }

    #[test]
    fn test_tick_negative_one() {
        let result = tick_to_sqrt_price_x64(-1).unwrap();
        assert!(result < Q64, "negative tick should produce price < 2^64");
        let ratio = result as f64 / Q64 as f64;
        assert!((ratio - 0.999_95).abs() < 0.000_001, "tick -1 ratio {ratio} not close to 0.99995");
    }

    #[test]
    fn test_min_tick() {
        let result = tick_to_sqrt_price_x64(MIN_TICK).unwrap();
        assert_eq!(result, MIN_SQRT_PRICE, "min tick should produce min sqrt price");
    }

    #[test]
    fn test_max_tick() {
        let result = tick_to_sqrt_price_x64(MAX_TICK).unwrap();
        assert_eq!(result, MAX_SQRT_PRICE, "max tick should produce max sqrt price");
    }

    #[test]
    fn test_tick_out_of_range() {
        assert!(tick_to_sqrt_price_x64(MIN_TICK - 1).is_err());
        assert!(tick_to_sqrt_price_x64(MAX_TICK + 1).is_err());
    }

    #[test]
    fn test_sqrt_price_to_tick_zero() {
        let tick = sqrt_price_x64_to_tick(Q64).unwrap();
        assert_eq!(tick, 0);
    }

    #[test]
    fn test_sqrt_price_to_tick_min() {
        let tick = sqrt_price_x64_to_tick(MIN_SQRT_PRICE).unwrap();
        assert_eq!(tick, MIN_TICK);
    }

    #[test]
    fn test_sqrt_price_to_tick_max() {
        let tick = sqrt_price_x64_to_tick(MAX_SQRT_PRICE).unwrap();
        assert_eq!(tick, MAX_TICK);
    }

    #[test]
    fn test_roundtrip_positive_ticks() {
        for tick in [0, 1, 10, 100, 1000, 10000, 100000] {
            let price = tick_to_sqrt_price_x64(tick).unwrap();
            let recovered = sqrt_price_x64_to_tick(price).unwrap();
            assert_eq!(recovered, tick, "roundtrip failed for tick {tick}");
        }
    }

    #[test]
    fn test_roundtrip_negative_ticks() {
        for tick in [-1, -10, -100, -1000, -10000, -100000] {
            let price = tick_to_sqrt_price_x64(tick).unwrap();
            let recovered = sqrt_price_x64_to_tick(price).unwrap();
            assert_eq!(recovered, tick, "roundtrip failed for tick {tick}");
        }
    }

    #[test]
    fn test_sqrt_price_out_of_range() {
        assert!(sqrt_price_x64_to_tick(0).is_err());
        assert!(sqrt_price_x64_to_tick(MIN_SQRT_PRICE - 1).is_err());
        assert!(sqrt_price_x64_to_tick(MAX_SQRT_PRICE + 1).is_err());
    }

    #[test]
    fn test_monotonically_increasing() {
        let ticks = [-100000, -10000, -1000, -100, -10, 0, 10, 100, 1000, 10000, 100000];
        let prices: Vec<u128> = ticks.iter().map(|&t| tick_to_sqrt_price_x64(t).unwrap()).collect();
        for i in 1..prices.len() {
            assert!(
                prices[i] > prices[i - 1],
                "prices not monotonic: tick {} => {}, tick {} => {}",
                ticks[i - 1],
                prices[i - 1],
                ticks[i],
                prices[i]
            );
        }
    }

    #[test]
    fn test_symmetry() {
        // price(tick) * price(-tick) should be close to (2^64)^2
        for tick in [1, 10, 100, 1000, 10000] {
            let p_pos = tick_to_sqrt_price_x64(tick).unwrap();
            let p_neg = tick_to_sqrt_price_x64(-tick).unwrap();
            let product = (p_pos as f64) * (p_neg as f64);
            let expected = (Q64 as f64) * (Q64 as f64);
            let ratio = product / expected;
            assert!((ratio - 1.0).abs() < 0.001, "symmetry broken for tick {tick}: ratio={ratio}");
        }
    }

    #[test]
    fn sqrt_price_roundtrip() {
        for tick in [-100_000, -1000, -1, 0, 1, 1000, 100_000] {
            let sqrt = tick_to_sqrt_price_x64(tick).unwrap();
            let back = sqrt_price_x64_to_tick(sqrt).unwrap();
            assert!((back - tick).abs() <= 1, "tick={} roundtrip={}", tick, back);
        }
    }

    #[test]
    fn test_known_values() {
        // tick = 0 => price = 1.0 => sqrt_price = 2^64
        assert_eq!(tick_to_sqrt_price_x64(0).unwrap(), Q64);

        // tick = 10000 => 1.0001^5000 ~ 1.6487
        let price_10000 = tick_to_sqrt_price_x64(10000).unwrap();
        let ratio = price_10000 as f64 / Q64 as f64;
        assert!((ratio - 1.6487).abs() < 0.001, "tick 10000 ratio: {ratio}");

        // tick = -10000 => 1.0001^(-5000) ~ 0.6065
        let price_neg_10000 = tick_to_sqrt_price_x64(-10000).unwrap();
        let ratio_neg = price_neg_10000 as f64 / Q64 as f64;
        assert!((ratio_neg - 0.6065).abs() < 0.001, "tick -10000 ratio: {ratio_neg}");
    }

    // ---- price_to_tick_index / tick_index_to_price (Decimal) tests ----

    #[test]
    fn test_price_to_tick_index_known_values() {
        // These match orca_whirlpools_core test vectors exactly.
        assert_eq!(price_to_tick_index(Decimal::from_str("0.009998").unwrap(), 8, 6), -92111);
        assert_eq!(price_to_tick_index(Decimal::ONE, 6, 6), 0);
        assert_eq!(price_to_tick_index(Decimal::from_str("99.999912").unwrap(), 6, 8), 92108);
    }

    #[test]
    fn test_tick_index_to_price_known_values() {
        let p1 = tick_index_to_price(-92111, 8, 6);
        let diff1 = (p1 - Decimal::from_str("0.009998").unwrap()).abs();
        assert!(diff1 < Decimal::from_str("0.00001").unwrap(), "got {p1}");

        let p2 = tick_index_to_price(0, 6, 6);
        let diff2 = (p2 - Decimal::ONE).abs();
        assert!(diff2 < Decimal::from_str("0.0000000001").unwrap(), "got {p2}");

        let p3 = tick_index_to_price(92108, 6, 8);
        let diff3 = (p3 - Decimal::from_str("99.999912").unwrap()).abs();
        assert!(diff3 < Decimal::from_str("0.001").unwrap(), "got {p3}");
    }

    #[test]
    fn test_price_tick_roundtrip() {
        // price -> tick -> price should be close to the original
        let cases: &[(Decimal, u8, u8)] = &[
            (Decimal::ONE, 6, 6),
            (Decimal::from_str("100.0").unwrap(), 9, 6),
            (Decimal::from_str("0.001").unwrap(), 6, 9),
            (Decimal::from_str("50000.0").unwrap(), 8, 6),
        ];
        for &(price, da, db) in cases {
            let tick = price_to_tick_index(price, da, db);
            let recovered = tick_index_to_price(tick, da, db);
            let err = ((recovered - price) / price).abs();
            assert!(
                err < Decimal::from_str("0.0002").unwrap(),
                "roundtrip error too large for price={price}: got {recovered}, err={err}"
            );
        }
    }

    // ---- New tests for extreme prices and round-trip precision ----

    #[test]
    fn test_price_to_tick_normal_range() {
        // SOL/USDC ~$150, decimals (9, 6)
        let price = Decimal::from_str("150.0").unwrap();
        let tick = price_to_tick_index(price, 9, 6);
        let recovered = tick_index_to_price(tick, 9, 6);
        let err = ((recovered - price) / price).abs();
        assert!(
            err < Decimal::from_str("0.001").unwrap(),
            "normal range: price={price}, recovered={recovered}, err={err}"
        );
    }

    #[test]
    fn test_price_to_tick_extreme_low() {
        // SHIB-like price: 0.00001
        let price = Decimal::from_str("0.00001").unwrap();
        let tick = price_to_tick_index(price, 9, 6);
        let recovered = tick_index_to_price(tick, 9, 6);
        let err = ((recovered - price) / price).abs();
        assert!(
            err < Decimal::from_str("0.001").unwrap(),
            "extreme low: price={price}, recovered={recovered}, err={err}"
        );
    }

    #[test]
    fn test_price_to_tick_extreme_high() {
        // BTC-like price: 100000
        let price = Decimal::from_str("100000.0").unwrap();
        let tick = price_to_tick_index(price, 8, 6);
        let recovered = tick_index_to_price(tick, 8, 6);
        let err = ((recovered - price) / price).abs();
        assert!(
            err < Decimal::from_str("0.001").unwrap(),
            "extreme high: price={price}, recovered={recovered}, err={err}"
        );
    }

    #[test]
    fn test_price_to_tick_roundtrip_precision() {
        // Round-trip error must be < 0.1% across a wide range of prices
        let cases: &[(Decimal, u8, u8)] = &[
            (Decimal::from_str("0.0000001").unwrap(), 9, 6), // very small
            (Decimal::from_str("0.001").unwrap(), 6, 6),     // small
            (Decimal::from_str("1.0").unwrap(), 6, 6),       // unity
            (Decimal::from_str("1000.0").unwrap(), 9, 6),    // medium
            (Decimal::from_str("100000.0").unwrap(), 8, 6),  // large (BTC)
        ];
        for &(price, da, db) in cases {
            let tick = price_to_tick_index(price, da, db);
            let recovered = tick_index_to_price(tick, da, db);
            let err = ((recovered - price) / price).abs();
            assert!(
                err < Decimal::from_str("0.001").unwrap(),
                "round-trip error >= 0.1% for price={price} (da={da}, db={db}): \
                 recovered={recovered}, err={err}"
            );
        }
    }

    // ---- Deterministic RNG fuzz tests ----

    #[test]
    fn fuzz_tick_to_price_roundtrip() {
        use rand::Rng;
        let mut rng = rand::rng();
        for _ in 0..1000 {
            let tick: i32 = rng.random_range(MIN_TICK..=MAX_TICK);
            let price = tick_to_sqrt_price_x64(tick).unwrap();
            let recovered = sqrt_price_x64_to_tick(price).unwrap();
            assert!(
                (recovered - tick).abs() <= 1,
                "roundtrip failed: tick={tick}, price={price}, recovered={recovered}"
            );
        }
    }

    #[test]
    fn fuzz_tick_monotonicity() {
        use rand::Rng;
        let mut rng = rand::rng();
        for _ in 0..1000 {
            let tick_a: i32 = rng.random_range(MIN_TICK..=MAX_TICK);
            let tick_b: i32 = rng.random_range(MIN_TICK..=MAX_TICK);
            let price_a = tick_to_sqrt_price_x64(tick_a).unwrap();
            let price_b = tick_to_sqrt_price_x64(tick_b).unwrap();
            if tick_a < tick_b {
                assert!(
                    price_a < price_b,
                    "monotonicity violated: tick_a={tick_a} (price={price_a}) >= tick_b={tick_b} \
                     (price={price_b})"
                );
            } else if tick_a > tick_b {
                assert!(
                    price_a > price_b,
                    "monotonicity violated: tick_a={tick_a} (price={price_a}) <= tick_b={tick_b} \
                     (price={price_b})"
                );
            } else {
                assert_eq!(price_a, price_b);
            }
        }
    }

    #[test]
    fn fuzz_sqrt_price_bounds() {
        use rand::Rng;
        let mut rng = rand::rng();
        for _ in 0..1000 {
            let tick: i32 = rng.random_range(MIN_TICK..=MAX_TICK);
            let price = tick_to_sqrt_price_x64(tick).unwrap();
            assert!(
                (MIN_SQRT_PRICE..=MAX_SQRT_PRICE).contains(&price),
                "price out of bounds: tick={tick}, price={price}"
            );
        }
    }

    #[test]
    fn test_deprecated_f64_still_works() {
        // Ensure the deprecated f64 variants still produce reasonable results.
        #[allow(deprecated)]
        {
            let tick = price_to_tick_index_f64(1.0, 6, 6);
            assert_eq!(tick, 0);

            let price = tick_index_to_price_f64(0, 6, 6);
            assert!((price - 1.0).abs() < 1e-10, "got {price}");
        }
    }

    // ---- Tick array helper tests ----

    #[test]
    fn test_tick_count() {
        assert_eq!(tick_count(64, WHIRLPOOL_TICK_ARRAY_SIZE), 88 * 64);
        assert_eq!(tick_count(1, WHIRLPOOL_TICK_ARRAY_SIZE), 88);
        assert_eq!(tick_count(10, WHIRLPOOL_TICK_ARRAY_SIZE), 880);
        assert_eq!(tick_count(1, RAYDIUM_TICK_ARRAY_SIZE), 60);
    }

    #[test]
    fn test_get_tick_array_start_index_at_zero() {
        assert_eq!(get_tick_array_start_index(0, 64, WHIRLPOOL_TICK_ARRAY_SIZE), 0,);
    }

    #[test]
    fn test_get_tick_array_start_index_positive() {
        // tick 100 with spacing 64 => ticks_in_array = 5632
        // 100 / 5632 = 0 => start = 0
        assert_eq!(get_tick_array_start_index(100, 64, WHIRLPOOL_TICK_ARRAY_SIZE), 0,);
        // tick 5632 => start = 5632
        assert_eq!(get_tick_array_start_index(5632, 64, WHIRLPOOL_TICK_ARRAY_SIZE), 5632,);
    }

    #[test]
    fn test_get_tick_array_start_index_negative() {
        let s = WHIRLPOOL_TICK_ARRAY_SIZE;
        assert_eq!(get_tick_array_start_index(-1, 64, s), -5632);
        assert_eq!(get_tick_array_start_index(-5632, 64, s), -5632);
        assert_eq!(get_tick_array_start_index(-5633, 64, s), -11264);
    }

    #[test]
    fn test_get_tick_index_in_array_at_start() {
        assert_eq!(get_tick_index_in_array(0, 0, 64, WHIRLPOOL_TICK_ARRAY_SIZE), Some(0),);
    }

    #[test]
    fn test_get_tick_index_in_array_zero_spacing() {
        assert_eq!(get_tick_index_in_array(0, 0, 0, WHIRLPOOL_TICK_ARRAY_SIZE), None,);
    }

    #[test]
    fn test_get_tick_index_in_array_out_of_range() {
        assert_eq!(get_tick_index_in_array(-1, 0, 64, WHIRLPOOL_TICK_ARRAY_SIZE), None,);
    }

    #[test]
    fn test_get_tick_index_in_array_middle() {
        // tick 128 in array starting at 0, spacing 64 => offset 128/64 = 2
        assert_eq!(get_tick_index_in_array(128, 0, 64, WHIRLPOOL_TICK_ARRAY_SIZE), Some(2),);
    }

    #[test]
    fn test_get_initializable_tick_index_round_down() {
        assert_eq!(get_initializable_tick_index(5, 64, None), 0);
        assert_eq!(get_initializable_tick_index(33, 64, Some(false)), 0);
    }

    #[test]
    fn test_get_initializable_tick_index_round_up() {
        assert_eq!(get_initializable_tick_index(33, 64, None), 64);
        assert_eq!(get_initializable_tick_index(33, 64, Some(true)), 64);
    }

    #[test]
    fn test_get_initializable_tick_index_negative() {
        assert_eq!(get_initializable_tick_index(-33, 64, Some(false)), -64);
        assert_eq!(get_initializable_tick_index(-33, 64, Some(true)), 0);
    }

    #[test]
    fn test_get_initializable_tick_index_exact() {
        assert_eq!(get_initializable_tick_index(128, 64, Some(true)), 128);
        assert_eq!(get_initializable_tick_index(128, 64, Some(false)), 128);
    }

    // ---- Utility function tests ----

    #[test]
    fn test_is_tick_in_bounds() {
        assert!(is_tick_in_bounds(0));
        assert!(is_tick_in_bounds(MIN_TICK));
        assert!(is_tick_in_bounds(MAX_TICK));
        assert!(!is_tick_in_bounds(MIN_TICK - 1));
        assert!(!is_tick_in_bounds(MAX_TICK + 1));
    }

    #[test]
    fn test_is_tick_initializable() {
        assert!(is_tick_initializable(0, 64));
        assert!(is_tick_initializable(128, 64));
        assert!(is_tick_initializable(-128, 64));
        assert!(!is_tick_initializable(1, 64));
        assert!(!is_tick_initializable(63, 64));
        // zero spacing always returns false
        assert!(!is_tick_initializable(0, 0));
    }

    #[test]
    fn test_invert_tick_index() {
        assert_eq!(invert_tick_index(100), -100);
        assert_eq!(invert_tick_index(-100), 100);
        assert_eq!(invert_tick_index(0), 0);
    }

    #[test]
    fn test_get_full_range_tick_indexes() {
        let range = get_full_range_tick_indexes(64);
        assert_eq!(range.tick_lower_index % 64, 0);
        assert_eq!(range.tick_upper_index % 64, 0);
        assert!(range.tick_lower_index >= MIN_TICK);
        assert!(range.tick_upper_index <= MAX_TICK);
        // Widest possible
        assert!(range.tick_lower_index <= MIN_TICK + 63);
        assert!(range.tick_upper_index >= MAX_TICK - 63);
    }

    #[test]
    fn test_get_prev_initializable_tick_index() {
        assert_eq!(get_prev_initializable_tick_index(100, 64), 64);
        assert_eq!(get_prev_initializable_tick_index(128, 64), 128);
        assert_eq!(get_prev_initializable_tick_index(0, 64), 0);
        // Negative: -1 floors to -64
        assert_eq!(get_prev_initializable_tick_index(-1, 64), -64);
        assert_eq!(get_prev_initializable_tick_index(-64, 64), -64);
    }

    #[test]
    fn test_get_next_initializable_tick_index() {
        assert_eq!(get_next_initializable_tick_index(100, 64), 128);
        assert_eq!(get_next_initializable_tick_index(128, 64), 128);
        assert_eq!(get_next_initializable_tick_index(0, 64), 0);
        assert_eq!(get_next_initializable_tick_index(-1, 64), 0);
        assert_eq!(get_next_initializable_tick_index(-64, 64), -64);
        assert_eq!(get_next_initializable_tick_index(-65, 64), -64);
    }

    #[test]
    fn test_is_position_in_range() {
        let sqrt_price = tick_to_sqrt_price_x64(0).unwrap();
        // Position [-100, 100] should contain tick 0
        assert!(is_position_in_range(sqrt_price, -100, 100).unwrap());
        // Position [1, 100] should NOT contain tick 0
        assert!(!is_position_in_range(sqrt_price, 1, 100).unwrap());
        // Upper bound is exclusive
        let sqrt_upper = tick_to_sqrt_price_x64(100).unwrap();
        assert!(!is_position_in_range(sqrt_upper, 0, 100).unwrap());
    }

    #[test]
    fn test_is_position_in_range_error() {
        // Out-of-range ticks should error
        assert!(is_position_in_range(Q64, MIN_TICK - 1, 0).is_err());
        assert!(is_position_in_range(Q64, 0, MAX_TICK + 1).is_err());
    }
}