mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-04-28 03:36:16 -04:00
Take advantage of upgraded electrum-client dependency
The new version implements std::error::Error and fixes a bug that allows us to use the default config again.
This commit is contained in:
parent
7f6299075c
commit
96b2a76971
@ -3,10 +3,10 @@ use crate::bitcoin::{Address, Amount, Transaction};
|
|||||||
use crate::env;
|
use crate::env;
|
||||||
use ::bitcoin::util::psbt::PartiallySignedTransaction;
|
use ::bitcoin::util::psbt::PartiallySignedTransaction;
|
||||||
use ::bitcoin::Txid;
|
use ::bitcoin::Txid;
|
||||||
use anyhow::{anyhow, bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use bdk::blockchain::{noop_progress, Blockchain, ElectrumBlockchain};
|
use bdk::blockchain::{noop_progress, Blockchain, ElectrumBlockchain};
|
||||||
use bdk::descriptor::Segwitv0;
|
use bdk::descriptor::Segwitv0;
|
||||||
use bdk::electrum_client::{self, ElectrumApi, GetHistoryRes};
|
use bdk::electrum_client::{ElectrumApi, GetHistoryRes};
|
||||||
use bdk::keys::DerivableKey;
|
use bdk::keys::DerivableKey;
|
||||||
use bdk::{FeeRate, KeychainKind};
|
use bdk::{FeeRate, KeychainKind};
|
||||||
use bitcoin::Script;
|
use bitcoin::Script;
|
||||||
@ -35,12 +35,8 @@ impl Wallet {
|
|||||||
key: impl DerivableKey<Segwitv0> + Clone,
|
key: impl DerivableKey<Segwitv0> + Clone,
|
||||||
env_config: env::Config,
|
env_config: env::Config,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
// Workaround for https://github.com/bitcoindevkit/rust-electrum-client/issues/47.
|
let client = bdk::electrum_client::Client::new(electrum_rpc_url.as_str())
|
||||||
let config = electrum_client::ConfigBuilder::default().retry(2).build();
|
.context("Failed to initialize Electrum RPC client")?;
|
||||||
|
|
||||||
let client =
|
|
||||||
bdk::electrum_client::Client::from_config(electrum_rpc_url.as_str(), config.clone())
|
|
||||||
.map_err(|e| anyhow!("Failed to init electrum rpc client: {:?}", e))?;
|
|
||||||
|
|
||||||
let db = bdk::sled::open(wallet_dir)?.open_tree(SLED_TREE_NAME)?;
|
let db = bdk::sled::open(wallet_dir)?.open_tree(SLED_TREE_NAME)?;
|
||||||
|
|
||||||
@ -52,8 +48,8 @@ impl Wallet {
|
|||||||
ElectrumBlockchain::from(client),
|
ElectrumBlockchain::from(client),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let electrum = bdk::electrum_client::Client::from_config(electrum_rpc_url.as_str(), config)
|
let electrum = bdk::electrum_client::Client::new(electrum_rpc_url.as_str())
|
||||||
.map_err(|e| anyhow!("Failed to init electrum rpc client {:?}", e))?;
|
.context("Failed to initialize Electrum RPC client")?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
wallet: Arc::new(Mutex::new(bdk_wallet)),
|
wallet: Arc::new(Mutex::new(bdk_wallet)),
|
||||||
@ -101,9 +97,7 @@ impl Wallet {
|
|||||||
.list_transactions(true)?
|
.list_transactions(true)?
|
||||||
.iter()
|
.iter()
|
||||||
.find(|tx| tx.txid == txid)
|
.find(|tx| tx.txid == txid)
|
||||||
.ok_or_else(|| {
|
.context("Could not find tx in bdk wallet when trying to determine fees")?
|
||||||
anyhow!("Could not find tx in bdk wallet when trying to determine fees")
|
|
||||||
})?
|
|
||||||
.fees;
|
.fees;
|
||||||
|
|
||||||
Ok(Amount::from_sat(fees))
|
Ok(Amount::from_sat(fees))
|
||||||
@ -205,7 +199,7 @@ impl Wallet {
|
|||||||
pub async fn get_raw_transaction(&self, txid: Txid) -> Result<Transaction> {
|
pub async fn get_raw_transaction(&self, txid: Txid) -> Result<Transaction> {
|
||||||
self.get_tx(txid)
|
self.get_tx(txid)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| anyhow!("Could not get raw tx with id: {}", txid))
|
.with_context(|| format!("Could not get raw tx with id: {}", txid))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn status_of_script<T>(&self, tx: &T) -> Result<ScriptStatus>
|
pub async fn status_of_script<T>(&self, tx: &T) -> Result<ScriptStatus>
|
||||||
@ -313,12 +307,9 @@ struct Client {
|
|||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
fn new(electrum: bdk::electrum_client::Client, interval: Duration) -> Result<Self> {
|
fn new(electrum: bdk::electrum_client::Client, interval: Duration) -> Result<Self> {
|
||||||
let latest_block = electrum.block_headers_subscribe().map_err(|e| {
|
let latest_block = electrum
|
||||||
anyhow!(
|
.block_headers_subscribe()
|
||||||
"Electrum client failed to subscribe to header notifications: {:?}",
|
.context("Failed to subscribe to header notifications")?;
|
||||||
e
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
electrum,
|
electrum,
|
||||||
@ -409,7 +400,7 @@ impl Client {
|
|||||||
let latest_block = std::iter::from_fn(|| self.electrum.block_headers_pop().transpose())
|
let latest_block = std::iter::from_fn(|| self.electrum.block_headers_pop().transpose())
|
||||||
.last()
|
.last()
|
||||||
.transpose()
|
.transpose()
|
||||||
.map_err(|e| anyhow!("Failed to pop header notification: {:?}", e))?;
|
.context("Failed to pop header notification")?;
|
||||||
|
|
||||||
if let Some(new_block) = latest_block {
|
if let Some(new_block) = latest_block {
|
||||||
tracing::debug!(
|
tracing::debug!(
|
||||||
@ -426,7 +417,7 @@ impl Client {
|
|||||||
let histories = self
|
let histories = self
|
||||||
.electrum
|
.electrum
|
||||||
.batch_script_get_history(self.script_history.keys())
|
.batch_script_get_history(self.script_history.keys())
|
||||||
.map_err(|e| anyhow!("Failed to get script histories {:?}", e))?;
|
.context("Failed to get script histories")?;
|
||||||
|
|
||||||
if histories.len() != self.script_history.len() {
|
if histories.len() != self.script_history.len() {
|
||||||
bail!(
|
bail!(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user