]> Untitled Git - bdk/commitdiff
refactor: Rename `wallet_esplora` to...
authorDaniela Brozzoni <danielabrozzoni@protonmail.com>
Tue, 22 Aug 2023 12:03:47 +0000 (14:03 +0200)
committerDaniela Brozzoni <danielabrozzoni@protonmail.com>
Wed, 23 Aug 2023 14:02:44 +0000 (16:02 +0200)
...`wallet_esplora_blocking`

Cargo.toml
README.md
example-crates/wallet_esplora/Cargo.toml [deleted file]
example-crates/wallet_esplora/src/main.rs [deleted file]
example-crates/wallet_esplora_blocking/Cargo.toml [new file with mode: 0644]
example-crates/wallet_esplora_blocking/src/main.rs [new file with mode: 0644]

index c5f2692dabd75c6d2a19fb5970ed44017a937629..9fafb8b78c364da598f1ec3c5c3e7e56733e50bf 100644 (file)
@@ -8,7 +8,7 @@ members = [
     "example-crates/example_cli",
     "example-crates/example_electrum",
     "example-crates/wallet_electrum",
-    "example-crates/wallet_esplora",
+    "example-crates/wallet_esplora_blocking",
     "example-crates/wallet_esplora_async",
     "nursery/tmp_plan",
     "nursery/coin_select"
index 4edb716df7ff8f5fb59e40a2accc2f059abc7add..ae230abbde66510a3ca568a9658533f1fddfab60 100644 (file)
--- a/README.md
+++ b/README.md
@@ -48,7 +48,7 @@ The project is split up into several crates in the `/crates` directory:
 Fully working examples of how to use these components are in `/example-crates`:
 - [`example_cli`](./example-crates/example_cli): Library used by the `example_*` crates. Provides utilities for syncing, showing the balance, generating addresses and creating transactions without using the bdk `Wallet`.
 - [`example_electrum`](./example-crates/example_electrum): A command line Bitcoin wallet application built on top of `example_cli` and the `electrum` crate. It shows the power of the bdk tools (`chain` + `file_store` + `electrum`), without depending on the main `bdk` library.
-- [`wallet_esplora`](./example-crates/wallet_esplora): Uses the `Wallet` to sync and spend using the Esplora blocking interface.
+- [`wallet_esplora_blocking`](./example-crates/wallet_esplora_blocking): Uses the `Wallet` to sync and spend using the Esplora blocking interface.
 - [`wallet_esplora_async`](./example-crates/wallet_esplora_async): Uses the `Wallet` to sync and spend using the Esplora asynchronous interface.
 - [`wallet_electrum`](./example-crates/wallet_electrum): Uses the `Wallet` to sync and spend using Electrum.
 
diff --git a/example-crates/wallet_esplora/Cargo.toml b/example-crates/wallet_esplora/Cargo.toml
deleted file mode 100644 (file)
index 1dcef38..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-[package]
-name = "wallet_esplora"
-version = "0.2.0"
-edition = "2021"
-publish = false
-
-# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
-
-[dependencies]
-bdk = { path = "../../crates/bdk" }
-bdk_esplora = { path = "../../crates/esplora", features = ["blocking"] }
-bdk_file_store = { path = "../../crates/file_store" }
diff --git a/example-crates/wallet_esplora/src/main.rs b/example-crates/wallet_esplora/src/main.rs
deleted file mode 100644 (file)
index a9691f3..0000000
+++ /dev/null
@@ -1,99 +0,0 @@
-const DB_MAGIC: &str = "bdk_wallet_esplora_example";
-const SEND_AMOUNT: u64 = 1000;
-const STOP_GAP: usize = 5;
-const PARALLEL_REQUESTS: usize = 1;
-
-use std::{io::Write, str::FromStr};
-
-use bdk::{
-    bitcoin::{Address, Network},
-    chain::keychain::WalletUpdate,
-    wallet::AddressIndex,
-    SignOptions, Wallet,
-};
-use bdk_esplora::{esplora_client, EsploraExt};
-use bdk_file_store::Store;
-
-fn main() -> Result<(), Box<dyn std::error::Error>> {
-    let db_path = std::env::temp_dir().join("bdk-esplora-example");
-    let db = Store::<bdk::wallet::ChangeSet>::new_from_path(DB_MAGIC.as_bytes(), db_path)?;
-    let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/0/*)";
-    let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/1/*)";
-
-    let mut wallet = Wallet::new(
-        external_descriptor,
-        Some(internal_descriptor),
-        db,
-        Network::Testnet,
-    )?;
-
-    let address = wallet.get_address(AddressIndex::New);
-    println!("Generated Address: {}", address);
-
-    let balance = wallet.get_balance();
-    println!("Wallet balance before syncing: {} sats", balance.total());
-
-    print!("Syncing...");
-    let client =
-        esplora_client::Builder::new("https://blockstream.info/testnet/api").build_blocking()?;
-
-    let prev_tip = wallet.latest_checkpoint();
-    let keychain_spks = wallet
-        .spks_of_all_keychains()
-        .into_iter()
-        .map(|(k, k_spks)| {
-            let mut once = Some(());
-            let mut stdout = std::io::stdout();
-            let k_spks = k_spks
-                .inspect(move |(spk_i, _)| match once.take() {
-                    Some(_) => print!("\nScanning keychain [{:?}]", k),
-                    None => print!(" {:<3}", spk_i),
-                })
-                .inspect(move |_| stdout.flush().expect("must flush"));
-            (k, k_spks)
-        })
-        .collect();
-
-    let (update_graph, last_active_indices) =
-        client.update_tx_graph(keychain_spks, None, None, STOP_GAP, PARALLEL_REQUESTS)?;
-    let missing_heights = wallet.tx_graph().missing_heights(wallet.local_chain());
-    let chain_update = client.update_local_chain(prev_tip, missing_heights)?;
-    let update = WalletUpdate {
-        last_active_indices,
-        graph: update_graph,
-        ..WalletUpdate::new(chain_update)
-    };
-
-    wallet.apply_update(update)?;
-    wallet.commit()?;
-    println!();
-
-    let balance = wallet.get_balance();
-    println!("Wallet balance after syncing: {} sats", balance.total());
-
-    if balance.total() < SEND_AMOUNT {
-        println!(
-            "Please send at least {} sats to the receiving address",
-            SEND_AMOUNT
-        );
-        std::process::exit(0);
-    }
-
-    let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")?
-        .require_network(Network::Testnet)?;
-
-    let mut tx_builder = wallet.build_tx();
-    tx_builder
-        .add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
-        .enable_rbf();
-
-    let (mut psbt, _) = tx_builder.finish()?;
-    let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
-    assert!(finalized);
-
-    let tx = psbt.extract_tx();
-    client.broadcast(&tx)?;
-    println!("Tx broadcasted! Txid: {}", tx.txid());
-
-    Ok(())
-}
diff --git a/example-crates/wallet_esplora_blocking/Cargo.toml b/example-crates/wallet_esplora_blocking/Cargo.toml
new file mode 100644 (file)
index 0000000..f07f64e
--- /dev/null
@@ -0,0 +1,12 @@
+[package]
+name = "wallet_esplora_blocking"
+version = "0.2.0"
+edition = "2021"
+publish = false
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+bdk = { path = "../../crates/bdk" }
+bdk_esplora = { path = "../../crates/esplora", features = ["blocking"] }
+bdk_file_store = { path = "../../crates/file_store" }
diff --git a/example-crates/wallet_esplora_blocking/src/main.rs b/example-crates/wallet_esplora_blocking/src/main.rs
new file mode 100644 (file)
index 0000000..a9691f3
--- /dev/null
@@ -0,0 +1,99 @@
+const DB_MAGIC: &str = "bdk_wallet_esplora_example";
+const SEND_AMOUNT: u64 = 1000;
+const STOP_GAP: usize = 5;
+const PARALLEL_REQUESTS: usize = 1;
+
+use std::{io::Write, str::FromStr};
+
+use bdk::{
+    bitcoin::{Address, Network},
+    chain::keychain::WalletUpdate,
+    wallet::AddressIndex,
+    SignOptions, Wallet,
+};
+use bdk_esplora::{esplora_client, EsploraExt};
+use bdk_file_store::Store;
+
+fn main() -> Result<(), Box<dyn std::error::Error>> {
+    let db_path = std::env::temp_dir().join("bdk-esplora-example");
+    let db = Store::<bdk::wallet::ChangeSet>::new_from_path(DB_MAGIC.as_bytes(), db_path)?;
+    let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/0/*)";
+    let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/1/*)";
+
+    let mut wallet = Wallet::new(
+        external_descriptor,
+        Some(internal_descriptor),
+        db,
+        Network::Testnet,
+    )?;
+
+    let address = wallet.get_address(AddressIndex::New);
+    println!("Generated Address: {}", address);
+
+    let balance = wallet.get_balance();
+    println!("Wallet balance before syncing: {} sats", balance.total());
+
+    print!("Syncing...");
+    let client =
+        esplora_client::Builder::new("https://blockstream.info/testnet/api").build_blocking()?;
+
+    let prev_tip = wallet.latest_checkpoint();
+    let keychain_spks = wallet
+        .spks_of_all_keychains()
+        .into_iter()
+        .map(|(k, k_spks)| {
+            let mut once = Some(());
+            let mut stdout = std::io::stdout();
+            let k_spks = k_spks
+                .inspect(move |(spk_i, _)| match once.take() {
+                    Some(_) => print!("\nScanning keychain [{:?}]", k),
+                    None => print!(" {:<3}", spk_i),
+                })
+                .inspect(move |_| stdout.flush().expect("must flush"));
+            (k, k_spks)
+        })
+        .collect();
+
+    let (update_graph, last_active_indices) =
+        client.update_tx_graph(keychain_spks, None, None, STOP_GAP, PARALLEL_REQUESTS)?;
+    let missing_heights = wallet.tx_graph().missing_heights(wallet.local_chain());
+    let chain_update = client.update_local_chain(prev_tip, missing_heights)?;
+    let update = WalletUpdate {
+        last_active_indices,
+        graph: update_graph,
+        ..WalletUpdate::new(chain_update)
+    };
+
+    wallet.apply_update(update)?;
+    wallet.commit()?;
+    println!();
+
+    let balance = wallet.get_balance();
+    println!("Wallet balance after syncing: {} sats", balance.total());
+
+    if balance.total() < SEND_AMOUNT {
+        println!(
+            "Please send at least {} sats to the receiving address",
+            SEND_AMOUNT
+        );
+        std::process::exit(0);
+    }
+
+    let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")?
+        .require_network(Network::Testnet)?;
+
+    let mut tx_builder = wallet.build_tx();
+    tx_builder
+        .add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
+        .enable_rbf();
+
+    let (mut psbt, _) = tx_builder.finish()?;
+    let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
+    assert!(finalized);
+
+    let tx = psbt.extract_tx();
+    client.broadcast(&tx)?;
+    println!("Tx broadcasted! Txid: {}", tx.txid());
+
+    Ok(())
+}