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, 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 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 flash loan transaction.
44 pub sender: Address,
45 /// The address that received the flash loan.
46 pub recipient: Address,
47 /// The amount of token0 borrowed.
48 pub amount0: U256,
49 /// The amount of token1 borrowed.
50 pub amount1: U256,
51 /// The amount of token0 paid back (including fees).
52 pub paid0: U256,
53 /// The amount of token1 paid back (including fees).
54 pub paid1: U256,
55}
56
57impl FlashEvent {
58 /// Creates a new [`FlashEvent`] instance with the specified parameters.
59 #[must_use]
60 #[expect(clippy::too_many_arguments)]
61 pub fn new(
62 dex: SharedDex,
63 pool_identifier: PoolIdentifier,
64 block_number: u64,
65 transaction_hash: String,
66 transaction_index: u32,
67 log_index: u32,
68 sender: Address,
69 recipient: Address,
70 amount0: U256,
71 amount1: U256,
72 paid0: U256,
73 paid1: U256,
74 ) -> Self {
75 Self {
76 dex,
77 pool_identifier,
78 block_number,
79 transaction_hash,
80 transaction_index,
81 log_index,
82 sender,
83 recipient,
84 amount0,
85 amount1,
86 paid0,
87 paid1,
88 }
89 }
90
91 /// Converts a flash event into a `PoolFlash`.
92 #[must_use]
93 pub fn to_pool_flash(
94 &self,
95 chain: SharedChain,
96 instrument_id: InstrumentId,
97 timestamp: UnixNanos,
98 ) -> PoolFlash {
99 PoolFlash::new(
100 chain,
101 Arc::clone(&self.dex),
102 instrument_id,
103 self.pool_identifier,
104 self.block_number,
105 self.transaction_hash.clone(),
106 self.transaction_index,
107 self.log_index,
108 timestamp, // ts_event
109 timestamp, // ts_init (same block timestamp)
110 self.sender,
111 self.recipient,
112 self.amount0,
113 self.amount1,
114 self.paid0,
115 self.paid1,
116 )
117 }
118}