Skip to main content

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.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 hash of the block observed when this flash loan was ingested.
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub block_hash: Option<String>,
55    /// The unique hash identifier of the blockchain transaction containing the flash loan.
56    pub transaction_hash: String,
57    /// The index position of the transaction within the block.
58    pub transaction_index: u32,
59    /// The index position of the flash loan event log within the transaction.
60    pub log_index: u32,
61    /// UNIX timestamp (nanoseconds) when the flash event occurred.
62    pub ts_event: UnixNanos,
63    /// The blockchain address of the user or contract that initiated the flash loan.
64    pub sender: Address,
65    /// The blockchain address that received the flash loan.
66    pub recipient: Address,
67    /// The amount of token0 borrowed.
68    pub amount0: U256,
69    /// The amount of token1 borrowed.
70    pub amount1: U256,
71    /// The amount of token0 paid back (including fees).
72    pub paid0: U256,
73    /// The amount of token1 paid back (including fees).
74    pub paid1: U256,
75    /// UNIX timestamp (nanoseconds) when the instance was created.
76    pub ts_init: UnixNanos,
77}
78
79impl PoolFlash {
80    /// Creates a new [`PoolFlash`] instance with the specified parameters.
81    #[must_use]
82    #[expect(clippy::too_many_arguments)]
83    pub fn new(
84        chain: SharedChain,
85        dex: SharedDex,
86        instrument_id: InstrumentId,
87        pool_identifier: PoolIdentifier,
88        block_number: u64,
89        transaction_hash: String,
90        transaction_index: u32,
91        log_index: u32,
92        ts_event: UnixNanos,
93        ts_init: UnixNanos,
94        sender: Address,
95        recipient: Address,
96        amount0: U256,
97        amount1: U256,
98        paid0: U256,
99        paid1: U256,
100    ) -> Self {
101        Self {
102            chain,
103            dex,
104            instrument_id,
105            pool_identifier,
106            block: block_number,
107            block_hash: None,
108            transaction_hash,
109            transaction_index,
110            log_index,
111            ts_event,
112            sender,
113            recipient,
114            amount0,
115            amount1,
116            paid0,
117            paid1,
118            ts_init,
119        }
120    }
121}
122
123impl Display for PoolFlash {
124    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125        write!(
126            f,
127            "PoolFlash(instrument={}, recipient={}, amount0={}, amount1={}, paid0={}, paid1={})",
128            self.instrument_id, self.recipient, self.amount0, self.amount1, self.paid0, self.paid1,
129        )
130    }
131}