nautilus_blockchain/events/collect.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 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, U256};
17use nautilus_core::UnixNanos;
18use nautilus_model::{
19 defi::{SharedChain, SharedDex, data::PoolFeeCollect},
20 identifiers::InstrumentId,
21 types::Quantity,
22};
23
24/// Represents a collect event that occurs when fees are collected from a position in a liquidity pool.
25#[derive(Debug, Clone)]
26pub struct CollectEvent {
27 /// The decentralized exchange where the event happened.
28 pub dex: SharedDex,
29 /// The address of the smart contract which emitted the event.
30 pub pool_address: Address,
31 /// The block number when the collect occurred.
32 pub block_number: u64,
33 /// The unique hash identifier of the transaction containing this event.
34 pub transaction_hash: String,
35 /// The position of this transaction within the block.
36 pub transaction_index: u32,
37 /// The position of this event log within the transaction.
38 pub log_index: u32,
39 /// The owner of the position.
40 pub owner: Address,
41 /// The recipient of the collected fees.
42 pub recipient: Address,
43 /// The lower tick boundary of the position.
44 pub tick_lower: i32,
45 /// The upper tick boundary of the position.
46 pub tick_upper: i32,
47 /// The amount of token0 fees collected.
48 pub amount0: U256,
49 /// The amount of token1 fees collected.
50 pub amount1: U256,
51}
52
53impl CollectEvent {
54 /// Creates a new [`CollectEvent`] instance with the specified parameters.
55 #[must_use]
56 #[allow(clippy::too_many_arguments)]
57 pub const fn new(
58 dex: SharedDex,
59 pool_address: Address,
60 block_number: u64,
61 transaction_hash: String,
62 transaction_index: u32,
63 log_index: u32,
64 owner: Address,
65 recipient: Address,
66 tick_lower: i32,
67 tick_upper: i32,
68 amount0: U256,
69 amount1: U256,
70 ) -> Self {
71 Self {
72 dex,
73 pool_address,
74 block_number,
75 transaction_hash,
76 transaction_index,
77 log_index,
78 owner,
79 recipient,
80 tick_lower,
81 tick_upper,
82 amount0,
83 amount1,
84 }
85 }
86
87 /// Converts a collect event into a `PoolFeeCollect`.
88 #[allow(clippy::too_many_arguments)]
89 pub fn to_pool_fee_collect(
90 &self,
91 chain: SharedChain,
92 dex: SharedDex,
93 instrument_id: InstrumentId,
94 pool_address: Address,
95 fee0: Quantity,
96 fee1: Quantity,
97 timestamp: Option<UnixNanos>,
98 ) -> PoolFeeCollect {
99 PoolFeeCollect::new(
100 chain,
101 dex,
102 instrument_id,
103 pool_address,
104 self.block_number,
105 self.transaction_hash.clone(),
106 self.transaction_index,
107 self.log_index,
108 self.owner,
109 fee0,
110 fee1,
111 self.tick_lower,
112 self.tick_upper,
113 timestamp,
114 )
115 }
116}