Using tracing and simplifying a lot of console output in Bitcoin driver
This commit is contained in:
@@ -52,42 +52,8 @@ impl EsploraWallet {
|
||||
|
||||
/// Syncs the wallet with the latest state of the Bitcoin blockchain
|
||||
pub(crate) async fn sync(&mut self) -> Result<(), anyhow::Error> {
|
||||
print!("Syncing...");
|
||||
|
||||
fn generate_inspect(
|
||||
kind: KeychainKind,
|
||||
) -> impl FnMut(u32, &Script) + Send + Sync + 'static {
|
||||
let mut once = Some(());
|
||||
let mut stdout = std::io::stdout();
|
||||
move |spk_i, _| {
|
||||
match once.take() {
|
||||
Some(_) => print!("\nScanning keychain [{:?}]", kind),
|
||||
None => print!(" {:<3}", spk_i),
|
||||
};
|
||||
stdout.flush().expect("must flush");
|
||||
}
|
||||
}
|
||||
let request = self
|
||||
.wallet
|
||||
.start_full_scan()
|
||||
.inspect_spks_for_all_keychains({
|
||||
let mut once = BTreeSet::<KeychainKind>::new();
|
||||
move |keychain, spk_i, _| {
|
||||
match once.insert(keychain) {
|
||||
true => print!("\nScanning keychain [{:?}]", keychain),
|
||||
false => print!(" {:<3}", spk_i),
|
||||
}
|
||||
std::io::stdout().flush().expect("must flush")
|
||||
}
|
||||
})
|
||||
.inspect_spks_for_keychain(
|
||||
KeychainKind::External,
|
||||
generate_inspect(KeychainKind::External),
|
||||
)
|
||||
.inspect_spks_for_keychain(
|
||||
KeychainKind::Internal,
|
||||
generate_inspect(KeychainKind::Internal),
|
||||
);
|
||||
tracing::info!("{} full scan sync start", self.name);
|
||||
let request = self.wallet.start_full_scan();
|
||||
let mut update = self
|
||||
.client
|
||||
.full_scan(request, STOP_GAP, PARALLEL_REQUESTS)
|
||||
@@ -96,7 +62,7 @@ impl EsploraWallet {
|
||||
let _ = update.graph_update.update_last_seen_unconfirmed(now);
|
||||
self.wallet.apply_update(update)?;
|
||||
self.persist_local()?;
|
||||
println!("Sync complete for {}", self.name);
|
||||
tracing::info!("{} sync complete", self.name);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -110,7 +76,7 @@ impl EsploraWallet {
|
||||
pub(crate) fn next_unused_address(&mut self) -> Result<AddressInfo, anyhow::Error> {
|
||||
let address = self.wallet.next_unused_address(KeychainKind::External);
|
||||
self.persist_local()?;
|
||||
println!(
|
||||
tracing::info!(
|
||||
"Generated address: https://mutinynet.com/address/{}",
|
||||
address
|
||||
);
|
||||
@@ -119,6 +85,11 @@ impl EsploraWallet {
|
||||
|
||||
/// Returns the balance of the wallet.
|
||||
pub(crate) fn balance(&self) -> bdk_wallet::wallet::Balance {
|
||||
tracing::info!(
|
||||
"{}'s balance is {}",
|
||||
self.name,
|
||||
self.wallet.balance().total()
|
||||
);
|
||||
self.wallet.balance()
|
||||
}
|
||||
|
||||
@@ -127,7 +98,7 @@ impl EsploraWallet {
|
||||
&self,
|
||||
tx: &bitcoin::Transaction,
|
||||
) -> Result<(), esplora_client::Error> {
|
||||
println!(
|
||||
tracing::info!(
|
||||
"{} broadcasting tx https://mutinynet.com/tx/{}",
|
||||
self.name,
|
||||
tx.compute_txid()
|
||||
@@ -143,7 +114,7 @@ pub(crate) fn create_wallet(name: &str, network: Network) -> anyhow::Result<Espl
|
||||
let mnemonic_path = crate::utils::side_paths(keys_dir).1; // TODO: this tuple stinks
|
||||
let mnemonic_words = fs::read_to_string(mnemonic_path).expect("couldn't read bitcoin key file");
|
||||
|
||||
println!("Creating wallet from mnemonic: {mnemonic_words}");
|
||||
tracing::info!("Creating {name}'s wallet from mnemonic: {mnemonic_words}");
|
||||
let mnemonic = Mnemonic::parse(mnemonic_words).unwrap();
|
||||
|
||||
// Generate the extended key
|
||||
@@ -156,7 +127,7 @@ pub(crate) fn create_wallet(name: &str, network: Network) -> anyhow::Result<Espl
|
||||
.expect("problem converting xkey to xprv")
|
||||
.to_string();
|
||||
|
||||
println!("Setting up esplora database for {name}");
|
||||
tracing::info!("Setting up esplora database for {name}");
|
||||
|
||||
let db_path = format!("/tmp/{name}-bdk-esplora-async-example.sqlite");
|
||||
let conn = Connection::open(db_path)?;
|
||||
|
||||
@@ -1,43 +1,32 @@
|
||||
use bdk_wallet::bitcoin::{Amount, Network};
|
||||
|
||||
use crate::bitcoin::clients;
|
||||
use bdk_wallet::bitcoin::{Amount, Network};
|
||||
use tracing_subscriber::filter::EnvFilter;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
use tracing_subscriber::{fmt, layer::SubscriberExt};
|
||||
|
||||
pub(crate) async fn simple_transfer() -> Result<(), anyhow::Error> {
|
||||
tracing_setup();
|
||||
|
||||
let mut dave = clients::esplora::create_wallet("dave", Network::Signet)?;
|
||||
let mut sammy = clients::esplora::create_wallet("sammy", Network::Signet)?;
|
||||
|
||||
let _next_address = dave.next_unused_address()?;
|
||||
|
||||
let dave_balance = dave.balance();
|
||||
println!(
|
||||
"Dave wallet balance before syncing: {} sats",
|
||||
dave_balance.total()
|
||||
);
|
||||
let _ = dave.balance();
|
||||
|
||||
dave.sync().await?;
|
||||
|
||||
let dave_balance = dave.balance();
|
||||
println!("Wallet balance after syncing: {} sats", dave_balance);
|
||||
|
||||
let sammy_address = sammy.next_unused_address()?.address;
|
||||
println!("Sammy's address: {}", sammy_address);
|
||||
|
||||
let sammy_balance = sammy.balance();
|
||||
println!(
|
||||
"Sammy wallet balance before syncing: {} sats",
|
||||
sammy_balance
|
||||
);
|
||||
|
||||
let _sammy = sammy.balance();
|
||||
sammy.sync().await?;
|
||||
|
||||
let sammy_balance = sammy.balance();
|
||||
println!("Sammy wallet balance after syncing: {} sats", sammy_balance);
|
||||
let _sammy = sammy.balance();
|
||||
|
||||
let send_amount = Amount::from_sat(500);
|
||||
|
||||
if dave_balance.total() < send_amount {
|
||||
println!(
|
||||
"Please send at least {} sats to the receiving address",
|
||||
tracing::error!(
|
||||
"Please send at least {} sats to the receiving address. Exiting.",
|
||||
send_amount
|
||||
);
|
||||
std::process::exit(0);
|
||||
@@ -58,3 +47,12 @@ pub(crate) async fn htlc() -> anyhow::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn tracing_setup() {
|
||||
tracing_subscriber::registry()
|
||||
.with(fmt::layer())
|
||||
.with(EnvFilter::from_default_env())
|
||||
.init();
|
||||
|
||||
tracing::info!("Tracing initialized.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user