Skip to main content

nautilus_model/defi/data/
liquidity.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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
16use std::fmt::Display;
17
18use alloy_primitives::{Address, U256};
19use nautilus_core::UnixNanos;
20use serde::{Deserialize, Serialize};
21use strum::{Display, EnumIter, EnumString};
22
23use crate::{
24    defi::{PoolIdentifier, SharedChain, SharedDex},
25    identifiers::InstrumentId,
26};
27
28#[derive(
29    Debug,
30    Clone,
31    Copy,
32    Hash,
33    PartialOrd,
34    PartialEq,
35    Ord,
36    Eq,
37    Display,
38    EnumIter,
39    EnumString,
40    Serialize,
41    Deserialize,
42)]
43#[cfg_attr(
44    feature = "python",
45    pyo3::pyclass(
46        frozen,
47        eq,
48        eq_int,
49        module = "nautilus_trader.core.nautilus_pyo3.model",
50        from_py_object,
51        rename_all = "SCREAMING_SNAKE_CASE",
52    )
53)]
54#[cfg_attr(
55    feature = "python",
56    pyo3_stub_gen::derive::gen_stub_pyclass_enum(module = "nautilus_trader.model")
57)]
58/// Represents the type of liquidity update operation in a DEX pool.
59#[non_exhaustive]
60pub enum PoolLiquidityUpdateType {
61    /// Liquidity is being added to the pool
62    Mint,
63    /// Liquidity is being removed from the pool
64    Burn,
65}
66
67/// Represents a liquidity update event in a decentralized exchange (DEX) pool.
68#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
69#[cfg_attr(
70    feature = "python",
71    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
72)]
73#[cfg_attr(
74    feature = "python",
75    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
76)]
77pub struct PoolLiquidityUpdate {
78    /// The blockchain network where the liquidity update occurred.
79    pub chain: SharedChain,
80    /// The decentralized exchange where the liquidity update was executed.
81    pub dex: SharedDex,
82    /// The instrument ID for this pool's trading pair.
83    pub instrument_id: InstrumentId,
84    /// The unique identifier for this pool (could be an address or other protocol-specific hex string).
85    pub pool_identifier: PoolIdentifier,
86    /// The type of the pool liquidity update.
87    pub kind: PoolLiquidityUpdateType,
88    /// The blockchain block number where the liquidity update occurred.
89    pub block: u64,
90    /// The unique hash identifier of the blockchain transaction containing the liquidity update.
91    pub transaction_hash: String,
92    /// The index position of the transaction within the block.
93    pub transaction_index: u32,
94    /// The index position of the liquidity update event log within the transaction.
95    pub log_index: u32,
96    /// The blockchain address that initiated the liquidity update transaction.
97    pub sender: Option<Address>,
98    /// The blockchain address that owns the liquidity position.
99    pub owner: Address,
100    /// The amount of liquidity tokens affected in the position.
101    pub position_liquidity: u128,
102    /// The amount of the first token in the pool pair.
103    pub amount0: U256,
104    /// The amount of the second token in the pool pair.
105    pub amount1: U256,
106    /// The lower price tick boundary of the liquidity position.
107    pub tick_lower: i32,
108    /// The upper price tick boundary of the liquidity position.
109    pub tick_upper: i32,
110    /// The timestamp of the liquidity update in Unix nanoseconds.
111    pub timestamp: Option<UnixNanos>,
112    /// UNIX timestamp (nanoseconds) when the instance was created.
113    pub ts_init: Option<UnixNanos>,
114}
115
116impl PoolLiquidityUpdate {
117    /// Creates a new [`PoolLiquidityUpdate`] instance with the specified properties.
118    #[must_use]
119    #[expect(clippy::too_many_arguments)]
120    pub const fn new(
121        chain: SharedChain,
122        dex: SharedDex,
123        instrument_id: InstrumentId,
124        pool_identifier: PoolIdentifier,
125        kind: PoolLiquidityUpdateType,
126        block: u64,
127        transaction_hash: String,
128        transaction_index: u32,
129        log_index: u32,
130        sender: Option<Address>,
131        owner: Address,
132        position_liquidity: u128,
133        amount0: U256,
134        amount1: U256,
135        tick_lower: i32,
136        tick_upper: i32,
137        timestamp: Option<UnixNanos>,
138    ) -> Self {
139        Self {
140            chain,
141            dex,
142            instrument_id,
143            pool_identifier,
144            kind,
145            block,
146            transaction_hash,
147            transaction_index,
148            log_index,
149            sender,
150            owner,
151            position_liquidity,
152            amount0,
153            amount1,
154            tick_lower,
155            tick_upper,
156            timestamp,
157            ts_init: timestamp,
158        }
159    }
160}
161
162impl Display for PoolLiquidityUpdate {
163    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
164        write!(
165            f,
166            "PoolLiquidityUpdate(instrument_id={}, kind={}, amount0={}, amount1={}, liquidity={})",
167            self.instrument_id, self.kind, self.amount0, self.amount1, self.position_liquidity
168        )
169    }
170}