Skip to content

Commit

Permalink
Refactor TransactionDetails to include confirmation_time
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Aug 31, 2022
1 parent 159e7ab commit 8614d81
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 45 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Remove `generate_extended_key`, returned ExtendedKeyInfo [#154]
- Remove `restore_extended_key`, returned ExtendedKeyInfo [#154]
- Remove dictionary `ExtendedKeyInfo {mnenonic, xprv, fingerprint}` [#154]
- Remove interface `Transaction` [#190]
- Changed `Wallet` interface `list_transaction()` to return array of `TransactionDetail` [#190]
- APIs Added [#154]
- `generate_mnemonic()`, returns string mnemonic
- `interface DescriptorSecretKey`
Expand All @@ -25,7 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `extend(DerivationPath)` extends and returns DescriptorPublicKey
- `as_string()` returns DescriptorPublicKey as String
- Add to `interface Blockchain` the `get_height()` and `get_block_hash()` methods [#184]
- Add to `interface TxBuilder` the `set_recipients(recipient: Vec<AddressAmount>)` method [#186]
- Add to `interface TxBuilder` the `set_recipients(recipient: Vec<AddressAmount>)` method [#186]
- Add to `dictionary TransactionDetails` the `confirmation_time` field [#190]
- Interfaces Added [#154]
- `DescriptorSecretKey`
- `DescriptorPublicKey`
Expand All @@ -46,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#158]: https://github.com/bitcoindevkit/bdk-ffi/pull/158
[#164]: https://github.com/bitcoindevkit/bdk-ffi/pull/164
[#169]: https://github.com/bitcoindevkit/bdk-ffi/pull/169
[#190]: https://github.com/bitcoindevkit/bdk-ffi/pull/190

## [v0.7.0]
- Update BDK to version 0.19.0
Expand Down
15 changes: 5 additions & 10 deletions src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,14 @@ dictionary TransactionDetails {
u64 received;
u64 sent;
string txid;
BlockTime? confirmation_time;
};

dictionary BlockTime {
u32 height;
u64 timestamp;
};

[Enum]
interface Transaction {
Unconfirmed(TransactionDetails details);
Confirmed(TransactionDetails details, BlockTime confirmation);
};

enum WordCount {
"Words12",
"Words15",
Expand Down Expand Up @@ -187,7 +182,7 @@ interface Wallet {
boolean sign([ByRef] PartiallySignedBitcoinTransaction psbt);

[Throws=BdkError]
sequence<Transaction> list_transactions();
sequence<TransactionDetails> list_transactions();

Network network();

Expand Down Expand Up @@ -239,9 +234,9 @@ interface TxBuilder {
TxBuilder enable_rbf_with_sequence(u32 nsequence);

TxBuilder add_data(sequence<u8> data);

TxBuilder set_recipients(sequence<AddressAmount> recipients);

[Throws=BdkError]
PartiallySignedBitcoinTransaction finish([ByRef] Wallet wallet);
};
Expand Down Expand Up @@ -285,4 +280,4 @@ interface DescriptorPublicKey {
DescriptorPublicKey extend(DerivationPath path);

string as_string();
};
};
44 changes: 10 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,9 @@ pub struct TransactionDetails {
/// Server backend, but it could be None with a Bitcoin RPC node without txindex that receive
/// funds while offline.
pub fee: Option<u64>,
}

/// A transaction, either of type Confirmed or Unconfirmed
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Transaction {
/// A transaction that has yet to be included in a block
Unconfirmed {
/// The details of wallet transaction.
details: TransactionDetails,
},
/// A transaction that has been mined and is part of a block
Confirmed {
/// The details of wallet transaction
details: TransactionDetails,
/// Timestamp and block height of the block in which this transaction was mined
confirmation: BlockTime,
},
/// If the transaction is confirmed, contains height and timestamp of the block containing the
/// transaction, unconfirmed transaction contains `None`.
pub confirmation_time: Option<BlockTime>,
}

impl From<&bdk::TransactionDetails> for TransactionDetails {
Expand All @@ -181,20 +167,7 @@ impl From<&bdk::TransactionDetails> for TransactionDetails {
txid: 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(block_time) => Transaction::Confirmed {
details: TransactionDetails::from(x),
confirmation: block_time,
},
None => Transaction::Unconfirmed {
details: TransactionDetails::from(x),
},
confirmation_time: x.confirmation_time.clone(),
}
}
}
Expand Down Expand Up @@ -440,9 +413,12 @@ impl Wallet {
}

/// Return the list of transactions made and received by the wallet. Note that this method only operate on the internal database, which first needs to be [Wallet.sync] manually.
fn list_transactions(&self) -> Result<Vec<Transaction>, Error> {
let transactions = self.get_wallet().list_transactions(true)?;
Ok(transactions.iter().map(Transaction::from).collect())
fn list_transactions(&self) -> Result<Vec<TransactionDetails>, Error> {
let transaction_details = self.get_wallet().list_transactions(true)?;
Ok(transaction_details
.iter()
.map(TransactionDetails::from)
.collect())
}

/// Return the list of unspent outputs of this wallet. Note that this method only operates on the internal database,
Expand Down

0 comments on commit 8614d81

Please sign in to comment.