nautilus_model/defi/dex.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::{borrow::Cow, fmt::Display};
17
18use crate::defi::{amm::Pool, chain::Chain};
19
20/// Represents different types of Automated Market Makers (AMMs) in DeFi protocols.
21#[derive(Debug, Clone)]
22pub enum AmmType {
23 /// Constant Product Automated Market Maker.
24 CPAMM,
25 /// Concentrated Liquidity Automated Market Maker.
26 CLAMM,
27 /// Enhanced CLAMM with Additional Features (Uniswap V4 with Hooks).
28 CLAMEnhanced,
29 /// Specialized AMM for Stable Assets (Curve Style).
30 StableSwap,
31 /// AMM with customizable token weights (e.g., Balancer style).
32 WeightedPool,
33 /// Advanced pool type that can nest other pools (Balancer V3).
34 ComposablePool,
35}
36
37/// Represents a decentralized exchange (DEX) in a blockchain ecosystem.
38#[derive(Debug, Clone)]
39pub struct Dex {
40 /// The blockchain network where this DEX operates.
41 pub chain: Chain,
42 /// The name of the DEX protocol.
43 pub name: Cow<'static, str>,
44 /// The blockchain address of the DEX factory contract.
45 pub factory: Cow<'static, str>,
46 /// The event signature or identifier used to detect pool creation events.
47 pub pool_created_event: Cow<'static, str>,
48 /// The type of automated market maker (AMM) algorithm used by this DEX.
49 pub amm_type: AmmType,
50 /// Collection of liquidity pools managed by this DEX.
51 #[allow(dead_code)] // TBD
52 pairs: Vec<Pool>,
53}
54
55impl Dex {
56 /// Creates a new [`Dex`] instance with the specified properties.
57 #[must_use]
58 pub fn new(
59 chain: Chain,
60 name: impl Into<Cow<'static, str>>,
61 factory: impl Into<Cow<'static, str>>,
62 amm_type: AmmType,
63 pool_created_event: impl Into<Cow<'static, str>>,
64 ) -> Self {
65 Self {
66 chain,
67 name: name.into(),
68 factory: factory.into(),
69 pool_created_event: pool_created_event.into(),
70 amm_type,
71 pairs: vec![],
72 }
73 }
74
75 /// Returns a unique identifier for this DEX, combining chain and name.
76 ///
77 /// Format: "{chain_id}:{name_snake_case}"
78 pub fn id(&self) -> String {
79 format!(
80 "{}:{}",
81 self.chain.name,
82 self.name.to_lowercase().replace(" ", "_")
83 )
84 }
85}
86
87impl Display for Dex {
88 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89 write!(f, "Dex(chain={}, name={})", self.chain, self.name)
90 }
91}