Skip to main content

nautilus_model/defi/data/
collect.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;
19use nautilus_core::UnixNanos;
20use serde::{Deserialize, Serialize};
21
22use crate::{
23    defi::{PoolIdentifier, SharedChain, SharedDex},
24    identifiers::InstrumentId,
25};
26
27/// Represents a fee collection event in a decentralized exchange (DEX) pool.
28#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
29#[cfg_attr(
30    feature = "python",
31    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
32)]
33#[cfg_attr(
34    feature = "python",
35    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
36)]
37pub struct PoolFeeCollect {
38    /// The blockchain network where the fee collection occurred.
39    pub chain: SharedChain,
40    /// The decentralized exchange where the fee collection was executed.
41    pub dex: SharedDex,
42    /// The instrument ID for this pool's trading pair.
43    pub instrument_id: InstrumentId,
44    /// The unique identifier for this pool (could be an address or other protocol-specific hex string).
45    pub pool_identifier: PoolIdentifier,
46    /// The blockchain block number where the fee collection occurred.
47    pub block: u64,
48    /// The unique hash identifier of the blockchain transaction containing the fee collection.
49    pub transaction_hash: String,
50    /// The index position of the transaction within the block.
51    pub transaction_index: u32,
52    /// The index position of the fee collection event log within the transaction.
53    pub log_index: u32,
54    /// The blockchain address that owns the liquidity position.
55    pub owner: Address,
56    /// The amount of the first token fees collected.
57    pub amount0: u128,
58    /// The amount of the second token fees collected.
59    pub amount1: u128,
60    /// The lower price tick boundary of the liquidity position.
61    pub tick_lower: i32,
62    /// The upper price tick boundary of the liquidity position.
63    pub tick_upper: i32,
64    /// The timestamp of the fee collection in Unix nanoseconds.
65    pub timestamp: Option<UnixNanos>,
66    /// UNIX timestamp (nanoseconds) when the instance was created.
67    pub ts_init: Option<UnixNanos>,
68}
69
70impl PoolFeeCollect {
71    /// Creates a new [`PoolFeeCollect`] instance with the specified properties.
72    #[must_use]
73    #[expect(clippy::too_many_arguments)]
74    pub const fn new(
75        chain: SharedChain,
76        dex: SharedDex,
77        instrument_id: InstrumentId,
78        pool_identifier: PoolIdentifier,
79        block: u64,
80        transaction_hash: String,
81        transaction_index: u32,
82        log_index: u32,
83        owner: Address,
84        amount0: u128,
85        amount1: u128,
86        tick_lower: i32,
87        tick_upper: i32,
88        timestamp: Option<UnixNanos>,
89    ) -> Self {
90        Self {
91            chain,
92            dex,
93            instrument_id,
94            pool_identifier,
95            block,
96            transaction_hash,
97            transaction_index,
98            log_index,
99            owner,
100            amount0,
101            amount1,
102            tick_lower,
103            tick_upper,
104            timestamp,
105            ts_init: timestamp,
106        }
107    }
108}
109
110impl Display for PoolFeeCollect {
111    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
112        write!(
113            f,
114            "PoolFeeCollect({} fees collected: token0={}, token1={}, owner={}, tick_range=[{}, {}], tx={}:{}:{})",
115            self.instrument_id,
116            self.amount0,
117            self.amount1,
118            self.owner,
119            self.tick_lower,
120            self.tick_upper,
121            self.block,
122            self.transaction_index,
123            self.log_index,
124        )
125    }
126}