Skip to content

Commit

Permalink
feat: add transaction details
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Apr 7, 2024
1 parent aa03558 commit ab9763b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class LiveWalletTest {
println("Transactions count: ${wallet.transactions().count()}")
val transactions = wallet.transactions().take(3)
for (tx in transactions) {
val sentAndReceived = wallet.sentAndReceived(tx)
println("Transaction: ${tx.txid()}")
val sentAndReceived = wallet.sentAndReceived(tx.transaction)
println("Transaction: ${tx.transaction.txid()}")
println("Sent ${sentAndReceived.sent}")
println("Received ${sentAndReceived.received}")
}
Expand Down
13 changes: 12 additions & 1 deletion bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ interface Wallet {

SentAndReceivedValues sent_and_received([ByRef] Transaction tx);

sequence<Transaction> transactions();
sequence<TransactionDetails> transactions();

[Throws=CalculateFeeError]
u64 calculate_fee([ByRef] Transaction tx);
Expand Down Expand Up @@ -363,6 +363,17 @@ interface Address {
boolean is_valid_for_network(Network network);
};

[Enum]
interface ConfirmationDetails {
Confirmed(u32 height, u64 timestamp);
Unconfirmed(u64 timestamp);
};

dictionary TransactionDetails {
Transaction transaction;
ConfirmationDetails confirmation;
};

interface Transaction {
[Throws=Alpha3Error]
constructor(sequence<u8> transaction_bytes);
Expand Down
32 changes: 32 additions & 0 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use bdk::bitcoin::Network;
use bdk::bitcoin::OutPoint as BdkOutPoint;
use bdk::bitcoin::Transaction as BdkTransaction;
use bdk::bitcoin::Txid;
use bdk::chain::tx_graph::CanonicalTx as BdkCanonicalTx;

use crate::error::Alpha3Error;
use bdk::chain::{ChainPosition, ConfirmationTimeHeightAnchor};
use std::io::Cursor;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -114,6 +116,36 @@ impl From<BdkAddress> for Address {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfirmationDetails {
Confirmed { height: u32, timestamp: u64 },
Unconfirmed { timestamp: u64 },
}

pub struct TransactionDetails {
pub transaction: Arc<Transaction>,
pub confirmation: ConfirmationDetails,
}

impl<'a> From<BdkCanonicalTx<'a, BdkTransaction, ConfirmationTimeHeightAnchor>>
for TransactionDetails
{
fn from(tx: BdkCanonicalTx<'a, BdkTransaction, ConfirmationTimeHeightAnchor>) -> Self {
let confirmation = match tx.chain_position {
ChainPosition::Confirmed(anchor) => ConfirmationDetails::Confirmed {
height: anchor.confirmation_height,
timestamp: anchor.confirmation_time,
},
ChainPosition::Unconfirmed(timestamp) => ConfirmationDetails::Unconfirmed { timestamp },
};

TransactionDetails {
transaction: Arc::new(Transaction::from(tx.tx_node.tx)),
confirmation,
}
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Transaction {
inner: BdkTransaction,
Expand Down
3 changes: 3 additions & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ use crate::wallet::TxBuilder;
use crate::wallet::Update;
use crate::wallet::Wallet;

use crate::bitcoin::ConfirmationDetails;
use crate::bitcoin::TransactionDetails;

use crate::error::PersistenceError;
use crate::error::WalletCreationError;
use bdk::bitcoin::Network;
Expand Down
6 changes: 3 additions & 3 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction};
use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction, TransactionDetails};
use crate::descriptor::Descriptor;
use crate::error::{Alpha3Error, CalculateFeeError, PersistenceError, WalletCreationError};
use crate::types::ScriptAmount;
Expand Down Expand Up @@ -104,10 +104,10 @@ impl Wallet {
SentAndReceivedValues { sent, received }
}

pub fn transactions(&self) -> Vec<Arc<Transaction>> {
pub fn transactions(&self) -> Vec<TransactionDetails> {
self.get_wallet()
.transactions()
.map(|tx| Arc::new(tx.tx_node.tx.into()))
.map(|tx| tx.into())
.collect()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class LiveWalletTest {
println("Transactions count: ${wallet.transactions().count()}")
val transactions = wallet.transactions().take(3)
for (tx in transactions) {
val sentAndReceived = wallet.sentAndReceived(tx)
println("Transaction: ${tx.txid()}")
val sentAndReceived = wallet.sentAndReceived(tx.transaction)
println("Transaction: ${tx.transaction.txid()}")
println("Sent ${sentAndReceived.sent}")
println("Received ${sentAndReceived.received}")
}
Expand Down
4 changes: 2 additions & 2 deletions bdk-python/tests/test_live_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def test_synced_balance(self):
print(f"Transactions count: {len(wallet.transactions())}")
transactions = wallet.transactions()[:3]
for tx in transactions:
sent_and_received = wallet.sent_and_received(tx)
print(f"Transaction: {tx.txid()}")
sent_and_received = wallet.sent_and_received(tx.transaction)
print(f"Transaction: {tx.transaction.txid()}")
print(f"Sent {sent_and_received.sent}")
print(f"Received {sent_and_received.received}")

Expand Down
4 changes: 2 additions & 2 deletions bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ final class LiveWalletTests: XCTestCase {
print("Transactions count: \(wallet.transactions().count)")
let transactions = wallet.transactions().prefix(3)
for tx in transactions {
let sentAndReceived = wallet.sentAndReceived(tx: tx)
print("Transaction: \(tx.txid())")
let sentAndReceived = wallet.sentAndReceived(tx: tx.transaction)
print("Transaction: \(tx.transaction.txid())")
print("Sent \(sentAndReceived.sent)")
print("Received \(sentAndReceived.received)")
}
Expand Down

0 comments on commit ab9763b

Please sign in to comment.