nautilus_model/defi/data/
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 std::fmt::Display;
17
18use alloy_primitives::{Address, U256};
19use nautilus_core::UnixNanos;
20use serde::{Deserialize, Serialize};
21
22use crate::{
23    defi::{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")
36)]
37pub struct PoolFlash {
38    /// The blockchain network where the flash loan occurred.
39    pub chain: SharedChain,
40    /// The decentralized exchange where the flash loan was executed.
41    pub dex: SharedDex,
42    /// The instrument ID for this pool's trading pair.
43    pub instrument_id: InstrumentId,
44    /// The blockchain address of the pool smart contract.
45    pub pool_address: Address,
46    /// The blockchain block number at which the flash loan was executed.
47    pub block: u64,
48    /// The unique hash identifier of the blockchain transaction containing the flash loan.
49    pub transaction_hash: String,
50    /// The index position of the transaction within the block.
51    pub transaction_index: u32,
52    /// The index position of the flash loan event log within the transaction.
53    pub log_index: u32,
54    /// The UNIX timestamp (nanoseconds) when the event occurred.
55    pub ts_event: Option<UnixNanos>,
56    /// The blockchain address of the user or contract that initiated the flash loan.
57    pub sender: Address,
58    /// The blockchain address that received the flash loan.
59    pub recipient: Address,
60    /// The amount of token0 borrowed.
61    pub amount0: U256,
62    /// The amount of token1 borrowed.
63    pub amount1: U256,
64    /// The amount of token0 paid back (including fees).
65    pub paid0: U256,
66    /// The amount of token1 paid back (including fees).
67    pub paid1: U256,
68}
69
70impl PoolFlash {
71    /// Creates a new [`PoolFlash`] instance with the specified parameters.
72    #[must_use]
73    #[allow(clippy::too_many_arguments)]
74    pub fn new(
75        chain: SharedChain,
76        dex: SharedDex,
77        instrument_id: InstrumentId,
78        pool_address: Address,
79        block_number: u64,
80        transaction_hash: String,
81        transaction_index: u32,
82        log_index: u32,
83        ts_event: Option<UnixNanos>,
84        sender: Address,
85        recipient: Address,
86        amount0: U256,
87        amount1: U256,
88        paid0: U256,
89        paid1: U256,
90    ) -> Self {
91        Self {
92            chain,
93            dex,
94            instrument_id,
95            pool_address,
96            block: block_number,
97            transaction_hash,
98            transaction_index,
99            log_index,
100            ts_event,
101            sender,
102            recipient,
103            amount0,
104            amount1,
105            paid0,
106            paid1,
107        }
108    }
109
110    /// Returns the instrument ID for this pool's trading pair.
111    #[must_use]
112    pub const fn instrument_id(&self) -> InstrumentId {
113        self.instrument_id
114    }
115}
116
117impl Display for PoolFlash {
118    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119        write!(
120            f,
121            "PoolFlash(instrument={}, recipient={}, amount0={}, amount1={}, paid0={}, paid1={})",
122            self.instrument_id, self.recipient, self.amount0, self.amount1, self.paid0, self.paid1,
123        )
124    }
125}