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