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 /// UNIX timestamp (nanoseconds) when the flash event occurred.
59 pub ts_event: 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 /// UNIX timestamp (nanoseconds) when the instance was created.
73 pub ts_init: UnixNanos,
74}
75
76impl PoolFlash {
77 /// Creates a new [`PoolFlash`] instance with the specified parameters.
78 #[must_use]
79 #[expect(clippy::too_many_arguments)]
80 pub fn new(
81 chain: SharedChain,
82 dex: SharedDex,
83 instrument_id: InstrumentId,
84 pool_identifier: PoolIdentifier,
85 block_number: u64,
86 transaction_hash: String,
87 transaction_index: u32,
88 log_index: u32,
89 ts_event: UnixNanos,
90 ts_init: UnixNanos,
91 sender: Address,
92 recipient: Address,
93 amount0: U256,
94 amount1: U256,
95 paid0: U256,
96 paid1: U256,
97 ) -> Self {
98 Self {
99 chain,
100 dex,
101 instrument_id,
102 pool_identifier,
103 block: block_number,
104 transaction_hash,
105 transaction_index,
106 log_index,
107 ts_event,
108 sender,
109 recipient,
110 amount0,
111 amount1,
112 paid0,
113 paid1,
114 ts_init,
115 }
116 }
117}
118
119impl Display for PoolFlash {
120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121 write!(
122 f,
123 "PoolFlash(instrument={}, recipient={}, amount0={}, amount1={}, paid0={}, paid1={})",
124 self.instrument_id, self.recipient, self.amount0, self.amount1, self.paid0, self.paid1,
125 )
126 }
127}