Skip to main content

nautilus_blockchain/events/
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::sync::Arc;
17
18use nautilus_core::UnixNanos;
19use nautilus_model::{
20    defi::{PoolIdentifier, SharedChain, SharedDex, data::PoolFeeProtocolUpdate},
21    identifiers::InstrumentId,
22};
23
24/// Represents a `SetFeeProtocol` event that changes a pool's protocol-fee configuration.
25///
26/// Only the new per-token values are retained; the previous values carried by the event are not
27/// needed to rebuild pool state.
28#[derive(Debug, Clone)]
29pub struct FeeProtocolUpdateEvent {
30    /// The decentralized exchange where the event happened.
31    pub dex: SharedDex,
32    /// The unique identifier for the pool.
33    pub pool_identifier: PoolIdentifier,
34    /// The block number in which this event was included.
35    pub block_number: u64,
36    /// The unique hash identifier of the transaction containing this event.
37    pub transaction_hash: String,
38    /// The position of this transaction within the block.
39    pub transaction_index: u32,
40    /// The position of this event log within the transaction.
41    pub log_index: u32,
42    /// The new token0 protocol-fee value.
43    pub fee_protocol0_new: u32,
44    /// The new token1 protocol-fee value.
45    pub fee_protocol1_new: u32,
46}
47
48impl FeeProtocolUpdateEvent {
49    /// Creates a new [`FeeProtocolUpdateEvent`] instance with the specified parameters.
50    #[must_use]
51    #[expect(clippy::too_many_arguments)]
52    pub fn new(
53        dex: SharedDex,
54        pool_identifier: PoolIdentifier,
55        block_number: u64,
56        transaction_hash: String,
57        transaction_index: u32,
58        log_index: u32,
59        fee_protocol0_new: u32,
60        fee_protocol1_new: u32,
61    ) -> Self {
62        Self {
63            dex,
64            pool_identifier,
65            block_number,
66            transaction_hash,
67            transaction_index,
68            log_index,
69            fee_protocol0_new,
70            fee_protocol1_new,
71        }
72    }
73
74    /// Converts a fee-protocol update event into a `PoolFeeProtocolUpdate`.
75    #[must_use]
76    pub fn to_pool_fee_protocol_update(
77        &self,
78        chain: SharedChain,
79        instrument_id: InstrumentId,
80        timestamp: UnixNanos,
81    ) -> PoolFeeProtocolUpdate {
82        PoolFeeProtocolUpdate::new(
83            chain,
84            Arc::clone(&self.dex),
85            instrument_id,
86            self.pool_identifier,
87            self.block_number,
88            self.transaction_hash.clone(),
89            self.transaction_index,
90            self.log_index,
91            self.fee_protocol0_new,
92            self.fee_protocol1_new,
93            timestamp, // ts_event
94            timestamp, // ts_init (same block timestamp)
95        )
96    }
97}