Skip to main content

nautilus_blockchain/events/
swap.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::sync::Arc;
17
18use alloy::primitives::{Address, I256, U160};
19use nautilus_core::UnixNanos;
20use nautilus_model::{
21    defi::{PoolIdentifier, PoolSwap, SharedChain, SharedDex},
22    identifiers::InstrumentId,
23};
24
25/// Represents a token swap event from liquidity pools emitted from smart contract.
26///
27/// This struct captures the essential data from a swap transaction on decentralized
28/// exchanges (DEXs) that use automated market maker (AMM) protocols.
29#[derive(Debug, Clone)]
30pub struct SwapEvent {
31    /// The decentralized exchange where the event happened.
32    pub dex: SharedDex,
33    /// The unique identifier for the pool.
34    pub pool_identifier: PoolIdentifier,
35    /// The block number in which this swap transaction was included.
36    pub block_number: u64,
37    /// The unique hash identifier of the transaction containing this event.
38    pub transaction_hash: String,
39    /// The position of this transaction within the block.
40    pub transaction_index: u32,
41    /// The position of this event log within the transaction.
42    pub log_index: u32,
43    /// The address that initiated the swap transaction.
44    pub sender: Address,
45    /// The address that received the swapped tokens.
46    pub receiver: Address,
47    /// The amount of token0 involved in the swap.
48    /// Negative values indicate tokens flowing out of the pool, positive values indicate tokens flowing in.
49    pub amount0: I256,
50    /// The amount of token1 involved in the swap.
51    /// Negative values indicate tokens flowing out of the pool, positive values indicate tokens flowing in.
52    pub amount1: I256,
53    /// The square root of the price ratio encoded as a Q64.96 fixed-point number.
54    /// This represents the price of token1 in terms of token0 after the swap.
55    pub sqrt_price_x96: U160,
56    /// The liquidity of the pool after the swap occurred.
57    pub liquidity: u128,
58    /// The current tick of the pool after the swap occurred.
59    pub tick: i32,
60}
61
62impl SwapEvent {
63    /// Creates a new [`SwapEvent`] instance with the specified parameters.
64    #[must_use]
65    #[expect(clippy::too_many_arguments)]
66    pub fn new(
67        dex: SharedDex,
68        pool_identifier: PoolIdentifier,
69        block_number: u64,
70        transaction_hash: String,
71        transaction_index: u32,
72        log_index: u32,
73        sender: Address,
74        receiver: Address,
75        amount0: I256,
76        amount1: I256,
77        sqrt_price_x96: U160,
78        liquidity: u128,
79        tick: i32,
80    ) -> Self {
81        Self {
82            dex,
83            pool_identifier,
84            block_number,
85            transaction_hash,
86            transaction_index,
87            log_index,
88            sender,
89            receiver,
90            amount0,
91            amount1,
92            sqrt_price_x96,
93            liquidity,
94            tick,
95        }
96    }
97
98    /// Converts a swap event into a `PoolSwap`.
99    #[must_use]
100    pub fn to_pool_swap(
101        &self,
102        chain: SharedChain,
103        instrument_id: InstrumentId,
104        pool_identifier: PoolIdentifier,
105        timestamp: UnixNanos,
106    ) -> PoolSwap {
107        PoolSwap::new(
108            chain,
109            Arc::clone(&self.dex),
110            instrument_id,
111            pool_identifier,
112            self.block_number,
113            self.transaction_hash.clone(),
114            self.transaction_index,
115            self.log_index,
116            timestamp, // ts_event
117            timestamp, // ts_init (same block timestamp)
118            self.sender,
119            self.receiver,
120            self.amount0,
121            self.amount1,
122            self.sqrt_price_x96,
123            self.liquidity,
124            self.tick,
125        )
126    }
127}