nautilus_model/defi/data/
swap.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, I256, U160};
19use nautilus_core::UnixNanos;
20use serde::{Deserialize, Serialize};
21
22use crate::{
23    defi::{SharedChain, SharedDex},
24    enums::OrderSide,
25    identifiers::InstrumentId,
26    types::{Price, Quantity},
27};
28
29/// Represents a token swap transaction on a decentralized exchange (DEX).
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31#[cfg_attr(
32    feature = "python",
33    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
34)]
35pub struct PoolSwap {
36    /// The blockchain network where the swap occurred.
37    pub chain: SharedChain,
38    /// The decentralized exchange where the swap was executed.
39    pub dex: SharedDex,
40    /// The instrument ID for this pool's trading pair.
41    pub instrument_id: InstrumentId,
42    /// The blockchain address of the pool smart contract.
43    pub pool_address: Address,
44    /// The blockchain block number at which the swap was executed.
45    pub block: u64,
46    /// The unique hash identifier of the blockchain transaction containing the swap.
47    pub transaction_hash: String,
48    /// The index position of the transaction within the block.
49    pub transaction_index: u32,
50    /// The index position of the swap event log within the transaction.
51    pub log_index: u32,
52    /// The blockchain address of the user or contract that initiated the swap.
53    pub sender: Address,
54    /// The blockchain address that received the swapped tokens.
55    pub recipient: Address,
56    /// The sqrt price after the swap (Q64.96 format).
57    pub sqrt_price_x96: U160,
58    /// The amount of token0 involved in the swap.
59    pub amount0: I256,
60    /// The amount of token1 involved in the swap.
61    pub amount1: I256,
62    /// The liquidity of the pool after the swap occurred.
63    pub liquidity: u128,
64    /// The current tick of the pool after the swap occurred.
65    pub tick: i32,
66    /// The direction of the swap from the perspective of the base token.
67    pub side: Option<OrderSide>,
68    /// The amount of tokens swapped.
69    pub size: Option<Quantity>,
70    /// The exchange rate at which the swap occurred.
71    pub price: Option<Price>,
72    /// UNIX timestamp (nanoseconds) when the swap occurred.
73    pub timestamp: Option<UnixNanos>,
74    /// UNIX timestamp (nanoseconds) when the instance was initialized.
75    pub ts_init: Option<UnixNanos>,
76}
77
78impl PoolSwap {
79    /// Creates a new [`PoolSwap`] instance with the specified properties.
80    #[must_use]
81    #[allow(clippy::too_many_arguments)]
82    pub fn new(
83        chain: SharedChain,
84        dex: SharedDex,
85        instrument_id: InstrumentId,
86        pool_address: Address,
87        block: u64,
88        transaction_hash: String,
89        transaction_index: u32,
90        log_index: u32,
91        timestamp: Option<UnixNanos>,
92        sender: Address,
93        recipient: Address,
94        amount0: I256,
95        amount1: I256,
96        sqrt_price_x96: U160,
97        liquidity: u128,
98        tick: i32,
99        side: Option<OrderSide>,
100        size: Option<Quantity>,
101        price: Option<Price>,
102    ) -> Self {
103        Self {
104            chain,
105            dex,
106            instrument_id,
107            pool_address,
108            block,
109            transaction_hash,
110            transaction_index,
111            log_index,
112            timestamp,
113            sender,
114            recipient,
115            amount0,
116            amount1,
117            sqrt_price_x96,
118            liquidity,
119            tick,
120            side,
121            size,
122            price,
123            ts_init: timestamp, // TODO: Use swap timestamp as init timestamp for now
124        }
125    }
126}
127
128impl Display for PoolSwap {
129    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130        write!(
131            f,
132            "{}(instrument_id={})",
133            stringify!(PoolSwap),
134            self.instrument_id,
135        )
136    }
137}