semioscan 0.10.1

Production-grade Rust library for blockchain analytics: gas calculation, price extraction, and block window calculations for EVM chains
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
// SPDX-FileCopyrightText: 2025 Semiotic AI, Inc.
//
// SPDX-License-Identifier: Apache-2.0

//! Odos DEX price source implementation
//!
//! This module provides a reference implementation of [`PriceSource`]
//! for the Odos DEX aggregator. It demonstrates how to extract swap data from Odos router events.
//!
//! # Features
//!
//! This module is only available when the `odos-example` feature is enabled:
//!
//! ```toml
//! [dependencies]
//! semioscan = { version = "0.5", features = ["odos-example"] }
//! ```
//!
//! # Supported Events
//!
//! ## V2 Router
//! - **Swap** - Single-token swap event
//! - **SwapMulti** - Multi-token swap event
//!
//! ## V3 Router
//! - **Swap** - Single-token swap event (with referral/slippage data)
//! - **SwapMulti** - Multi-token swap event (with referral/slippage data)
//!
//! # Example Usage
//!
//! ```rust,ignore
//! use semioscan::price::odos::OdosPriceSource;
//! use alloy_chains::NamedChain;
//! use alloy_primitives::Address;
//! use odos_sdk::RouterType;
//!
//! // Chain-aware constructor (recommended)
//! let price_source = OdosPriceSource::for_chain(NamedChain::Arbitrum, RouterType::V2)?
//!     .with_sender_filter("0x123...".parse().unwrap());
//!
//! // Manual router address (fallback)
//! let router_address = "0xa669e7A0d4b3e4Fa48af2dE86BD4CD7126Be4e13".parse().unwrap();
//! let price_source = OdosPriceSource::new(router_address);
//!
//! // Use with PriceCalculator
//! let calculator = PriceCalculator::with_price_source(
//!     provider,
//!     Box::new(price_source),
//! );
//! ```

use super::{PriceSource, PriceSourceError, SwapData};
use alloy_chains::NamedChain;
use alloy_primitives::{Address, B256};
use alloy_rpc_types::Log;
use alloy_sol_types::SolEvent;
use odos_sdk::OdosV2Router::{Swap as SwapV2, SwapMulti as SwapMultiV2};
use odos_sdk::OdosV3Router::{Swap as SwapV3, SwapMulti as SwapMultiV3};
pub use odos_sdk::RouterType;
use odos_sdk::{get_v2_router_by_chain_id, get_v3_router_by_chain_id};

/// Errors from Odos price source operations
#[derive(Debug, thiserror::Error)]
pub enum OdosError {
    /// The specified chain does not have a router of the requested type
    #[error("Odos {router_type} router not available on chain {chain_name} (id: {chain_id})")]
    UnsupportedChain {
        /// The chain that was requested
        chain_name: &'static str,
        /// The chain ID
        chain_id: u64,
        /// The router type that was requested
        router_type: &'static str,
    },
    /// Router type does not emit swap events and cannot be used for price extraction
    #[error("{router_type} router not supported for price extraction (does not emit Swap/SwapMulti events)")]
    NonSwapRouterNotSupported {
        /// The router type that was rejected
        router_type: &'static str,
    },
}

impl OdosError {
    /// Create an error for an unsupported chain
    fn unsupported_chain(chain: NamedChain, router_type: RouterType) -> Self {
        Self::UnsupportedChain {
            chain_name: chain.as_str(),
            chain_id: chain as u64,
            router_type: router_type.as_str(),
        }
    }
}

/// Odos DEX price source implementation
///
/// Extracts swap data from Odos V2 router events (`Swap` and `SwapMulti`).
///
/// # Filtering
///
/// Optionally filter swaps by sender address using [`with_sender_filter`](OdosPriceSource::with_sender_filter).
/// This is useful when analyzing swaps from a specific address (e.g., your own liquidation bot).
///
/// # Event Handling
///
/// - **Single swaps** (`Swap` event): Direct token-to-token swaps
/// - **Multi swaps** (`SwapMulti` event): Complex multi-hop swaps with multiple input/output tokens
///
/// For `SwapMulti` events with multiple tokens, this implementation currently extracts
/// simple 1-to-1 token pairs. More complex multi-token handling can be added in the future.
#[derive(Debug)]
pub struct OdosPriceSource {
    router_address: Address,
    router_type: RouterType,
    sender_address: Option<Address>,
}

