Skip to main content

nautilus_common/defi/
switchboard.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
16//! DeFi-specific switchboard functionality.
17
18use ahash::AHashMap;
19use nautilus_model::{defi::Blockchain, identifiers::InstrumentId};
20
21use crate::msgbus::{MStr, MessagingSwitchboard, Topic, get_message_bus};
22
23/// DeFi-specific switchboard state.
24#[derive(Clone, Debug, Default)]
25pub(crate) struct DefiSwitchboard {
26    pub(crate) block_topics: AHashMap<Blockchain, MStr<Topic>>,
27    pub(crate) pool_topics: AHashMap<InstrumentId, MStr<Topic>>,
28    pub(crate) pool_swap_topics: AHashMap<InstrumentId, MStr<Topic>>,
29    pub(crate) pool_liquidity_topics: AHashMap<InstrumentId, MStr<Topic>>,
30    pub(crate) pool_collect_topics: AHashMap<InstrumentId, MStr<Topic>>,
31    pub(crate) pool_flash_topics: AHashMap<InstrumentId, MStr<Topic>>,
32}
33
34#[must_use]
35pub fn get_defi_blocks_topic(chain: Blockchain) -> MStr<Topic> {
36    get_message_bus()
37        .borrow_mut()
38        .switchboard
39        .get_defi_blocks_topic(chain)
40}
41
42#[must_use]
43pub fn get_defi_pool_topic(instrument_id: InstrumentId) -> MStr<Topic> {
44    get_message_bus()
45        .borrow_mut()
46        .switchboard
47        .get_defi_pool_topic(instrument_id)
48}
49
50#[must_use]
51pub fn get_defi_pool_swaps_topic(instrument_id: InstrumentId) -> MStr<Topic> {
52    get_message_bus()
53        .borrow_mut()
54        .switchboard
55        .get_defi_pool_swaps_topic(instrument_id)
56}
57
58#[must_use]
59pub fn get_defi_liquidity_topic(instrument_id: InstrumentId) -> MStr<Topic> {
60    get_message_bus()
61        .borrow_mut()
62        .switchboard
63        .get_defi_pool_liquidity_topic(instrument_id)
64}
65
66#[must_use]
67pub fn get_defi_collect_topic(instrument_id: InstrumentId) -> MStr<Topic> {
68    get_message_bus()
69        .borrow_mut()
70        .switchboard
71        .get_defi_pool_collect_topic(instrument_id)
72}
73
74#[must_use]
75pub fn get_defi_flash_topic(instrument_id: InstrumentId) -> MStr<Topic> {
76    get_message_bus()
77        .borrow_mut()
78        .switchboard
79        .get_defi_pool_flash_topic(instrument_id)
80}
81
82impl MessagingSwitchboard {
83    #[must_use]
84    pub fn get_defi_blocks_topic(&mut self, chain: Blockchain) -> MStr<Topic> {
85        *self
86            .defi
87            .block_topics
88            .entry(chain)
89            .or_insert_with(|| format!("data.defi.blocks.{chain}").into())
90    }
91
92    #[must_use]
93    pub fn get_defi_pool_topic(&mut self, instrument_id: InstrumentId) -> MStr<Topic> {
94        *self
95            .defi
96            .pool_topics
97            .entry(instrument_id)
98            .or_insert_with(|| format!("data.defi.pool.{instrument_id}").into())
99    }
100
101    #[must_use]
102    pub fn get_defi_pool_swaps_topic(&mut self, instrument_id: InstrumentId) -> MStr<Topic> {
103        *self
104            .defi
105            .pool_swap_topics
106            .entry(instrument_id)
107            .or_insert_with(|| format!("data.defi.pool_swaps.{instrument_id}").into())
108    }
109
110    #[must_use]
111    pub fn get_defi_pool_liquidity_topic(&mut self, instrument_id: InstrumentId) -> MStr<Topic> {
112        *self
113            .defi
114            .pool_liquidity_topics
115            .entry(instrument_id)
116            .or_insert_with(|| format!("data.defi.pool_liquidity.{instrument_id}").into())
117    }
118
119    #[must_use]
120    pub fn get_defi_pool_collect_topic(&mut self, instrument_id: InstrumentId) -> MStr<Topic> {
121        *self
122            .defi
123            .pool_collect_topics
124            .entry(instrument_id)
125            .or_insert_with(|| format!("data.defi.pool_collect.{instrument_id}").into())
126    }
127
128    #[must_use]
129    pub fn get_defi_pool_flash_topic(&mut self, instrument_id: InstrumentId) -> MStr<Topic> {
130        *self
131            .defi
132            .pool_flash_topics
133            .entry(instrument_id)
134            .or_insert_with(|| format!("data.defi.pool_flash.{instrument_id}").into())
135    }
136}