feat: add transaction details

This commit is contained in:
Matthew 2024-04-06 21:25:43 -05:00
parent aa035588a0
commit ab9763bb58
No known key found for this signature in database
GPG Key ID: 8D4FCD82DD54DDD2
8 changed files with 58 additions and 12 deletions

View File

@ -37,8 +37,8 @@ class LiveWalletTest {
println("Transactions count: ${wallet.transactions().count()}") println("Transactions count: ${wallet.transactions().count()}")
val transactions = wallet.transactions().take(3) val transactions = wallet.transactions().take(3)
for (tx in transactions) { for (tx in transactions) {
val sentAndReceived = wallet.sentAndReceived(tx) val sentAndReceived = wallet.sentAndReceived(tx.transaction)
println("Transaction: ${tx.txid()}") println("Transaction: ${tx.transaction.txid()}")
println("Sent ${sentAndReceived.sent}") println("Sent ${sentAndReceived.sent}")
println("Received ${sentAndReceived.received}") println("Received ${sentAndReceived.received}")
} }

View File

@ -147,7 +147,7 @@ interface Wallet {
SentAndReceivedValues sent_and_received([ByRef] Transaction tx); SentAndReceivedValues sent_and_received([ByRef] Transaction tx);
sequence<Transaction> transactions(); sequence<TransactionDetails> transactions();
[Throws=CalculateFeeError] [Throws=CalculateFeeError]
u64 calculate_fee([ByRef] Transaction tx); u64 calculate_fee([ByRef] Transaction tx);
@ -363,6 +363,17 @@ interface Address {
boolean is_valid_for_network(Network network); 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 { interface Transaction {
[Throws=Alpha3Error] [Throws=Alpha3Error]
constructor(sequence<u8> transaction_bytes); constructor(sequence<u8> transaction_bytes);

View File

@ -8,8 +8,10 @@ use bdk::bitcoin::Network;
use bdk::bitcoin::OutPoint as BdkOutPoint; use bdk::bitcoin::OutPoint as BdkOutPoint;
use bdk::bitcoin::Transaction as BdkTransaction; use bdk::bitcoin::Transaction as BdkTransaction;
use bdk::bitcoin::Txid; use bdk::bitcoin::Txid;
use bdk::chain::tx_graph::CanonicalTx as BdkCanonicalTx;
use crate::error::Alpha3Error; use crate::error::Alpha3Error;
use bdk::chain::{ChainPosition, ConfirmationTimeHeightAnchor};
use std::io::Cursor; use std::io::Cursor;
use std::str::FromStr; use std::str::FromStr;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -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)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Transaction { pub struct Transaction {
inner: BdkTransaction, inner: BdkTransaction,

View File

@ -34,6 +34,9 @@ use crate::wallet::TxBuilder;
use crate::wallet::Update; use crate::wallet::Update;
use crate::wallet::Wallet; use crate::wallet::Wallet;
use crate::bitcoin::ConfirmationDetails;
use crate::bitcoin::TransactionDetails;
use crate::error::PersistenceError; use crate::error::PersistenceError;
use crate::error::WalletCreationError; use crate::error::WalletCreationError;
use bdk::bitcoin::Network; use bdk::bitcoin::Network;

View File

@ -1,4 +1,4 @@
use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction}; use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction, TransactionDetails};
use crate::descriptor::Descriptor; use crate::descriptor::Descriptor;
use crate::error::{Alpha3Error, CalculateFeeError, PersistenceError, WalletCreationError}; use crate::error::{Alpha3Error, CalculateFeeError, PersistenceError, WalletCreationError};
use crate::types::ScriptAmount; use crate::types::ScriptAmount;
@ -104,10 +104,10 @@ impl Wallet {
SentAndReceivedValues { sent, received } SentAndReceivedValues { sent, received }
} }
pub fn transactions(&self) -> Vec<Arc<Transaction>> { pub fn transactions(&self) -> Vec<TransactionDetails> {
self.get_wallet() self.get_wallet()
.transactions() .transactions()
.map(|tx| Arc::new(tx.tx_node.tx.into())) .map(|tx| tx.into())
.collect() .collect()
} }

View File

@ -35,8 +35,8 @@ class LiveWalletTest {
println("Transactions count: ${wallet.transactions().count()}") println("Transactions count: ${wallet.transactions().count()}")
val transactions = wallet.transactions().take(3) val transactions = wallet.transactions().take(3)
for (tx in transactions) { for (tx in transactions) {
val sentAndReceived = wallet.sentAndReceived(tx) val sentAndReceived = wallet.sentAndReceived(tx.transaction)
println("Transaction: ${tx.txid()}") println("Transaction: ${tx.transaction.txid()}")
println("Sent ${sentAndReceived.sent}") println("Sent ${sentAndReceived.sent}")
println("Received ${sentAndReceived.received}") println("Received ${sentAndReceived.received}")
} }

View File

@ -32,8 +32,8 @@ class LiveWalletTest(unittest.TestCase):
print(f"Transactions count: {len(wallet.transactions())}") print(f"Transactions count: {len(wallet.transactions())}")
transactions = wallet.transactions()[:3] transactions = wallet.transactions()[:3]
for tx in transactions: for tx in transactions:
sent_and_received = wallet.sent_and_received(tx) sent_and_received = wallet.sent_and_received(tx.transaction)
print(f"Transaction: {tx.txid()}") print(f"Transaction: {tx.transaction.txid()}")
print(f"Sent {sent_and_received.sent}") print(f"Sent {sent_and_received.sent}")
print(f"Received {sent_and_received.received}") print(f"Received {sent_and_received.received}")

View File

@ -47,8 +47,8 @@ final class LiveWalletTests: XCTestCase {
print("Transactions count: \(wallet.transactions().count)") print("Transactions count: \(wallet.transactions().count)")
let transactions = wallet.transactions().prefix(3) let transactions = wallet.transactions().prefix(3)
for tx in transactions { for tx in transactions {
let sentAndReceived = wallet.sentAndReceived(tx: tx) let sentAndReceived = wallet.sentAndReceived(tx: tx.transaction)
print("Transaction: \(tx.txid())") print("Transaction: \(tx.transaction.txid())")
print("Sent \(sentAndReceived.sent)") print("Sent \(sentAndReceived.sent)")
print("Received \(sentAndReceived.received)") print("Received \(sentAndReceived.received)")
} }