nautilus_model/defi/data/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::fmt::Display;
17
18use alloy_primitives::{Address, U256};
19use nautilus_core::UnixNanos;
20use serde::{Deserialize, Serialize};
21
22use crate::{
23 defi::{PoolIdentifier, SharedChain, SharedDex},
24 identifiers::InstrumentId,
25};
26
27/// Represents a flash loan event from a Uniswap V3 pool.
28///
29/// Flash loans allow users to borrow tokens without collateral as long as they are returned
30/// within the same transaction. Fees are paid on the borrowed amount, which are added to
31/// the pool's fee growth accumulators.
32#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
33#[cfg_attr(
34 feature = "python",
35 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
36)]
37#[cfg_attr(
38 feature = "python",
39 pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
40)]
41pub struct PoolFlash {
42 /// The blockchain network where the flash loan occurred.
43 pub chain: SharedChain,
44 /// The decentralized exchange where the flash loan was executed.
45 pub dex: SharedDex,
46 /// The instrument ID for this pool's trading pair.
47 pub instrument_id: InstrumentId,
48 /// The unique identifier for this pool (could be an address or other protocol-specific hex string).
49 pub pool_identifier: PoolIdentifier,
50 /// The blockchain block number at which the flash loan was executed.
51 pub block: u64,
52 /// The unique hash identifier of the blockchain transaction containing the flash loan.
53 pub transaction_hash: String,
54 /// The index position of the transaction within the block.
55 pub transaction_index: u32,
56 /// The index position of the flash loan event log within the transaction.
57 pub log_index: u32,
58 /// The UNIX timestamp (nanoseconds) when the event occurred.
59 pub ts_event: Option<UnixNanos>,
60 /// The blockchain address of the user or contract that initiated the flash loan.
61 pub sender: Address,
62 /// The blockchain address that received the flash loan.
63 pub recipient: Address,
64 /// The amount of token0 borrowed.
65 pub amount0: U256,
66 /// The amount of token1 borrowed.
67 pub amount1: U256,
68 /// The amount of token0 paid back (including fees).
69 pub paid0: U256,
70 /// The amount of token1 paid back (including fees).
71 pub paid1: U256,
72}
73
74impl PoolFlash {
75 /// Creates a new [`PoolFlash`] instance with the specified parameters.
76 #[must_use]
77 #[expect(clippy::too_many_arguments)]
78 pub fn new(
79 chain: SharedChain,
80 dex: SharedDex,
81 instrument_id: InstrumentId,
82 pool_identifier: PoolIdentifier,
83 block_number: u64,
84 transaction_hash: String,
85 transaction_index: u32,
86 log_index: u32,
87 ts_event: Option<UnixNanos>,
88 sender: Address,
89 recipient: Address,
90 amount0: U256,
91 amount1: U256,
92 paid0: U256,
93 paid1: U256,
94 ) -> Self {
95 Self {
96 chain,
97 dex,
98 instrument_id,
99 pool_identifier,
100 block: block_number,
101 transaction_hash,
102 transaction_index,
103 log_index,
104 ts_event,
105 sender,
106 recipient,
107 amount0,
108 amount1,
109 paid0,
110 paid1,
111 }
112 }
113}
114
115impl Display for PoolFlash {
116 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117 write!(
118 f,
119 "PoolFlash(instrument={}, recipient={}, amount0={}, amount1={}, paid0={}, paid1={})",
120 self.instrument_id, self.recipient, self.amount0, self.amount1, self.paid0, self.paid1,
121 )
122 }
123}