nautilus_blockchain/exchanges/
extended.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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
16use std::ops::Deref;
17
18use hypersync_client::simple_types::Log;
19use nautilus_model::defi::dex::Dex;
20
21use crate::events::pool_created::PoolCreated;
22
23/// Extended DEX wrapper that adds provider-specific event parsing capabilities to the domain `Dex` model.
24#[derive(Debug, Clone)]
25pub struct DexExtended {
26    /// The core domain Dex object being extended
27    pub dex: Dex,
28    /// Function to parse pool creation events for this specific DEX
29    pub parse_pool_created_event_fn: Option<fn(Log) -> anyhow::Result<PoolCreated>>,
30}
31
32impl DexExtended {
33    /// Creates a new [`DexExtended`] wrapper around a domain `Dex` object.
34    #[must_use]
35    pub fn new(dex: Dex) -> Self {
36        Self {
37            dex,
38            parse_pool_created_event_fn: None,
39        }
40    }
41
42    /// Sets the function used to parse pool creation events for this Dex.
43    pub fn set_pool_created_event_parsing(
44        &mut self,
45        parse_pool_created_event: fn(Log) -> anyhow::Result<PoolCreated>,
46    ) {
47        self.parse_pool_created_event_fn = Some(parse_pool_created_event);
48    }
49
50    /// Parses a pool creation event log using this DEX's specific parsing function.
51    pub fn parse_pool_created_event(&self, log: Log) -> anyhow::Result<PoolCreated> {
52        if let Some(parse_pool_created_event_fn) = &self.parse_pool_created_event_fn {
53            parse_pool_created_event_fn(log)
54        } else {
55            Err(anyhow::anyhow!(
56                "Parsing of pool created event in not defined in this dex: {}",
57                self.dex.name
58            ))
59        }
60    }
61}
62
63impl Deref for DexExtended {
64    type Target = Dex;
65
66    fn deref(&self) -> &Self::Target {
67        &self.dex
68    }
69}