breez_sdk_spark/chain/
mod.rs

1use serde::{Deserialize, Serialize};
2use thiserror::Error;
3
4pub mod rest_client;
5
6#[derive(Debug, Error, Clone)]
7#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
8pub enum ChainServiceError {
9    #[error("Invalid address: {0}")]
10    InvalidAddress(String),
11    #[error("Service connectivity: {0}")]
12    ServiceConnectivity(String),
13    #[error("Generic: {0}")]
14    Generic(String),
15}
16
17impl From<breez_sdk_common::error::ServiceConnectivityError> for ChainServiceError {
18    fn from(value: breez_sdk_common::error::ServiceConnectivityError) -> Self {
19        ChainServiceError::ServiceConnectivity(value.to_string())
20    }
21}
22
23impl From<bitcoin::address::ParseError> for ChainServiceError {
24    fn from(value: bitcoin::address::ParseError) -> Self {
25        ChainServiceError::InvalidAddress(value.to_string())
26    }
27}
28
29#[cfg_attr(feature = "uniffi", uniffi::export(with_foreign))]
30#[macros::async_trait]
31pub trait BitcoinChainService: Send + Sync {
32    async fn get_address_utxos(&self, address: String) -> Result<Vec<Utxo>, ChainServiceError>;
33    async fn get_transaction_status(&self, txid: String) -> Result<TxStatus, ChainServiceError>;
34    async fn get_transaction_hex(&self, txid: String) -> Result<String, ChainServiceError>;
35    async fn broadcast_transaction(&self, tx: String) -> Result<(), ChainServiceError>;
36    async fn recommended_fees(&self) -> Result<RecommendedFees, ChainServiceError>;
37}
38
39#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
40#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
41pub struct TxStatus {
42    pub confirmed: bool,
43    pub block_height: Option<u32>,
44    pub block_time: Option<u64>,
45}
46
47#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
48#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
49pub struct Utxo {
50    pub txid: String,
51    pub vout: u32,
52    pub value: u64,
53    pub status: TxStatus,
54}
55
56#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
57#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
58pub struct RecommendedFees {
59    pub fastest_fee: u64,
60    pub half_hour_fee: u64,
61    pub hour_fee: u64,
62    pub economy_fee: u64,
63    pub minimum_fee: u64,
64}