nautilus_data/defi/
engine.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! DeFi-specific data engine functionality.
17//!
18//! This module provides DeFi processing methods for the `DataEngine`.
19//! All code in this module requires the `defi` feature flag.
20
21use std::{any::Any, rc::Rc, sync::Arc};
22
23use nautilus_common::{
24    defi,
25    messages::defi::{
26        DefiRequestCommand, DefiSubscribeCommand, DefiUnsubscribeCommand, RequestPoolSnapshot,
27    },
28    msgbus::{self, handler::ShareableMessageHandler},
29};
30use nautilus_core::UUID4;
31use nautilus_model::{
32    defi::{
33        Blockchain, DefiData, PoolProfiler,
34        data::{DexPoolData, block::BlockPosition},
35    },
36    identifiers::{ClientId, InstrumentId},
37};
38
39use crate::engine::{DataEngine, pool::PoolUpdater};
40
41/// Extracts the block position tuple from a DexPoolData event.
42fn get_event_block_position(event: &DexPoolData) -> (u64, u32, u32) {
43    match event {
44        DexPoolData::Swap(s) => (s.block, s.transaction_index, s.log_index),
45        DexPoolData::LiquidityUpdate(u) => (u.block, u.transaction_index, u.log_index),
46        DexPoolData::FeeCollect(c) => (c.block, c.transaction_index, c.log_index),
47        DexPoolData::Flash(f) => (f.block, f.transaction_index, f.log_index),
48    }
49}
50
51/// Converts buffered DefiData events to DexPoolData and sorts by block position.
52fn convert_and_sort_buffered_events(buffered_events: Vec<DefiData>) -> Vec<DexPoolData> {
53    let mut events: Vec<DexPoolData> = buffered_events
54        .into_iter()
55        .filter_map(|event| match event {
56            DefiData::PoolSwap(swap) => Some(DexPoolData::Swap(swap)),
57            DefiData::PoolLiquidityUpdate(update) => Some(DexPoolData::LiquidityUpdate(update)),
58            DefiData::PoolFeeCollect(collect) => Some(DexPoolData::FeeCollect(collect)),
59            DefiData::PoolFlash(flash) => Some(DexPoolData::Flash(flash)),
60            _ => None,
61        })
62        .collect();
63
64    events.sort_by(|a, b| {
65        let pos_a = get_event_block_position(a);
66        let pos_b = get_event_block_position(b);
67        pos_a.cmp(&pos_b)
68    });
69
70    events
71}
72
73impl DataEngine {
74    /// Returns all blockchains for which blocks subscriptions exist.
75    #[must_use]
76    pub fn subscribed_blocks(&self) -> Vec<Blockchain> {
77        self.collect_subscriptions(|client| &client.subscriptions_blocks)
78    }
79
80    /// Returns all instrument IDs for which pool subscriptions exist.
81    #[must_use]
82    pub fn subscribed_pools(&self) -> Vec<InstrumentId> {
83        self.collect_subscriptions(|client| &client.subscriptions_pools)
84    }
85
86    /// Returns all instrument IDs for which swap subscriptions exist.
87    #[must_use]
88    pub fn subscribed_pool_swaps(&self) -> Vec<InstrumentId> {
89        self.collect_subscriptions(|client| &client.subscriptions_pool_swaps)
90    }
91
92    /// Returns all instrument IDs for which liquidity update subscriptions exist.
93    #[must_use]
94    pub fn subscribed_pool_liquidity_updates(&self) -> Vec<InstrumentId> {
95        self.collect_subscriptions(|client| &client.subscriptions_pool_liquidity_updates)
96    }
97
98    /// Returns all instrument IDs for which fee collect subscriptions exist.
99    #[must_use]
100    pub fn subscribed_pool_fee_collects(&self) -> Vec<InstrumentId> {
101        self.collect_subscriptions(|client| &client.subscriptions_pool_fee_collects)
102    }
103
104    /// Returns all instrument IDs for which flash loan subscriptions exist.
105    #[must_use]
106    pub fn subscribed_pool_flash(&self) -> Vec<InstrumentId> {
107        self.collect_subscriptions(|client| &client.subscriptions_pool_flash)
108    }
109
110    /// Handles a subscribe command, updating internal state and forwarding to the client.
111    ///
112    /// # Errors
113    ///
114    /// Returns an error if the subscription is invalid (e.g., synthetic instrument for book data),
115    /// or if the underlying client operation fails.
116    pub fn execute_defi_subscribe(&mut self, cmd: &DefiSubscribeCommand) -> anyhow::Result<()> {
117        if let Some(client_id) = cmd.client_id()
118            && self.external_clients.contains(client_id)
119        {
120            if self.config.debug {
121                log::debug!("Skipping defi subscribe for external client {client_id}: {cmd:?}",);
122            }
123            return Ok(());
124        }
125
126        if let Some(client) = self.get_client(cmd.client_id(), cmd.venue()) {
127            log::info!("Forwarding subscription to client {}", client.client_id);
128            client.execute_defi_subscribe(cmd);
129        } else {
130            log::error!(
131                "Cannot handle command: no client found for client_id={:?}, venue={:?}",
132                cmd.client_id(),
133                cmd.venue(),
134            );
135        }
136
137        match cmd {
138            DefiSubscribeCommand::Pool(cmd) => {
139                self.setup_pool_updater(&cmd.instrument_id, cmd.client_id.as_ref());
140            }
141            DefiSubscribeCommand::PoolSwaps(cmd) => {
142                self.setup_pool_updater(&cmd.instrument_id, cmd.client_id.as_ref());
143            }
144            DefiSubscribeCommand::PoolLiquidityUpdates(cmd) => {
145                self.setup_pool_updater(&cmd.instrument_id, cmd.client_id.as_ref());
146            }
147            DefiSubscribeCommand::PoolFeeCollects(cmd) => {
148                self.setup_pool_updater(&cmd.instrument_id, cmd.client_id.as_ref());
149            }
150            DefiSubscribeCommand::PoolFlashEvents(cmd) => {
151                self.setup_pool_updater(&cmd.instrument_id, cmd.client_id.as_ref());
152            }
153            DefiSubscribeCommand::Blocks(_) => {} // No pool setup needed for blocks
154        }
155
156        Ok(())
157    }
158
159    /// Handles an unsubscribe command, updating internal state and forwarding to the client.
160    ///
161    /// # Errors
162    ///
163    /// Returns an error if the underlying client operation fails.
164    pub fn execute_defi_unsubscribe(&mut self, cmd: &DefiUnsubscribeCommand) -> anyhow::Result<()> {
165        if let Some(client_id) = cmd.client_id()
166            && self.external_clients.contains(client_id)
167        {
168            if self.config.debug {
169                log::debug!("Skipping defi unsubscribe for external client {client_id}: {cmd:?}",);
170            }
171            return Ok(());
172        }
173
174        if let Some(client) = self.get_client(cmd.client_id(), cmd.venue()) {
175            client.execute_defi_unsubscribe(cmd);
176        } else {
177            log::error!(
178                "Cannot handle command: no client found for client_id={:?}, venue={:?}",
179                cmd.client_id(),
180                cmd.venue(),
181            );
182        }
183
184        Ok(())
185    }
186
187    /// Sends a [`DefiRequestCommand`] to a suitable data client implementation.
188    ///
189    /// # Errors
190    ///
191    /// Returns an error if no client is found for the given client ID or venue,
192    /// or if the client fails to process the request.
193    pub fn execute_defi_request(&mut self, req: &DefiRequestCommand) -> anyhow::Result<()> {
194        // Skip requests for external clients
195        if let Some(cid) = req.client_id()
196            && self.external_clients.contains(cid)
197        {
198            if self.config.debug {
199                log::debug!("Skipping defi data request for external client {cid}: {req:?}");
200            }
201            return Ok(());
202        }
203
204        if let Some(client) = self.get_client(req.client_id(), req.venue()) {
205            client.execute_defi_request(req)
206        } else {
207            anyhow::bail!(
208                "Cannot handle request: no client found for {:?} {:?}",
209                req.client_id(),
210                req.venue()
211            );
212        }
213    }
214
215    /// Processes DeFi-specific data events.
216    pub fn process_defi_data(&mut self, data: DefiData) {
217        match data {
218            DefiData::Block(block) => {
219                let topic = defi::switchboard::get_defi_blocks_topic(block.chain());
220                msgbus::publish(topic, &block as &dyn Any);
221            }
222            DefiData::Pool(pool) => {
223                if let Err(e) = self.cache.borrow_mut().add_pool(pool.clone()) {
224                    log::error!("Failed to add Pool to cache: {e}");
225                }
226
227                // Check if pool profiler creation was deferred
228                if self.pool_updaters_pending.remove(&pool.instrument_id) {
229                    log::info!(
230                        "Pool {} now loaded, creating deferred pool profiler",
231                        pool.instrument_id
232                    );
233                    self.setup_pool_updater(&pool.instrument_id, None);
234                }
235
236                let topic = defi::switchboard::get_defi_pool_topic(pool.instrument_id);
237                msgbus::publish(topic, &pool as &dyn Any);
238            }
239            DefiData::PoolSnapshot(snapshot) => {
240                let instrument_id = snapshot.instrument_id;
241                log::info!(
242                    "Received pool snapshot for {instrument_id} at block {} with {} positions and {} ticks",
243                    snapshot.block_position.number,
244                    snapshot.positions.len(),
245                    snapshot.ticks.len()
246                );
247
248                // Validate we're expecting this snapshot
249                if !self.pool_snapshot_pending.contains(&instrument_id) {
250                    log::warn!(
251                        "Received unexpected pool snapshot for {instrument_id} (not in pending set)"
252                    );
253                    return;
254                }
255
256                // Get pool from cache
257                let pool = match self.cache.borrow().pool(&instrument_id) {
258                    Some(pool) => Arc::new(pool.clone()),
259                    None => {
260                        log::error!(
261                            "Pool {instrument_id} not found in cache when processing snapshot"
262                        );
263                        return;
264                    }
265                };
266
267                // Create profiler and restore from snapshot
268                let mut profiler = PoolProfiler::new(pool);
269                if let Err(e) = profiler.restore_from_snapshot(snapshot.clone()) {
270                    log::error!(
271                        "Failed to restore profiler from snapshot for {instrument_id}: {e}"
272                    );
273                    return;
274                }
275                log::debug!("Restored pool profiler for {instrument_id} from snapshot");
276
277                // Process buffered events
278                let buffered_events = self
279                    .pool_event_buffers
280                    .remove(&instrument_id)
281                    .unwrap_or_default();
282
283                if !buffered_events.is_empty() {
284                    log::info!(
285                        "Processing {} buffered events for {instrument_id}",
286                        buffered_events.len()
287                    );
288
289                    let events_to_apply = convert_and_sort_buffered_events(buffered_events);
290                    let applied_count = Self::apply_buffered_events_to_profiler(
291                        &mut profiler,
292                        events_to_apply,
293                        &snapshot.block_position,
294                        instrument_id,
295                    );
296
297                    log::info!(
298                        "Applied {applied_count} buffered events to profiler for {instrument_id}"
299                    );
300                }
301
302                // Add profiler to cache
303                if let Err(e) = self.cache.borrow_mut().add_pool_profiler(profiler) {
304                    log::error!("Failed to add pool profiler to cache for {instrument_id}: {e}");
305                    return;
306                }
307
308                // Create updater and subscribe to topics
309                self.pool_snapshot_pending.remove(&instrument_id);
310                let updater = Rc::new(PoolUpdater::new(&instrument_id, self.cache.clone()));
311                let handler = ShareableMessageHandler(updater.clone());
312
313                self.subscribe_pool_updater_topics(instrument_id, handler);
314                self.pool_updaters.insert(instrument_id, updater);
315
316                log::info!(
317                    "Pool profiler setup completed for {instrument_id}, now processing live events"
318                );
319            }
320            DefiData::PoolSwap(swap) => {
321                let instrument_id = swap.instrument_id;
322                // Buffer if waiting for snapshot, otherwise publish
323                if self.pool_snapshot_pending.contains(&instrument_id) {
324                    log::debug!("Buffering swap event for {instrument_id} (waiting for snapshot)");
325                    self.pool_event_buffers
326                        .entry(instrument_id)
327                        .or_default()
328                        .push(DefiData::PoolSwap(swap));
329                } else {
330                    let topic = defi::switchboard::get_defi_pool_swaps_topic(instrument_id);
331                    msgbus::publish(topic, &swap as &dyn Any);
332                }
333            }
334            DefiData::PoolLiquidityUpdate(update) => {
335                let instrument_id = update.instrument_id;
336                // Buffer if waiting for snapshot, otherwise publish
337                if self.pool_snapshot_pending.contains(&instrument_id) {
338                    log::debug!(
339                        "Buffering liquidity update event for {instrument_id} (waiting for snapshot)"
340                    );
341                    self.pool_event_buffers
342                        .entry(instrument_id)
343                        .or_default()
344                        .push(DefiData::PoolLiquidityUpdate(update));
345                } else {
346                    let topic = defi::switchboard::get_defi_liquidity_topic(instrument_id);
347                    msgbus::publish(topic, &update as &dyn Any);
348                }
349            }
350            DefiData::PoolFeeCollect(collect) => {
351                let instrument_id = collect.instrument_id;
352                // Buffer if waiting for snapshot, otherwise publish
353                if self.pool_snapshot_pending.contains(&instrument_id) {
354                    log::debug!(
355                        "Buffering fee collect event for {instrument_id} (waiting for snapshot)"
356                    );
357                    self.pool_event_buffers
358                        .entry(instrument_id)
359                        .or_default()
360                        .push(DefiData::PoolFeeCollect(collect));
361                } else {
362                    let topic = defi::switchboard::get_defi_collect_topic(instrument_id);
363                    msgbus::publish(topic, &collect as &dyn Any);
364                }
365            }
366            DefiData::PoolFlash(flash) => {
367                let instrument_id = flash.instrument_id;
368                // Buffer if waiting for snapshot, otherwise publish
369                if self.pool_snapshot_pending.contains(&instrument_id) {
370                    log::debug!("Buffering flash event for {instrument_id} (waiting for snapshot)");
371                    self.pool_event_buffers
372                        .entry(instrument_id)
373                        .or_default()
374                        .push(DefiData::PoolFlash(flash));
375                } else {
376                    let topic = defi::switchboard::get_defi_flash_topic(instrument_id);
377                    msgbus::publish(topic, &flash as &dyn Any);
378                }
379            }
380        }
381    }
382
383    /// Subscribes a pool updater handler to all relevant pool data topics.
384    fn subscribe_pool_updater_topics(
385        &self,
386        instrument_id: InstrumentId,
387        handler: ShareableMessageHandler,
388    ) {
389        let topics = [
390            defi::switchboard::get_defi_pool_swaps_topic(instrument_id),
391            defi::switchboard::get_defi_liquidity_topic(instrument_id),
392            defi::switchboard::get_defi_collect_topic(instrument_id),
393            defi::switchboard::get_defi_flash_topic(instrument_id),
394        ];
395
396        for topic in topics {
397            if !msgbus::is_subscribed(topic.as_str(), handler.clone()) {
398                msgbus::subscribe(topic.into(), handler.clone(), Some(self.msgbus_priority));
399            }
400        }
401    }
402
403    /// Applies buffered events to a pool profiler, filtering to events after the snapshot.
404    ///
405    /// Returns the count of successfully applied events.
406    fn apply_buffered_events_to_profiler(
407        profiler: &mut PoolProfiler,
408        events: Vec<DexPoolData>,
409        snapshot_block: &BlockPosition,
410        instrument_id: InstrumentId,
411    ) -> usize {
412        let mut applied_count = 0;
413
414        for event in events {
415            let event_block = get_event_block_position(&event);
416
417            // Only apply events that occurred after the snapshot
418            let is_after_snapshot = event_block.0 > snapshot_block.number
419                || (event_block.0 == snapshot_block.number
420                    && event_block.1 > snapshot_block.transaction_index)
421                || (event_block.0 == snapshot_block.number
422                    && event_block.1 == snapshot_block.transaction_index
423                    && event_block.2 > snapshot_block.log_index);
424
425            if is_after_snapshot {
426                if let Err(e) = profiler.process(&event) {
427                    log::error!(
428                        "Failed to apply buffered event to profiler for {instrument_id}: {e}"
429                    );
430                } else {
431                    applied_count += 1;
432                }
433            }
434        }
435
436        applied_count
437    }
438
439    fn setup_pool_updater(&mut self, instrument_id: &InstrumentId, client_id: Option<&ClientId>) {
440        // Early return if updater already exists or we are in the middle of setting it up.
441        if self.pool_updaters.contains_key(instrument_id)
442            || self.pool_updaters_pending.contains(instrument_id)
443        {
444            log::debug!("Pool updater for {instrument_id} already exists");
445            return;
446        }
447
448        log::info!("Setting up pool updater for {instrument_id}");
449
450        // Check cache state and ensure profiler exists
451        {
452            let mut cache = self.cache.borrow_mut();
453
454            if cache.pool_profiler(instrument_id).is_some() {
455                // Profiler already exists, proceed to create updater
456                log::debug!("Pool profiler already exists for {instrument_id}");
457            } else if let Some(pool) = cache.pool(instrument_id) {
458                // Pool exists but no profiler, create profiler from pool
459                let pool = Arc::new(pool.clone());
460                let mut pool_profiler = PoolProfiler::new(pool.clone());
461
462                if let Some(initial_sqrt_price_x96) = pool.initial_sqrt_price_x96 {
463                    pool_profiler.initialize(initial_sqrt_price_x96);
464                    log::debug!(
465                        "Initialized pool profiler for {instrument_id} with sqrt_price {initial_sqrt_price_x96}"
466                    );
467                } else {
468                    log::debug!("Created pool profiler for {instrument_id}");
469                }
470
471                if let Err(e) = cache.add_pool_profiler(pool_profiler) {
472                    log::error!("Failed to add pool profiler for {instrument_id}: {e}");
473                    drop(cache);
474                    return;
475                }
476                drop(cache);
477            } else {
478                // Neither profiler nor pool exists, request snapshot
479                drop(cache);
480
481                let request_id = UUID4::new();
482                let ts_init = self.clock.borrow().timestamp_ns();
483                let request = RequestPoolSnapshot::new(
484                    *instrument_id,
485                    client_id.copied(),
486                    request_id,
487                    ts_init,
488                    None,
489                );
490
491                if let Err(e) =
492                    self.execute_defi_request(&DefiRequestCommand::PoolSnapshot(request))
493                {
494                    log::warn!("Failed to request pool snapshot for {instrument_id}: {e}");
495                } else {
496                    log::debug!("Requested pool snapshot for {instrument_id}");
497                    self.pool_snapshot_pending.insert(*instrument_id);
498                    self.pool_updaters_pending.insert(*instrument_id);
499                    self.pool_event_buffers.entry(*instrument_id).or_default();
500                }
501                return;
502            }
503        }
504
505        // Profiler exists, create updater and subscribe to topics
506        let updater = Rc::new(PoolUpdater::new(instrument_id, self.cache.clone()));
507        let handler = ShareableMessageHandler(updater.clone());
508
509        self.subscribe_pool_updater_topics(*instrument_id, handler);
510        self.pool_updaters.insert(*instrument_id, updater);
511
512        log::debug!("Created PoolUpdater for instrument ID {instrument_id}");
513    }
514}
515
516////////////////////////////////////////////////////////////////////////////////
517// Tests
518////////////////////////////////////////////////////////////////////////////////
519
520#[cfg(test)]
521mod tests {
522    use std::sync::Arc;
523
524    use alloy_primitives::{Address, I256, U160, U256};
525    use nautilus_model::{
526        defi::{
527            DefiData, PoolFeeCollect, PoolFlash, PoolLiquidityUpdate, PoolSwap,
528            chain::chains,
529            data::DexPoolData,
530            dex::{AmmType, Dex, DexType},
531        },
532        identifiers::{InstrumentId, Symbol, Venue},
533    };
534    use rstest::*;
535
536    use super::*;
537
538    // Test fixtures
539    #[fixture]
540    fn test_instrument_id() -> InstrumentId {
541        InstrumentId::new(Symbol::from("ETH/USDC"), Venue::from("UNISWAPV3"))
542    }
543
544    #[fixture]
545    fn test_chain() -> Arc<nautilus_model::defi::Chain> {
546        Arc::new(chains::ETHEREUM.clone())
547    }
548
549    #[fixture]
550    fn test_dex(test_chain: Arc<nautilus_model::defi::Chain>) -> Arc<Dex> {
551        Arc::new(Dex::new(
552            (*test_chain).clone(),
553            DexType::UniswapV3,
554            "0x1F98431c8aD98523631AE4a59f267346ea31F984",
555            12369621,
556            AmmType::CLAMM,
557            "PoolCreated(address,address,uint24,int24,address)",
558            "Swap(address,address,int256,int256,uint160,uint128,int24)",
559            "Mint(address,address,int24,int24,uint128,uint256,uint256)",
560            "Burn(address,int24,int24,uint128,uint256,uint256)",
561            "Collect(address,address,int24,int24,uint128,uint128)",
562        ))
563    }
564
565    fn create_test_swap(
566        test_instrument_id: InstrumentId,
567        test_chain: Arc<nautilus_model::defi::Chain>,
568        test_dex: Arc<Dex>,
569        block: u64,
570        tx_index: u32,
571        log_index: u32,
572    ) -> PoolSwap {
573        PoolSwap::new(
574            test_chain,
575            test_dex,
576            test_instrument_id,
577            Address::ZERO,
578            block,
579            format!("0x{:064x}", block),
580            tx_index,
581            log_index,
582            None,
583            Address::ZERO,
584            Address::ZERO,
585            I256::ZERO,
586            I256::ZERO,
587            U160::ZERO,
588            0,
589            0,
590            None,
591            None,
592            None,
593        )
594    }
595
596    fn create_test_liquidity_update(
597        test_instrument_id: InstrumentId,
598        test_chain: Arc<nautilus_model::defi::Chain>,
599        test_dex: Arc<Dex>,
600        block: u64,
601        tx_index: u32,
602        log_index: u32,
603    ) -> PoolLiquidityUpdate {
604        use nautilus_model::defi::PoolLiquidityUpdateType;
605
606        PoolLiquidityUpdate::new(
607            test_chain,
608            test_dex,
609            test_instrument_id,
610            Address::ZERO,
611            PoolLiquidityUpdateType::Mint,
612            block,
613            format!("0x{:064x}", block),
614            tx_index,
615            log_index,
616            None,
617            Address::ZERO,
618            0,
619            U256::ZERO,
620            U256::ZERO,
621            0,
622            0,
623            None,
624        )
625    }
626
627    fn create_test_fee_collect(
628        test_instrument_id: InstrumentId,
629        test_chain: Arc<nautilus_model::defi::Chain>,
630        test_dex: Arc<Dex>,
631        block: u64,
632        tx_index: u32,
633        log_index: u32,
634    ) -> PoolFeeCollect {
635        PoolFeeCollect::new(
636            test_chain,
637            test_dex,
638            test_instrument_id,
639            Address::ZERO,
640            block,
641            format!("0x{:064x}", block),
642            tx_index,
643            log_index,
644            Address::ZERO,
645            0,
646            0,
647            0,
648            0,
649            None,
650        )
651    }
652
653    fn create_test_flash(
654        test_instrument_id: InstrumentId,
655        test_chain: Arc<nautilus_model::defi::Chain>,
656        test_dex: Arc<Dex>,
657        block: u64,
658        tx_index: u32,
659        log_index: u32,
660    ) -> PoolFlash {
661        PoolFlash::new(
662            test_chain,
663            test_dex,
664            test_instrument_id,
665            Address::ZERO,
666            block,
667            format!("0x{:064x}", block),
668            tx_index,
669            log_index,
670            None,
671            Address::ZERO,
672            Address::ZERO,
673            U256::ZERO,
674            U256::ZERO,
675            U256::ZERO,
676            U256::ZERO,
677        )
678    }
679
680    #[rstest]
681    fn test_get_event_block_position_swap(
682        test_instrument_id: InstrumentId,
683        test_chain: Arc<nautilus_model::defi::Chain>,
684        test_dex: Arc<Dex>,
685    ) {
686        let swap = create_test_swap(test_instrument_id, test_chain, test_dex, 100, 5, 3);
687        let pos = get_event_block_position(&DexPoolData::Swap(swap));
688        assert_eq!(pos, (100, 5, 3));
689    }
690
691    #[rstest]
692    fn test_get_event_block_position_liquidity_update(
693        test_instrument_id: InstrumentId,
694        test_chain: Arc<nautilus_model::defi::Chain>,
695        test_dex: Arc<Dex>,
696    ) {
697        let update =
698            create_test_liquidity_update(test_instrument_id, test_chain, test_dex, 200, 10, 7);
699        let pos = get_event_block_position(&DexPoolData::LiquidityUpdate(update));
700        assert_eq!(pos, (200, 10, 7));
701    }
702
703    #[rstest]
704    fn test_get_event_block_position_fee_collect(
705        test_instrument_id: InstrumentId,
706        test_chain: Arc<nautilus_model::defi::Chain>,
707        test_dex: Arc<Dex>,
708    ) {
709        let collect = create_test_fee_collect(test_instrument_id, test_chain, test_dex, 300, 15, 2);
710        let pos = get_event_block_position(&DexPoolData::FeeCollect(collect));
711        assert_eq!(pos, (300, 15, 2));
712    }
713
714    #[rstest]
715    fn test_get_event_block_position_flash(
716        test_instrument_id: InstrumentId,
717        test_chain: Arc<nautilus_model::defi::Chain>,
718        test_dex: Arc<Dex>,
719    ) {
720        let flash = create_test_flash(test_instrument_id, test_chain, test_dex, 400, 20, 8);
721        let pos = get_event_block_position(&DexPoolData::Flash(flash));
722        assert_eq!(pos, (400, 20, 8));
723    }
724
725    #[rstest]
726    fn test_convert_and_sort_empty_events() {
727        let events = convert_and_sort_buffered_events(vec![]);
728        assert!(events.is_empty());
729    }
730
731    #[rstest]
732    fn test_convert_and_sort_filters_non_pool_events(
733        test_instrument_id: InstrumentId,
734        test_chain: Arc<nautilus_model::defi::Chain>,
735        test_dex: Arc<Dex>,
736    ) {
737        let events = vec![
738            DefiData::PoolSwap(create_test_swap(
739                test_instrument_id,
740                test_chain,
741                test_dex,
742                100,
743                0,
744                0,
745            )),
746            // Block events would be filtered out
747        ];
748        let sorted = convert_and_sort_buffered_events(events);
749        assert_eq!(sorted.len(), 1);
750    }
751
752    #[rstest]
753    fn test_convert_and_sort_single_event(
754        test_instrument_id: InstrumentId,
755        test_chain: Arc<nautilus_model::defi::Chain>,
756        test_dex: Arc<Dex>,
757    ) {
758        let swap = create_test_swap(test_instrument_id, test_chain, test_dex, 100, 5, 3);
759        let events = vec![DefiData::PoolSwap(swap)];
760        let sorted = convert_and_sort_buffered_events(events);
761        assert_eq!(sorted.len(), 1);
762        assert_eq!(get_event_block_position(&sorted[0]), (100, 5, 3));
763    }
764
765    #[rstest]
766    fn test_convert_and_sort_already_sorted(
767        test_instrument_id: InstrumentId,
768        test_chain: Arc<nautilus_model::defi::Chain>,
769        test_dex: Arc<Dex>,
770    ) {
771        let events = vec![
772            DefiData::PoolSwap(create_test_swap(
773                test_instrument_id,
774                test_chain.clone(),
775                test_dex.clone(),
776                100,
777                0,
778                0,
779            )),
780            DefiData::PoolSwap(create_test_swap(
781                test_instrument_id,
782                test_chain.clone(),
783                test_dex.clone(),
784                100,
785                0,
786                1,
787            )),
788            DefiData::PoolSwap(create_test_swap(
789                test_instrument_id,
790                test_chain,
791                test_dex,
792                100,
793                1,
794                0,
795            )),
796        ];
797        let sorted = convert_and_sort_buffered_events(events);
798        assert_eq!(sorted.len(), 3);
799        assert_eq!(get_event_block_position(&sorted[0]), (100, 0, 0));
800        assert_eq!(get_event_block_position(&sorted[1]), (100, 0, 1));
801        assert_eq!(get_event_block_position(&sorted[2]), (100, 1, 0));
802    }
803
804    #[rstest]
805    fn test_convert_and_sort_reverse_order(
806        test_instrument_id: InstrumentId,
807        test_chain: Arc<nautilus_model::defi::Chain>,
808        test_dex: Arc<Dex>,
809    ) {
810        let events = vec![
811            DefiData::PoolSwap(create_test_swap(
812                test_instrument_id,
813                test_chain.clone(),
814                test_dex.clone(),
815                100,
816                2,
817                5,
818            )),
819            DefiData::PoolSwap(create_test_swap(
820                test_instrument_id,
821                test_chain.clone(),
822                test_dex.clone(),
823                100,
824                1,
825                3,
826            )),
827            DefiData::PoolSwap(create_test_swap(
828                test_instrument_id,
829                test_chain,
830                test_dex,
831                100,
832                0,
833                1,
834            )),
835        ];
836        let sorted = convert_and_sort_buffered_events(events);
837        assert_eq!(sorted.len(), 3);
838        assert_eq!(get_event_block_position(&sorted[0]), (100, 0, 1));
839        assert_eq!(get_event_block_position(&sorted[1]), (100, 1, 3));
840        assert_eq!(get_event_block_position(&sorted[2]), (100, 2, 5));
841    }
842
843    #[rstest]
844    fn test_convert_and_sort_mixed_blocks(
845        test_instrument_id: InstrumentId,
846        test_chain: Arc<nautilus_model::defi::Chain>,
847        test_dex: Arc<Dex>,
848    ) {
849        let events = vec![
850            DefiData::PoolSwap(create_test_swap(
851                test_instrument_id,
852                test_chain.clone(),
853                test_dex.clone(),
854                102,
855                0,
856                0,
857            )),
858            DefiData::PoolSwap(create_test_swap(
859                test_instrument_id,
860                test_chain.clone(),
861                test_dex.clone(),
862                100,
863                5,
864                2,
865            )),
866            DefiData::PoolSwap(create_test_swap(
867                test_instrument_id,
868                test_chain,
869                test_dex,
870                101,
871                3,
872                1,
873            )),
874        ];
875        let sorted = convert_and_sort_buffered_events(events);
876        assert_eq!(sorted.len(), 3);
877        assert_eq!(get_event_block_position(&sorted[0]), (100, 5, 2));
878        assert_eq!(get_event_block_position(&sorted[1]), (101, 3, 1));
879        assert_eq!(get_event_block_position(&sorted[2]), (102, 0, 0));
880    }
881
882    #[rstest]
883    fn test_convert_and_sort_mixed_event_types(
884        test_instrument_id: InstrumentId,
885        test_chain: Arc<nautilus_model::defi::Chain>,
886        test_dex: Arc<Dex>,
887    ) {
888        let events = vec![
889            DefiData::PoolSwap(create_test_swap(
890                test_instrument_id,
891                test_chain.clone(),
892                test_dex.clone(),
893                100,
894                2,
895                0,
896            )),
897            DefiData::PoolLiquidityUpdate(create_test_liquidity_update(
898                test_instrument_id,
899                test_chain.clone(),
900                test_dex.clone(),
901                100,
902                0,
903                0,
904            )),
905            DefiData::PoolFeeCollect(create_test_fee_collect(
906                test_instrument_id,
907                test_chain.clone(),
908                test_dex.clone(),
909                100,
910                1,
911                0,
912            )),
913            DefiData::PoolFlash(create_test_flash(
914                test_instrument_id,
915                test_chain,
916                test_dex,
917                100,
918                3,
919                0,
920            )),
921        ];
922        let sorted = convert_and_sort_buffered_events(events);
923        assert_eq!(sorted.len(), 4);
924        assert_eq!(get_event_block_position(&sorted[0]), (100, 0, 0));
925        assert_eq!(get_event_block_position(&sorted[1]), (100, 1, 0));
926        assert_eq!(get_event_block_position(&sorted[2]), (100, 2, 0));
927        assert_eq!(get_event_block_position(&sorted[3]), (100, 3, 0));
928    }
929
930    #[rstest]
931    fn test_convert_and_sort_same_block_and_tx_different_log_index(
932        test_instrument_id: InstrumentId,
933        test_chain: Arc<nautilus_model::defi::Chain>,
934        test_dex: Arc<Dex>,
935    ) {
936        let events = vec![
937            DefiData::PoolSwap(create_test_swap(
938                test_instrument_id,
939                test_chain.clone(),
940                test_dex.clone(),
941                100,
942                5,
943                10,
944            )),
945            DefiData::PoolSwap(create_test_swap(
946                test_instrument_id,
947                test_chain.clone(),
948                test_dex.clone(),
949                100,
950                5,
951                5,
952            )),
953            DefiData::PoolSwap(create_test_swap(
954                test_instrument_id,
955                test_chain,
956                test_dex,
957                100,
958                5,
959                1,
960            )),
961        ];
962        let sorted = convert_and_sort_buffered_events(events);
963        assert_eq!(sorted.len(), 3);
964        assert_eq!(get_event_block_position(&sorted[0]), (100, 5, 1));
965        assert_eq!(get_event_block_position(&sorted[1]), (100, 5, 5));
966        assert_eq!(get_event_block_position(&sorted[2]), (100, 5, 10));
967    }
968}