Skip to main content

evm_dex_pool/v3/
utils.rs

1use alloy::primitives::{address, uint, Address, U160, U256};
2
3pub(crate) const ONE: U160 = uint!(1_U160);
4pub(crate) const TWO: U256 = uint!(2_U256);
5pub(crate) const THREE: U256 = uint!(3_U256);
6pub const Q96: U256 = U256::from_limbs([0, 1 << 32, 0, 0]);
7pub const Q128: U256 = U256::from_limbs([0, 0, 1, 0]);
8pub const Q192: U256 = U256::from_limbs([0, 0, 0, 1]);
9
10/// Convert a tick to its corresponding word index in the tick bitmap
11pub fn tick_to_word(tick: i32, tick_spacing: i32) -> i32 {
12    let compressed = tick / tick_spacing;
13    let compressed = if tick < 0 && tick % tick_spacing != 0 {
14        compressed - 1
15    } else {
16        compressed
17    };
18    compressed >> 8
19}
20
21/// Convert a word index back to the tick range it represents
22pub fn word_to_tick_range(word: i32, tick_spacing: i32) -> (i32, i32) {
23    let min_tick = (word << 8) * tick_spacing;
24    let max_tick = ((word + 1) << 8) * tick_spacing - 1;
25    (min_tick, max_tick)
26}
27
28// Factory/quoter
29pub const RAMSES_FACTORIES: &[(Address, Address)] = &[(
30    address!("0xAAA32926fcE6bE95ea2c51cB4Fcb60836D320C42"),
31    address!("0xAAAbFD1E45Cc93d16c2751645e50F2594bE12680"),
32)];
33
34pub fn is_ramses_factory(factory: Address) -> bool {
35    RAMSES_FACTORIES.iter().any(|(f, _)| *f == factory)
36}
37
38pub fn get_ramses_quoter(factory: Address) -> Option<Address> {
39    RAMSES_FACTORIES
40        .iter()
41        .find(|(f, _)| *f == factory)
42        .map(|(_, q)| *q)
43}