nautilus_model/defi/data/fee_protocol_update.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 nautilus_core::UnixNanos;
19use serde::{Deserialize, Serialize};
20
21use crate::{
22 defi::{PoolIdentifier, SharedChain, SharedDex},
23 identifiers::InstrumentId,
24};
25
26/// Represents a protocol-fee configuration change in a Uniswap V3-style pool.
27///
28/// Emitted by `SetFeeProtocol`, this carries the new protocol-fee values for each token. Uniswap
29/// V3 uses 4-bit denominators, while PancakeSwap V3 uses `uint32` basis-point shares. Only the new
30/// values are kept; the previous values in the event are not needed to rebuild state.
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32#[cfg_attr(
33 feature = "python",
34 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
35)]
36#[cfg_attr(
37 feature = "python",
38 pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
39)]
40pub struct PoolFeeProtocolUpdate {
41 /// The blockchain network where the protocol-fee change occurred.
42 pub chain: SharedChain,
43 /// The decentralized exchange where the protocol-fee change occurred.
44 pub dex: SharedDex,
45 /// The instrument ID for this pool's trading pair.
46 pub instrument_id: InstrumentId,
47 /// The unique identifier for this pool (could be an address or other protocol-specific hex string).
48 pub pool_identifier: PoolIdentifier,
49 /// The blockchain block number where the protocol-fee change occurred.
50 pub block: u64,
51 /// The unique hash identifier of the blockchain transaction containing the protocol-fee change.
52 pub transaction_hash: String,
53 /// The index position of the transaction within the block.
54 pub transaction_index: u32,
55 /// The index position of the protocol-fee change event log within the transaction.
56 pub log_index: u32,
57 /// The new token0 protocol-fee value.
58 pub fee_protocol0_new: u32,
59 /// The new token1 protocol-fee value.
60 pub fee_protocol1_new: u32,
61 /// UNIX timestamp (nanoseconds) when the protocol-fee change event occurred.
62 pub ts_event: UnixNanos,
63 /// UNIX timestamp (nanoseconds) when the instance was created.
64 pub ts_init: UnixNanos,
65}
66
67impl PoolFeeProtocolUpdate {
68 /// Creates a new [`PoolFeeProtocolUpdate`] instance with the specified properties.
69 #[must_use]
70 #[expect(clippy::too_many_arguments)]
71 pub const fn new(
72 chain: SharedChain,
73 dex: SharedDex,
74 instrument_id: InstrumentId,
75 pool_identifier: PoolIdentifier,
76 block: u64,
77 transaction_hash: String,
78 transaction_index: u32,
79 log_index: u32,
80 fee_protocol0_new: u32,
81 fee_protocol1_new: u32,
82 ts_event: UnixNanos,
83 ts_init: UnixNanos,
84 ) -> Self {
85 Self {
86 chain,
87 dex,
88 instrument_id,
89 pool_identifier,
90 block,
91 transaction_hash,
92 transaction_index,
93 log_index,
94 fee_protocol0_new,
95 fee_protocol1_new,
96 ts_event,
97 ts_init,
98 }
99 }
100
101 /// Returns the new Uniswap V3 protocol-fee setting packed into a single byte.
102 ///
103 /// The token0 denominator occupies the lower four bits and token1 the upper four bits. Returns
104 /// `None` when either value does not fit the Uniswap V3 nibble layout.
105 #[must_use]
106 pub fn uniswap_v3_packed(&self) -> Option<u8> {
107 if self.fee_protocol0_new < 16 && self.fee_protocol1_new < 16 {
108 Some((self.fee_protocol0_new | (self.fee_protocol1_new << 4)) as u8)
109 } else {
110 None
111 }
112 }
113}
114
115impl Display for PoolFeeProtocolUpdate {
116 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117 write!(
118 f,
119 "PoolFeeProtocolUpdate({}, fee_protocol0_new={}, fee_protocol1_new={}, tx={}:{}:{})",
120 self.instrument_id,
121 self.fee_protocol0_new,
122 self.fee_protocol1_new,
123 self.block,
124 self.transaction_index,
125 self.log_index,
126 )
127 }
128}