breez_sdk_spark/token_conversion/mod.rs
1mod error;
2mod flashnet;
3mod middleware;
4mod models;
5
6pub use error::ConversionError;
7pub(crate) use flashnet::FlashnetTokenConverter;
8pub(crate) use middleware::TokenConversionMiddleware;
9pub use models::*;
10
11use spark_wallet::TransferId;
12
13/// Trait for conversion implementations.
14///
15/// This trait abstracts the conversion mechanics, allowing different
16/// implementations (e.g., Flashnet) to be used interchangeably.
17/// Business logic for when/how much to convert is handled by `StableBalance`.
18#[macros::async_trait]
19pub(crate) trait TokenConverter: Send + Sync {
20 /// Execute a conversion swap.
21 ///
22 /// # Arguments
23 /// * `options` - The conversion options including type and slippage
24 /// * `purpose` - The purpose of the conversion
25 /// * `token_identifier` - Optional token identifier for `FromBitcoin` conversions
26 /// * `amount` - Either the minimum output amount or exact input amount
27 /// * `transfer_id` - Optional transfer ID for idempotency
28 async fn convert(
29 &self,
30 options: &ConversionOptions,
31 purpose: &ConversionPurpose,
32 token_identifier: Option<&String>,
33 amount: ConversionAmount,
34 transfer_id: Option<TransferId>,
35 ) -> Result<TokenConversionResponse, ConversionError>;
36
37 /// Validate a conversion and return the estimated conversion.
38 ///
39 /// Called during `prepare_send_payment` to calculate the conversion fee,
40 /// and during auto-conversion to estimate the token output.
41 ///
42 /// # Arguments
43 /// * `options` - The conversion options to validate
44 /// * `token_identifier` - Optional token identifier for `FromBitcoin` conversions
45 /// * `amount` - Either the minimum output amount or exact input amount
46 ///
47 /// # Returns
48 /// The estimated conversion including amount and fee, or None if options is None.
49 /// `estimate.amount_in` is the input amount, `estimate.amount_out` is the estimated output.
50 async fn validate(
51 &self,
52 options: Option<&ConversionOptions>,
53 token_identifier: Option<&String>,
54 amount: ConversionAmount,
55 ) -> Result<Option<ConversionEstimate>, ConversionError>;
56
57 /// Fetch conversion limits for a given conversion type.
58 ///
59 /// # Arguments
60 /// * `request` - The request containing conversion type and optional token identifier
61 async fn fetch_limits(
62 &self,
63 request: &FetchConversionLimitsRequest,
64 ) -> Result<FetchConversionLimitsResponse, ConversionError>;
65}