Skip to main content

tycho_simulation/evm/protocol/uniswap_v3/
state.rs

1use std::{any::Any, collections::HashMap};
2
3use alloy::primitives::{Sign, I256, U256};
4use num_bigint::BigUint;
5use num_traits::Zero;
6use serde::{Deserialize, Serialize};
7use tracing::trace;
8use tycho_common::{
9    dto::ProtocolStateDelta,
10    models::token::Token,
11    simulation::{
12        errors::{SimulationError, TransitionError},
13        protocol_sim::{
14            Balances, GetAmountOutResult, PoolSwap, ProtocolSim, QueryPoolSwapParams,
15            SwapConstraint,
16        },
17    },
18    Bytes,
19};
20
21use super::enums::FeeAmount;
22use crate::evm::protocol::{
23    clmm::clmm_swap_to_price,
24    safe_math::{safe_add_u256, safe_sub_u256},
25    u256_num::u256_to_biguint,
26    utils::{
27        add_fee_markup,
28        uniswap::{
29            i24_be_bytes_to_i32, liquidity_math,
30            sqrt_price_math::{get_amount0_delta, get_amount1_delta, sqrt_price_q96_to_f64},
31            swap_math,
32            tick_list::{TickInfo, TickList, TickListErrorKind},
33            tick_math::{
34                get_sqrt_ratio_at_tick, get_tick_at_sqrt_ratio, MAX_SQRT_RATIO, MAX_TICK,
35                MIN_SQRT_RATIO, MIN_TICK,
36            },
37            StepComputation, SwapResults, SwapState,
38        },
39    },
40};
41
42// Gas limit constants for capping get_limits calculations
43// These prevent simulations from exceeding Ethereum's block gas limit
44// The names of the constants reflect the exact method from the tenderly log.
45const SWAP_BASE_GAS: u64 = 130_000;
46// Bitmap word scan
47const GAS_PER_BITMAP_WORD: u64 = 2_100;
48// swap math step: getSqrtRatioAtTick + computeSwapStep + amount accounting
49const GAS_PER_SWAP_MATH_STEP: u64 = 4_000;
50// Initialized tick crossing
51const GAS_PER_INITIALIZED_TICK_CROSS: u64 = 17_540;
52// Output transfer + balanceBefore + callback + balanceAfter.
53const V3_CALLBACK_SETTLEMENT_GAS: u64 = 70_000;
54// Conservative max gas budget for a single swap (Ethereum transaction gas limit)
55const MAX_SWAP_GAS: u64 = 16_700_000;
56const MAX_TICKS_CROSSED: u64 = (MAX_SWAP_GAS - SWAP_BASE_GAS) / GAS_PER_INITIALIZED_TICK_CROSS;
57
58#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
59pub struct UniswapV3State {
60    liquidity: u128,
61    sqrt_price: U256,
62    fee: FeeAmount,
63    tick: i32,
64    ticks: TickList,
65}
66
67impl UniswapV3State {
68    /// Creates a new instance of `UniswapV3State`.
69    ///
70    /// # Arguments
71    /// - `liquidity`: The initial liquidity of the pool.
72    /// - `sqrt_price`: The square root of the current price.
73    /// - `fee`: The fee tier for the pool.
74    /// - `tick`: The current tick of the pool.
75    /// - `ticks`: A vector of `TickInfo` representing the tick information for the pool.
76    pub fn new(
77        liquidity: u128,
78        sqrt_price: U256,
79        fee: FeeAmount,
80        tick: i32,
81        ticks: Vec<TickInfo>,
82    ) -> Result<Self, SimulationError> {
83        let spacing = UniswapV3State::get_spacing(fee);
84        let tick_list = TickList::from(spacing, ticks)?;
85        Ok(UniswapV3State { liquidity, sqrt_price, fee, tick, ticks: tick_list })
86    }
87
88    fn get_spacing(fee: FeeAmount) -> u16 {
89        match fee {
90            FeeAmount::Lowest => 1,
91            FeeAmount::Lowest2 => 2,
92            FeeAmount::Lowest3 => 3,
93            FeeAmount::Lowest4 => 4,
94            FeeAmount::Low => 10,
95            FeeAmount::MediumLow => 50,
96            FeeAmount::Medium => 60,
97            FeeAmount::MediumHigh => 100,
98            FeeAmount::High => 200,
99        }
100    }
101
102    fn swap(
103        &self,
104        zero_for_one: bool,
105        amount_specified: I256,
106        sqrt_price_limit: Option<U256>,
107    ) -> Result<SwapResults, SimulationError> {
108        if self.liquidity == 0 {
109            return Err(SimulationError::RecoverableError("No liquidity".to_string()));
110        }
111        let price_limit = if let Some(limit) = sqrt_price_limit {
112            limit
113        } else if zero_for_one {
114            safe_add_u256(MIN_SQRT_RATIO, U256::from(1u64))?
115        } else {
116            safe_sub_u256(MAX_SQRT_RATIO, U256::from(1u64))?
117        };
118
119        let price_limit_valid = if zero_for_one {
120            price_limit > MIN_SQRT_RATIO && price_limit < self.sqrt_price
121        } else {
122            price_limit < MAX_SQRT_RATIO && price_limit > self.sqrt_price
123        };
124        if !price_limit_valid {
125            return Err(SimulationError::InvalidInput("Price limit out of range".into(), None));
126        }
127
128        let exact_input = amount_specified > I256::from_raw(U256::from(0u64));
129
130        let mut state = SwapState {
131            amount_remaining: amount_specified,
132            amount_calculated: I256::from_raw(U256::from(0u64)),
133            sqrt_price: self.sqrt_price,
134            tick: self.tick,
135            liquidity: self.liquidity,
136        };
137        let mut gas_used = U256::from(0);
138
139        while state.amount_remaining != I256::from_raw(U256::from(0u64)) &&
140            state.sqrt_price != price_limit
141        {
142            let (mut next_tick, initialized) = match self
143                .ticks
144                .next_initialized_tick_within_one_word(state.tick, zero_for_one)
145            {
146                Ok((tick, init)) => {
147                    gas_used = safe_add_u256(gas_used, U256::from(GAS_PER_BITMAP_WORD))?;
148                    (tick, init)
149                }
150                Err(tick_err) => match tick_err.kind {
151                    TickListErrorKind::TicksExeeded => {
152                        let mut new_state = self.clone();
153                        new_state.liquidity = state.liquidity;
154                        new_state.tick = state.tick;
155                        new_state.sqrt_price = state.sqrt_price;
156                        return Err(SimulationError::InvalidInput(
157                            "Ticks exceeded".into(),
158                            Some(GetAmountOutResult::new(
159                                u256_to_biguint(state.amount_calculated.abs().into_raw()),
160                                u256_to_biguint(gas_used),
161                                Box::new(new_state),
162                            )),
163                        ));
164                    }
165                    _ => return Err(SimulationError::FatalError("Unknown error".to_string())),
166                },
167            };
168
169            next_tick = next_tick.clamp(MIN_TICK, MAX_TICK);
170
171            let sqrt_price_start = state.sqrt_price;
172            let sqrt_price_next = get_sqrt_ratio_at_tick(next_tick)?;
173            let (sqrt_price, amount_in, amount_out, fee_amount) = swap_math::compute_swap_step(
174                state.sqrt_price,
175                UniswapV3State::get_sqrt_ratio_target(sqrt_price_next, price_limit, zero_for_one),
176                state.liquidity,
177                state.amount_remaining,
178                self.fee as u32,
179            )?;
180            state.sqrt_price = sqrt_price;
181
182            let step = StepComputation {
183                sqrt_price_start,
184                tick_next: next_tick,
185                initialized,
186                sqrt_price_next,
187                amount_in,
188                amount_out,
189                fee_amount,
190            };
191
192            gas_used = safe_add_u256(gas_used, U256::from(GAS_PER_SWAP_MATH_STEP))?;
193
194            if exact_input {
195                state.amount_remaining -= I256::checked_from_sign_and_abs(
196                    Sign::Positive,
197                    safe_add_u256(step.amount_in, step.fee_amount)?,
198                )
199                .unwrap();
200                state.amount_calculated -=
201                    I256::checked_from_sign_and_abs(Sign::Positive, step.amount_out).unwrap();
202            } else {
203                state.amount_remaining +=
204                    I256::checked_from_sign_and_abs(Sign::Positive, step.amount_out).unwrap();
205                state.amount_calculated += I256::checked_from_sign_and_abs(
206                    Sign::Positive,
207                    safe_add_u256(step.amount_in, step.fee_amount)?,
208                )
209                .unwrap();
210            }
211            if state.sqrt_price == step.sqrt_price_next {
212                if step.initialized {
213                    let liquidity_raw = self
214                        .ticks
215                        .get_tick(step.tick_next)
216                        .unwrap()
217                        .net_liquidity;
218                    let liquidity_net = if zero_for_one { -liquidity_raw } else { liquidity_raw };
219                    state.liquidity =
220                        liquidity_math::add_liquidity_delta(state.liquidity, liquidity_net)?;
221                    gas_used = safe_add_u256(gas_used, U256::from(GAS_PER_INITIALIZED_TICK_CROSS))?;
222                }
223                state.tick = if zero_for_one { step.tick_next - 1 } else { step.tick_next };
224            } else if state.sqrt_price != step.sqrt_price_start {
225                state.tick = get_tick_at_sqrt_ratio(state.sqrt_price)?;
226            }
227        }
228        Ok(SwapResults {
229            amount_calculated: state.amount_calculated,
230            amount_specified,
231            amount_remaining: state.amount_remaining,
232            sqrt_price: state.sqrt_price,
233            liquidity: state.liquidity,
234            tick: state.tick,
235            gas_used: safe_add_u256(gas_used, U256::from(V3_CALLBACK_SETTLEMENT_GAS))?,
236        })
237    }
238
239    fn get_sqrt_ratio_target(
240        sqrt_price_next: U256,
241        sqrt_price_limit: U256,
242        zero_for_one: bool,
243    ) -> U256 {
244        let cond1 = if zero_for_one {
245            sqrt_price_next < sqrt_price_limit
246        } else {
247            sqrt_price_next > sqrt_price_limit
248        };
249
250        if cond1 {
251            sqrt_price_limit
252        } else {
253            sqrt_price_next
254        }
255    }
256}
257
258#[typetag::serde]
259impl ProtocolSim for UniswapV3State {
260    fn fee(&self) -> f64 {
261        (self.fee as u32) as f64 / 1_000_000.0
262    }
263
264    fn spot_price(&self, a: &Token, b: &Token) -> Result<f64, SimulationError> {
265        let price = if a < b {
266            sqrt_price_q96_to_f64(self.sqrt_price, a.decimals, b.decimals)?
267        } else {
268            1.0f64 / sqrt_price_q96_to_f64(self.sqrt_price, b.decimals, a.decimals)?
269        };
270        Ok(add_fee_markup(price, self.fee()))
271    }
272
273    fn get_amount_out(
274        &self,
275        amount_in: BigUint,
276        token_a: &Token,
277        token_b: &Token,
278    ) -> Result<GetAmountOutResult, SimulationError> {
279        let zero_for_one = token_a < token_b;
280        let amount_specified = I256::checked_from_sign_and_abs(
281            Sign::Positive,
282            U256::from_be_slice(&amount_in.to_bytes_be()),
283        )
284        .ok_or_else(|| {
285            SimulationError::InvalidInput("I256 overflow: amount_in".to_string(), None)
286        })?;
287
288        let result = self.swap(zero_for_one, amount_specified, None)?;
289
290        trace!(?amount_in, ?token_a, ?token_b, ?zero_for_one, ?result, "V3 SWAP");
291        let mut new_state = self.clone();
292        new_state.liquidity = result.liquidity;
293        new_state.tick = result.tick;
294        new_state.sqrt_price = result.sqrt_price;
295
296        Ok(GetAmountOutResult::new(
297            u256_to_biguint(
298                result
299                    .amount_calculated
300                    .abs()
301                    .into_raw(),
302            ),
303            u256_to_biguint(result.gas_used),
304            Box::new(new_state),
305        ))
306    }
307
308    fn get_limits(
309        &self,
310        token_in: Bytes,
311        token_out: Bytes,
312    ) -> Result<(BigUint, BigUint), SimulationError> {
313        // If the pool has no liquidity, return zeros for both limits
314        if self.liquidity == 0 {
315            return Ok((BigUint::zero(), BigUint::zero()));
316        }
317
318        let zero_for_one = token_in < token_out;
319        let mut current_tick = self.tick;
320        let mut current_sqrt_price = self.sqrt_price;
321        let mut current_liquidity = self.liquidity;
322        let mut total_amount_in = U256::from(0u64);
323        let mut total_amount_out = U256::from(0u64);
324        let mut ticks_crossed: u64 = 0;
325
326        // Iterate through ticks in the direction of the swap
327        // Stops when: no more liquidity, no more ticks, or gas limit would be exceeded
328        while let Ok((tick, initialized)) = self
329            .ticks
330            .next_initialized_tick_within_one_word(current_tick, zero_for_one)
331        {
332            // Cap iteration to prevent exceeding Ethereum's gas limit
333            if ticks_crossed >= MAX_TICKS_CROSSED {
334                break;
335            }
336            ticks_crossed += 1;
337
338            // Clamp the tick value to ensure it's within valid range
339            let next_tick = tick.clamp(MIN_TICK, MAX_TICK);
340
341            // Calculate the sqrt price at the next tick boundary
342            let sqrt_price_next = get_sqrt_ratio_at_tick(next_tick)?;
343
344            // Calculate the amount of tokens swapped when moving from current_sqrt_price to
345            // sqrt_price_next. Direction determines which token is being swapped in vs out
346            let (amount_in, amount_out) = if zero_for_one {
347                let amount0 = get_amount0_delta(
348                    sqrt_price_next,
349                    current_sqrt_price,
350                    current_liquidity,
351                    true,
352                )?;
353                let amount1 = get_amount1_delta(
354                    sqrt_price_next,
355                    current_sqrt_price,
356                    current_liquidity,
357                    false,
358                )?;
359                (amount0, amount1)
360            } else {
361                let amount0 = get_amount0_delta(
362                    sqrt_price_next,
363                    current_sqrt_price,
364                    current_liquidity,
365                    false,
366                )?;
367                let amount1 = get_amount1_delta(
368                    sqrt_price_next,
369                    current_sqrt_price,
370                    current_liquidity,
371                    true,
372                )?;
373                (amount1, amount0)
374            };
375
376            // Accumulate total amounts for this tick range
377            total_amount_in = safe_add_u256(total_amount_in, amount_in)?;
378            total_amount_out = safe_add_u256(total_amount_out, amount_out)?;
379
380            // If this tick is "initialized" (meaning its someone's position boundary), update the
381            // liquidity when crossing it
382            // For zero_for_one, liquidity is removed when crossing a tick
383            // For one_for_zero, liquidity is added when crossing a tick
384            if initialized {
385                let liquidity_raw = self
386                    .ticks
387                    .get_tick(next_tick)
388                    .unwrap()
389                    .net_liquidity;
390                let liquidity_delta = if zero_for_one { -liquidity_raw } else { liquidity_raw };
391
392                // Check if applying this liquidity delta would cause underflow
393                // If so, stop here rather than continuing with invalid state
394                match liquidity_math::add_liquidity_delta(current_liquidity, liquidity_delta) {
395                    Ok(new_liquidity) => {
396                        current_liquidity = new_liquidity;
397                    }
398                    Err(_) => {
399                        // Liquidity would underflow, stop iteration here
400                        // This represents the maximum liquidity we can actually use
401                        break;
402                    }
403                }
404            }
405
406            // Move to the next tick position
407            current_tick = if zero_for_one { next_tick - 1 } else { next_tick };
408            current_sqrt_price = sqrt_price_next;
409        }
410
411        Ok((u256_to_biguint(total_amount_in), u256_to_biguint(total_amount_out)))
412    }
413
414    fn delta_transition(
415        &mut self,
416        delta: ProtocolStateDelta,
417        _tokens: &HashMap<Bytes, Token>,
418        _balances: &Balances,
419    ) -> Result<(), TransitionError> {
420        // apply attribute changes
421        if let Some(liquidity) = delta
422            .updated_attributes
423            .get("liquidity")
424        {
425            // This is a hotfix because if the liquidity has never been updated after creation, it's
426            // currently encoded as H256::zero(), therefore, we can't decode this as u128.
427            // We can remove this once it has been fixed on the tycho side.
428            let liq_16_bytes = if liquidity.len() == 32 {
429                // Make sure it only happens for 0 values, otherwise error.
430                if liquidity == &Bytes::zero(32) {
431                    Bytes::from([0; 16])
432                } else {
433                    return Err(TransitionError::DecodeError(format!(
434                        "Liquidity bytes too long for {liquidity}, expected 16",
435                    )));
436                }
437            } else {
438                liquidity.clone()
439            };
440
441            self.liquidity = u128::from(liq_16_bytes);
442        }
443        if let Some(sqrt_price) = delta
444            .updated_attributes
445            .get("sqrt_price_x96")
446        {
447            self.sqrt_price = U256::from_be_slice(sqrt_price);
448        }
449        if let Some(tick) = delta.updated_attributes.get("tick") {
450            // This is a hotfix because if the tick has never been updated after creation, it's
451            // currently encoded as H256::zero(), therefore, we can't decode this as i32.
452            // We can remove this once it has been fixed on the tycho side.
453            let ticks_4_bytes = if tick.len() == 32 {
454                // Make sure it only happens for 0 values, otherwise error.
455                if tick == &Bytes::zero(32) {
456                    Bytes::from([0; 4])
457                } else {
458                    return Err(TransitionError::DecodeError(format!(
459                        "Tick bytes too long for {tick}, expected 4"
460                    )));
461                }
462            } else {
463                tick.clone()
464            };
465            self.tick = i24_be_bytes_to_i32(&ticks_4_bytes);
466        }
467
468        // apply tick changes
469        for (key, value) in delta.updated_attributes.iter() {
470            // tick liquidity keys are in the format "ticks/{tick_index}/net_liquidity"
471            if key.starts_with("ticks/") {
472                let parts: Vec<&str> = key.split('/').collect();
473                self.ticks
474                    .set_tick_liquidity(
475                        parts[1]
476                            .parse::<i32>()
477                            .map_err(|err| TransitionError::DecodeError(err.to_string()))?,
478                        i128::from(value.clone()),
479                    )
480                    .map_err(|err| TransitionError::DecodeError(err.to_string()))?;
481            }
482        }
483        // delete ticks - ignores deletes for attributes other than tick liquidity
484        for key in delta.deleted_attributes.iter() {
485            // tick liquidity keys are in the format "ticks/{tick_index}/net_liquidity"
486            if key.starts_with("ticks/") {
487                let parts: Vec<&str> = key.split('/').collect();
488                self.ticks
489                    .set_tick_liquidity(
490                        parts[1]
491                            .parse::<i32>()
492                            .map_err(|err| TransitionError::DecodeError(err.to_string()))?,
493                        0,
494                    )
495                    .map_err(|err| TransitionError::DecodeError(err.to_string()))?;
496            }
497        }
498        Ok(())
499    }
500
501    /// See [`ProtocolSim::query_pool_swap`] for the trait documentation.
502    ///
503    /// This method uses Uniswap V3 internal swap logic by swapping an infinite amount of token_in
504    /// until the target price is reached.
505    fn query_pool_swap(&self, params: &QueryPoolSwapParams) -> Result<PoolSwap, SimulationError> {
506        if self.liquidity == 0 {
507            return Err(SimulationError::FatalError("No liquidity".to_string()));
508        }
509
510        match params.swap_constraint() {
511            SwapConstraint::TradeLimitPrice { .. } => Err(SimulationError::InvalidInput(
512                "Uniswap V3 does not support TradeLimitPrice constraint in query_pool_swap"
513                    .to_string(),
514                None,
515            )),
516            SwapConstraint::PoolTargetPrice {
517                target,
518                tolerance: _,
519                min_amount_in: _,
520                max_amount_in: _,
521            } => {
522                let (amount_in, amount_out, swap_result) = clmm_swap_to_price(
523                    self.sqrt_price,
524                    &params.token_in().address,
525                    &params.token_out().address,
526                    target,
527                    self.fee as u32,
528                    Sign::Positive,
529                    |zero_for_one, amount_specified, sqrt_price_limit| {
530                        self.swap(zero_for_one, amount_specified, Some(sqrt_price_limit))
531                    },
532                )?;
533
534                let mut new_state = self.clone();
535                new_state.liquidity = swap_result.liquidity;
536                new_state.tick = swap_result.tick;
537                new_state.sqrt_price = swap_result.sqrt_price;
538
539                Ok(PoolSwap::new(amount_in, amount_out, Box::new(new_state), None))
540            }
541        }
542    }
543
544    fn clone_box(&self) -> Box<dyn ProtocolSim> {
545        Box::new(self.clone())
546    }
547
548    fn as_any(&self) -> &dyn Any {
549        self
550    }
551
552    fn as_any_mut(&mut self) -> &mut dyn Any {
553        self
554    }
555
556    fn eq(&self, other: &dyn ProtocolSim) -> bool {
557        if let Some(other_state) = other
558            .as_any()
559            .downcast_ref::<UniswapV3State>()
560        {
561            self.liquidity == other_state.liquidity &&
562                self.sqrt_price == other_state.sqrt_price &&
563                self.fee == other_state.fee &&
564                self.tick == other_state.tick &&
565                self.ticks == other_state.ticks
566        } else {
567            false
568        }
569    }
570}
571
572#[cfg(test)]
573mod tests {
574    use std::{
575        collections::{HashMap, HashSet},
576        fs,
577        path::Path,
578        str::FromStr,
579    };
580
581    use num_bigint::ToBigUint;
582    use num_traits::FromPrimitive;
583    use serde_json::Value;
584    use tycho_client::feed::synchronizer::ComponentWithState;
585    use tycho_common::{hex_bytes::Bytes, models::Chain, simulation::protocol_sim::Price};
586
587    use super::*;
588    use crate::{
589        evm::protocol::utils::uniswap::sqrt_price_math::get_sqrt_price_q96,
590        protocol::models::{DecoderContext, TryFromWithBlock},
591    };
592
593    #[test]
594    fn test_get_amount_out_full_range_liquidity() {
595        let token_x = Token::new(
596            &Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap(),
597            "X",
598            18,
599            0,
600            &[Some(10_000)],
601            Chain::Ethereum,
602            100,
603        );
604        let token_y = Token::new(
605            &Bytes::from_str("0xf1ca9cb74685755965c7458528a36934df52a3ef").unwrap(),
606            "Y",
607            18,
608            0,
609            &[Some(10_000)],
610            Chain::Ethereum,
611            100,
612        );
613
614        let pool = UniswapV3State::new(
615            8330443394424070888454257,
616            U256::from_str("188562464004052255423565206602").unwrap(),
617            FeeAmount::Medium,
618            17342,
619            vec![TickInfo::new(0, 0).unwrap(), TickInfo::new(46080, 0).unwrap()],
620        )
621        .unwrap();
622        let sell_amount = BigUint::from_str("11_000_000000000000000000").unwrap();
623        let expected = BigUint::from_str("61927070842678722935941").unwrap();
624
625        let res = pool
626            .get_amount_out(sell_amount, &token_x, &token_y)
627            .unwrap();
628
629        assert_eq!(res.amount, expected);
630    }
631
632    struct SwapTestCase {
633        symbol: &'static str,
634        sell: BigUint,
635        exp: BigUint,
636    }
637
638    #[test]
639    fn test_get_amount_out() {
640        let wbtc = Token::new(
641            &Bytes::from_str("0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599").unwrap(),
642            "WBTC",
643            8,
644            0,
645            &[Some(10_000)],
646            Chain::Ethereum,
647            100,
648        );
649        let weth = Token::new(
650            &Bytes::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap(),
651            "WETH",
652            18,
653            0,
654            &[Some(10_000)],
655            Chain::Ethereum,
656            100,
657        );
658        let pool = UniswapV3State::new(
659            377952820878029838,
660            U256::from_str("28437325270877025820973479874632004").unwrap(),
661            FeeAmount::Low,
662            255830,
663            vec![
664                TickInfo::new(255760, 1759015528199933i128).unwrap(),
665                TickInfo::new(255770, 6393138051835308i128).unwrap(),
666                TickInfo::new(255780, 228206673808681i128).unwrap(),
667                TickInfo::new(255820, 1319490609195820i128).unwrap(),
668                TickInfo::new(255830, 678916926147901i128).unwrap(),
669                TickInfo::new(255840, 12208947683433103i128).unwrap(),
670                TickInfo::new(255850, 1177970713095301i128).unwrap(),
671                TickInfo::new(255860, 8752304680520407i128).unwrap(),
672                TickInfo::new(255880, 1486478248067104i128).unwrap(),
673                TickInfo::new(255890, 1878744276123248i128).unwrap(),
674                TickInfo::new(255900, 77340284046725227i128).unwrap(),
675            ],
676        )
677        .unwrap();
678        let cases = vec![
679            SwapTestCase {
680                symbol: "WBTC",
681                sell: 500000000.to_biguint().unwrap(),
682                exp: BigUint::from_str("64352395915550406461").unwrap(),
683            },
684            SwapTestCase {
685                symbol: "WBTC",
686                sell: 550000000.to_biguint().unwrap(),
687                exp: BigUint::from_str("70784271504035662865").unwrap(),
688            },
689            SwapTestCase {
690                symbol: "WBTC",
691                sell: 600000000.to_biguint().unwrap(),
692                exp: BigUint::from_str("77215534856185613494").unwrap(),
693            },
694            SwapTestCase {
695                symbol: "WBTC",
696                sell: BigUint::from_str("1000000000").unwrap(),
697                exp: BigUint::from_str("128643569649663616249").unwrap(),
698            },
699            SwapTestCase {
700                symbol: "WBTC",
701                sell: BigUint::from_str("3000000000").unwrap(),
702                exp: BigUint::from_str("385196519076234662939").unwrap(),
703            },
704            SwapTestCase {
705                symbol: "WETH",
706                sell: BigUint::from_str("64000000000000000000").unwrap(),
707                exp: BigUint::from_str("496294784").unwrap(),
708            },
709            SwapTestCase {
710                symbol: "WETH",
711                sell: BigUint::from_str("70000000000000000000").unwrap(),
712                exp: BigUint::from_str("542798479").unwrap(),
713            },
714            SwapTestCase {
715                symbol: "WETH",
716                sell: BigUint::from_str("77000000000000000000").unwrap(),
717                exp: BigUint::from_str("597047757").unwrap(),
718            },
719            SwapTestCase {
720                symbol: "WETH",
721                sell: BigUint::from_str("128000000000000000000").unwrap(),
722                exp: BigUint::from_str("992129037").unwrap(),
723            },
724            SwapTestCase {
725                symbol: "WETH",
726                sell: BigUint::from_str("385000000000000000000").unwrap(),
727                exp: BigUint::from_str("2978713582").unwrap(),
728            },
729        ];
730
731        for case in cases {
732            let (token_a, token_b) =
733                if case.symbol == "WBTC" { (&wbtc, &weth) } else { (&weth, &wbtc) };
734            let res = pool
735                .get_amount_out(case.sell, token_a, token_b)
736                .unwrap();
737
738            assert_eq!(res.amount, case.exp);
739        }
740    }
741
742    #[test]
743    fn test_err_with_partial_trade() {
744        let dai = Token::new(
745            &Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap(),
746            "DAI",
747            18,
748            0,
749            &[Some(10_000)],
750            Chain::Ethereum,
751            100,
752        );
753        let usdc = Token::new(
754            &Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(),
755            "USDC",
756            6,
757            0,
758            &[Some(10_000)],
759            Chain::Ethereum,
760            100,
761        );
762        let pool = UniswapV3State::new(
763            73015811375239994,
764            U256::from_str("148273042406850898575413").unwrap(),
765            FeeAmount::High,
766            -263789,
767            vec![
768                TickInfo::new(-269600, 3612326326695492i128).unwrap(),
769                TickInfo::new(-268800, 1487613939516867i128).unwrap(),
770                TickInfo::new(-267800, 1557587121322546i128).unwrap(),
771                TickInfo::new(-267400, 424592076717375i128).unwrap(),
772                TickInfo::new(-267200, 11691597431643916i128).unwrap(),
773                TickInfo::new(-266800, -218742815100986i128).unwrap(),
774                TickInfo::new(-266600, 1118947532495477i128).unwrap(),
775                TickInfo::new(-266200, 1233064286622365i128).unwrap(),
776                TickInfo::new(-265000, 4252603063356107i128).unwrap(),
777                TickInfo::new(-263200, -351282010325232i128).unwrap(),
778                TickInfo::new(-262800, -2352011819117842i128).unwrap(),
779                TickInfo::new(-262600, -424592076717375i128).unwrap(),
780                TickInfo::new(-262200, -11923662433672566i128).unwrap(),
781                TickInfo::new(-261600, -2432911749667741i128).unwrap(),
782                TickInfo::new(-260200, -4032727022572273i128).unwrap(),
783                TickInfo::new(-260000, -22889492064625028i128).unwrap(),
784                TickInfo::new(-259400, -1557587121322546i128).unwrap(),
785                TickInfo::new(-259200, -1487613939516867i128).unwrap(),
786                TickInfo::new(-258400, -400137022888262i128).unwrap(),
787            ],
788        )
789        .unwrap();
790        let amount_in = BigUint::from_str("50000000000").unwrap();
791        let exp = BigUint::from_str("6820591625999718100883").unwrap();
792
793        let err = pool
794            .get_amount_out(amount_in, &usdc, &dai)
795            .unwrap_err();
796
797        match err {
798            SimulationError::InvalidInput(ref _err, ref amount_out_result) => {
799                match amount_out_result {
800                    Some(amount_out_result) => {
801                        assert_eq!(amount_out_result.amount, exp);
802                        let new_state = amount_out_result
803                            .new_state
804                            .as_any()
805                            .downcast_ref::<UniswapV3State>()
806                            .unwrap();
807                        assert_ne!(new_state.tick, pool.tick);
808                        assert_ne!(new_state.liquidity, pool.liquidity);
809                    }
810                    _ => panic!("Partial amount out result is None. Expected partial result."),
811                }
812            }
813            _ => panic!("Test failed: was expecting a SimulationError::InsufficientData"),
814        }
815    }
816
817    #[test]
818    fn test_delta_transition() {
819        let mut pool = UniswapV3State::new(
820            1000,
821            U256::from_str("1000").unwrap(),
822            FeeAmount::Low,
823            100,
824            vec![TickInfo::new(255760, 10000).unwrap(), TickInfo::new(255900, -10000).unwrap()],
825        )
826        .unwrap();
827        let attributes: HashMap<String, Bytes> = [
828            ("liquidity".to_string(), Bytes::from(2000_u64.to_be_bytes().to_vec())),
829            ("sqrt_price_x96".to_string(), Bytes::from(1001_u64.to_be_bytes().to_vec())),
830            ("tick".to_string(), Bytes::from(120_i32.to_be_bytes().to_vec())),
831            (
832                "ticks/-255760/net_liquidity".to_string(),
833                Bytes::from(10200_u64.to_be_bytes().to_vec()),
834            ),
835            (
836                "ticks/255900/net_liquidity".to_string(),
837                Bytes::from(9800_u64.to_be_bytes().to_vec()),
838            ),
839        ]
840        .into_iter()
841        .collect();
842        let delta = ProtocolStateDelta {
843            component_id: "State1".to_owned(),
844            updated_attributes: attributes,
845            deleted_attributes: HashSet::new(),
846        };
847
848        pool.delta_transition(delta, &HashMap::new(), &Balances::default())
849            .unwrap();
850
851        assert_eq!(pool.liquidity, 2000);
852        assert_eq!(pool.sqrt_price, U256::from(1001));
853        assert_eq!(pool.tick, 120);
854        assert_eq!(
855            pool.ticks
856                .get_tick(-255760)
857                .unwrap()
858                .net_liquidity,
859            10200
860        );
861        assert_eq!(
862            pool.ticks
863                .get_tick(255900)
864                .unwrap()
865                .net_liquidity,
866            9800
867        );
868    }
869
870    #[tokio::test]
871    async fn test_get_limits() {
872        use tycho_client::feed::dto;
873        let project_root = env!("CARGO_MANIFEST_DIR");
874        let asset_path =
875            Path::new(project_root).join("tests/assets/decoder/uniswap_v3_snapshot.json");
876        let json_data = fs::read_to_string(asset_path).expect("Failed to read test asset");
877        let data: Value = serde_json::from_str(&json_data).expect("Failed to parse JSON");
878        let state: ComponentWithState = serde_json::from_value::<dto::ComponentWithState>(data)
879            .expect("Expected json to match ComponentWithState structure")
880            .into();
881
882        let usv3_state = UniswapV3State::try_from_with_header(
883            state,
884            Default::default(),
885            &Default::default(),
886            &Default::default(),
887            &DecoderContext::new(),
888        )
889        .await
890        .unwrap();
891
892        let t0 = Token::new(
893            &Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap(),
894            "WBTC",
895            8,
896            0,
897            &[Some(10_000)],
898            Chain::Ethereum,
899            100,
900        );
901        let t1 = Token::new(
902            &Bytes::from_str("0xcbb7c0000ab88b473b1f5afd9ef808440eed33bf").unwrap(),
903            "cbBTC",
904            8,
905            0,
906            &[Some(10_000)],
907            Chain::Ethereum,
908            100,
909        );
910
911        let res = usv3_state
912            .get_limits(t0.address.clone(), t1.address.clone())
913            .unwrap();
914
915        assert_eq!(&res.0, &BigUint::from_u128(155144999154).unwrap());
916
917        let out = usv3_state
918            .get_amount_out(res.0, &t0, &t1)
919            .expect("swap for limit in didn't work");
920
921        assert_eq!(&res.1, &out.amount);
922    }
923
924    // Helper to create a basic test pool
925    fn create_basic_test_pool() -> UniswapV3State {
926        let liquidity = 100_000_000_000_000_000_000u128; // 100e18
927        let sqrt_price = get_sqrt_price_q96(U256::from(20_000_000u64), U256::from(10_000_000u64))
928            .expect("Failed to calculate sqrt price");
929        let tick = get_tick_at_sqrt_ratio(sqrt_price).expect("Failed to calculate tick");
930
931        let ticks = vec![TickInfo::new(0, 0).unwrap(), TickInfo::new(46080, 0).unwrap()];
932
933        UniswapV3State::new(liquidity, sqrt_price, FeeAmount::Low, tick, ticks)
934            .expect("Failed to create pool")
935    }
936
937    fn create_tick_boundary_test_pool() -> UniswapV3State {
938        let sqrt_price = get_sqrt_ratio_at_tick(0).expect("Failed to calculate sqrt price");
939        let ticks = vec![TickInfo::new(-120, 0).unwrap(), TickInfo::new(120, 0).unwrap()];
940
941        UniswapV3State::new(100_000_000_000_000_000_000u128, sqrt_price, FeeAmount::Low, 0, ticks)
942            .expect("Failed to create pool")
943    }
944
945    #[test]
946    fn test_partial_step_updates_tick_when_price_moves_without_crossing_initialized_tick() {
947        let pool = create_tick_boundary_test_pool();
948        let amount =
949            I256::checked_from_sign_and_abs(Sign::Positive, U256::from(100_000_000_000_000_000u64))
950                .unwrap();
951
952        let result = pool
953            .swap(true, amount, None)
954            .expect("swap should stay within the current liquidity range");
955        let expected_tick =
956            get_tick_at_sqrt_ratio(result.sqrt_price).expect("new sqrt price should map to a tick");
957
958        assert_ne!(result.sqrt_price, pool.sqrt_price);
959        assert_ne!(result.sqrt_price, get_sqrt_ratio_at_tick(-120).unwrap());
960        assert_ne!(expected_tick, pool.tick);
961        assert_eq!(result.tick, expected_tick);
962    }
963
964    #[test]
965    fn test_swap_keeps_boundary_tick_when_price_does_not_move() {
966        let mut pool = create_tick_boundary_test_pool();
967        pool.tick = -1;
968        let amount = I256::checked_from_sign_and_abs(Sign::Positive, U256::from(1u64)).unwrap();
969
970        let result = pool
971            .swap(true, amount, None)
972            .expect("swap should consume the input as fee without moving price");
973
974        assert_eq!(result.sqrt_price, pool.sqrt_price);
975        assert_eq!(get_tick_at_sqrt_ratio(result.sqrt_price).unwrap(), 0);
976        assert_eq!(result.tick, pool.tick);
977    }
978
979    #[test]
980    fn test_swap_basic() {
981        let pool = create_basic_test_pool();
982
983        // Test selling token X for token Y
984        let amount_in =
985            I256::checked_from_sign_and_abs(Sign::Positive, U256::from(1000000u64)).unwrap();
986        let result = pool
987            .swap(true, amount_in, None)
988            .unwrap();
989
990        // At current pool price, we should get a little less than 2 times the amount of X
991        let expected_amount = U256::from(2000000u64);
992        let actual_amount = result
993            .amount_calculated
994            .abs()
995            .into_raw();
996        assert_eq!(expected_amount - actual_amount, U256::from(1001u64));
997        println!("Swap X->Y: amount_in={}, amount_out={}", amount_in, actual_amount);
998    }
999
1000    #[test]
1001    fn test_swap_to_price_basic() {
1002        // Create pool with Medium fee (0.3%) to match V4's basic test
1003        let liquidity = 100_000_000_000_000_000_000u128;
1004        let sqrt_price = get_sqrt_price_q96(U256::from(20_000_000u64), U256::from(10_000_000u64))
1005            .expect("Failed to calculate sqrt price");
1006        let tick = get_tick_at_sqrt_ratio(sqrt_price).expect("Failed to calculate tick");
1007        let ticks = vec![TickInfo::new(0, 0).unwrap(), TickInfo::new(46080, 0).unwrap()];
1008
1009        let pool = UniswapV3State::new(liquidity, sqrt_price, FeeAmount::Medium, tick, ticks)
1010            .expect("Failed to create pool");
1011
1012        // Token X has lower address (0x01), Y has higher address (0x02)
1013        let token_x = Token::new(
1014            &Bytes::from_str("0x0000000000000000000000000000000000000001").unwrap(),
1015            "X",
1016            18,
1017            0,
1018            &[Some(10_000)],
1019            Chain::Ethereum,
1020            100,
1021        );
1022        let token_y = Token::new(
1023            &Bytes::from_str("0x0000000000000000000000000000000000000002").unwrap(),
1024            "Y",
1025            18,
1026            0,
1027            &[Some(10_000)],
1028            Chain::Ethereum,
1029            100,
1030        );
1031
1032        // Swap price: buying X for Y (token_out/token_in)
1033        let target_price =
1034            Price::new(2_000_000u64.to_biguint().unwrap(), 1_010_000u64.to_biguint().unwrap());
1035
1036        // Query how much Y the pool can supply when buying X at this price
1037        let trade = pool
1038            .query_pool_swap(&QueryPoolSwapParams::new(
1039                token_x,
1040                token_y,
1041                SwapConstraint::PoolTargetPrice {
1042                    target: target_price,
1043                    tolerance: 0f64,
1044                    min_amount_in: None,
1045                    max_amount_in: None,
1046                },
1047            ))
1048            .expect("swap_to_price failed");
1049
1050        // Should match V4's output exactly with same fees (0.3%)
1051        let expected_amount_in =
1052            BigUint::from_str("246739021727519745").expect("Failed to parse expected amount_in");
1053        let expected_amount_out =
1054            BigUint::from_str("490291909043340795").expect("Failed to parse expected amount_out");
1055
1056        assert_eq!(
1057            trade.amount_in().clone(),
1058            expected_amount_in,
1059            "amount_in should match expected value"
1060        );
1061        assert_eq!(
1062            trade.amount_out().clone(),
1063            expected_amount_out,
1064            "amount_out should match expected value"
1065        );
1066    }
1067
1068    #[test]
1069    fn test_swap_to_price_price_too_high() {
1070        let pool = create_basic_test_pool();
1071
1072        let token_x = Token::new(
1073            &Bytes::from_str("0x0000000000000000000000000000000000000001").unwrap(),
1074            "X",
1075            18,
1076            0,
1077            &[Some(10_000)],
1078            Chain::Ethereum,
1079            100,
1080        );
1081        let token_y = Token::new(
1082            &Bytes::from_str("0x0000000000000000000000000000000000000002").unwrap(),
1083            "Y",
1084            18,
1085            0,
1086            &[Some(10_000)],
1087            Chain::Ethereum,
1088            100,
1089        );
1090
1091        // Price far above pool price - should return zero
1092        let target_price =
1093            Price::new(10_000_000u64.to_biguint().unwrap(), 1_000_000u64.to_biguint().unwrap());
1094
1095        let result = pool.query_pool_swap(&QueryPoolSwapParams::new(
1096            token_x,
1097            token_y,
1098            SwapConstraint::PoolTargetPrice {
1099                target: target_price,
1100                tolerance: 0f64,
1101                min_amount_in: None,
1102                max_amount_in: None,
1103            },
1104        ));
1105        assert!(result.is_err(), "Should return error when target price is unreachable");
1106    }
1107
1108    #[test]
1109    fn test_swap_parameterized() {
1110        // Parameterized swap tests with real WBTC/WETH pool data
1111        let liquidity = 377_952_820_878_029_838u128;
1112        let sqrt_price = U256::from_str("28437325270877025820973479874632004")
1113            .expect("Failed to parse sqrt_price");
1114        let tick = 255830;
1115
1116        let ticks = vec![
1117            TickInfo::new(255760, 1_759_015_528_199_933).unwrap(),
1118            TickInfo::new(255770, 6_393_138_051_835_308).unwrap(),
1119            TickInfo::new(255780, 228_206_673_808_681).unwrap(),
1120            TickInfo::new(255820, 1_319_490_609_195_820).unwrap(),
1121            TickInfo::new(255830, 678_916_926_147_901).unwrap(),
1122            TickInfo::new(255840, 12_208_947_683_433_103).unwrap(),
1123            TickInfo::new(255850, 1_177_970_713_095_301).unwrap(),
1124            TickInfo::new(255860, 8_752_304_680_520_407).unwrap(),
1125            TickInfo::new(255880, 1_486_478_248_067_104).unwrap(),
1126            TickInfo::new(255890, 1_878_744_276_123_248).unwrap(),
1127            TickInfo::new(255900, 77_340_284_046_725_227).unwrap(),
1128        ];
1129
1130        let pool = UniswapV3State::new(liquidity, sqrt_price, FeeAmount::Low, tick, ticks)
1131            .expect("Failed to create pool");
1132
1133        // Test cases: (zero_for_one, amount_in, expected_amount_out, test_id)
1134        // WBTC address (0x2260...) < WETH address (0xC02a...), so WBTC is token0
1135        let test_cases = vec![
1136            // WBTC to WETH cases (zero_for_one = true)
1137            (true, "500000000", "64352395915550406461", "WBTC->WETH 500000000"),
1138            (true, "550000000", "70784271504035662865", "WBTC->WETH 550000000"),
1139            (true, "600000000", "77215534856185613494", "WBTC->WETH 600000000"),
1140            (true, "1000000000", "128643569649663616249", "WBTC->WETH 1000000000"),
1141            (true, "3000000000", "385196519076234662939", "WBTC->WETH 3000000000"),
1142            // WETH to WBTC cases (zero_for_one = false)
1143            (false, "64000000000000000000", "496294784", "WETH->WBTC 64 ETH"),
1144            (false, "70000000000000000000", "542798479", "WETH->WBTC 70 ETH"),
1145            (false, "77000000000000000000", "597047757", "WETH->WBTC 77 ETH"),
1146            (false, "128000000000000000000", "992129037", "WETH->WBTC 128 ETH"),
1147            (false, "385000000000000000000", "2978713582", "WETH->WBTC 385 ETH"),
1148        ];
1149
1150        for (zero_for_one, amount_in_str, expected_amount_out_str, test_id) in test_cases {
1151            let amount_in = U256::from_str(amount_in_str).expect("Failed to parse amount_in");
1152            let amount_specified = I256::checked_from_sign_and_abs(Sign::Positive, amount_in)
1153                .unwrap_or_else(|| panic!("{} - Failed to convert amount to I256", test_id));
1154
1155            let result = pool
1156                .swap(zero_for_one, amount_specified, None)
1157                .unwrap_or_else(|e| panic!("{} - swap failed: {:?}", test_id, e));
1158
1159            let amount_out = result
1160                .amount_calculated
1161                .abs()
1162                .into_raw();
1163            let expected = U256::from_str(expected_amount_out_str)
1164                .expect("Failed to parse expected_amount_out");
1165
1166            assert_eq!(amount_out, expected, "{}", test_id);
1167        }
1168    }
1169
1170    #[test]
1171    fn test_swap_to_price_parameterized() {
1172        // Tests query_supply with various price points
1173        let wbtc = Token::new(
1174            &Bytes::from_str("0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599").unwrap(),
1175            "WBTC",
1176            8,
1177            0,
1178            &[Some(10_000)],
1179            Chain::Ethereum,
1180            100,
1181        );
1182        let weth = Token::new(
1183            &Bytes::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap(),
1184            "WETH",
1185            18,
1186            0,
1187            &[Some(10_000)],
1188            Chain::Ethereum,
1189            100,
1190        );
1191
1192        let liquidity = 377_952_820_878_029_838u128;
1193        let sqrt_price = get_sqrt_price_q96(U256::from(130_000_000u64), U256::from(10_000_000u64))
1194            .expect("Failed to calculate sqrt price");
1195        let tick = get_tick_at_sqrt_ratio(sqrt_price).expect("Failed to calculate tick");
1196
1197        let ticks = vec![
1198            TickInfo::new(25560, 1759015528199933).unwrap(),
1199            TickInfo::new(25570, 6393138051835308).unwrap(),
1200            TickInfo::new(25580, 228206673808681).unwrap(),
1201            TickInfo::new(25620, 1319490609195820).unwrap(),
1202            TickInfo::new(25630, 678916926147901).unwrap(),
1203            TickInfo::new(25640, 12208947683433103).unwrap(),
1204            TickInfo::new(25660, 8752304680520407).unwrap(),
1205            TickInfo::new(25680, 1486478248067104).unwrap(),
1206            TickInfo::new(25690, 1878744276123248).unwrap(),
1207            TickInfo::new(25700, 77340284046725227).unwrap(),
1208        ];
1209
1210        let pool = UniswapV3State::new(liquidity, sqrt_price, FeeAmount::Low, tick, ticks)
1211            .expect("Failed to create pool");
1212
1213        // Test cases: (sell_token, sell_price, buy_price, expected_supply, test_id)
1214        let test_cases = vec![
1215            (&wbtc, 129u64, 10u64, "0", "WBTC sell_price=129, buy_price=10"),
1216            (&wbtc, 130u64, 10u64, "0", "WBTC sell_price=130, buy_price=10"),
1217            (&wbtc, 1305u64, 100u64, "163535995630461", "WBTC sell_price=1305, buy_price=100"),
1218            (&weth, 99u64, 1300u64, "0", "WETH sell_price=99, buy_price=1300"),
1219            (&weth, 100u64, 1300u64, "0", "WETH sell_price=100, buy_price=1300"),
1220            (&weth, 101u64, 1299u64, "524227092059180", "WETH sell_price=101, buy_price=1299"),
1221        ];
1222
1223        for (sell_token, sell_price, buy_price, expected_str, test_id) in test_cases {
1224            let buy_token = if sell_token == &wbtc { &weth } else { &wbtc };
1225
1226            let target_price =
1227                Price::new(buy_price.to_biguint().unwrap(), sell_price.to_biguint().unwrap());
1228
1229            if expected_str == "0" {
1230                let result = pool.query_pool_swap(&QueryPoolSwapParams::new(
1231                    buy_token.clone(),
1232                    sell_token.clone(),
1233                    SwapConstraint::PoolTargetPrice {
1234                        target: target_price,
1235                        tolerance: 0f64,
1236                        min_amount_in: None,
1237                        max_amount_in: None,
1238                    },
1239                ));
1240                assert!(result.is_err(), "Should return error when target price is unreachable");
1241            } else {
1242                let expected =
1243                    BigUint::from_str(expected_str).expect("Failed to parse expected value");
1244
1245                let trade = pool
1246                    .query_pool_swap(&QueryPoolSwapParams::new(
1247                        buy_token.clone(),
1248                        sell_token.clone(),
1249                        SwapConstraint::PoolTargetPrice {
1250                            target: target_price,
1251                            tolerance: 0f64,
1252                            min_amount_in: None,
1253                            max_amount_in: None,
1254                        },
1255                    ))
1256                    .unwrap_or_else(|e| panic!("{} - query_supply failed: {:?}", test_id, e));
1257                assert_eq!(trade.amount_out().clone(), expected, "{}", test_id);
1258            }
1259        }
1260    }
1261
1262    #[test]
1263    fn test_swap_to_price_around_spot_price() {
1264        // Tests query_supply edge cases around the spot price with fees
1265        let liquidity = 10_000_000_000_000_000u128;
1266        let sqrt_price =
1267            get_sqrt_price_q96(U256::from(2_000_000_000u64), U256::from(1_000_000_000u64))
1268                .expect("Failed to calculate sqrt price");
1269        let tick = get_tick_at_sqrt_ratio(sqrt_price).expect("Failed to calculate tick");
1270
1271        let ticks = vec![TickInfo::new(0, 0).unwrap(), TickInfo::new(46080, 0).unwrap()];
1272
1273        let pool = UniswapV3State::new(liquidity, sqrt_price, FeeAmount::Low, tick, ticks)
1274            .expect("Failed to create pool");
1275
1276        let token_x = Token::new(
1277            &Bytes::from_str("0x0000000000000000000000000000000000000001").unwrap(),
1278            "X",
1279            18,
1280            0,
1281            &[Some(10_000)],
1282            Chain::Ethereum,
1283            100,
1284        );
1285        let token_y = Token::new(
1286            &Bytes::from_str("0x0000000000000000000000000000000000000002").unwrap(),
1287            "Y",
1288            18,
1289            0,
1290            &[Some(10_000)],
1291            Chain::Ethereum,
1292            100,
1293        );
1294
1295        // Test 1: Price just above spot price, too little to cover fees
1296        let target_price =
1297            Price::new(1_999_750u64.to_biguint().unwrap(), 1_000_250u64.to_biguint().unwrap());
1298
1299        let result = pool.query_pool_swap(&QueryPoolSwapParams::new(
1300            token_x.clone(),
1301            token_y.clone(),
1302            SwapConstraint::PoolTargetPrice {
1303                target: target_price,
1304                tolerance: 0f64,
1305                min_amount_in: None,
1306                max_amount_in: None,
1307            },
1308        ));
1309        assert!(result.is_err(), "Should return error when target price is unreachable");
1310
1311        // Test 2: Price high enough to cover fees (0.1% higher)
1312        let target_price =
1313            Price::new(1_999_000u64.to_biguint().unwrap(), 1_001_000u64.to_biguint().unwrap());
1314
1315        let pool_swap = pool
1316            .query_pool_swap(&QueryPoolSwapParams::new(
1317                token_x,
1318                token_y,
1319                SwapConstraint::PoolTargetPrice {
1320                    target: target_price,
1321                    tolerance: 0f64,
1322                    min_amount_in: None,
1323                    max_amount_in: None,
1324                },
1325            ))
1326            .expect("swap_to_price failed");
1327
1328        let expected_amount_out =
1329            BigUint::from_str("7062236922008").expect("Failed to parse expected value");
1330        assert_eq!(
1331            pool_swap.amount_out().clone(),
1332            expected_amount_out,
1333            "Expected amount out when price covers fees"
1334        );
1335    }
1336
1337    #[test]
1338    fn test_swap_to_price_matches_get_amount_out() {
1339        let liquidity = 100_000_000_000_000_000_000u128;
1340        let sqrt_price = get_sqrt_price_q96(U256::from(20_000_000u64), U256::from(10_000_000u64))
1341            .expect("Failed to calculate sqrt price");
1342        let tick = get_tick_at_sqrt_ratio(sqrt_price).expect("Failed to calculate tick");
1343
1344        let ticks = vec![TickInfo::new(0, 0).unwrap(), TickInfo::new(46080, 0).unwrap()];
1345
1346        let pool = UniswapV3State::new(liquidity, sqrt_price, FeeAmount::Medium, tick, ticks)
1347            .expect("Failed to create pool");
1348
1349        let token_x_addr = Bytes::from_str("0x0000000000000000000000000000000000000001").unwrap();
1350        let token_y_addr = Bytes::from_str("0x0000000000000000000000000000000000000002").unwrap();
1351
1352        let token_x = Token::new(&token_x_addr, "X", 18, 0, &[], Chain::Ethereum, 1);
1353        let token_y = Token::new(&token_y_addr, "Y", 18, 0, &[], Chain::Ethereum, 1);
1354
1355        // Get the trade from swap_to_price
1356        let target_price = Price::new(BigUint::from(2_000_000u64), BigUint::from(1_010_000u64));
1357        let pool_swap = pool
1358            .query_pool_swap(&QueryPoolSwapParams::new(
1359                token_x.clone(),
1360                token_y.clone(),
1361                SwapConstraint::PoolTargetPrice {
1362                    target: target_price,
1363                    tolerance: 0f64,
1364                    min_amount_in: None,
1365                    max_amount_in: None,
1366                },
1367            ))
1368            .expect("swap_to_price failed");
1369        assert!(pool_swap.amount_in().clone() > BigUint::ZERO, "Amount in should be positive");
1370
1371        // Use the amount_in from swap_to_price with get_amount_out
1372        let result = pool
1373            .get_amount_out(pool_swap.amount_in().clone(), &token_x, &token_y)
1374            .expect("get_amount_out failed");
1375
1376        // The amount_out from get_amount_out should be close to swap_to_price's amount_out
1377        // Allow for small rounding differences
1378        assert!(result.amount > BigUint::ZERO);
1379        assert!(result.amount >= *pool_swap.amount_out());
1380    }
1381
1382    #[test]
1383    fn test_swap_price_limit_out_of_range_returns_error() {
1384        let pool = create_basic_test_pool();
1385        let amount = I256::checked_from_sign_and_abs(Sign::Positive, U256::from(1000u64)).unwrap();
1386
1387        // zero_for_one: price_limit equal to sqrt_price is invalid (must be strictly less)
1388        let result = pool.swap(true, amount, Some(pool.sqrt_price));
1389        assert!(matches!(result, Err(SimulationError::InvalidInput(_, None))));
1390
1391        // zero_for_one: price_limit at MIN_SQRT_RATIO is invalid (must be strictly greater)
1392        let result = pool.swap(true, amount, Some(MIN_SQRT_RATIO));
1393        assert!(matches!(result, Err(SimulationError::InvalidInput(_, None))));
1394
1395        // one_for_zero: price_limit equal to sqrt_price is invalid (must be strictly greater)
1396        let result = pool.swap(false, amount, Some(pool.sqrt_price));
1397        assert!(matches!(result, Err(SimulationError::InvalidInput(_, None))));
1398
1399        // one_for_zero: price_limit at MAX_SQRT_RATIO is invalid (must be strictly less)
1400        let result = pool.swap(false, amount, Some(MAX_SQRT_RATIO));
1401        assert!(matches!(result, Err(SimulationError::InvalidInput(_, None))));
1402    }
1403
1404    #[test]
1405    fn test_swap_at_extreme_price_returns_error() {
1406        // Simulates the depth calculation scenario: pool sqrt_price is at MIN_SQRT_RATIO + 1,
1407        // so the default price limit for zero_for_one equals sqrt_price and fails validation.
1408        let sqrt_price = MIN_SQRT_RATIO + U256::from(1u64);
1409        let tick = get_tick_at_sqrt_ratio(sqrt_price).expect("Failed to calculate tick");
1410        // FeeAmount::Low has tick spacing 10; ticks must be aligned
1411        let aligned_tick = (MIN_TICK / 10) * 10 + 10; // first multiple of 10 above MIN_TICK
1412        let ticks = vec![
1413            TickInfo::new(aligned_tick, 0).unwrap(),
1414            TickInfo::new(aligned_tick + 10, 0).unwrap(),
1415        ];
1416        let pool = UniswapV3State::new(
1417            100_000_000_000_000_000_000u128,
1418            sqrt_price,
1419            FeeAmount::Low,
1420            tick,
1421            ticks,
1422        )
1423        .unwrap();
1424
1425        let amount = I256::checked_from_sign_and_abs(Sign::Positive, U256::from(1000u64)).unwrap();
1426        // Default price limit for zero_for_one is MIN_SQRT_RATIO + 1 == sqrt_price, so invalid
1427        let result = pool.swap(true, amount, None);
1428        assert!(matches!(result, Err(SimulationError::InvalidInput(_, None))));
1429    }
1430}
1431
1432#[cfg(test)]
1433mod tests_forks {
1434    use std::str::FromStr;
1435
1436    use tycho_client::feed::synchronizer::ComponentWithState;
1437    use tycho_common::{hex_bytes::Bytes, models::Chain};
1438
1439    use super::*;
1440    use crate::protocol::models::{DecoderContext, TryFromWithBlock};
1441
1442    #[tokio::test]
1443    async fn test_pancakeswap_get_amount_out() {
1444        use std::{fs, path::Path};
1445
1446        use serde_json::Value;
1447        use tycho_client::feed::dto;
1448        let project_root = env!("CARGO_MANIFEST_DIR");
1449        let asset_path =
1450            Path::new(project_root).join("tests/assets/decoder/pancakeswap_v3_snapshot.json");
1451        let json_data = fs::read_to_string(asset_path).expect("Failed to read test asset");
1452        let data: Value = serde_json::from_str(&json_data).expect("Failed to parse JSON");
1453        let state: ComponentWithState = serde_json::from_value::<dto::ComponentWithState>(data)
1454            .expect("Expected json to match ComponentWithState structure")
1455            .into();
1456
1457        let pool_state = UniswapV3State::try_from_with_header(
1458            state,
1459            Default::default(),
1460            &Default::default(),
1461            &Default::default(),
1462            &DecoderContext::new(),
1463        )
1464        .await
1465        .unwrap();
1466
1467        let usdc = Token::new(
1468            &Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(),
1469            "USDC",
1470            6,
1471            0,
1472            &[Some(10_000)],
1473            Chain::Ethereum,
1474            100,
1475        );
1476        let usdt = Token::new(
1477            &Bytes::from_str("0xdac17f958d2ee523a2206206994597c13d831ec7").unwrap(),
1478            "USDT",
1479            6,
1480            0,
1481            &[Some(10_000)],
1482            Chain::Ethereum,
1483            100,
1484        );
1485
1486        let res = pool_state
1487            .get_amount_out(BigUint::from_str("5976361609").unwrap(), &usdt, &usdc)
1488            .unwrap();
1489
1490        assert_eq!(res.amount, BigUint::from_str("5975901673").unwrap());
1491    }
1492
1493    #[test]
1494    fn test_get_limits_graceful_underflow() {
1495        // Verifies graceful handling of liquidity underflow in get_limits for V3
1496        let pool = UniswapV3State::new(
1497            1000000,
1498            U256::from_str("79228162514264337593543950336").unwrap(),
1499            FeeAmount::Medium,
1500            0,
1501            vec![
1502                // A tick with net_liquidity > current_liquidity
1503                // When zero_for_one=true, this gets negated and would cause underflow
1504                TickInfo::new(-60, 2000000).unwrap(), // 2x current liquidity
1505            ],
1506        )
1507        .unwrap();
1508
1509        let usdc = Token::new(
1510            &Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(),
1511            "USDC",
1512            6,
1513            0,
1514            &[Some(10_000)],
1515            Chain::Ethereum,
1516            100,
1517        );
1518        let weth = Token::new(
1519            &Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap(),
1520            "WETH",
1521            18,
1522            0,
1523            &[Some(10_000)],
1524            Chain::Ethereum,
1525            100,
1526        );
1527
1528        let (limit_in, limit_out) = pool
1529            .get_limits(usdc.address.clone(), weth.address.clone())
1530            .unwrap();
1531
1532        // Should return some conservative limits
1533        assert!(limit_in > BigUint::zero());
1534        assert!(limit_out > BigUint::zero());
1535    }
1536}