Skip to content

Commit 528fde7

Browse files
committed
Remove examples folder, add rustdoc examples
1 parent 1074a86 commit 528fde7

File tree

15 files changed

+162
-2088
lines changed

15 files changed

+162
-2088
lines changed

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ members = [
88
"crates/esplora",
99
"crates/bitcoind_rpc",
1010
"crates/testenv",
11-
"examples/example_cli",
12-
"examples/example_electrum",
13-
"examples/example_esplora",
14-
"examples/example_bitcoind_rpc_polling",
1511
]
1612

1713
[workspace.package]

crates/chain/src/indexed_tx_graph.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ where
7777
///
7878
/// The underlying `TxGraph` is initialized with `TxGraph::default()`, and the provided
7979
/// `index`er is used as‐is (since there are no existing transactions to process).
80+
///
81+
/// # Example
82+
///
83+
/// ```
84+
/// use bdk_chain::{IndexedTxGraph, keychain_txout::KeychainTxOutIndex};
85+
///
86+
/// let index = KeychainTxOutIndex::<&str>::new(10);
87+
/// let graph = IndexedTxGraph::new(index);
88+
/// ```
8089
pub fn new(index: I) -> Self {
8190
Self {
8291
index,
@@ -351,6 +360,18 @@ where
351360
///
352361
/// Relevancy is determined by the internal [`Indexer::is_tx_relevant`] implementation of `I`.
353362
/// Irrelevant transactions in `txs` will be ignored.
363+
///
364+
/// # Example
365+
///
366+
/// ```no_run
367+
/// use bdk_chain::{IndexedTxGraph, keychain_txout::KeychainTxOutIndex, BlockId};
368+
/// use bitcoin::Block;
369+
///
370+
/// let mut graph = IndexedTxGraph::<BlockId, _>::new(KeychainTxOutIndex::<&str>::new(10));
371+
/// # let block = Block { header: bitcoin::block::Header::from(bitcoin::constants::genesis_block(bitcoin::Network::Bitcoin).header), txdata: vec![] };
372+
///
373+
/// let changeset = graph.apply_block_relevant(&block, 100);
374+
/// ```
354375
pub fn apply_block_relevant(
355376
&mut self,
356377
block: &Block,

crates/chain/src/indexer/keychain_txout.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,22 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
453453
/// (one keychain just becomes the defacto owner of that spk arbitrarily) but this may have
454454
/// subtle implications up the application stack like one UTXO being missing from one keychain
455455
/// because it has been assigned to another which produces the same script pubkey.
456+
///
457+
/// # Example
458+
///
459+
/// ```
460+
/// use bdk_chain::keychain_txout::KeychainTxOutIndex;
461+
/// use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
462+
/// # use std::str::FromStr;
463+
///
464+
/// let mut index = KeychainTxOutIndex::<&str>::new(10);
465+
/// let desc = Descriptor::<DescriptorPublicKey>::from_str(
466+
/// "wpkh([d34db33f/84h/0h/0h]xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/0/*)"
467+
/// )?;
468+
///
469+
/// index.insert_descriptor("external", desc)?;
470+
/// # Ok::<_, Box<dyn std::error::Error>>(())
471+
/// ```
456472
pub fn insert_descriptor(
457473
&mut self,
458474
keychain: K,
@@ -834,6 +850,16 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
834850
/// 1. The descriptor has no wildcard and already has one script revealed.
835851
/// 2. The descriptor has already revealed scripts up to the numeric bound.
836852
/// 3. There is no descriptor associated with the given keychain.
853+
///
854+
/// # Example
855+
///
856+
/// ```
857+
/// use bdk_chain::keychain_txout::KeychainTxOutIndex;
858+
///
859+
/// let mut index = KeychainTxOutIndex::<&str>::new(10);
860+
/// let (idx, spk, changeset) = index.reveal_next_spk("external").unwrap();
861+
/// assert_eq!(idx.0, 0);
862+
/// ```
837863
pub fn reveal_next_spk(&mut self, keychain: K) -> Option<(Indexed<ScriptBuf>, ChangeSet)> {
838864
let mut changeset = ChangeSet::default();
839865
let indexed_spk = self._reveal_next_spk(&mut changeset, keychain)?;

crates/chain/src/tx_graph.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,24 @@ impl<A: Anchor> TxGraph<A> {
655655
/// * A smaller witness has precedence over a larger witness.
656656
/// * If the witness sizes are the same, we prioritize the two witnesses with lexicographical
657657
/// order.
658+
///
659+
/// # Example
660+
///
661+
/// ```
662+
/// use bdk_chain::tx_graph::TxGraph;
663+
/// use bitcoin::Transaction;
664+
///
665+
/// let mut graph = TxGraph::<()>::default();
666+
/// let tx = Transaction {
667+
/// version: bitcoin::transaction::Version::ONE,
668+
/// lock_time: bitcoin::locktime::absolute::LockTime::ZERO,
669+
/// input: vec![],
670+
/// output: vec![],
671+
/// };
672+
///
673+
/// let changeset = graph.insert_tx(tx.clone());
674+
/// assert_eq!(changeset.txs.len(), 1);
675+
/// ```
658676
pub fn insert_tx<T: Into<Arc<Transaction>>>(&mut self, tx: T) -> ChangeSet<A> {
659677
// This returns `Some` only if the merged tx is different to the `original_tx`.
660678
fn _merge_tx_witnesses(
@@ -1247,6 +1265,24 @@ impl<A: Anchor> TxGraph<A> {
12471265
///
12481266
/// This is the infallible version of [`try_filter_chain_unspents`].
12491267
///
1268+
/// # Example
1269+
///
1270+
/// ```
1271+
/// use bdk_chain::{tx_graph::TxGraph, BlockId, CanonicalizationParams};
1272+
/// use bdk_chain::local_chain::LocalChain;
1273+
/// use bitcoin::{OutPoint, TxOut, Amount};
1274+
/// use std::sync::Arc;
1275+
///
1276+
/// let mut graph = TxGraph::<BlockId>::default();
1277+
/// let chain = LocalChain::from_blocks([
1278+
/// (0, BlockId { height: 0, hash: bitcoin::constants::genesis_block(bitcoin::Network::Bitcoin).block_hash() })
1279+
/// ].into_iter().collect());
1280+
///
1281+
/// // Get unspent outputs
1282+
/// let outpoints = vec![(0, OutPoint::default())];
1283+
/// let utxos: Vec<_> = graph.filter_chain_unspents(&chain, chain.tip(), CanonicalizationParams::default(), outpoints).collect();
1284+
/// ```
1285+
///
12501286
/// [`try_filter_chain_unspents`]: Self::try_filter_chain_unspents
12511287
pub fn filter_chain_unspents<'a, C: ChainOracle<Error = Infallible> + 'a, OI: Clone + 'a>(
12521288
&'a self,
@@ -1315,6 +1351,22 @@ impl<A: Anchor> TxGraph<A> {
13151351
///
13161352
/// This is the infallible version of [`try_balance`].
13171353
///
1354+
/// # Example
1355+
///
1356+
/// ```
1357+
/// use bdk_chain::{tx_graph::TxGraph, Balance, BlockId, CanonicalizationParams};
1358+
/// use bdk_chain::local_chain::LocalChain;
1359+
/// use bitcoin::OutPoint;
1360+
///
1361+
/// let graph = TxGraph::<BlockId>::default();
1362+
/// let chain = LocalChain::from_blocks([
1363+
/// (0, BlockId { height: 0, hash: bitcoin::constants::genesis_block(bitcoin::Network::Bitcoin).block_hash() })
1364+
/// ].into_iter().collect());
1365+
///
1366+
/// let outpoints = vec![(0, OutPoint::default())];
1367+
/// let balance = graph.balance(&chain, chain.tip(), CanonicalizationParams::default(), outpoints, |_, _| true);
1368+
/// ```
1369+
///
13181370
/// [`try_balance`]: Self::try_balance
13191371
pub fn balance<C: ChainOracle<Error = Infallible>, OI: Clone>(
13201372
&self,

crates/electrum/src/bdk_electrum_client.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ pub struct BdkElectrumClient<E> {
2828

2929
impl<E: ElectrumApi> BdkElectrumClient<E> {
3030
/// Creates a new bdk client from a [`electrum_client::ElectrumApi`]
31+
///
32+
/// # Example
33+
///
34+
/// ```no_run
35+
/// use bdk_electrum::{BdkElectrumClient, electrum_client};
36+
///
37+
/// let client = electrum_client::Client::new("ssl://electrum.blockstream.info:50002")?;
38+
/// let bdk_client = BdkElectrumClient::new(client);
39+
/// # Ok::<_, electrum_client::Error>(())
40+
/// ```
3141
pub fn new(client: E) -> Self {
3242
Self {
3343
inner: client,
@@ -90,6 +100,23 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
90100
/// [`CalculateFeeError::MissingTxOut`] error if those `TxOut`s are not present in the
91101
/// transaction graph.
92102
///
103+
/// # Example
104+
///
105+
/// ```no_run
106+
/// use bdk_electrum::BdkElectrumClient;
107+
/// use bdk_core::spk_client::FullScanRequest;
108+
/// # use bdk_electrum::electrum_client;
109+
///
110+
/// # let client = electrum_client::Client::new("ssl://electrum.blockstream.info:50002")?;
111+
/// # let bdk_client = BdkElectrumClient::new(client);
112+
/// let request = FullScanRequest::<&str>::builder()
113+
/// .chain_tip(Default::default())
114+
/// .build();
115+
///
116+
/// let response = bdk_client.full_scan(request, 10, 50, false)?;
117+
/// # Ok::<_, electrum_client::Error>(())
118+
/// ```
119+
///
93120
/// [`bdk_chain`]: ../bdk_chain/index.html
94121
/// [`CalculateFeeError::MissingTxOut`]: ../bdk_chain/tx_graph/enum.CalculateFeeError.html#variant.MissingTxOut
95122
/// [`Wallet.calculate_fee`]: ../bdk_wallet/struct.Wallet.html#method.calculate_fee
@@ -173,6 +200,24 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
173200
/// If the scripts to sync are unknown, such as when restoring or importing a keychain that
174201
/// may include scripts that have been used, use [`full_scan`] with the keychain.
175202
///
203+
/// # Example
204+
///
205+
/// ```no_run
206+
/// use bdk_electrum::BdkElectrumClient;
207+
/// use bdk_core::spk_client::SyncRequest;
208+
/// use bdk_core::bitcoin::ScriptBuf;
209+
/// # use bdk_electrum::electrum_client;
210+
///
211+
/// # let client = electrum_client::Client::new("ssl://electrum.blockstream.info:50002")?;
212+
/// # let bdk_client = BdkElectrumClient::new(client);
213+
/// let request = SyncRequest::builder()
214+
/// .spks([ScriptBuf::new_op_return(&[0x00; 20])])
215+
/// .build();
216+
///
217+
/// let response = bdk_client.sync(request, 50, false)?;
218+
/// # Ok::<_, electrum_client::Error>(())
219+
/// ```
220+
///
176221
/// [`full_scan`]: Self::full_scan
177222
/// [`bdk_chain`]: ../bdk_chain/index.html
178223
/// [`CalculateFeeError::MissingTxOut`]: ../bdk_chain/tx_graph/enum.CalculateFeeError.html#variant.MissingTxOut

crates/esplora/src/blocking_ext.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ pub trait EsploraExt {
2626
/// `stop_gap` script pubkeys with no associated transactions. `parallel_requests` specifies
2727
/// the maximum number of HTTP requests to make in parallel.
2828
///
29+
/// # Example
30+
///
31+
/// ```no_run
32+
/// use bdk_esplora::{EsploraExt, esplora_client};
33+
/// use bdk_core::spk_client::FullScanRequest;
34+
/// use bdk_core::bitcoin::Network;
35+
///
36+
/// let client = esplora_client::Builder::new("https://blockstream.info/api")
37+
/// .build_blocking();
38+
///
39+
/// let request = FullScanRequest::<&str>::builder()
40+
/// .chain_tip(Default::default())
41+
/// .build();
42+
///
43+
/// let response = client.full_scan(request, 10, 5)?;
44+
/// # Ok::<_, Box<dyn std::error::Error>>(())
45+
/// ```
46+
///
2947
/// Refer to [crate-level docs](crate) for more.
3048
fn full_scan<K: Ord + Clone, R: Into<FullScanRequest<K>>>(
3149
&self,

examples/example_bitcoind_rpc_polling/Cargo.toml

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/example_bitcoind_rpc_polling/README.md

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)