2023-05-18 14:04:48 +08:00
|
|
|
use std::{io::Write, str::FromStr};
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
use bdk::{
|
|
|
|
bitcoin::{Address, Network},
|
2023-10-27 14:14:25 +08:00
|
|
|
chain::PersistBackend,
|
2023-09-06 09:47:45 +03:00
|
|
|
wallet::{AddressIndex, Update},
|
2023-05-18 14:04:48 +08:00
|
|
|
SignOptions, Wallet,
|
|
|
|
};
|
2023-05-24 11:37:26 +08:00
|
|
|
use bdk_esplora::{esplora_client, EsploraAsyncExt};
|
2023-05-18 14:04:48 +08:00
|
|
|
use bdk_file_store::Store;
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
const DB_MAGIC: &str = "bdk_wallet_esplora_async_example";
|
|
|
|
const SEND_AMOUNT: u64 = 5000;
|
|
|
|
const STOP_GAP: usize = 50;
|
|
|
|
const PARALLEL_REQUESTS: usize = 5;
|
2023-03-09 10:59:18 +13:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2023-05-18 14:04:48 +08:00
|
|
|
let db_path = std::env::temp_dir().join("bdk-esplora-async-example");
|
2023-10-30 11:02:50 +08:00
|
|
|
let mut db = Store::<bdk::wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
|
2023-08-21 16:51:12 -05:00
|
|
|
let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
|
|
|
|
let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-10-26 06:20:37 +08:00
|
|
|
let mut wallet = if db.is_empty()? {
|
|
|
|
Wallet::new(
|
|
|
|
external_descriptor,
|
|
|
|
Some(internal_descriptor),
|
|
|
|
db,
|
|
|
|
Network::Testnet,
|
|
|
|
)?
|
|
|
|
} else {
|
|
|
|
Wallet::load(external_descriptor, Some(internal_descriptor), db)?
|
|
|
|
};
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
let address = wallet.get_address(AddressIndex::New);
|
|
|
|
println!("Generated Address: {}", address);
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
let balance = wallet.get_balance();
|
|
|
|
println!("Wallet balance before syncing: {} sats", balance.total());
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
print!("Syncing...");
|
|
|
|
let client =
|
|
|
|
esplora_client::Builder::new("https://blockstream.info/testnet/api").build_async()?;
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-07-19 17:42:52 +08:00
|
|
|
let prev_tip = wallet.latest_checkpoint();
|
2023-05-18 14:04:48 +08:00
|
|
|
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();
|
2023-07-19 17:42:52 +08:00
|
|
|
let (update_graph, last_active_indices) = client
|
2023-08-09 12:42:04 +03:00
|
|
|
.scan_txs_with_keychains(keychain_spks, None, None, STOP_GAP, PARALLEL_REQUESTS)
|
2023-05-18 14:04:48 +08:00
|
|
|
.await?;
|
fix(wallet_esplora): missing_heights uses the...
...graph update
Fixes #1151.
When wallet_esplora_* was used to sync a wallet containing a transaction
confirmed some time ago (more than 10-15 blocks ago), the transaction would
be stuck in an "unconfirmed" state forever.
At the first scan time, `update_local_chain` would just fetch the last 10 to
15 blocks (depending on the server used), and `tx_graph.missing_heights`
wouldn't return the tx's confirmation block as it was called on the
original, non-updated tx_graph.
So, after the first scan, we would have a transaction in memory with an
anchor that doesn't exist in our local_chain, and try_get_chain_position
would return unconfirmed.
When scanning again, missing_heights would find the missing anchor, but
`update_local_chain` wouldn't include it as it's older than
ASSUME_FINAL_DEPTH.
The missing block would be downloaded every time, but never included in
the local_chain, and the transaction would remain unconfirmed forever.
Here we call missing_heights on the updated graph, so that it can
correctly return the anchor height, and `update_local_chain` can
fetch it and include it in the chain.
2023-10-06 16:29:21 +02:00
|
|
|
let missing_heights = update_graph.missing_heights(wallet.local_chain());
|
2023-08-01 18:27:24 +08:00
|
|
|
let chain_update = client.update_local_chain(prev_tip, missing_heights).await?;
|
2023-09-06 09:47:45 +03:00
|
|
|
let update = Update {
|
2023-07-22 19:42:12 +08:00
|
|
|
last_active_indices,
|
2023-07-19 17:42:52 +08:00
|
|
|
graph: update_graph,
|
2023-08-25 12:49:29 +03:00
|
|
|
chain: Some(chain_update),
|
2023-07-19 17:42:52 +08:00
|
|
|
};
|
2023-05-18 14:04:48 +08:00
|
|
|
wallet.apply_update(update)?;
|
|
|
|
wallet.commit()?;
|
2023-07-19 17:42:52 +08:00
|
|
|
println!();
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
let balance = wallet.get_balance();
|
|
|
|
println!("Wallet balance after syncing: {} sats", balance.total());
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
if balance.total() < SEND_AMOUNT {
|
|
|
|
println!(
|
|
|
|
"Please send at least {} sats to the receiving address",
|
|
|
|
SEND_AMOUNT
|
|
|
|
);
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-06-26 12:15:09 +02:00
|
|
|
let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")?
|
|
|
|
.require_network(Network::Testnet)?;
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
let mut tx_builder = wallet.build_tx();
|
|
|
|
tx_builder
|
|
|
|
.add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
|
|
|
|
.enable_rbf();
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-07-26 19:46:40 -05:00
|
|
|
let mut psbt = tx_builder.finish()?;
|
2023-05-18 14:04:48 +08:00
|
|
|
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
|
|
|
|
assert!(finalized);
|
2023-03-09 10:59:18 +13:00
|
|
|
|
2023-05-18 14:04:48 +08:00
|
|
|
let tx = psbt.extract_tx();
|
|
|
|
client.broadcast(&tx).await?;
|
|
|
|
println!("Tx broadcasted! Txid: {}", tx.txid());
|
|
|
|
|
|
|
|
Ok(())
|
2023-03-09 10:59:18 +13:00
|
|
|
}
|