Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transaction details #486

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -100,6 +100,17 @@ dictionary TxOut {
Script script_pubkey;
};

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

dictionary CanonicalTx {
Transaction transaction;
ChainPosition chain_position;
};

// ------------------------------------------------------------------------
// bdk crate - wallet module
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -147,7 +158,7 @@ interface Wallet {

SentAndReceivedValues sent_and_received([ByRef] Transaction tx);

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

[Throws=CalculateFeeError]
u64 calculate_fee([ByRef] Transaction tx);
Expand Down
3 changes: 2 additions & 1 deletion bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::error::Alpha3Error;

use bdk::bitcoin::address::{NetworkChecked, NetworkUnchecked};
use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf;
use bdk::bitcoin::blockdata::transaction::TxOut as BdkTxOut;
Expand All @@ -9,7 +11,6 @@ use bdk::bitcoin::OutPoint as BdkOutPoint;
use bdk::bitcoin::Transaction as BdkTransaction;
use bdk::bitcoin::Txid;

use crate::error::Alpha3Error;
use std::io::Cursor;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
Expand Down
6 changes: 4 additions & 2 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::error::Alpha3Error;
use crate::error::CalculateFeeError;
use crate::error::EsploraError;
use crate::error::FeeRateError;
use crate::error::PersistenceError;
use crate::error::WalletCreationError;
use crate::esplora::EsploraClient;
use crate::keys::DerivationPath;
use crate::keys::DescriptorPublicKey;
Expand All @@ -25,6 +27,8 @@ use crate::keys::Mnemonic;
use crate::types::AddressIndex;
use crate::types::AddressInfo;
use crate::types::Balance;
use crate::types::CanonicalTx;
use crate::types::ChainPosition;
use crate::types::FeeRate;
use crate::types::LocalOutput;
use crate::types::ScriptAmount;
Expand All @@ -34,8 +38,6 @@ use crate::wallet::TxBuilder;
use crate::wallet::Update;
use crate::wallet::Wallet;

use crate::error::PersistenceError;
use crate::error::WalletCreationError;
use bdk::bitcoin::Network;
use bdk::keys::bip39::WordCount;
use bdk::wallet::tx_builder::ChangeSpendPolicy;
Expand Down
38 changes: 34 additions & 4 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
use crate::bitcoin::{Address, OutPoint, Script, TxOut};
use crate::bitcoin::{Address, OutPoint, Script, Transaction, TxOut};
use crate::error::FeeRateError;

use bdk::bitcoin::FeeRate as BdkFeeRate;
use bdk::bitcoin::Transaction as BdkTransaction;
use bdk::chain::tx_graph::CanonicalTx as BdkCanonicalTx;
use bdk::chain::{ChainPosition as BdkChainPosition, ConfirmationTimeHeightAnchor};
use bdk::wallet::AddressIndex as BdkAddressIndex;
use bdk::wallet::AddressInfo as BdkAddressInfo;
use bdk::wallet::Balance as BdkBalance;
use bdk::KeychainKind;
use bdk::LocalOutput as BdkLocalOutput;

use bdk::bitcoin::FeeRate as BdkFeeRate;

use crate::error::FeeRateError;
use std::sync::Arc;

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

pub struct CanonicalTx {
pub transaction: Arc<Transaction>,
pub chain_position: ChainPosition,
}

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

CanonicalTx {
transaction: Arc::new(Transaction::from(tx.tx_node.tx)),
chain_position,
}
}
}

#[derive(Clone, Debug)]
pub struct FeeRate(pub BdkFeeRate);

Expand Down
11 changes: 4 additions & 7 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction};
use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Script, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{Alpha3Error, CalculateFeeError, PersistenceError, WalletCreationError};
use crate::types::ScriptAmount;
use crate::types::{Balance, FeeRate};
use crate::Script;
use crate::{AddressIndex, AddressInfo};
use crate::types::{AddressIndex, AddressInfo, Balance, CanonicalTx, FeeRate, ScriptAmount};

use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf;
use bdk::bitcoin::psbt::PartiallySignedTransaction as BdkPartiallySignedTransaction;
Expand Down Expand Up @@ -104,10 +101,10 @@ impl Wallet {
SentAndReceivedValues { sent, received }
}

pub fn transactions(&self) -> Vec<Arc<Transaction>> {
pub fn transactions(&self) -> Vec<CanonicalTx> {
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
Loading