mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-10-12 05:20:53 -04:00
Remove traits in favor of using the wallet struct directly
Abstracting over the individual bits of functionality of the wallet does have its place, especially if one wants to keep a separation of an abstract protocol library that other people can use with their own wallets. However, at the moment, the traits only cause unnecessary friction. We can always add such abstraction layers again once we need them.
This commit is contained in:
parent
8c0df23647
commit
45cff81ea5
6 changed files with 96 additions and 203 deletions
|
@ -1,14 +1,9 @@
|
|||
use crate::{
|
||||
bitcoin::{
|
||||
timelocks::BlockHeight, Address, Amount, BroadcastSignedTransaction, GetBlockHeight,
|
||||
GetRawTransaction, SignTxLock, Transaction, TransactionBlockHeight, TxLock,
|
||||
WaitForTransactionFinality, WatchForRawTransaction,
|
||||
},
|
||||
bitcoin::{timelocks::BlockHeight, Address, Amount, Transaction, TxLock},
|
||||
execution_params::ExecutionParams,
|
||||
};
|
||||
use ::bitcoin::{util::psbt::PartiallySignedTransaction, Txid};
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use backoff::{backoff::Constant as ConstantBackoff, future::retry};
|
||||
use bdk::{
|
||||
blockchain::{noop_progress, Blockchain, ElectrumBlockchain},
|
||||
|
@ -152,17 +147,19 @@ impl Wallet {
|
|||
self.inner.lock().await.network()
|
||||
}
|
||||
|
||||
/// Selects an appropriate [`FeeRate`] to be used for getting transactions
|
||||
/// confirmed within a reasonable amount of time.
|
||||
fn select_feerate(&self) -> FeeRate {
|
||||
// TODO: This should obviously not be a const :)
|
||||
FeeRate::from_sat_per_vb(5.0)
|
||||
}
|
||||
}
|
||||
pub async fn broadcast_signed_transaction(&self, transaction: Transaction) -> Result<Txid> {
|
||||
let txid = transaction.txid();
|
||||
|
||||
#[async_trait]
|
||||
impl SignTxLock for Wallet {
|
||||
async fn sign_tx_lock(&self, tx_lock: TxLock) -> Result<Transaction> {
|
||||
self.inner
|
||||
.lock()
|
||||
.await
|
||||
.broadcast(transaction)
|
||||
.with_context(|| format!("failed to broadcast transaction {}", txid))?;
|
||||
|
||||
Ok(txid)
|
||||
}
|
||||
|
||||
pub async fn sign_tx_lock(&self, tx_lock: TxLock) -> Result<Transaction> {
|
||||
let txid = tx_lock.txid();
|
||||
tracing::debug!("signing tx lock: {}", txid);
|
||||
let psbt = PartiallySignedTransaction::from(tx_lock);
|
||||
|
@ -174,26 +171,14 @@ impl SignTxLock for Wallet {
|
|||
tracing::debug!("signed tx lock: {}", txid);
|
||||
Ok(tx)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl BroadcastSignedTransaction for Wallet {
|
||||
async fn broadcast_signed_transaction(&self, transaction: Transaction) -> Result<Txid> {
|
||||
let txid = transaction.txid();
|
||||
|
||||
self.inner
|
||||
.lock()
|
||||
.await
|
||||
.broadcast(transaction)
|
||||
.with_context(|| format!("failed to broadcast transaction {}", txid))?;
|
||||
|
||||
Ok(txid)
|
||||
pub async fn get_raw_transaction(&self, txid: Txid) -> Result<Transaction> {
|
||||
self.get_tx(txid)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow!("Could not get raw tx with id: {}", txid))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl WatchForRawTransaction for Wallet {
|
||||
async fn watch_for_raw_transaction(&self, txid: Txid) -> Result<Transaction> {
|
||||
pub async fn watch_for_raw_transaction(&self, txid: Txid) -> Result<Transaction> {
|
||||
tracing::debug!("watching for tx: {}", txid);
|
||||
let tx = retry(ConstantBackoff::new(Duration::from_secs(1)), || async {
|
||||
let client = Client::new(self.rpc_url.as_ref())
|
||||
|
@ -214,20 +199,8 @@ impl WatchForRawTransaction for Wallet {
|
|||
|
||||
Ok(tx)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl GetRawTransaction for Wallet {
|
||||
async fn get_raw_transaction(&self, txid: Txid) -> Result<Transaction> {
|
||||
self.get_tx(txid)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow!("Could not get raw tx with id: {}", txid))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl GetBlockHeight for Wallet {
|
||||
async fn get_block_height(&self) -> Result<BlockHeight> {
|
||||
pub async fn get_block_height(&self) -> Result<BlockHeight> {
|
||||
let url = blocks_tip_height_url(&self.http_url)?;
|
||||
let height = retry(ConstantBackoff::new(Duration::from_secs(1)), || async {
|
||||
let height = reqwest::Client::new()
|
||||
|
@ -247,11 +220,8 @@ impl GetBlockHeight for Wallet {
|
|||
|
||||
Ok(BlockHeight::new(height))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl TransactionBlockHeight for Wallet {
|
||||
async fn transaction_block_height(&self, txid: Txid) -> Result<BlockHeight> {
|
||||
pub async fn transaction_block_height(&self, txid: Txid) -> Result<BlockHeight> {
|
||||
let url = tx_status_url(txid, &self.http_url)?;
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
struct TransactionStatus {
|
||||
|
@ -281,11 +251,8 @@ impl TransactionBlockHeight for Wallet {
|
|||
|
||||
Ok(BlockHeight::new(height))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl WaitForTransactionFinality for Wallet {
|
||||
async fn wait_for_transaction_finality(
|
||||
pub async fn wait_for_transaction_finality(
|
||||
&self,
|
||||
txid: Txid,
|
||||
execution_params: ExecutionParams,
|
||||
|
@ -315,6 +282,13 @@ impl WaitForTransactionFinality for Wallet {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Selects an appropriate [`FeeRate`] to be used for getting transactions
|
||||
/// confirmed within a reasonable amount of time.
|
||||
fn select_feerate(&self) -> FeeRate {
|
||||
// TODO: This should obviously not be a const :)
|
||||
FeeRate::from_sat_per_vb(5.0)
|
||||
}
|
||||
}
|
||||
|
||||
fn tx_status_url(txid: Txid, base_url: &Url) -> Result<Url> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue