nautilus_blockchain/events/
swap.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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 alloy::primitives::{Address, I256, U160};
17
18/// Represents a token swap event from liquidity pools emitted from smart contract.
19///
20/// This struct captures the essential data from a swap transaction on decentralized
21/// exchanges (DEXs) that use automated market maker (AMM) protocols.
22#[derive(Debug, Clone)]
23pub struct SwapEvent {
24    /// The block number in which this swap transaction was included.
25    pub block_number: u64,
26    /// The unique hash identifier of the transaction containing this event.
27    pub transaction_hash: String,
28    /// The position of this transaction within the block.
29    pub transaction_index: u32,
30    /// The position of this event log within the transaction.
31    pub log_index: u32,
32    /// The address that initiated the swap transaction.
33    pub sender: Address,
34    /// The address that received the swapped tokens.
35    pub receiver: Address,
36    /// The amount of token0 involved in the swap.
37    /// Negative values indicate tokens flowing out of the pool, positive values indicate tokens flowing in.
38    pub amount0: I256,
39    /// The amount of token1 involved in the swap.
40    /// Negative values indicate tokens flowing out of the pool, positive values indicate tokens flowing in.
41    pub amount1: I256,
42    /// The square root of the price ratio encoded as a Q64.96 fixed-point number.
43    /// This represents the price of token1 in terms of token0 after the swap.
44    pub sqrt_price_x96: U160,
45}
46
47impl SwapEvent {
48    /// Creates a new [`SwapEvent`] instance with the specified parameters.
49    #[must_use]
50    #[allow(clippy::too_many_arguments)]
51    pub const fn new(
52        block_number: u64,
53        transaction_hash: String,
54        transaction_index: u32,
55        log_index: u32,
56        sender: Address,
57        receiver: Address,
58        amount0: I256,
59        amount1: I256,
60        sqrt_price_x96: U160,
61    ) -> Self {
62        Self {
63            block_number,
64            transaction_hash,
65            transaction_index,
66            log_index,
67            sender,
68            receiver,
69            amount0,
70            amount1,
71            sqrt_price_x96,
72        }
73    }
74}