Skip to content

Commit d0889f3

Browse files
committed
docs(core): add detailed SyncRequest output example
1 parent 5d715c2 commit d0889f3

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

crates/core/src/spk_client.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ impl<I, D> SyncRequestBuilder<I, D> {
225225
/// [`chain_tip`](SyncRequestBuilder::chain_tip) (if provided).
226226
///
227227
/// ```rust
228+
/// # use std::io::{self, Write};
228229
/// # use bdk_chain::{bitcoin::{hashes::Hash, ScriptBuf}, local_chain::LocalChain};
229230
/// # use bdk_chain::spk_client::SyncRequest;
230231
/// # let (local_chain, _) = LocalChain::from_genesis(Hash::all_zeros());
@@ -236,7 +237,22 @@ impl<I, D> SyncRequestBuilder<I, D> {
236237
/// // Provide list of scripts to scan for transactions against.
237238
/// .spks(scripts)
238239
/// // This is called for every synced item.
239-
/// .inspect(|item, progress| println!("{} (remaining: {})", item, progress.remaining()))
240+
/// .inspect(|item, progress| {
241+
/// let pc = (100.0 * progress.consumed() as f32) / progress.total() as f32;
242+
/// match item {
243+
/// // In this example I = (), so the first field of Spk is unit.
244+
/// bdk_chain::spk_client::SyncItem::Spk((), spk) => {
245+
/// eprintln!("[ SCANNING {pc:03.0}% ] script {}", spk);
246+
/// }
247+
/// bdk_chain::spk_client::SyncItem::Txid(txid) => {
248+
/// eprintln!("[ SCANNING {pc:03.0}% ] txid {}", txid);
249+
/// }
250+
/// bdk_chain::spk_client::SyncItem::OutPoint(op) => {
251+
/// eprintln!("[ SCANNING {pc:03.0}% ] outpoint {}", op);
252+
/// }
253+
/// }
254+
/// let _ = io::stderr().flush();
255+
/// })
240256
/// // Finish constructing the sync request.
241257
/// .build();
242258
/// ```

examples/example_electrum/src/main.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,22 @@ fn main() -> anyhow::Result<()> {
210210
.chain_tip(chain_tip.clone())
211211
.inspect(|item, progress| {
212212
let pc = (100 * progress.consumed()) as f32 / progress.total() as f32;
213-
eprintln!("[ SCANNING {pc:03.0}% ] {item}");
213+
match item {
214+
bdk_chain::spk_client::SyncItem::Spk((keychain, index), spk) => {
215+
eprintln!(
216+
"[ SCANNING {pc:3.0}% ] script {} {} {}",
217+
keychain, index, spk
218+
);
219+
}
220+
bdk_chain::spk_client::SyncItem::Txid(txid) => {
221+
eprintln!("[ SCANNING {pc:3.0}% ] txid {}", txid);
222+
}
223+
bdk_chain::spk_client::SyncItem::OutPoint(op) => {
224+
eprintln!("[ SCANNING {pc:3.0}% ] outpoint {}", op);
225+
}
226+
}
227+
let _ = io::stderr().flush();
214228
});
215-
216229
let canonical_view = graph.canonical_view(
217230
&*chain,
218231
chain_tip.block_id(),

examples/example_esplora/src/main.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,20 @@ fn main() -> anyhow::Result<()> {
215215
.chain_tip(local_tip.clone())
216216
.inspect(|item, progress| {
217217
let pc = (100 * progress.consumed()) as f32 / progress.total() as f32;
218-
eprintln!("[ SCANNING {pc:03.0}% ] {item}");
219-
// Flush early to ensure we print at every iteration.
218+
match item {
219+
bdk_chain::spk_client::SyncItem::Spk((keychain, index), spk) => {
220+
eprintln!(
221+
"[ SCANNING {pc:3.0}% ] script {} {} {}",
222+
keychain, index, spk
223+
);
224+
}
225+
bdk_chain::spk_client::SyncItem::Txid(txid) => {
226+
eprintln!("[ SCANNING {pc:3.0}% ] txid {}", txid);
227+
}
228+
bdk_chain::spk_client::SyncItem::OutPoint(op) => {
229+
eprintln!("[ SCANNING {pc:3.0}% ] outpoint {}", op);
230+
}
231+
}
220232
let _ = io::stderr().flush();
221233
});
222234

0 commit comments

Comments
 (0)