@@ -19,12 +19,16 @@ use bdk_electrum::electrum_client::ElectrumApi;
1919use std:: collections:: BTreeMap ;
2020use std:: sync:: Arc ;
2121
22- // NOTE: We are keeping our naming convention where the alias of the inner type is the Rust type
23- // prefixed with `Bdk`. In this case the inner type is `BdkElectrumClient`, so the alias is
24- // funnily enough named `BdkBdkElectrumClient`.
22+ /// Wrapper around an electrum_client::ElectrumApi which includes an internal in-memory transaction
23+ /// cache to avoid re-fetching already downloaded transactions.
24+ # [ derive ( uniffi :: Object ) ]
2525pub struct ElectrumClient ( BdkBdkElectrumClient < bdk_electrum:: electrum_client:: Client > ) ;
2626
27+ #[ uniffi:: export]
2728impl ElectrumClient {
29+ /// Creates a new bdk client from a electrum_client::ElectrumApi
30+ /// Optional: Set the proxy of the builder
31+ #[ uniffi:: constructor( default ( socks5 = None ) ) ]
2832 pub fn new ( url : String , socks5 : Option < String > ) -> Result < Self , ElectrumError > {
2933 let mut config = bdk_electrum:: electrum_client:: ConfigBuilder :: new ( ) ;
3034 if let Some ( socks5) = socks5 {
@@ -38,6 +42,21 @@ impl ElectrumClient {
3842 Ok ( Self ( client) )
3943 }
4044
45+ /// Full scan the keychain scripts specified with the blockchain (via an Electrum client) and
46+ /// returns updates for bdk_chain data structures.
47+ ///
48+ /// - `request`: struct with data required to perform a spk-based blockchain client
49+ /// full scan, see `FullScanRequest`.
50+ /// - `stop_gap`: the full scan for each keychain stops after a gap of script pubkeys with no
51+ /// associated transactions.
52+ /// - `batch_size`: specifies the max number of script pubkeys to request for in a single batch
53+ /// request.
54+ /// - `fetch_prev_txouts`: specifies whether we want previous `TxOuts` for fee calculation. Note
55+ /// that this requires additional calls to the Electrum server, but is necessary for
56+ /// calculating the fee on a transaction if your wallet does not own the inputs. Methods like
57+ /// `Wallet.calculate_fee` and `Wallet.calculate_fee_rate` will return a
58+ /// `CalculateFeeError::MissingTxOut` error if those TxOuts are not present in the transaction
59+ /// graph.
4160 pub fn full_scan (
4261 & self ,
4362 request : Arc < FullScanRequest > ,
@@ -69,6 +88,21 @@ impl ElectrumClient {
6988 Ok ( Arc :: new ( Update ( update) ) )
7089 }
7190
91+ /// Sync a set of scripts with the blockchain (via an Electrum client) for the data specified and returns updates for bdk_chain data structures.
92+ ///
93+ /// - `request`: struct with data required to perform a spk-based blockchain client
94+ /// sync, see `SyncRequest`.
95+ /// - `batch_size`: specifies the max number of script pubkeys to request for in a single batch
96+ /// request.
97+ /// - `fetch_prev_txouts`: specifies whether we want previous `TxOuts` for fee calculation. Note
98+ /// that this requires additional calls to the Electrum server, but is necessary for
99+ /// calculating the fee on a transaction if your wallet does not own the inputs. Methods like
100+ /// `Wallet.calculate_fee` and `Wallet.calculate_fee_rate` will return a
101+ /// `CalculateFeeError::MissingTxOut` error if those TxOuts are not present in the transaction
102+ /// graph.
103+ ///
104+ /// If the scripts to sync are unknown, such as when restoring or importing a keychain that may
105+ /// include scripts that have been used, use full_scan with the keychain.
72106 pub fn sync (
73107 & self ,
74108 request : Arc < SyncRequest > ,
@@ -96,6 +130,7 @@ impl ElectrumClient {
96130 Ok ( Arc :: new ( Update ( update) ) )
97131 }
98132
133+ /// Broadcasts a transaction to the network.
99134 pub fn transaction_broadcast ( & self , tx : & Transaction ) -> Result < String , ElectrumError > {
100135 let bdk_transaction: BdkTransaction = tx. into ( ) ;
101136 self . 0
@@ -104,6 +139,7 @@ impl ElectrumClient {
104139 . map ( |txid| txid. to_string ( ) )
105140 }
106141
142+ /// Returns the capabilities of the server.
107143 pub fn server_features ( & self ) -> Result < ServerFeaturesRes , ElectrumError > {
108144 self . 0
109145 . inner
@@ -112,13 +148,15 @@ impl ElectrumClient {
112148 . map ( ServerFeaturesRes :: from)
113149 }
114150
151+ /// Estimates the fee required in bitcoin per kilobyte to confirm a transaction in `number` blocks.
115152 pub fn estimate_fee ( & self , number : u64 ) -> Result < f64 , ElectrumError > {
116153 self . 0
117154 . inner
118155 . estimate_fee ( number as usize )
119156 . map_err ( ElectrumError :: from)
120157 }
121158
159+ /// Subscribes to notifications for new block headers, by sending a blockchain.headers.subscribe call.
122160 pub fn block_headers_subscribe ( & self ) -> Result < HeaderNotification , ElectrumError > {
123161 self . 0
124162 . inner
@@ -127,17 +165,26 @@ impl ElectrumClient {
127165 . map ( HeaderNotification :: from)
128166 }
129167
168+ /// Pings the server.
130169 pub fn ping ( & self ) -> Result < ( ) , ElectrumError > {
131170 self . 0 . inner . ping ( ) . map_err ( ElectrumError :: from)
132171 }
133172}
134173
174+ /// Response to an ElectrumClient.server_features request.
175+ #[ derive( uniffi:: Record ) ]
135176pub struct ServerFeaturesRes {
177+ /// Server version reported.
136178 pub server_version : String ,
179+ /// Hash of the genesis block.
137180 pub genesis_hash : String ,
181+ /// Minimum supported version of the protocol.
138182 pub protocol_min : String ,
183+ /// Maximum supported version of the protocol.
139184 pub protocol_max : String ,
185+ /// Hash function used to create the `ScriptHash`.
140186 pub hash_function : Option < String > ,
187+ /// Pruned height of the server.
141188 pub pruning : Option < i64 > ,
142189}
143190
@@ -154,8 +201,12 @@ impl From<BdkServerFeaturesRes> for ServerFeaturesRes {
154201 }
155202}
156203
204+ /// Notification of a new block header.
205+ #[ derive( uniffi:: Record ) ]
157206pub struct HeaderNotification {
207+ /// New block height.
158208 pub height : u64 ,
209+ /// Newly added header.
159210 pub header : Header ,
160211}
161212
0 commit comments