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.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 hash of the block observed when this withdrawal was ingested.
53 #[serde(default, skip_serializing_if = "Option::is_none")]
54 pub block_hash: Option<String>,
55 /// The unique hash identifier of the blockchain transaction containing the protocol-fee withdrawal.
56 pub transaction_hash: String,
57 /// The index position of the transaction within the block.
58 pub transaction_index: u32,
59 /// The index position of the protocol-fee withdrawal event log within the transaction.
60 pub log_index: u32,
61 /// The address that initiated the withdrawal (the factory owner).
62 pub sender: Address,
63 /// The address that received the withdrawn protocol fees.
64 pub recipient: Address,
65 /// The amount of token0 protocol fees withdrawn.
66 pub amount0: u128,
67 /// The amount of token1 protocol fees withdrawn.
68 pub amount1: u128,
69 /// UNIX timestamp (nanoseconds) when the protocol-fee withdrawal event occurred.
70 pub ts_event: UnixNanos,
71 /// UNIX timestamp (nanoseconds) when the instance was created.
72 pub ts_init: UnixNanos,
73}
74
75impl PoolFeeProtocolCollect {
76 /// Creates a new [`PoolFeeProtocolCollect`] instance with the specified properties.
77 #[must_use]
78 #[expect(clippy::too_many_arguments)]
79 pub const fn new(
80 chain: SharedChain,
81 dex: SharedDex,
82 instrument_id: InstrumentId,
83 pool_identifier: PoolIdentifier,
84 block: u64,
85 transaction_hash: String,
86 transaction_index: u32,
87 log_index: u32,
88 sender: Address,
89 recipient: Address,
90 amount0: u128,
91 amount1: u128,
92 ts_event: UnixNanos,
93 ts_init: UnixNanos,
94 ) -> Self {
95 Self {
96 chain,
97 dex,
98 instrument_id,
99 pool_identifier,
100 block,
101 block_hash: None,
102 transaction_hash,
103 transaction_index,
104 log_index,
105 sender,
106 recipient,
107 amount0,
108 amount1,
109 ts_event,
110 ts_init,
111 }
112 }
113}
114
115impl Display for PoolFeeProtocolCollect {
116 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117 write!(
118 f,
119 "PoolFeeProtocolCollect({} protocol fees withdrawn: token0={}, token1={}, recipient={}, tx={}:{}:{})",
120 self.instrument_id,
121 self.amount0,
122 self.amount1,
123 self.recipient,
124 self.block,
125 self.transaction_index,
126 self.log_index,
127 )
128 }
129}