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