Skip to content

Commit

Permalink
example(wallet): update esplora examples to use full_scan and sync re…
Browse files Browse the repository at this point in the history
…quests
  • Loading branch information
notmandatory committed Jan 10, 2024
1 parent 5b7ac63 commit 614f2b6
Showing 5 changed files with 157 additions and 82 deletions.
2 changes: 2 additions & 0 deletions example-crates/wallet_esplora_async/Cargo.toml
Original file line number Diff line number Diff line change
@@ -11,3 +11,5 @@ bdk_esplora = { path = "../../crates/esplora", features = ["async-https"] }
bdk_file_store = { path = "../../crates/file_store" }
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
anyhow = "1"
env_logger = { version = "0.10.1", default-features = false }
log = "0.4.20"
122 changes: 81 additions & 41 deletions example-crates/wallet_esplora_async/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{io::Write, str::FromStr};
use std::env;
use std::str::FromStr;

use bdk::{
bitcoin::{Address, Network},
@@ -7,6 +8,7 @@ use bdk::{
};
use bdk_esplora::{esplora_client, EsploraAsyncExt};
use bdk_file_store::Store;
use log::info;

const DB_MAGIC: &str = "bdk_wallet_esplora_async_example";
const SEND_AMOUNT: u64 = 5000;
@@ -15,7 +17,14 @@ const PARALLEL_REQUESTS: usize = 5;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let db_path = std::env::temp_dir().join("bdk-esplora-async-example");
env_logger::builder()
.filter_level(log::LevelFilter::Info)
.init();

let args: Vec<String> = env::args().collect();
let cmd = args.get(1);

let db_path = "bdk_wallet_esplora_async_example.dat";
let db = Store::<bdk::wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";
@@ -24,62 +33,93 @@ async fn main() -> Result<(), anyhow::Error> {
external_descriptor,
Some(internal_descriptor),
db,
Network::Testnet,
Network::Signet,
)?;

let address = wallet.try_get_address(AddressIndex::New)?;
println!("Generated Address: {}", address);
info!("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_async()?;

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
.full_scan(keychain_spks, STOP_GAP, PARALLEL_REQUESTS)
.await?;
let missing_heights = update_graph.missing_heights(wallet.local_chain());
let chain_update = client.update_local_chain(prev_tip, missing_heights).await?;
let update = Update {
last_active_indices,
graph: update_graph,
chain: Some(chain_update),
};
info!("Wallet balance: {} sats", balance.total());

let client = esplora_client::Builder::new("http://signet.bitcoindevkit.net").build_async()?;

let (update, cmd) = match cmd.map(|c| c.as_str()) {
Some(cmd) if cmd == "fullscan" => {
info!("Start full scan...");
// 1. get data required to do a wallet full_scan
let request = wallet.full_scan_request();
// 2. full scan to discover wallet transactions
let (graph_update, last_active_indices) = client
.full_scan(request.spks_by_keychain, STOP_GAP, PARALLEL_REQUESTS)
.await?;
// 3. from wallet transactions determine missing blockchain heights
let missing_heights = graph_update.missing_heights(wallet.local_chain());
// 4. get blockchain update from original request checkpoint and missing heights
let chain_update = client
.update_local_chain(request.checkpoint, missing_heights)
.await?;
// 5. create wallet update
Ok((
Update {
last_active_indices,
graph: graph_update,
chain: Some(chain_update),
},
cmd,
))
}
Some(cmd) if cmd == "sync" => {
info!("Start sync...");
// 1. get data required to do a wallet sync, if also syncing previously used addresses set unused_spks_only = false
let request = wallet.sync_request(true);
// 2. sync unused wallet spks (addresses), unconfirmed tx, and utxos
let graph_update = client
.sync(
request.spks,
request.txids,
request.outpoints,
PARALLEL_REQUESTS,
)
.await?;
// 3. from wallet transactions determine missing blockchain heights
let missing_heights = graph_update.missing_heights(wallet.local_chain());
// 4. get blockchain update from original request checkpoint and missing heights
let chain_update = client
.update_local_chain(request.checkpoint, missing_heights)
.await?;
// 5. create wallet update
Ok((
Update {
graph: graph_update,
chain: Some(chain_update),
..Update::default()
},
cmd,
))
}
_ => Err(()),
}
.expect("Specify if you want to do a wallet 'fullscan' or a 'sync'.");

// 6. apply update to wallet
wallet.apply_update(update)?;
// 7. commit wallet update to database
wallet.commit()?;
println!();

let balance = wallet.get_balance();
println!("Wallet balance after syncing: {} sats", balance.total());
info!("Wallet balance after {}: {} sats", cmd, balance.total());

if balance.total() < SEND_AMOUNT {
println!(
info!(
"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)?;
.require_network(Network::Signet)?;

let mut tx_builder = wallet.build_tx();
tx_builder
@@ -92,7 +132,7 @@ async fn main() -> Result<(), anyhow::Error> {

let tx = psbt.extract_tx();
client.broadcast(&tx).await?;
println!("Tx broadcasted! Txid: {}", tx.txid());
info!("Tx broadcasted! Txid: {}", tx.txid());

Ok(())
}
2 changes: 1 addition & 1 deletion example-crates/wallet_esplora_blocking/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bdk_wallet_esplora_async_example.dat
bdk_wallet_esplora_blocking_example.dat
2 changes: 2 additions & 0 deletions example-crates/wallet_esplora_blocking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -11,3 +11,5 @@ bdk = { path = "../../crates/bdk" }
bdk_esplora = { path = "../../crates/esplora", features = ["blocking"] }
bdk_file_store = { path = "../../crates/file_store" }
anyhow = "1"
env_logger = { version = "0.10.1", default-features = false }
log = "0.4.20"
111 changes: 71 additions & 40 deletions example-crates/wallet_esplora_blocking/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const DB_MAGIC: &str = "bdk_wallet_esplora_example";
const DB_MAGIC: &str = "bdk_wallet_esplora_blocking_example";
const SEND_AMOUNT: u64 = 1000;
const STOP_GAP: usize = 5;
const PARALLEL_REQUESTS: usize = 1;

use std::{io::Write, str::FromStr};
use log::info;
use std::env;
use std::str::FromStr;

use bdk::{
bitcoin::{Address, Network},
@@ -14,7 +16,14 @@ use bdk_esplora::{esplora_client, EsploraExt};
use bdk_file_store::Store;

fn main() -> Result<(), anyhow::Error> {
let db_path = std::env::temp_dir().join("bdk-esplora-example");
env_logger::builder()
.filter_level(log::LevelFilter::Info)
.init();

let args: Vec<String> = env::args().collect();
let cmd = args.get(1);

let db_path = "bdk_wallet_esplora_blocking_example.dat";
let db = Store::<bdk::wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";
@@ -23,63 +32,85 @@ fn main() -> Result<(), anyhow::Error> {
external_descriptor,
Some(internal_descriptor),
db,
Network::Testnet,
Network::Signet,
)?;

let address = wallet.try_get_address(AddressIndex::New)?;
println!("Generated Address: {}", address);
info!("Generated Address: {}", address);

let balance = wallet.get_balance();
println!("Wallet balance before syncing: {} sats", balance.total());
info!("Wallet balance: {} 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.full_scan(keychain_spks, STOP_GAP, PARALLEL_REQUESTS)?;
let missing_heights = update_graph.missing_heights(wallet.local_chain());
let chain_update = client.update_local_chain(prev_tip, missing_heights)?;
let update = Update {
last_active_indices,
graph: update_graph,
chain: Some(chain_update),
};
esplora_client::Builder::new("http://signet.bitcoindevkit.net").build_blocking()?;

let (update, cmd) = match cmd.map(|c| c.as_str()) {
Some(cmd) if cmd == "fullscan" => {
info!("Start full scan...");
// 1. get data required to do a wallet full_scan
let request = wallet.full_scan_request();
// 2. full scan to discover wallet transactions
let (graph_update, last_active_indices) =
client.full_scan(request.spks_by_keychain, STOP_GAP, PARALLEL_REQUESTS)?;
// 3. from wallet transactions determine missing blockchain heights
let missing_heights = graph_update.missing_heights(wallet.local_chain());
// 4. get blockchain update from original request checkpoint and missing heights
let chain_update = client.update_local_chain(request.checkpoint, missing_heights)?;
// 5. create wallet update
Ok((
Update {
last_active_indices,
graph: graph_update,
chain: Some(chain_update),
},
cmd,
))
}
Some(cmd) if cmd == "sync" => {
info!("Start sync...");
// 1. get data required to do a wallet sync, if also syncing previously used addresses set unused_spks_only = false
let request = wallet.sync_request(true);
// 2. sync unused wallet spks (addresses), unconfirmed tx, and utxos
let graph_update = client.sync(
request.spks,
request.txids,
request.outpoints,
PARALLEL_REQUESTS,
)?;
// 3. from wallet transactions determine missing blockchain heights
let missing_heights = graph_update.missing_heights(wallet.local_chain());
// 4. get blockchain update from original request checkpoint and missing heights
let chain_update = client.update_local_chain(request.checkpoint, missing_heights)?;
// 5. create wallet update
Ok((
Update {
graph: graph_update,
chain: Some(chain_update),
..Update::default()
},
cmd,
))
}
_ => Err(()),
}
.expect("Specify if you want to do a wallet 'fullscan' or a 'sync'.");

wallet.apply_update(update)?;
wallet.commit()?;
println!();

let balance = wallet.get_balance();
println!("Wallet balance after syncing: {} sats", balance.total());
info!("Wallet balance after {}: {} sats", cmd, balance.total());

if balance.total() < SEND_AMOUNT {
println!(
info!(
"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)?;
.require_network(Network::Signet)?;

let mut tx_builder = wallet.build_tx();
tx_builder
@@ -92,7 +123,7 @@ fn main() -> Result<(), anyhow::Error> {

let tx = psbt.extract_tx();
client.broadcast(&tx)?;
println!("Tx broadcasted! Txid: {}", tx.txid());
info!("Tx broadcasted! Txid: {}", tx.txid());

Ok(())
}

0 comments on commit 614f2b6

Please sign in to comment.