13
13
// limitations under the License.
14
14
// -------------------------------------------------------------------------------------------------
15
15
16
- use std:: collections:: HashMap ;
17
-
18
- use ahash:: { HashSet , HashSetExt } ;
16
+ use ahash:: { AHashMap , AHashSet } ;
19
17
use alloy:: primitives:: { Address , keccak256} ;
20
18
use nautilus_model:: defi:: DexType ;
21
19
@@ -25,30 +23,30 @@ use nautilus_model::defi::DexType;
25
23
/// and maintains the event signature encodings for efficient filtering.
26
24
#[ derive( Debug ) ]
27
25
pub struct DefiDataSubscriptionManager {
28
- subscribed_pool_swaps : HashMap < DexType , HashSet < Address > > ,
29
- pool_swap_event_encoded : HashMap < DexType , String > ,
30
- subscribed_pool_mints : HashMap < DexType , HashSet < Address > > ,
31
- pool_mint_event_encoded : HashMap < DexType , String > ,
32
- subscribed_pool_burns : HashMap < DexType , HashSet < Address > > ,
33
- pool_burn_event_encoded : HashMap < DexType , String > ,
26
+ subscribed_pool_swaps : AHashMap < DexType , AHashSet < Address > > ,
27
+ pool_swap_event_encoded : AHashMap < DexType , String > ,
28
+ subscribed_pool_mints : AHashMap < DexType , AHashSet < Address > > ,
29
+ pool_mint_event_encoded : AHashMap < DexType , String > ,
30
+ subscribed_pool_burns : AHashMap < DexType , AHashSet < Address > > ,
31
+ pool_burn_event_encoded : AHashMap < DexType , String > ,
34
32
}
35
33
36
34
impl DefiDataSubscriptionManager {
37
35
/// Creates a new [`DefiDataSubscriptionManager`] instance.
38
36
pub fn new ( ) -> Self {
39
37
Self {
40
- subscribed_pool_burns : HashMap :: new ( ) ,
41
- subscribed_pool_mints : HashMap :: new ( ) ,
42
- subscribed_pool_swaps : HashMap :: new ( ) ,
43
- pool_swap_event_encoded : HashMap :: new ( ) ,
44
- pool_burn_event_encoded : HashMap :: new ( ) ,
45
- pool_mint_event_encoded : HashMap :: new ( ) ,
38
+ subscribed_pool_burns : AHashMap :: new ( ) ,
39
+ subscribed_pool_mints : AHashMap :: new ( ) ,
40
+ subscribed_pool_swaps : AHashMap :: new ( ) ,
41
+ pool_swap_event_encoded : AHashMap :: new ( ) ,
42
+ pool_burn_event_encoded : AHashMap :: new ( ) ,
43
+ pool_mint_event_encoded : AHashMap :: new ( ) ,
46
44
}
47
45
}
48
46
49
47
/// Gets all unique contract addresses subscribed for any event type for a given DEX.
50
48
pub fn get_subscribed_dex_contract_addresses ( & self , dex : & DexType ) -> Vec < Address > {
51
- let mut unique_addresses = HashSet :: new ( ) ;
49
+ let mut unique_addresses = AHashSet :: new ( ) ;
52
50
53
51
if let Some ( addresses) = self . subscribed_pool_swaps . get ( dex) {
54
52
unique_addresses. extend ( addresses. iter ( ) . cloned ( ) ) ;
@@ -105,39 +103,39 @@ impl DefiDataSubscriptionManager {
105
103
mint_event_signature : & str ,
106
104
burn_event_signature : & str ,
107
105
) {
108
- self . subscribed_pool_swaps . insert ( dex, HashSet :: new ( ) ) ;
106
+ self . subscribed_pool_swaps . insert ( dex, AHashSet :: new ( ) ) ;
109
107
let swap_event_hash = keccak256 ( swap_event_signature. as_bytes ( ) ) ;
110
108
let encoded_swap_event = format ! (
111
109
"0x{encoded_hash}" ,
112
110
encoded_hash = hex:: encode( swap_event_hash)
113
111
) ;
114
112
self . pool_swap_event_encoded . insert ( dex, encoded_swap_event) ;
115
113
116
- self . subscribed_pool_mints . insert ( dex, HashSet :: new ( ) ) ;
114
+ self . subscribed_pool_mints . insert ( dex, AHashSet :: new ( ) ) ;
117
115
let mint_event_hash = keccak256 ( mint_event_signature. as_bytes ( ) ) ;
118
116
let encoded_mint_event = format ! (
119
117
"0x{encoded_hash}" ,
120
118
encoded_hash = hex:: encode( mint_event_hash)
121
119
) ;
122
120
self . pool_mint_event_encoded . insert ( dex, encoded_mint_event) ;
123
121
124
- self . subscribed_pool_burns . insert ( dex, HashSet :: new ( ) ) ;
122
+ self . subscribed_pool_burns . insert ( dex, AHashSet :: new ( ) ) ;
125
123
let burn_event_hash = keccak256 ( burn_event_signature. as_bytes ( ) ) ;
126
124
let encoded_burn_event = format ! (
127
125
"0x{encoded_hash}" ,
128
126
encoded_hash = hex:: encode( burn_event_hash)
129
127
) ;
130
128
self . pool_burn_event_encoded . insert ( dex, encoded_burn_event) ;
131
129
132
- tracing:: info!( "Registered dex for subscriptions: {:?}" , dex ) ;
130
+ tracing:: info!( "Registered DEX for subscriptions: {dex :?}" ) ;
133
131
}
134
132
135
133
/// Subscribes to swap events for a specific pool address on a DEX.
136
134
pub fn subscribe_swaps ( & mut self , dex : DexType , address : Address ) {
137
135
if let Some ( pool_set) = self . subscribed_pool_swaps . get_mut ( & dex) {
138
136
pool_set. insert ( address) ;
139
137
} else {
140
- tracing:: error!( "Dex not registered for swap subscriptions: {:?}" , dex ) ;
138
+ tracing:: error!( "DEX not registered for swap subscriptions: {dex :?}" ) ;
141
139
}
142
140
}
143
141
@@ -146,7 +144,7 @@ impl DefiDataSubscriptionManager {
146
144
if let Some ( pool_set) = self . subscribed_pool_mints . get_mut ( & dex) {
147
145
pool_set. insert ( address) ;
148
146
} else {
149
- tracing:: error!( "Dex not registered for mint subscriptions: {:?}" , dex ) ;
147
+ tracing:: error!( "DEX not registered for mint subscriptions: {dex :?}" ) ;
150
148
}
151
149
}
152
150
@@ -155,7 +153,7 @@ impl DefiDataSubscriptionManager {
155
153
if let Some ( pool_set) = self . subscribed_pool_burns . get_mut ( & dex) {
156
154
pool_set. insert ( address) ;
157
155
} else {
158
- tracing:: warn!( "Dex not registered for burn subscriptions: {:?}" , dex ) ;
156
+ tracing:: warn!( "DEX not registered for burn subscriptions: {dex :?}" ) ;
159
157
}
160
158
}
161
159
@@ -164,7 +162,7 @@ impl DefiDataSubscriptionManager {
164
162
if let Some ( pool_set) = self . subscribed_pool_swaps . get_mut ( & dex) {
165
163
pool_set. remove ( & address) ;
166
164
} else {
167
- tracing:: error!( "Dex not registered for swap subscriptions: {:?}" , dex ) ;
165
+ tracing:: error!( "DEX not registered for swap subscriptions: {dex :?}" ) ;
168
166
}
169
167
}
170
168
@@ -173,7 +171,7 @@ impl DefiDataSubscriptionManager {
173
171
if let Some ( pool_set) = self . subscribed_pool_mints . get_mut ( & dex) {
174
172
pool_set. remove ( & address) ;
175
173
} else {
176
- tracing:: error!( "Dex not registered for mint subscriptions: {:?}" , dex ) ;
174
+ tracing:: error!( "DEX not registered for mint subscriptions: {dex :?}" ) ;
177
175
}
178
176
}
179
177
@@ -182,11 +180,15 @@ impl DefiDataSubscriptionManager {
182
180
if let Some ( pool_set) = self . subscribed_pool_burns . get_mut ( & dex) {
183
181
pool_set. remove ( & address) ;
184
182
} else {
185
- tracing:: error!( "Dex not registered for burn subscriptions: {:?}" , dex ) ;
183
+ tracing:: error!( "DEX not registered for burn subscriptions: {dex :?}" ) ;
186
184
}
187
185
}
188
186
}
189
187
188
+ ////////////////////////////////////////////////////////////////////////////////
189
+ // Tests
190
+ ////////////////////////////////////////////////////////////////////////////////
191
+
190
192
#[ cfg( test) ]
191
193
mod tests {
192
194
use alloy:: primitives:: address;
0 commit comments