impl OdosPriceSource {
    /// Create a new Odos price source for the given router address
    ///
    /// Defaults to V2 router type for backward compatibility.
    /// Use [`for_chain`](Self::for_chain) for chain-aware construction with explicit router type.
    ///
    /// # Arguments
    ///
    /// * `router_address` - The Odos router contract address to scan for events
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let router = "0xa669e7A0d4b3e4Fa48af2dE86BD4CD7126Be4e13".parse().unwrap();
    /// let price_source = OdosPriceSource::new(router);
    /// ```
    pub fn new(router_address: Address) -> Self {
        Self {
            router_address,
            router_type: RouterType::V2,
            sender_address: None,
        }
    }

    /// Create an Odos price source for a specific chain and router type
    ///
    /// Automatically resolves the router address using odos-sdk's chain registry.
    /// This is the recommended constructor for most use cases.
    ///
    /// # Arguments
    ///
    /// * `chain` - The EVM chain (e.g., `NamedChain::Arbitrum`, `NamedChain::Mainnet`)
    /// * `router_type` - The router version (`RouterType::V2` or `RouterType::V3`)
    ///
    /// # Errors
    ///
    /// - Returns [`OdosError::NonSwapRouterNotSupported`] for router types that don't
    ///   emit `Swap/SwapMulti` events (e.g., `RouterType::LimitOrder`)
    /// - Returns [`OdosError::UnsupportedChain`] if Odos doesn't have the requested
    ///   router type deployed on the specified chain.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use semioscan::price::odos::{OdosPriceSource, RouterType};
    /// use alloy_chains::NamedChain;
    ///
    /// // V2 router on Arbitrum
    /// let source = OdosPriceSource::for_chain(NamedChain::Arbitrum, RouterType::V2)?;
    ///
    /// // V3 router on Mainnet
    /// let source = OdosPriceSource::for_chain(NamedChain::Mainnet, RouterType::V3)?;
    /// ```
    pub fn for_chain(chain: NamedChain, router_type: RouterType) -> Result<Self, OdosError> {
        // Only swap routers (V2/V3) emit Swap/SwapMulti events
        if !router_type.emits_swap_events() {
            return Err(OdosError::NonSwapRouterNotSupported {
                router_type: router_type.as_str(),
            });
        }

        let chain_id: u64 = chain.into();

        let router_address = match router_type {
            RouterType::V2 => get_v2_router_by_chain_id(chain_id),
            RouterType::V3 => get_v3_router_by_chain_id(chain_id),
            RouterType::LimitOrder => unreachable!("handled above"),
        }
        .ok_or_else(|| OdosError::unsupported_chain(chain, router_type))?;

        Ok(Self {
            router_address,
            router_type,
            sender_address: None,
        })
    }

    /// Create Odos price sources for all supported router types on a chain
    ///
    /// Returns price sources for V2 and V3 routers deployed on the specified chain.
    /// LimitOrder routers are excluded as they emit different events (`LimitOrderFilled`).
    ///
    /// # Arguments
    ///
    /// * `chain` - The EVM chain (e.g., `NamedChain::Arbitrum`, `NamedChain::Mainnet`)
    ///
    /// # Returns
    ///
    /// A vector of `OdosPriceSource` instances for V2 and V3 routers.
    /// The vector may be empty if the chain has no Odos V2/V3 routers deployed.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use semioscan::price::odos::OdosPriceSource;
    /// use alloy_chains::NamedChain;
    ///
    /// // Get all routers on Arbitrum (typically V2 and V3)
    /// let sources = OdosPriceSource::all_routers_for_chain(NamedChain::Arbitrum);
    /// println!("Found {} routers on Arbitrum", sources.len());
    ///
    /// // Use with multiple PriceCalculators or combine results
    /// for source in sources {
    ///     println!("Router: {:?}", source.router_address());
    /// }
    /// ```
    pub fn all_routers_for_chain(chain: NamedChain) -> Vec<Self> {
        RouterType::swap_routers()
            .into_iter()
            .filter_map(|router_type| Self::for_chain(chain, router_type).ok())
            .collect()
    }

