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