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