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, B256, 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 hash of the block containing this event, when the source log reported it.
38    pub block_hash: Option<B256>,
39    /// The unique hash identifier of the transaction containing this event.
40    pub transaction_hash: String,
41    /// The position of this transaction within the block.
42    pub transaction_index: u32,
43    /// The position of this event log within the transaction.
44    pub log_index: u32,
45    /// The address that initiated the swap transaction.
46    pub sender: Address,
47    /// The address that received the swapped tokens.
48    pub receiver: Address,
49    /// The amount of token0 involved in the swap.
50    /// Negative values indicate tokens flowing out of the pool, positive values indicate tokens flowing in.
51    pub amount0: I256,
52    /// The amount of token1 involved in the swap.
53    /// Negative values indicate tokens flowing out of the pool, positive values indicate tokens flowing in.
54    pub amount1: I256,
55    /// The square root of the price ratio encoded as a Q64.96 fixed-point number.
56    /// This represents the price of token1 in terms of token0 after the swap.
57    pub sqrt_price_x96: U160,
58    /// The liquidity of the pool after the swap occurred.
59    pub liquidity: u128,
60    /// The current tick of the pool after the swap occurred.
61    pub tick: i32,
62}
63
64impl SwapEvent {
65    /// Creates a new [`SwapEvent`] instance with the specified parameters.
66    #[must_use]
67    #[expect(clippy::too_many_arguments)]
68    pub fn new(
69        dex: SharedDex,
70        pool_identifier: PoolIdentifier,
71        block_number: u64,
72        block_hash: Option<B256>,
73        transaction_hash: String,
74        transaction_index: u32,
75        log_index: u32,
76        sender: Address,
77        receiver: Address,
78        amount0: I256,
79        amount1: I256,
80        sqrt_price_x96: U160,
81        liquidity: u128,
82        tick: i32,
83    ) -> Self {
84        Self {
85            dex,
86            pool_identifier,
87            block_number,
88            block_hash,
89            transaction_hash,
90            transaction_index,
91            log_index,
92            sender,
93            receiver,
94            amount0,
95            amount1,
96            sqrt_price_x96,
97            liquidity,
98            tick,
99        }
100    }
101
102    /// Converts a swap event into a `PoolSwap`.
103    #[must_use]
104    pub fn to_pool_swap(
105        &self,
106        chain: SharedChain,
107        instrument_id: InstrumentId,
108        pool_identifier: PoolIdentifier,
109        timestamp: UnixNanos,
110    ) -> PoolSwap {
111        PoolSwap::new(
112            chain,
113            Arc::clone(&self.dex),
114            instrument_id,
115            pool_identifier,
116            self.block_number,
117            self.transaction_hash.clone(),
118            self.transaction_index,
119            self.log_index,
120            timestamp, // ts_event
121            timestamp, // ts_init (same block timestamp)
122            self.sender,
123            self.receiver,
124            self.amount0,
125            self.amount1,
126            self.sqrt_price_x96,
127            self.liquidity,
128            self.tick,
129        )
130    }
131}