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.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 hash of the block observed when this change was ingested.
52 #[serde(default, skip_serializing_if = "Option::is_none")]
53 pub block_hash: Option<String>,
54 /// The unique hash identifier of the blockchain transaction containing the protocol-fee change.
55 pub transaction_hash: String,
56 /// The index position of the transaction within the block.
57 pub transaction_index: u32,
58 /// The index position of the protocol-fee change event log within the transaction.
59 pub log_index: u32,
60 /// The new token0 protocol-fee value.
61 pub fee_protocol0_new: u32,
62 /// The new token1 protocol-fee value.
63 pub fee_protocol1_new: u32,
64 /// UNIX timestamp (nanoseconds) when the protocol-fee change event occurred.
65 pub ts_event: UnixNanos,
66 /// UNIX timestamp (nanoseconds) when the instance was created.
67 pub ts_init: UnixNanos,
68}
69
70impl PoolFeeProtocolUpdate {
71 /// Creates a new [`PoolFeeProtocolUpdate`] 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 fee_protocol0_new: u32,
84 fee_protocol1_new: u32,
85 ts_event: UnixNanos,
86 ts_init: UnixNanos,
87 ) -> Self {
88 Self {
89 chain,
90 dex,
91 instrument_id,
92 pool_identifier,
93 block,
94 block_hash: None,
95 transaction_hash,
96 transaction_index,
97 log_index,
98 fee_protocol0_new,
99 fee_protocol1_new,
100 ts_event,
101 ts_init,
102 }
103 }
104
105 /// Returns the new Uniswap V3 protocol-fee setting packed into a single byte.
106 ///
107 /// The token0 denominator occupies the lower four bits and token1 the upper four bits. Returns
108 /// `None` when either value does not fit the Uniswap V3 nibble layout.
109 #[must_use]
110 pub fn uniswap_v3_packed(&self) -> Option<u8> {
111 if self.fee_protocol0_new < 16 && self.fee_protocol1_new < 16 {
112 Some((self.fee_protocol0_new | (self.fee_protocol1_new << 4)) as u8)
113 } else {
114 None
115 }
116 }
117}
118
119impl Display for PoolFeeProtocolUpdate {
120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121 write!(
122 f,
123 "PoolFeeProtocolUpdate({}, fee_protocol0_new={}, fee_protocol1_new={}, tx={}:{}:{})",
124 self.instrument_id,
125 self.fee_protocol0_new,
126 self.fee_protocol1_new,
127 self.block,
128 self.transaction_index,
129 self.log_index,
130 )
131 }
132}