Skip to main content

ark_client/swap_storage/
memory.rs

1use super::SwapStorage;
2use crate::boltz::ReverseSwapData;
3use crate::boltz::SubmarineSwapData;
4use crate::boltz::SwapStatus;
5use crate::Error;
6use async_trait::async_trait;
7use std::collections::HashMap;
8use std::sync::Arc;
9use std::sync::Mutex;
10
11/// In-memory implementation of [`SwapStorage`].
12///
13/// This implementation stores swap data in memory using a [`HashMap`] protected by a [`Mutex`].
14/// Data is lost when the application restarts, making this suitable for development, testing,
15/// and scenarios where persistence is not required.
16pub struct InMemorySwapStorage {
17    submarine_swaps: Arc<Mutex<HashMap<String, SubmarineSwapData>>>,
18    reverse_swaps: Arc<Mutex<HashMap<String, ReverseSwapData>>>,
19}
20
21impl InMemorySwapStorage {
22    /// Create a new in-memory swap storage.
23    ///
24    /// # Examples
25    ///
26    /// ```rust
27    /// use ark_client::InMemorySwapStorage;
28    ///
29    /// let storage = InMemorySwapStorage::new();
30    /// ```
31    pub fn new() -> Self {
32        Self {
33            submarine_swaps: Arc::new(Mutex::new(HashMap::new())),
34            reverse_swaps: Arc::new(Mutex::new(HashMap::new())),
35        }
36    }
37}
38
39impl Default for InMemorySwapStorage {
40    fn default() -> Self {
41        Self::new()
42    }
43}
44
45#[async_trait]
46impl SwapStorage for InMemorySwapStorage {
47    async fn insert_submarine(&self, id: String, data: SubmarineSwapData) -> Result<(), Error> {
48        let mut swaps = self.submarine_swaps.lock().expect("lock");
49        swaps.insert(id, data);
50        Ok(())
51    }
52
53    async fn insert_reverse(&self, id: String, data: ReverseSwapData) -> Result<(), Error> {
54        let mut swaps = self.reverse_swaps.lock().expect("lock");
55        swaps.insert(id, data);
56        Ok(())
57    }
58
59    async fn get_submarine(&self, id: &str) -> Result<Option<SubmarineSwapData>, Error> {
60        let swaps = self.submarine_swaps.lock().expect("lock");
61        Ok(swaps.get(id).cloned())
62    }
63
64    async fn get_reverse(&self, id: &str) -> Result<Option<ReverseSwapData>, Error> {
65        let swaps = self.reverse_swaps.lock().expect("lock");
66        Ok(swaps.get(id).cloned())
67    }
68
69    async fn update_status_submarine(&self, id: &str, status: SwapStatus) -> Result<(), Error> {
70        let mut swaps = self.submarine_swaps.lock().expect("lock");
71        if let Some(swap) = swaps.get_mut(id) {
72            swap.status = status;
73            Ok(())
74        } else {
75            Err(Error::consumer(format!("swap not found: {id}")))
76        }
77    }
78
79    async fn update_status_reverse(&self, id: &str, status: SwapStatus) -> Result<(), Error> {
80        let mut swaps = self.reverse_swaps.lock().expect("lock");
81        if let Some(swap) = swaps.get_mut(id) {
82            swap.status = status;
83            Ok(())
84        } else {
85            Err(Error::consumer(format!("swap not found: {id}")))
86        }
87    }
88
89    async fn update_reverse(&self, id: &str, data: ReverseSwapData) -> Result<(), Error> {
90        let mut swaps = self.reverse_swaps.lock().expect("lock");
91        swaps.insert(id.to_string(), data);
92        Ok(())
93    }
94
95    async fn list_all_submarine(&self) -> Result<Vec<SubmarineSwapData>, Error> {
96        let swaps = self.submarine_swaps.lock().expect("lock");
97        Ok(swaps.values().cloned().collect())
98    }
99
100    async fn list_all_reverse(&self) -> Result<Vec<ReverseSwapData>, Error> {
101        let swaps = self.reverse_swaps.lock().expect("lock");
102
103        Ok(swaps.values().cloned().collect())
104    }
105
106    async fn remove_submarine(&self, id: &str) -> Result<Option<SubmarineSwapData>, Error> {
107        let mut swaps = self.submarine_swaps.lock().expect("lock");
108        Ok(swaps.remove(id))
109    }
110
111    async fn remove_reverse(&self, id: &str) -> Result<Option<ReverseSwapData>, Error> {
112        let mut swaps = self.reverse_swaps.lock().expect("lock");
113        Ok(swaps.remove(id))
114    }
115}