Skip to main content

nautilus_blockchain/events/
fee_protocol_collect.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::{Address, B256};
19use nautilus_core::UnixNanos;
20use nautilus_model::{
21    defi::{PoolIdentifier, SharedChain, SharedDex, data::PoolFeeProtocolCollect},
22    identifiers::InstrumentId,
23};
24
25/// Represents a `CollectProtocol` event that withdraws accrued protocol fees from a pool.
26///
27/// This is the owner-only protocol-fee withdrawal, distinct from the regular `Collect` event that
28/// withdraws an LP position's fees. The withdrawn amounts decrement the pool's protocol-fee balances.
29#[derive(Debug, Clone)]
30pub struct FeeProtocolCollectEvent {
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 address that initiated the withdrawal (the factory owner).
46    pub sender: Address,
47    /// The address that received the withdrawn protocol fees.
48    pub recipient: Address,
49    /// The amount of token0 protocol fees withdrawn.
50    pub amount0: u128,
51    /// The amount of token1 protocol fees withdrawn.
52    pub amount1: u128,
53}
54
55impl FeeProtocolCollectEvent {
56    /// Creates a new [`FeeProtocolCollectEvent`] instance with the specified parameters.
57    #[must_use]
58    #[expect(clippy::too_many_arguments)]
59    pub fn new(
60        dex: SharedDex,
61        pool_identifier: PoolIdentifier,
62        block_number: u64,
63        block_hash: Option<B256>,
64        transaction_hash: String,
65        transaction_index: u32,
66        log_index: u32,
67        sender: Address,
68        recipient: Address,
69        amount0: u128,
70        amount1: u128,
71    ) -> Self {
72        Self {
73            dex,
74            pool_identifier,
75            block_number,
76            block_hash,
77            transaction_hash,
78            transaction_index,
79            log_index,
80            sender,
81            recipient,
82            amount0,
83            amount1,
84        }
85    }
86
87    /// Converts a collect-protocol event into a `PoolFeeProtocolCollect`.
88    #[must_use]
89    pub fn to_pool_fee_protocol_collect(
90        &self,
91        chain: SharedChain,
92        instrument_id: InstrumentId,
93        timestamp: UnixNanos,
94    ) -> PoolFeeProtocolCollect {
95        PoolFeeProtocolCollect::new(
96            chain,
97            Arc::clone(&self.dex),
98            instrument_id,
99            self.pool_identifier,
100            self.block_number,
101            self.transaction_hash.clone(),
102            self.transaction_index,
103            self.log_index,
104            self.sender,
105            self.recipient,
106            self.amount0,
107            self.amount1,
108            timestamp, // ts_event
109            timestamp, // ts_init (same block timestamp)
110        )
111    }
112}