Use From trait for conversion

This commit is contained in:
Sudarsan Balaji 2021-11-05 01:08:50 +05:30
parent 358cc35b60
commit c6c4446092

View File

@ -82,19 +82,21 @@ pub enum Transaction {
}, },
} }
fn to_transaction(x: &bdk::TransactionDetails) -> Transaction { impl From<&bdk::TransactionDetails> for Transaction {
let details = TransactionDetails { fn from(x: &bdk::TransactionDetails) -> Transaction {
fees: x.fee, let details = TransactionDetails {
id: x.txid.to_string(), fees: x.fee,
received: x.received, id: x.txid.to_string(),
sent: x.sent, received: x.received,
}; sent: x.sent,
match x.confirmation_time.clone() { };
Some(confirmation) => Transaction::Confirmed { match x.confirmation_time.clone() {
details, Some(confirmation) => Transaction::Confirmed {
confirmation, details,
}, confirmation,
None => Transaction::Unconfirmed { details }, },
None => Transaction::Unconfirmed { details },
}
} }
} }
@ -133,7 +135,7 @@ trait OfflineWalletOperations<B>: WalletHolder<B> {
fn get_transactions(&self) -> Result<Vec<Transaction>, Error> { fn get_transactions(&self) -> Result<Vec<Transaction>, Error> {
let transactions = self.get_wallet().list_transactions(true)?; 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<Transaction, Error> { ) -> Result<Transaction, Error> {
let tx = psbt.internal.lock().unwrap().clone().extract_tx(); let tx = psbt.internal.lock().unwrap().clone().extract_tx();
self.get_wallet().broadcast(tx)?; self.get_wallet().broadcast(tx)?;
Ok(to_transaction(&psbt.details)) Ok(Transaction::from(&psbt.details))
} }
} }