Skip to main content

nautilus_blockchain/events/
mint.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, B256, U256};
17use nautilus_core::UnixNanos;
18use nautilus_model::{
19    defi::{PoolIdentifier, PoolLiquidityUpdate, PoolLiquidityUpdateType, SharedChain, SharedDex},
20    identifiers::InstrumentId,
21};
22
23/// Represents a mint event that occurs when liquidity is added to a position in a liquidity pool.
24#[derive(Debug, Clone)]
25pub struct MintEvent {
26    /// The decentralized exchange where the event happened.
27    pub dex: SharedDex,
28    /// The unique identifier for the pool.
29    pub pool_identifier: PoolIdentifier,
30    /// The block number when the mint occurred.
31    pub block_number: u64,
32    /// The hash of the block containing this event, when the source log reported it.
33    pub block_hash: Option<B256>,
34    /// The unique hash identifier of the transaction containing this event.
35    pub transaction_hash: String,
36    /// The position of this transaction within the block.
37    pub transaction_index: u32,
38    /// The position of this event log within the transaction.
39    pub log_index: u32,
40    /// The address that sent the transaction.
41    pub sender: Address,
42    /// The owner of the position.
43    pub owner: Address,
44    /// The lower tick boundary of the position.
45    pub tick_lower: i32,
46    /// The upper tick boundary of the position.
47    pub tick_upper: i32,
48    /// The amount of liquidity minted.
49    pub amount: u128,
50    /// The amount of token0 deposited.
51    pub amount0: U256,
52    /// The amount of token1 deposited.
53    pub amount1: U256,
54}
55
56impl MintEvent {
57    /// Creates a new [`MintEvent`] instance with the specified parameters.
58    #[must_use]
59    #[expect(clippy::too_many_arguments)]
60    pub fn new(
61        dex: SharedDex,
62        pool_identifier: PoolIdentifier,
63        block_number: u64,
64        block_hash: Option<B256>,
65        transaction_hash: String,
66        transaction_index: u32,
67        log_index: u32,
68        sender: Address,
69        owner: Address,
70        tick_lower: i32,
71        tick_upper: i32,
72        amount: u128,
73        amount0: U256,
74        amount1: U256,
75    ) -> Self {
76        Self {
77            dex,
78            pool_identifier,
79            block_number,
80            block_hash,
81            transaction_hash,
82            transaction_index,
83            log_index,
84            sender,
85            owner,
86            tick_lower,
87            tick_upper,
88            amount,
89            amount0,
90            amount1,
91        }
92    }
93
94    /// Converts a mint event into a `PoolLiquidityUpdate`.
95    #[must_use]
96    pub fn to_pool_liquidity_update(
97        &self,
98        chain: SharedChain,
99        dex: SharedDex,
100        instrument_id: InstrumentId,
101        timestamp: UnixNanos,
102    ) -> PoolLiquidityUpdate {
103        PoolLiquidityUpdate::new(
104            chain,
105            dex,
106            instrument_id,
107            self.pool_identifier,
108            PoolLiquidityUpdateType::Mint,
109            self.block_number,
110            self.transaction_hash.clone(),
111            self.transaction_index,
112            self.log_index,
113            Some(self.sender),
114            self.owner,
115            self.amount,
116            self.amount0,
117            self.amount1,
118            self.tick_lower,
119            self.tick_upper,
120            timestamp, // ts_event
121            timestamp, // ts_init (same block timestamp)
122        )
123    }
124}