    /// Add a filter to only include swaps from a specific sender address
    ///
    /// When set, only swaps where the sender matches this address will be included.
    ///
    /// # Arguments
    ///
    /// * `sender` - The address to filter by
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let price_source = OdosPriceSource::new(router)
    ///     .with_sender_filter("0x123...".parse().unwrap());
    /// ```
    pub fn with_sender_filter(mut self, sender: Address) -> Self {
        self.sender_address = Some(sender);
        self
    }
}

impl PriceSource for OdosPriceSource {
    fn router_address(&self) -> Address {
        self.router_address
    }

    fn sender_address(&self) -> Option<Address> {
        self.sender_address
    }

    fn event_topics(&self) -> Vec<B256> {
        match self.router_type {
            RouterType::V2 => {
                vec![SwapMultiV2::SIGNATURE_HASH, SwapV2::SIGNATURE_HASH]
            }
            RouterType::V3 => {
                vec![SwapMultiV3::SIGNATURE_HASH, SwapV3::SIGNATURE_HASH]
            }
            // LimitOrder is rejected in for_chain() and new() defaults to V2
            RouterType::LimitOrder => unreachable!("LimitOrder not supported for price extraction"),
        }
    }

    fn extract_swap_from_log(&self, log: &Log) -> Result<Option<SwapData>, PriceSourceError> {
        if log.topics().is_empty() {
            return Ok(None);
        }

        let topic = log.topics()[0];

        match self.router_type {
            RouterType::V2 => {
                if topic == SwapMultiV2::SIGNATURE_HASH {
                    return self.extract_swap_multi_v2(log);
                }
                if topic == SwapV2::SIGNATURE_HASH {
                    return self.extract_swap_single_v2(log);
                }
            }
            RouterType::V3 => {
                if topic == SwapMultiV3::SIGNATURE_HASH {
                    return self.extract_swap_multi_v3(log);
                }
                if topic == SwapV3::SIGNATURE_HASH {
                    return self.extract_swap_single_v3(log);
                }
            }
            RouterType::LimitOrder => unreachable!("LimitOrder not supported for price extraction"),
        }

        Ok(None)
    }

    /// Determine if a swap should be included based on the sender filter
    ///
    /// # Arguments
    ///
    /// * `swap` - The swap data to check
    ///
    /// # Returns
    ///
    /// `true` if the swap should be included, `false` otherwise
    ///
    /// If the sender filter is set, only include swaps from that address. Otherwise accept all swaps.
    fn should_include_swap(&self, swap: &SwapData) -> bool {
        match self.sender_address {
            Some(sender) => swap.sender == Some(sender),
            None => true,
        }
    }
}

impl OdosPriceSource {
    // ===== V2 Event Extraction =====

    /// Extract swap data from a V2 SwapMulti event
    fn extract_swap_multi_v2(&self, log: &Log) -> Result<Option<SwapData>, PriceSourceError> {
        let event = SwapMultiV2::decode_log(&log.clone().into())?;

        // Validate event data
        if event.tokensIn.is_empty() || event.tokensOut.is_empty() {
            return Err(PriceSourceError::empty_token_arrays());
        }

        if event.amountsIn.len() != event.tokensIn.len()
            || event.amountsOut.len() != event.tokensOut.len()
        {
            return Err(PriceSourceError::array_length_mismatch(
                event.tokensIn.len(),
                event.amountsIn.len(),
                event.tokensOut.len(),
                event.amountsOut.len(),
            ));
        }

        // Simple case: 1 input token + 1 output token
        if event.tokensIn.len() == 1 && event.tokensOut.len() == 1 {
            return Ok(Some(SwapData {
                token_in: event.tokensIn[0],
                token_in_amount: event.amountsIn[0],
                token_out: event.tokensOut[0],
                token_out_amount: event.amountsOut[0],
                sender: Some(event.sender),
                tx_hash: log.transaction_hash,
                block_number: log.block_number,
            }));
        }

        // Skip complex multi-token swaps for now
        Ok(None)
    }

