nautilus_blockchain/events/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 alloy::primitives::{Address, I256, U160};
17use nautilus_core::UnixNanos;
18use nautilus_model::{
19 defi::{PoolSwap, SharedChain, SharedDex},
20 enums::OrderSide,
21 identifiers::InstrumentId,
22 types::{Price, Quantity},
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 address of the smart contract which emitted the event.
34 pub pool_address: Address,
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 #[allow(clippy::too_many_arguments)]
66 pub const fn new(
67 dex: SharedDex,
68 pool_address: Address,
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_address,
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 #[allow(clippy::too_many_arguments)]
100 #[must_use]
101 pub fn to_pool_swap(
102 &self,
103 chain: SharedChain,
104 instrument_id: InstrumentId,
105 pool_address: Address,
106 normalized_side: Option<OrderSide>,
107 normalized_quantity: Option<Quantity>,
108 normalized_price: Option<Price>,
109 timestamp: Option<UnixNanos>,
110 ) -> PoolSwap {
111 PoolSwap::new(
112 chain,
113 self.dex.clone(),
114 instrument_id,
115 pool_address,
116 self.block_number,
117 self.transaction_hash.clone(),
118 self.transaction_index,
119 self.log_index,
120 timestamp,
121 self.sender,
122 self.receiver,
123 self.amount0,
124 self.amount1,
125 self.sqrt_price_x96,
126 self.liquidity,
127 self.tick,
128 normalized_side,
129 normalized_quantity,
130 normalized_price,
131 )
132 }
133}