|
|
|
@ -7,7 +7,7 @@ use crate::{
|
|
|
|
|
execution_params::ExecutionParams,
|
|
|
|
|
};
|
|
|
|
|
use ::bitcoin::{util::psbt::PartiallySignedTransaction, Txid};
|
|
|
|
|
use anyhow::{anyhow, bail, Result};
|
|
|
|
|
use anyhow::{anyhow, bail, Context, Result};
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
use backoff::{backoff::Constant as ConstantBackoff, tokio::retry};
|
|
|
|
|
use bdk::{
|
|
|
|
@ -23,12 +23,18 @@ use tokio::{sync::Mutex, time::interval};
|
|
|
|
|
|
|
|
|
|
const SLED_TREE_NAME: &str = "default_tree";
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
enum Error {
|
|
|
|
|
#[error("Sending the request failed")]
|
|
|
|
|
Io(reqwest::Error),
|
|
|
|
|
#[error("Conversion to Integer failed")]
|
|
|
|
|
Parse(std::num::ParseIntError),
|
|
|
|
|
#[error("The transaction is not minded yet")]
|
|
|
|
|
NotYetMined,
|
|
|
|
|
JsonDeserialisation(reqwest::Error),
|
|
|
|
|
#[error("Deserialization failed")]
|
|
|
|
|
JsonDeserialization(reqwest::Error),
|
|
|
|
|
#[error("Electrum client error")]
|
|
|
|
|
ElectrumClient(electrum_client::Error),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Wallet {
|
|
|
|
@ -162,14 +168,24 @@ impl BroadcastSignedTransaction for Wallet {
|
|
|
|
|
impl WatchForRawTransaction for Wallet {
|
|
|
|
|
async fn watch_for_raw_transaction(&self, txid: Txid) -> Result<Transaction> {
|
|
|
|
|
tracing::debug!("watching for tx: {}", txid);
|
|
|
|
|
retry(ConstantBackoff::new(Duration::from_secs(1)), || async {
|
|
|
|
|
let client = Client::new(self.rpc_url.as_ref())?;
|
|
|
|
|
let tx = client.transaction_get(&txid)?;
|
|
|
|
|
tracing::debug!("found tx: {}", txid);
|
|
|
|
|
Ok(tx)
|
|
|
|
|
let tx = retry(ConstantBackoff::new(Duration::from_secs(1)), || async {
|
|
|
|
|
let client = Client::new(self.rpc_url.as_ref())
|
|
|
|
|
.map_err(|err| backoff::Error::Permanent(Error::ElectrumClient(err)))?;
|
|
|
|
|
|
|
|
|
|
let tx = client.transaction_get(&txid).map_err(|err| match err {
|
|
|
|
|
electrum_client::Error::Protocol(err) => {
|
|
|
|
|
tracing::debug!("Received protocol error {} from Electrum, retrying...", err);
|
|
|
|
|
backoff::Error::Transient(Error::NotYetMined)
|
|
|
|
|
}
|
|
|
|
|
err => backoff::Error::Permanent(Error::ElectrumClient(err)),
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
Result::<_, backoff::Error<Error>>::Ok(tx)
|
|
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|err| anyhow!("transient errors to be retried: {:?}", err))
|
|
|
|
|
.context("transient errors to be retried")?;
|
|
|
|
|
|
|
|
|
|
Ok(tx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -200,7 +216,7 @@ impl GetBlockHeight for Wallet {
|
|
|
|
|
Result::<_, backoff::Error<Error>>::Ok(height)
|
|
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|err| anyhow!("transient errors to be retried: {:?}", err))?;
|
|
|
|
|
.context("transient errors to be retried")?;
|
|
|
|
|
|
|
|
|
|
Ok(BlockHeight::new(height))
|
|
|
|
|
}
|
|
|
|
@ -225,7 +241,7 @@ impl TransactionBlockHeight for Wallet {
|
|
|
|
|
let tx_status: TransactionStatus = resp
|
|
|
|
|
.json()
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|err| backoff::Error::Permanent(Error::JsonDeserialisation(err)))?;
|
|
|
|
|
.map_err(|err| backoff::Error::Permanent(Error::JsonDeserialization(err)))?;
|
|
|
|
|
|
|
|
|
|
let block_height = tx_status
|
|
|
|
|
.block_height
|
|
|
|
@ -234,7 +250,7 @@ impl TransactionBlockHeight for Wallet {
|
|
|
|
|
Result::<_, backoff::Error<Error>>::Ok(block_height)
|
|
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|err| anyhow!("transient errors to be retried: {:?}", err))?;
|
|
|
|
|
.context("transient errors to be retried")?;
|
|
|
|
|
|
|
|
|
|
Ok(BlockHeight::new(height))
|
|
|
|
|
}
|
|
|
|
|