Skip to main content

nautilus_blockchain/events/
flash.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, U256};
19use nautilus_core::UnixNanos;
20use nautilus_model::{
21    defi::{PoolIdentifier, SharedChain, SharedDex, data::PoolFlash},
22    identifiers::InstrumentId,
23};
24
25/// Represents a flash loan event from liquidity pools emitted from smart contract.
26///
27/// This struct captures the essential data from a flash loan transaction on decentralized
28/// exchanges (DEXs) that support flash loans.
29#[derive(Debug, Clone)]
30pub struct FlashEvent {
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 flash loan transaction 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 flash loan transaction.
46    pub sender: Address,
47    /// The address that received the flash loan.
48    pub recipient: Address,
49    /// The amount of token0 borrowed.
50    pub amount0: U256,
51    /// The amount of token1 borrowed.
52    pub amount1: U256,
53    /// The amount of token0 paid back (including fees).
54    pub paid0: U256,
55    /// The amount of token1 paid back (including fees).
56    pub paid1: U256,
57}
58
59impl FlashEvent {
60    /// Creates a new [`FlashEvent`] instance with the specified parameters.
61    #[must_use]
62    #[expect(clippy::too_many_arguments)]
63    pub fn new(
64        dex: SharedDex,
65        pool_identifier: PoolIdentifier,
66        block_number: u64,
67        block_hash: Option<B256>,
68        transaction_hash: String,
69        transaction_index: u32,
70        log_index: u32,
71        sender: Address,
72        recipient: Address,
73        amount0: U256,
74        amount1: U256,
75        paid0: U256,
76        paid1: U256,
77    ) -> Self {
78        Self {
79            dex,
80            pool_identifier,
81            block_number,
82            block_hash,
83            transaction_hash,
84            transaction_index,
85            log_index,
86            sender,
87            recipient,
88            amount0,
89            amount1,
90            paid0,
91            paid1,
92        }
93    }
94
95    /// Converts a flash event into a `PoolFlash`.
96    #[must_use]
97    pub fn to_pool_flash(
98        &self,
99        chain: SharedChain,
100        instrument_id: InstrumentId,
101        timestamp: UnixNanos,
102    ) -> PoolFlash {
103        PoolFlash::new(
104            chain,
105            Arc::clone(&self.dex),
106            instrument_id,
107            self.pool_identifier,
108            self.block_number,
109            self.transaction_hash.clone(),
110            self.transaction_index,
111            self.log_index,
112            timestamp, // ts_event
113            timestamp, // ts_init (same block timestamp)
114            self.sender,
115            self.recipient,
116            self.amount0,
117            self.amount1,
118            self.paid0,
119            self.paid1,
120        )
121    }
122}