breez_sdk_core/
buy.rs

1use std::sync::Arc;
2
3use anyhow::Result;
4use sdk_common::prelude::{BreezServer, BuyBitcoinProviderApi, MoonpayProvider};
5
6use crate::{BuyBitcoinProvider, SwapInfo};
7
8#[tonic::async_trait]
9pub(crate) trait BuyBitcoinApi: Send + Sync {
10    /// Initiate buying Bitcoin and return a URL to the selected third party provider
11    async fn buy_bitcoin(
12        &self,
13        provider: BuyBitcoinProvider,
14        swap_info: &SwapInfo,
15        redirect_url: Option<String>,
16    ) -> Result<String>;
17}
18
19pub(crate) struct BuyBitcoinService {
20    moonpay_provider: Arc<dyn BuyBitcoinProviderApi>,
21}
22
23impl BuyBitcoinService {
24    pub fn new(breez_server: Arc<BreezServer>) -> Self {
25        let moonpay_provider = Arc::new(MoonpayProvider::new(breez_server));
26        Self { moonpay_provider }
27    }
28}
29
30#[tonic::async_trait]
31impl BuyBitcoinApi for BuyBitcoinService {
32    async fn buy_bitcoin(
33        &self,
34        provider: BuyBitcoinProvider,
35        swap_info: &SwapInfo,
36        redirect_url: Option<String>,
37    ) -> Result<String> {
38        match provider {
39            BuyBitcoinProvider::Moonpay => {
40                self.moonpay_provider
41                    .buy_bitcoin(
42                        swap_info.bitcoin_address.clone(),
43                        None,
44                        Some(swap_info.max_allowed_deposit as u64),
45                        redirect_url,
46                    )
47                    .await
48            }
49        }
50    }
51}