    /// Extract swap data from a V2 Swap event
    fn extract_swap_single_v2(&self, log: &Log) -> Result<Option<SwapData>, PriceSourceError> {
        let event = SwapV2::decode_log(&log.clone().into())?;

        Ok(Some(SwapData {
            token_in: event.inputToken,
            token_in_amount: event.inputAmount,
            token_out: event.outputToken,
            token_out_amount: event.amountOut,
            sender: Some(event.sender),
            tx_hash: log.transaction_hash,
            block_number: log.block_number,
        }))
    }

    // ===== V3 Event Extraction =====

    /// Extract swap data from a V3 SwapMulti event
    fn extract_swap_multi_v3(&self, log: &Log) -> Result<Option<SwapData>, PriceSourceError> {
        let event = SwapMultiV3::decode_log(&log.clone().into())?;

        // Validate event data
        if event.tokensIn.is_empty() || event.tokensOut.is_empty() {
            return Err(PriceSourceError::empty_token_arrays());
        }

        if event.amountsIn.len() != event.tokensIn.len()
            || event.amountsOut.len() != event.tokensOut.len()
        {
            return Err(PriceSourceError::array_length_mismatch(
                event.tokensIn.len(),
                event.amountsIn.len(),
                event.tokensOut.len(),
                event.amountsOut.len(),
            ));
        }

        // Simple case: 1 input token + 1 output token
        if event.tokensIn.len() == 1 && event.tokensOut.len() == 1 {
            return Ok(Some(SwapData {
                token_in: event.tokensIn[0],
                token_in_amount: event.amountsIn[0],
                token_out: event.tokensOut[0],
                token_out_amount: event.amountsOut[0],
                sender: Some(event.sender),
                tx_hash: log.transaction_hash,
                block_number: log.block_number,
            }));
        }

        // Skip complex multi-token swaps for now
        Ok(None)
    }

