Skip to content

Commit 611fc8a

Browse files
authored
feat: apply unconfirmed txs (MetaMask#63)
1 parent f7a3775 commit 611fc8a

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

src/bitcoin/wallet.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
},
1414
};
1515

16-
use super::TxBuilder;
16+
use super::{TxBuilder, UnconfirmedTx};
1717

1818
// We wrap a `BdkWallet` in `Rc<RefCell<...>>` because `wasm_bindgen` do not
1919
// support Rust's lifetimes. This allows us to forward a reference to the
@@ -183,6 +183,12 @@ impl Wallet {
183183
.derivation_of_spk(spk.into())
184184
.map(|(keychain, index)| SpkIndexed(keychain.into(), index))
185185
}
186+
187+
pub fn apply_unconfirmed_txs(&self, unconfirmed_txs: Vec<UnconfirmedTx>) {
188+
self.0
189+
.borrow_mut()
190+
.apply_unconfirmed_txs(unconfirmed_txs.into_iter().map(Into::into))
191+
}
186192
}
187193

188194
#[wasm_bindgen]

src/bitcoin/wallet_tx.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::BTreeSet;
1+
use std::{collections::BTreeSet, sync::Arc};
22

33
use bdk_wallet::{
44
bitcoin::{Transaction as BdkTransaction, Txid as BdkTxid},
@@ -63,3 +63,32 @@ impl From<BdkWalletTx<'_>> for WalletTx {
6363
}
6464
}
6565
}
66+
67+
#[wasm_bindgen]
68+
pub struct UnconfirmedTx(Transaction, pub u64);
69+
70+
#[wasm_bindgen]
71+
impl UnconfirmedTx {
72+
/// `tx` – your wrapped `Transaction`
73+
/// `last_seen` – unix epoch seconds (same convention as the rest of the API)
74+
#[wasm_bindgen(constructor)]
75+
pub fn new(tx: Transaction, last_seen: u64) -> UnconfirmedTx {
76+
UnconfirmedTx(tx, last_seen)
77+
}
78+
79+
#[wasm_bindgen(getter)]
80+
pub fn tx(&self) -> Transaction {
81+
self.0.clone()
82+
}
83+
84+
#[wasm_bindgen(getter)]
85+
pub fn last_seen(&self) -> u64 {
86+
self.1
87+
}
88+
}
89+
90+
impl From<UnconfirmedTx> for (Arc<BdkTransaction>, u64) {
91+
fn from(uctx: UnconfirmedTx) -> Self {
92+
(Arc::new(uctx.0.into()), uctx.1)
93+
}
94+
}

src/types/fee.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,16 @@ impl FeeRate {
5959
}
6060

6161
/// Returns raw fee rate.
62-
#[wasm_bindgen(getter)]
6362
pub fn to_sat_per_kwu(&self) -> u64 {
6463
self.0.to_sat_per_kwu()
6564
}
6665

6766
/// Converts to sat/vB rounding up.
68-
#[wasm_bindgen(getter)]
6967
pub fn to_sat_per_vb_ceil(&self) -> u64 {
7068
self.0.to_sat_per_vb_ceil()
7169
}
7270

7371
/// Converts to sat/vB rounding down.
74-
#[wasm_bindgen(getter)]
7572
pub fn to_sat_per_vb_floor(&self) -> u64 {
7673
self.0.to_sat_per_vb_floor()
7774
}

tests/node/integration/esplora.test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
FeeRate,
66
Network,
77
Recipient,
8+
UnconfirmedTx,
89
Wallet,
910
SignOptions,
1011
} from "../../../pkg/bitcoindevkit";
@@ -23,6 +24,7 @@ describe("Esplora client", () => {
2324
"tb1qd28npep0s8frcm3y7dxqajkcy2m40eysplyr9v",
2425
network
2526
);
27+
const unixTimestamp = BigInt(Math.floor(Date.now() / 1000));
2628

2729
let feeRate: FeeRate;
2830
let wallet: Wallet;
@@ -83,14 +85,10 @@ describe("Esplora client", () => {
8385
const currentDerivationIndex = wallet.derivation_index("internal");
8486
expect(initialDerivationIndex).toBeLessThan(currentDerivationIndex);
8587

86-
// Synchronizes the wallet to get the new state
87-
const request = wallet.start_sync_with_revealed_spks();
88-
const update = await esploraClient.sync(request, parallelRequests);
89-
wallet.apply_update(update);
90-
91-
// Verify the sent transaction is part of the wallet in an unconfirmed state
88+
// Assert that the transaction is in the wallet
89+
wallet.apply_unconfirmed_txs([new UnconfirmedTx(tx, unixTimestamp)]);
9290
const walletTx = wallet.get_tx(txid);
93-
expect(walletTx.last_seen_unconfirmed).toBeDefined();
91+
expect(walletTx.last_seen_unconfirmed).toEqual(unixTimestamp);
9492
expect(walletTx.chain_position.is_confirmed).toBe(false);
9593
}, 30000);
9694

0 commit comments

Comments
 (0)