Merge pull request #69 from bitcoindevkit/return-transaction-details-on-broadcast

Return transaction details on broadcast
This commit is contained in:
Sudarsan Balaji 2021-11-05 01:19:04 +05:30 committed by GitHub
commit 379cbe0b59
2 changed files with 36 additions and 23 deletions

View File

@ -144,7 +144,7 @@ interface OnlineWallet {
[Throws=BdkError] [Throws=BdkError]
void sync(BdkProgress progress_update, u32? max_address_param); void sync(BdkProgress progress_update, u32? max_address_param);
[Throws=BdkError] [Throws=BdkError]
string broadcast([ByRef] PartiallySignedBitcoinTransaction psbt); Transaction broadcast([ByRef] PartiallySignedBitcoinTransaction psbt);
}; };
interface PartiallySignedBitcoinTransaction { interface PartiallySignedBitcoinTransaction {

View File

@ -70,6 +70,8 @@ pub struct TransactionDetails {
} }
type Confirmation = ConfirmationTime; type Confirmation = ConfirmationTime;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Transaction { pub enum Transaction {
Unconfirmed { Unconfirmed {
details: TransactionDetails, details: TransactionDetails,
@ -80,6 +82,29 @@ pub enum Transaction {
}, },
} }
impl From<&bdk::TransactionDetails> for TransactionDetails {
fn from(x: &bdk::TransactionDetails) -> TransactionDetails {
TransactionDetails {
fees: x.fee,
id: x.txid.to_string(),
received: x.received,
sent: x.sent,
}
}
}
impl From<&bdk::TransactionDetails> for Transaction {
fn from(x: &bdk::TransactionDetails) -> Transaction {
match x.confirmation_time.clone() {
Some(confirmation) => Transaction::Confirmed {
details: TransactionDetails::from(x),
confirmation,
},
None => Transaction::Unconfirmed { details: TransactionDetails::from(x) },
}
}
}
trait OfflineWalletOperations<B>: WalletHolder<B> { trait OfflineWalletOperations<B>: WalletHolder<B> {
fn get_new_address(&self) -> String { fn get_new_address(&self) -> String {
self.get_wallet() self.get_wallet()
@ -115,24 +140,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 Ok(transactions.iter().map(Transaction::from).collect())
.iter()
.map(|x| -> 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 },
}
})
.collect())
} }
} }
@ -175,6 +183,7 @@ impl Progress for BdkProgressHolder {
struct PartiallySignedBitcoinTransaction { struct PartiallySignedBitcoinTransaction {
internal: Mutex<PartiallySignedTransaction>, internal: Mutex<PartiallySignedTransaction>,
details: bdk::TransactionDetails,
} }
impl PartiallySignedBitcoinTransaction { impl PartiallySignedBitcoinTransaction {
@ -187,7 +196,7 @@ impl PartiallySignedBitcoinTransaction {
let wallet = online_wallet.get_wallet(); let wallet = online_wallet.get_wallet();
match Address::from_str(&recipient) { match Address::from_str(&recipient) {
Ok(address) => { Ok(address) => {
let (psbt, _) = { let (psbt, details) = {
let mut builder = wallet.build_tx(); let mut builder = wallet.build_tx();
builder.add_recipient(address.script_pubkey(), amount); builder.add_recipient(address.script_pubkey(), amount);
if let Some(sat_per_vb) = fee_rate { if let Some(sat_per_vb) = fee_rate {
@ -197,6 +206,7 @@ impl PartiallySignedBitcoinTransaction {
}; };
Ok(PartiallySignedBitcoinTransaction { Ok(PartiallySignedBitcoinTransaction {
internal: Mutex::new(psbt), internal: Mutex::new(psbt),
details,
}) })
} }
Err(..) => Err(BdkError::Generic( Err(..) => Err(BdkError::Generic(
@ -266,10 +276,13 @@ impl OnlineWallet {
.sync(BdkProgressHolder { progress_update }, max_address_param) .sync(BdkProgressHolder { progress_update }, max_address_param)
} }
fn broadcast<'a>(&self, psbt: &'a PartiallySignedBitcoinTransaction) -> Result<String, Error> { fn broadcast<'a>(
&self,
psbt: &'a PartiallySignedBitcoinTransaction,
) -> Result<Transaction, Error> {
let tx = psbt.internal.lock().unwrap().clone().extract_tx(); let tx = psbt.internal.lock().unwrap().clone().extract_tx();
let tx_id = self.get_wallet().broadcast(tx)?; self.get_wallet().broadcast(tx)?;
Ok(tx_id.to_string()) Ok(Transaction::from(&psbt.details))
} }
} }