    /// Extract swap data from a V3 Swap event
    fn extract_swap_single_v3(&self, log: &Log) -> Result<Option<SwapData>, PriceSourceError> {
        let event = SwapV3::decode_log(&log.clone().into())?;

        Ok(Some(SwapData {
            token_in: event.inputToken,
            token_in_amount: event.inputAmount,
            token_out: event.outputToken,
            token_out_amount: event.amountOut,
            sender: Some(event.sender),
            tx_hash: log.transaction_hash,
            block_number: log.block_number,
        }))
    }
}

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

    #[test]
    fn test_odos_price_source_creation() {
        let router: Address = "0xa669e7A0d4b3e4Fa48af2dE86BD4CD7126Be4e13"
            .parse()
            .unwrap();
        let price_source = OdosPriceSource::new(router);
        assert_eq!(price_source.router_address(), router);
    }

    #[test]
    fn test_for_chain_v2_arbitrum() {
        let source = OdosPriceSource::for_chain(NamedChain::Arbitrum, RouterType::V2)
            .expect("Arbitrum V2 should be supported");

        // Verify address matches known Arbitrum V2 router
        let expected: Address = "0xa669e7A0d4b3e4Fa48af2dE86BD4CD7126Be4e13"
            .parse()
            .unwrap();
        assert_eq!(source.router_address(), expected);
    }

    #[test]
    fn test_for_chain_v2_mainnet() {
        let source = OdosPriceSource::for_chain(NamedChain::Mainnet, RouterType::V2)
            .expect("Mainnet V2 should be supported");

        // Just verify it returns a valid address
        assert_ne!(source.router_address(), Address::ZERO);
    }

    #[test]
    fn test_for_chain_v3_mainnet() {
        let source = OdosPriceSource::for_chain(NamedChain::Mainnet, RouterType::V3)
            .expect("Mainnet V3 should be supported");

        assert_ne!(source.router_address(), Address::ZERO);
    }

    #[test]
    fn test_for_chain_unsupported() {
        // Use a chain that definitely doesn't have Odos deployed
        let result = OdosPriceSource::for_chain(NamedChain::Dev, RouterType::V2);

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, OdosError::UnsupportedChain { .. }));
    }

    #[test]
    fn test_for_chain_non_swap_router_rejected() {
        // LimitOrder emits different events (LimitOrderFilled), not Swap/SwapMulti
        let result = OdosPriceSource::for_chain(NamedChain::Mainnet, RouterType::LimitOrder);

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(
            err,
            OdosError::NonSwapRouterNotSupported { router_type: "LO" }
        ));
    }

    #[test]
    fn test_for_chain_with_sender_filter() {
        let sender: Address = "0x1234567890123456789012345678901234567890"
            .parse()
            .unwrap();

        let source = OdosPriceSource::for_chain(NamedChain::Arbitrum, RouterType::V2)
            .expect("Arbitrum V2 should be supported")
            .with_sender_filter(sender);

        // Verify filter is applied
        let swap = SwapData {
            token_in: Address::ZERO,
            token_in_amount: Default::default(),
            token_out: Address::ZERO,
            token_out_amount: Default::default(),
            sender: Some(sender),
            tx_hash: None,
            block_number: None,
        };
        assert!(source.should_include_swap(&swap));

        let other: Address = "0x9999999999999999999999999999999999999999"
            .parse()
            .unwrap();
        let swap_other = SwapData {
            sender: Some(other),
            ..swap
        };
        assert!(!source.should_include_swap(&swap_other));
    }

    #[test]
    fn test_sender_filter() {
        let router: Address = "0xa669e7A0d4b3e4Fa48af2dE86BD4CD7126Be4e13"
            .parse()
            .unwrap();
        let sender: Address = "0x1234567890123456789012345678901234567890"
            .parse()
            .unwrap();

        let price_source = OdosPriceSource::new(router).with_sender_filter(sender);

        // Test that swaps from sender are included
        let swap = SwapData {
            token_in: Address::ZERO,
            token_in_amount: Default::default(),
            token_out: Address::ZERO,
            token_out_amount: Default::default(),
            sender: Some(sender),
            tx_hash: None,
            block_number: None,
        };
        assert!(price_source.should_include_swap(&swap));

        // Test that swaps from other addresses are excluded
        let other_sender: Address = "0x9999999999999999999999999999999999999999"
            .parse()
            .unwrap();
        let swap_other = SwapData {
            sender: Some(other_sender),
            ..swap
        };
        assert!(!price_source.should_include_swap(&swap_other));
    }

    #[test]
    fn test_event_topics_v2() {
        let router: Address = "0xa669e7A0d4b3e4Fa48af2dE86BD4CD7126Be4e13"
            .parse()
            .unwrap();
        let price_source = OdosPriceSource::new(router);

        let topics = price_source.event_topics();
        assert_eq!(topics.len(), 2);
        assert_eq!(topics[0], SwapMultiV2::SIGNATURE_HASH);
        assert_eq!(topics[1], SwapV2::SIGNATURE_HASH);
    }

    #[test]
    fn test_event_topics_v3() {
        let source = OdosPriceSource::for_chain(NamedChain::Mainnet, RouterType::V3)
            .expect("Mainnet V3 should be supported");

        let topics = source.event_topics();
        assert_eq!(topics.len(), 2);
        assert_eq!(topics[0], SwapMultiV3::SIGNATURE_HASH);
        assert_eq!(topics[1], SwapV3::SIGNATURE_HASH);

        // V3 topics should differ from V2
        assert_ne!(SwapV2::SIGNATURE_HASH, SwapV3::SIGNATURE_HASH);
        assert_ne!(SwapMultiV2::SIGNATURE_HASH, SwapMultiV3::SIGNATURE_HASH);
    }

    #[test]
    fn test_v3_swapmulti_signature_matches_expected() {
        // Expected signature from user: 0x2c96555a96d94780f3a97aeb724514e80e331842f3143742d85da5aa68df9d30
        let expected: B256 = "0x2c96555a96d94780f3a97aeb724514e80e331842f3143742d85da5aa68df9d30"
            .parse()
            .unwrap();
        assert_eq!(
            SwapMultiV3::SIGNATURE_HASH,
            expected,
            "SwapMultiV3 signature hash should match expected value"
        );
    }

    #[test]
    fn test_for_chain_v3_base() {
        let source = OdosPriceSource::for_chain(NamedChain::Base, RouterType::V3)
            .expect("Base V3 should be supported");
        assert_ne!(source.router_address(), Address::ZERO);
    }

    #[test]
    fn test_all_routers_for_chain_mainnet() {
        let sources = OdosPriceSource::all_routers_for_chain(NamedChain::Mainnet);

        // Mainnet should have multiple routers (at least V2 and V3)
        assert!(sources.len() >= 2, "Expected at least 2 routers on Mainnet");

        // All addresses should be unique
        let mut addresses: Vec<_> = sources.iter().map(|s| s.router_address()).collect();
        addresses.sort();
        addresses.dedup();
        assert_eq!(
            addresses.len(),
            sources.len(),
            "Router addresses should be unique"
        );
    }

    #[test]
    fn test_all_routers_for_chain_unsupported() {
        let sources = OdosPriceSource::all_routers_for_chain(NamedChain::Dev);

        // Unsupported chain should return empty vec
        assert!(sources.is_empty());
    }

    #[test]
    fn test_extract_swap_multi_v3_simple_swap() {
        use alloy_primitives::{LogData, I256, U256};
        use alloy_sol_types::SolEvent;

        // Create a V3 price source for Base
        let source = OdosPriceSource::for_chain(NamedChain::Base, RouterType::V3)
            .expect("Base V3 should be supported");

        // Create test data for a simple 1-to-1 SwapMulti event
        let sender: Address = "0x1234567890123456789012345678901234567890"
            .parse()
            .unwrap();
        let token_in: Address = "0x4200000000000000000000000000000000000006"
            .parse()
            .unwrap(); // WETH on Base
        let token_out: Address = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
            .parse()
            .unwrap(); // USDC on Base
        let amount_in = U256::from(1_000_000_000_000_000_000u128); // 1 ETH
        let amount_out = U256::from(2_000_000_000u128); // 2000 USDC

        // Encode the SwapMulti event
        let event = SwapMultiV3 {
            sender,
            amountsIn: vec![amount_in],
            tokensIn: vec![token_in],
            amountsOut: vec![amount_out],
            tokensOut: vec![token_out],
            slippage: vec![I256::ZERO],
            referralCode: 0,
            referralFee: 0,
            referralFeeRecipient: Address::ZERO,
        };

        let log_data = LogData::new(
            vec![SwapMultiV3::SIGNATURE_HASH],
            event.encode_data().into(),
        )
        .expect("Log data should be valid");

        let log = Log {
            inner: alloy_primitives::Log {
                address: source.router_address(),
                data: log_data,
            },
            block_hash: None,
            block_number: Some(12345678),
            block_timestamp: None,
            transaction_hash: Some(
                "0x229c93653ee98127a71fda4c0be337acbbd459c4a4063a427bcfec67706ee11d"
                    .parse()
                    .unwrap(),
            ),
            transaction_index: None,
            log_index: None,
            removed: false,
        };

        // Extract swap data
        let result = source.extract_swap_from_log(&log);
        assert!(result.is_ok(), "Should successfully extract swap data");

        let swap_data = result.unwrap();
        assert!(swap_data.is_some(), "Should return Some(SwapData)");

        let swap = swap_data.unwrap();
        assert_eq!(swap.token_in, token_in);
        assert_eq!(swap.token_out, token_out);
        assert_eq!(swap.token_in_amount, amount_in);
        assert_eq!(swap.token_out_amount, amount_out);
        assert_eq!(swap.sender, Some(sender));
        assert_eq!(swap.block_number, Some(12345678));
    }
}