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 alloy::primitives::Address;
17use nautilus_core::UnixNanos;
18use nautilus_model::{
19    defi::{PoolIdentifier, SharedChain, SharedDex, data::PoolFeeProtocolCollect},
20    identifiers::InstrumentId,
21};
22
23/// Represents a `CollectProtocol` event that withdraws accrued protocol fees from a pool.
24///
25/// This is the owner-only protocol-fee withdrawal, distinct from the regular `Collect` event that
26/// withdraws an LP position's fees. The withdrawn amounts decrement the pool's protocol-fee balances.
27#[derive(Debug, Clone)]
28pub struct FeeProtocolCollectEvent {
29    /// The decentralized exchange where the event happened.
30    pub dex: SharedDex,
31    /// The unique identifier for the pool.
32    pub pool_identifier: PoolIdentifier,
33    /// The block number in which this event was included.
34    pub block_number: u64,
35    /// The unique hash identifier of the transaction containing this event.
36    pub transaction_hash: String,
37    /// The position of this transaction within the block.
38    pub transaction_index: u32,
39    /// The position of this event log within the transaction.
40    pub log_index: u32,
41    /// The address that initiated the withdrawal (the factory owner).
42    pub sender: Address,
43    /// The address that received the withdrawn protocol fees.
44    pub recipient: Address,
45    /// The amount of token0 protocol fees withdrawn.
46    pub amount0: u128,
47    /// The amount of token1 protocol fees withdrawn.
48    pub amount1: u128,
49}
50
51impl FeeProtocolCollectEvent {
52    /// Creates a new [`FeeProtocolCollectEvent`] 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        transaction_hash: String,
60        transaction_index: u32,
61        log_index: u32,
62        sender: Address,
63        recipient: Address,
64        amount0: u128,
65        amount1: u128,
66    ) -> Self {
67        Self {
68            dex,
69            pool_identifier,
70            block_number,
71            transaction_hash,
72            transaction_index,
73            log_index,
74            sender,
75            recipient,
76            amount0,
77            amount1,
78        }
79    }
80
81    /// Converts a collect-protocol event into a `PoolFeeProtocolCollect`.
82    #[must_use]
83    pub fn to_pool_fee_protocol_collect(
84        &self,
85        chain: SharedChain,
86        instrument_id: InstrumentId,
87        timestamp: UnixNanos,
88    ) -> PoolFeeProtocolCollect {
89        PoolFeeProtocolCollect::new(
90            chain,
91            self.dex.clone(),
92            instrument_id,
93            self.pool_identifier,
94            self.block_number,
95            self.transaction_hash.clone(),
96            self.transaction_index,
97            self.log_index,
98            self.sender,
99            self.recipient,
100            self.amount0,
101            self.amount1,
102            timestamp, // ts_event
103            timestamp, // ts_init (same block timestamp)
104        )
105    }
106}