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