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;
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.
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 direction of the swap from the perspective of the base token.
55 pub side: OrderSide,
56 /// The amount of tokens swapped.
57 pub size: Quantity,
58 /// The exchange rate at which the swap occurred.
59 pub price: Price,
60 /// UNIX timestamp (nanoseconds) when the swap occurred.
61 pub timestamp: Option<UnixNanos>,
62 /// UNIX timestamp (nanoseconds) when the instance was initialized.
63 pub ts_init: Option<UnixNanos>,
64}
65
66impl PoolSwap {
67 /// Creates a new [`PoolSwap`] instance with the specified properties.
68 #[must_use]
69 #[allow(clippy::too_many_arguments)]
70 pub fn new(
71 chain: SharedChain,
72 dex: SharedDex,
73 instrument_id: InstrumentId,
74 pool_address: Address,
75 block: u64,
76 transaction_hash: String,
77 transaction_index: u32,
78 log_index: u32,
79 timestamp: Option<UnixNanos>,
80 sender: Address,
81 side: OrderSide,
82 size: Quantity,
83 price: Price,
84 ) -> Self {
85 Self {
86 chain,
87 dex,
88 instrument_id,
89 pool_address,
90 block,
91 transaction_hash,
92 transaction_index,
93 log_index,
94 timestamp,
95 sender,
96 side,
97 size,
98 price,
99 ts_init: timestamp, // TODO: Use swap timestamp as init timestamp for now
100 }
101 }
102}
103
104impl Display for PoolSwap {
105 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106 write!(
107 f,
108 "{}(instrument_id={}, side={}, quantity={}, price={})",
109 stringify!(PoolSwap),
110 self.instrument_id,
111 self.side,
112 self.size,
113 self.price,
114 )
115 }
116}