From c6c4446092356e5e97e110575d69dcd864e8f93f Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Fri, 5 Nov 2021 01:08:50 +0530 Subject: [PATCH] Use From trait for conversion --- src/lib.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 944197e..3ad57a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,19 +82,21 @@ pub enum Transaction { }, } -fn to_transaction(x: &bdk::TransactionDetails) -> Transaction { - let details = TransactionDetails { - fees: x.fee, - id: x.txid.to_string(), - received: x.received, - sent: x.sent, - }; - match x.confirmation_time.clone() { - Some(confirmation) => Transaction::Confirmed { - details, - confirmation, - }, - None => Transaction::Unconfirmed { details }, +impl From<&bdk::TransactionDetails> for Transaction { + fn from(x: &bdk::TransactionDetails) -> Transaction { + let details = TransactionDetails { + fees: x.fee, + id: x.txid.to_string(), + received: x.received, + sent: x.sent, + }; + match x.confirmation_time.clone() { + Some(confirmation) => Transaction::Confirmed { + details, + confirmation, + }, + None => Transaction::Unconfirmed { details }, + } } } @@ -133,7 +135,7 @@ trait OfflineWalletOperations: WalletHolder { fn get_transactions(&self) -> Result, Error> { let transactions = self.get_wallet().list_transactions(true)?; - Ok(transactions.iter().map(to_transaction).collect()) + Ok(transactions.iter().map(Transaction::from).collect()) } } @@ -275,7 +277,7 @@ impl OnlineWallet { ) -> Result { let tx = psbt.internal.lock().unwrap().clone().extract_tx(); self.get_wallet().broadcast(tx)?; - Ok(to_transaction(&psbt.details)) + Ok(Transaction::from(&psbt.details)) } }