nautilus_model/defi/data/fee_protocol_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 protocol-fee withdrawal from a Uniswap V3-style pool.
28///
29/// Emitted by `CollectProtocol`, this carries the protocol-fee amounts withdrawn to the recipient.
30/// The amounts decrement the pool's accrued protocol-fee balances, leaving the on-chain remainder
31/// (Uniswap V3 keeps one wei in each slot to save gas).
32#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
33#[cfg_attr(
34 feature = "python",
35 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
36)]
37#[cfg_attr(
38 feature = "python",
39 pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
40)]
41pub struct PoolFeeProtocolCollect {
42 /// The blockchain network where the protocol-fee withdrawal occurred.
43 pub chain: SharedChain,
44 /// The decentralized exchange where the protocol-fee withdrawal occurred.
45 pub dex: SharedDex,
46 /// The instrument ID for this pool's trading pair.
47 pub instrument_id: InstrumentId,
48 /// The unique identifier for this pool (could be an address or other protocol-specific hex string).
49 pub pool_identifier: PoolIdentifier,
50 /// The blockchain block number where the protocol-fee withdrawal occurred.
51 pub block: u64,
52 /// The unique hash identifier of the blockchain transaction containing the protocol-fee withdrawal.
53 pub transaction_hash: String,
54 /// The index position of the transaction within the block.
55 pub transaction_index: u32,
56 /// The index position of the protocol-fee withdrawal event log within the transaction.
57 pub log_index: u32,
58 /// The address that initiated the withdrawal (the factory owner).
59 pub sender: Address,
60 /// The address that received the withdrawn protocol fees.
61 pub recipient: Address,
62 /// The amount of token0 protocol fees withdrawn.
63 pub amount0: u128,
64 /// The amount of token1 protocol fees withdrawn.
65 pub amount1: u128,
66 /// UNIX timestamp (nanoseconds) when the protocol-fee withdrawal event occurred.
67 pub ts_event: UnixNanos,
68 /// UNIX timestamp (nanoseconds) when the instance was created.
69 pub ts_init: UnixNanos,
70}
71
72impl PoolFeeProtocolCollect {
73 /// Creates a new [`PoolFeeProtocolCollect`] instance with the specified properties.
74 #[must_use]
75 #[expect(clippy::too_many_arguments)]
76 pub const fn new(
77 chain: SharedChain,
78 dex: SharedDex,
79 instrument_id: InstrumentId,
80 pool_identifier: PoolIdentifier,
81 block: u64,
82 transaction_hash: String,
83 transaction_index: u32,
84 log_index: u32,
85 sender: Address,
86 recipient: Address,
87 amount0: u128,
88 amount1: u128,
89 ts_event: UnixNanos,
90 ts_init: UnixNanos,
91 ) -> Self {
92 Self {
93 chain,
94 dex,
95 instrument_id,
96 pool_identifier,
97 block,
98 transaction_hash,
99 transaction_index,
100 log_index,
101 sender,
102 recipient,
103 amount0,
104 amount1,
105 ts_event,
106 ts_init,
107 }
108 }
109}
110
111impl Display for PoolFeeProtocolCollect {
112 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 write!(
114 f,
115 "PoolFeeProtocolCollect({} protocol fees withdrawn: token0={}, token1={}, recipient={}, tx={}:{}:{})",
116 self.instrument_id,
117 self.amount0,
118 self.amount1,
119 self.recipient,
120 self.block,
121 self.transaction_index,
122 self.log_index,
123 )
124 }
125}