Skip to content

Commit 1e6b283

Browse files
committed
refactor: use macros for ElectrumClient
1 parent c7534a8 commit 1e6b283

File tree

4 files changed

+54
-109
lines changed

4 files changed

+54
-109
lines changed

bdk-ffi/Untitled

Lines changed: 0 additions & 1 deletion
This file was deleted.

bdk-ffi/src/bdk.udl

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,6 @@ dictionary SignOptions {
466466
// bdk_wallet crate - wallet module
467467
// ------------------------------------------------------------------------
468468

469-
typedef interface Update;
470-
471469
interface Policy {
472470
string id();
473471

@@ -622,105 +620,6 @@ interface Descriptor {
622620
sequence<Descriptor> to_single_descriptors();
623621
};
624622

625-
// ------------------------------------------------------------------------
626-
// bdk_electrum crate
627-
// ------------------------------------------------------------------------
628-
629-
/// Wrapper around an electrum_client::ElectrumApi which includes an internal in-memory transaction
630-
/// cache to avoid re-fetching already downloaded transactions.
631-
interface ElectrumClient {
632-
/// Creates a new bdk client from a electrum_client::ElectrumApi
633-
/// Optional: Set the proxy of the builder
634-
[Throws=ElectrumError]
635-
constructor(string url, optional string? socks5 = null);
636-
637-
/// Full scan the keychain scripts specified with the blockchain (via an Electrum client) and
638-
/// returns updates for bdk_chain data structures.
639-
///
640-
/// - `request`: struct with data required to perform a spk-based blockchain client
641-
/// full scan, see `FullScanRequest`.
642-
/// - `stop_gap`: the full scan for each keychain stops after a gap of script pubkeys with no
643-
/// associated transactions.
644-
/// - `batch_size`: specifies the max number of script pubkeys to request for in a single batch
645-
/// request.
646-
/// - `fetch_prev_txouts`: specifies whether we want previous `TxOuts` for fee calculation. Note
647-
/// that this requires additional calls to the Electrum server, but is necessary for
648-
/// calculating the fee on a transaction if your wallet does not own the inputs. Methods like
649-
/// `Wallet.calculate_fee` and `Wallet.calculate_fee_rate` will return a
650-
/// `CalculateFeeError::MissingTxOut` error if those TxOuts are not present in the transaction
651-
/// graph.
652-
[Throws=ElectrumError]
653-
Update full_scan(FullScanRequest request, u64 stop_gap, u64 batch_size, boolean fetch_prev_txouts);
654-
655-
/// Sync a set of scripts with the blockchain (via an Electrum client) for the data specified and returns updates for bdk_chain data structures.
656-
///
657-
/// - `request`: struct with data required to perform a spk-based blockchain client
658-
/// sync, see `SyncRequest`.
659-
/// - `batch_size`: specifies the max number of script pubkeys to request for in a single batch
660-
/// request.
661-
/// - `fetch_prev_txouts`: specifies whether we want previous `TxOuts` for fee calculation. Note
662-
/// that this requires additional calls to the Electrum server, but is necessary for
663-
/// calculating the fee on a transaction if your wallet does not own the inputs. Methods like
664-
/// `Wallet.calculate_fee` and `Wallet.calculate_fee_rate` will return a
665-
/// `CalculateFeeError::MissingTxOut` error if those TxOuts are not present in the transaction
666-
/// graph.
667-
///
668-
/// If the scripts to sync are unknown, such as when restoring or importing a keychain that may
669-
/// include scripts that have been used, use full_scan with the keychain.
670-
[Throws=ElectrumError]
671-
Update sync(SyncRequest request, u64 batch_size, boolean fetch_prev_txouts);
672-
673-
/// Broadcasts a transaction to the network.
674-
[Throws=ElectrumError]
675-
string transaction_broadcast([ByRef] Transaction tx);
676-
677-
/// Returns the capabilities of the server.
678-
[Throws=ElectrumError]
679-
ServerFeaturesRes server_features();
680-
681-
/// Estimates the fee required in bitcoin per kilobyte to confirm a transaction in `number` blocks.
682-
[Throws=ElectrumError]
683-
f64 estimate_fee(u64 number);
684-
685-
/// Subscribes to notifications for new block headers, by sending a blockchain.headers.subscribe call.
686-
[Throws=ElectrumError]
687-
HeaderNotification block_headers_subscribe();
688-
689-
/// Pings the server.
690-
[Throws=ElectrumError]
691-
void ping();
692-
};
693-
694-
/// Response to an ElectrumClient.server_features request.
695-
dictionary ServerFeaturesRes {
696-
/// Server version reported.
697-
string server_version;
698-
699-
/// Hash of the genesis block.
700-
string genesis_hash;
701-
702-
/// Minimum supported version of the protocol.
703-
string protocol_min;
704-
705-
/// Maximum supported version of the protocol.
706-
string protocol_max;
707-
708-
/// Hash function used to create the `ScriptHash`.
709-
string? hash_function;
710-
711-
/// Pruned height of the server.
712-
i64? pruning;
713-
};
714-
715-
/// Notification of a new block header.
716-
dictionary HeaderNotification {
717-
/// New block height.
718-
u64 height;
719-
720-
/// Newly added header.
721-
Header header;
722-
};
723-
724623
// ------------------------------------------------------------------------
725624
// bdk-ffi-defined types
726625
// ------------------------------------------------------------------------

bdk-ffi/src/electrum.rs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ use bdk_electrum::electrum_client::ElectrumApi;
1919
use std::collections::BTreeMap;
2020
use 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)]
2525
pub struct ElectrumClient(BdkBdkElectrumClient<bdk_electrum::electrum_client::Client>);
2626

27+
#[uniffi::export]
2728
impl 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)]
135176
pub 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)]
157206
pub struct HeaderNotification {
207+
/// New block height.
158208
pub height: u64,
209+
/// Newly added header.
159210
pub header: Header,
160211
}
161212

bdk-ffi/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ use crate::bitcoin::TxIn;
2323
use crate::bitcoin::TxOut;
2424
use crate::bitcoin::WitnessProgram;
2525
use crate::descriptor::Descriptor;
26-
use crate::electrum::ElectrumClient;
27-
use crate::electrum::HeaderNotification;
28-
use crate::electrum::ServerFeaturesRes;
2926
use crate::error::AddressParseError;
3027
use crate::error::Bip32Error;
3128
use crate::error::Bip39Error;
@@ -73,7 +70,6 @@ use crate::types::SyncRequestBuilder;
7370
use crate::types::SyncScriptInspector;
7471
use crate::types::Tx;
7572
use crate::types::TxStatus;
76-
use crate::types::Update;
7773

7874
use bdk_wallet::bitcoin::Network;
7975
use bdk_wallet::keys::bip39::WordCount;

0 commit comments

Comments
 (0)