nautilus_blockchain/events/flash.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::PoolFlash},
20 identifiers::InstrumentId,
21};
22
23/// Represents a flash loan event from liquidity pools emitted from smart contract.
24///
25/// This struct captures the essential data from a flash loan transaction on decentralized
26/// exchanges (DEXs) that support flash loans.
27#[derive(Debug, Clone)]
28pub struct FlashEvent {
29 /// The decentralized exchange where the event happened.
30 pub dex: SharedDex,
31 /// The address of the smart contract which emitted the event.
32 pub pool_address: Address,
33 /// The block number in which this flash loan transaction was included.
34 pub block_number: u64,
35 /// The unique hash identifier of the transaction containing this event.
36 pub transaction_hash: String,
37 /// The position of this transaction within the block.
38 pub transaction_index: u32,
39 /// The position of this event log within the transaction.
40 pub log_index: u32,
41 /// The address that initiated the flash loan transaction.
42 pub sender: Address,
43 /// The address that received the flash loan.
44 pub recipient: Address,
45 /// The amount of token0 borrowed.
46 pub amount0: U256,
47 /// The amount of token1 borrowed.
48 pub amount1: U256,
49 /// The amount of token0 paid back (including fees).
50 pub paid0: U256,
51 /// The amount of token1 paid back (including fees).
52 pub paid1: U256,
53}
54
55impl FlashEvent {
56 /// Creates a new [`FlashEvent`] instance with the specified parameters.
57 #[must_use]
58 #[allow(clippy::too_many_arguments)]
59 pub const fn new(
60 dex: SharedDex,
61 pool_address: Address,
62 block_number: u64,
63 transaction_hash: String,
64 transaction_index: u32,
65 log_index: u32,
66 sender: Address,
67 recipient: Address,
68 amount0: U256,
69 amount1: U256,
70 paid0: U256,
71 paid1: U256,
72 ) -> Self {
73 Self {
74 dex,
75 pool_address,
76 block_number,
77 transaction_hash,
78 transaction_index,
79 log_index,
80 sender,
81 recipient,
82 amount0,
83 amount1,
84 paid0,
85 paid1,
86 }
87 }
88
89 /// Converts a flash event into a `PoolFlash`.
90 #[must_use]
91 pub fn to_pool_flash(
92 &self,
93 chain: SharedChain,
94 instrument_id: InstrumentId,
95 pool_address: Address,
96 timestamp: Option<UnixNanos>,
97 ) -> PoolFlash {
98 PoolFlash::new(
99 chain,
100 self.dex.clone(),
101 instrument_id,
102 pool_address,
103 self.block_number,
104 self.transaction_hash.clone(),
105 self.transaction_index,
106 self.log_index,
107 timestamp,
108 self.sender,
109 self.recipient,
110 self.amount0,
111 self.amount1,
112 self.paid0,
113 self.paid1,
114 )
115 }
116}