Bit of cleanup after all the excitement

This commit is contained in:
Dave Hrycyszyn
2024-06-24 16:43:17 +01:00
parent d59fa78cd7
commit 5c03a77e56
5 changed files with 12 additions and 51 deletions

View File

@@ -1,25 +1,12 @@
use crate::{keys, utils};
use bdk::bitcoin::base64;
use bdk::bitcoin::psbt::{PartiallySignedTransaction, Psbt};
use bdk::blockchain::{Blockchain, Progress};
use bdk::bitcoin::psbt::PartiallySignedTransaction;
use bdk::blockchain::Blockchain;
use bdk::database::MemoryDatabase;
use bdk::wallet::AddressIndex::{self, New};
use bdk::wallet::AddressInfo;
use bdk::{blockchain::ElectrumBlockchain, electrum_client, SyncOptions};
use bdk::{FeeRate, SignOptions, TransactionDetails, Wallet};
use toml::to_string;
use tracing::field::display;
#[derive(Debug)]
pub struct CustomProgress;
impl Progress for CustomProgress {
fn update(&self, progress: f32, message: Option<String>) -> Result<(), bdk::Error> {
println!("Progress: {} - {:?}", progress, message);
Ok(())
}
}
pub async fn run() -> Result<(), anyhow::Error> {
let dave = utils::home(&"dave".to_string());
@@ -27,11 +14,6 @@ pub async fn run() -> Result<(), anyhow::Error> {
let dave_wallet = keys::bitcoin::load_from_file(&dave).unwrap();
let sammy_wallet = keys::bitcoin::load_from_file(&sammy).unwrap();
// Verify explicitly that the address is derived as expected
let dave_expected_address = "tb1q88e45m4akrg2y00n4077vn90q8qw0svz7dss6k";
let derived_address = dave_wallet.get_address(AddressIndex::Peek(0))?.to_string();
assert_eq!(dave_expected_address, derived_address);
let dave_address = dave_wallet.get_address(AddressIndex::Peek(0))?.to_string();
let sammy_address = sammy_wallet.get_address(AddressIndex::Peek(0))?.to_string();
@@ -51,16 +33,14 @@ pub async fn run() -> Result<(), anyhow::Error> {
let (mut psbt, details) =
build_sending_tx(&dave_wallet, sammy_wallet.get_address(New)?).expect("psbt build error");
println!("Transaction details: {:#?}", details);
println!("Unsigned PSBT: {}", base64::encode(psbt.serialize()));
println!("About to sign the transaction: {:?}", details);
let finalized = dave_wallet.sign(&mut psbt, SignOptions::default())?;
println!("Finalized: {}", finalized);
dave_wallet.sign(&mut psbt, SignOptions::default())?;
let signed_tx = psbt.extract_tx();
let tx_id = blockchain
.broadcast(&psbt.extract_tx())
.expect("broadcast error");
println!("Transaction ID: {:?}", tx_id);
println!("Broadcasting...");
blockchain.broadcast(&signed_tx).expect("broadcast error");
println!("Transaction ID: {:?}", signed_tx.txid());
Ok(())
}

View File

@@ -12,6 +12,7 @@ const SEND_AMOUNT: Amount = Amount::from_sat(5000);
const STOP_GAP: usize = 50;
const PARALLEL_REQUESTS: usize = 5;
/// Demonstrates the use of bdk with the Esplora client.
pub async fn run() -> Result<(), anyhow::Error> {
let db_path = "/tmp/bdk-esplora-async-example.sqlite";
let conn = Connection::open(db_path)?;

View File

@@ -1,4 +1,3 @@
pub mod btc_other_rpc;
pub mod btc_rpc;
pub mod test;
pub mod btc_electrum_client;
pub mod btc_esplora_client;
pub mod websocket;

View File

@@ -1,19 +0,0 @@
use anyhow::Result;
use reqwest::blocking::Client;
pub async fn run() -> Result<(), Box<dyn std::error::Error>> {
// Set up the Esplora client
let esplora_url = "https://blockstream.info/signet/api";
let client = Client::new();
// Address to check
let address = "tb1q88e45m4akrg2y00n4077vn90q8qw0svz7dss6k";
// Query balance
let url = format!("{}/address/{}/utxo", esplora_url, address);
let response = client.get(&url).send()?.text()?;
println!("UTXO for address: {}", response);
Ok(())
}

View File

@@ -31,7 +31,7 @@ pub async fn run() {
node.start().await;
}
Some(Commands::Btc {}) => {
let _ = clients::btc_other_rpc::run().await;
let _ = clients::btc_electrum_client::run().await;
}
None => println!("No command provided. Exiting. See --help for more information."),
}