Renamed EsploraWallet to BitcoinClient

This commit is contained in:
Dave Hrycyszyn
2024-07-25 20:11:08 +01:00
parent 4b63245bfe
commit aa812d4101
3 changed files with 19 additions and 12 deletions

View File

@@ -12,20 +12,24 @@ use bdk::{
use crate::utils;
/// A wallet that uses the Esplora client to interact with the Bitcoin network.
pub struct EsploraWallet {
/// A client that uses Esplora to interact with the Bitcoin network.
pub struct BitcoinClient {
pub(crate) blockchain: bdk::blockchain::EsploraBlockchain,
name: String,
pub(crate) wallet: Wallet<MemoryDatabase>,
}
impl EsploraWallet {
impl BitcoinClient {
pub(crate) fn sync(&self) -> anyhow::Result<()> {
self.wallet.sync(&self.blockchain, SyncOptions::default())?;
Ok(())
}
pub(crate) fn broadcast(&self, tx: &Transaction) -> anyhow::Result<()> {
tracing::info!(
"broadcasting transaction, output will be at https://mutinynet.com/tx/{}",
tx.txid()
);
let _ = self.blockchain.broadcast(&tx);
Ok(())
}
@@ -71,7 +75,7 @@ impl EsploraWallet {
}
/// Creates a Bitcoin descriptor wallet with the mnemonic in the given user directory.
pub(crate) fn create_wallet(name: &str, network: Network) -> anyhow::Result<EsploraWallet> {
pub(crate) fn create_wallet(name: &str, network: Network) -> anyhow::Result<BitcoinClient> {
let keys_dir = utils::home(name);
let mnemonic_path = crate::utils::side_paths(keys_dir).1; // TODO: this tuple stinks
@@ -99,7 +103,7 @@ impl EsploraWallet {
let blockchain = EsploraBlockchain::new("https://mutinynet.com/api", 20);
let esplora = EsploraWallet {
let esplora = BitcoinClient {
name: name.to_string(),
wallet,
blockchain,

View File

@@ -5,7 +5,7 @@ use tracing_subscriber::filter::EnvFilter;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{fmt, layer::SubscriberExt};
use super::clients::esplora::EsploraWallet;
use super::clients::esplora::BitcoinClient;
pub(crate) async fn simple_transfer() -> Result<(), anyhow::Error> {
let (mut dave, sammy) = setup().await?;
@@ -20,10 +20,10 @@ pub(crate) async fn simple_transfer() -> Result<(), anyhow::Error> {
Ok(())
}
async fn setup() -> Result<(EsploraWallet, EsploraWallet), anyhow::Error> {
async fn setup() -> Result<(BitcoinClient, BitcoinClient), anyhow::Error> {
tracing_setup();
let dave = EsploraWallet::create_wallet("dave", Network::Signet)?;
let sammy = EsploraWallet::create_wallet("sammy", Network::Signet)?;
let dave = BitcoinClient::create_wallet("dave", Network::Signet)?;
let sammy = BitcoinClient::create_wallet("sammy", Network::Signet)?;
let _ = dave.wallet.get_balance();
dave.sync()?;
let _ = dave.wallet.get_balance();
@@ -34,7 +34,7 @@ async fn setup() -> Result<(EsploraWallet, EsploraWallet), anyhow::Error> {
}
/// Exit if the wallet does not have enough sats to send.
fn ensure_enough_sats(wallet: &EsploraWallet, send_amount: u64) -> anyhow::Result<()> {
fn ensure_enough_sats(wallet: &BitcoinClient, send_amount: u64) -> anyhow::Result<()> {
if wallet.wallet.get_balance()?.get_total() < send_amount {
tracing::error!(
"Please send at least {} sats to the receiving address. Exiting.",
@@ -46,6 +46,7 @@ fn ensure_enough_sats(wallet: &EsploraWallet, send_amount: u64) -> anyhow::Resul
}
pub(crate) async fn htlc() -> anyhow::Result<()> {
tracing::info!("starting htlc flow");
let (dave, sammy) = setup().await?;
// format a new commitment transaction like in Lightning

View File

@@ -1,7 +1,9 @@
use std::str::FromStr;
use bdk::miniscript::{descriptor::Wsh, policy::Concrete};
use bitcoin::Address;
use bdk::{
bitcoin::Address,
miniscript::{descriptor::Wsh, policy::Concrete},
};
/// A hash time locked contract between two parties.
///