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;
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 unique hash identifier of the transaction containing this event.
38    pub transaction_hash: String,
39    /// The position of this transaction within the block.
40    pub transaction_index: u32,
41    /// The position of this event log within the transaction.
42    pub log_index: u32,
43    /// The address that initiated the withdrawal (the factory owner).
44    pub sender: Address,
45    /// The address that received the withdrawn protocol fees.
46    pub recipient: Address,
47    /// The amount of token0 protocol fees withdrawn.
48    pub amount0: u128,
49    /// The amount of token1 protocol fees withdrawn.
50    pub amount1: u128,
51}
52
53impl FeeProtocolCollectEvent {
54    /// Creates a new [`FeeProtocolCollectEvent`] instance with the specified parameters.
55    #[must_use]
56    #[expect(clippy::too_many_arguments)]
57    pub fn new(
58        dex: SharedDex,
59        pool_identifier: PoolIdentifier,
60        block_number: u64,
61        transaction_hash: String,
62        transaction_index: u32,
63        log_index: u32,
64        sender: Address,
65        recipient: Address,
66        amount0: u128,
67        amount1: u128,
68    ) -> Self {
69        Self {
70            dex,
71            pool_identifier,
72            block_number,
73            transaction_hash,
74            transaction_index,
75            log_index,
76            sender,
77            recipient,
78            amount0,
79            amount1,
80        }
81    }
82
83    /// Converts a collect-protocol event into a `PoolFeeProtocolCollect`.
84    #[must_use]
85    pub fn to_pool_fee_protocol_collect(
86        &self,
87        chain: SharedChain,
88        instrument_id: InstrumentId,
89        timestamp: UnixNanos,
90    ) -> PoolFeeProtocolCollect {
91        PoolFeeProtocolCollect::new(
92            chain,
93            Arc::clone(&self.dex),
94            instrument_id,
95            self.pool_identifier,
96            self.block_number,
97            self.transaction_hash.clone(),
98            self.transaction_index,
99            self.log_index,
100            self.sender,
101            self.recipient,
102            self.amount0,
103            self.amount1,
104            timestamp, // ts_event
105            timestamp, // ts_init (same block timestamp)
106        )
107    }
108}