Skip to content

Commit 52e1360

Browse files
committed
feat: Update IntentTracker example.
1 parent 8e1b43c commit 52e1360

File tree

6 files changed

+69
-50
lines changed

6 files changed

+69
-50
lines changed

examples/example_wallet_electrum/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ edition = "2021"
55

66
[dependencies]
77
bdk_wallet = { path = "../../wallet", features = ["file_store"] }
8-
bdk_electrum = { version = "0.23.0" }
8+
bdk_electrum = { version = "0.23.0", path = "../../../bdk/crates/electrum" }
99
anyhow = "1"

examples/example_wallet_esplora_async/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ edition = "2021"
77

88
[dependencies]
99
bdk_wallet = { path = "../../wallet", features = ["rusqlite"] }
10-
bdk_esplora = { version = "0.22.0", features = ["async-https", "tokio"] }
10+
bdk_esplora = { version = "0.22.0", features = ["async-https", "tokio"], path = "../../../bdk/crates/esplora" }
1111
tokio = { version = "1.38.1", features = ["rt", "rt-multi-thread", "macros"] }
1212
anyhow = "1"

examples/example_wallet_esplora_blocking/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ publish = false
88

99
[dependencies]
1010
bdk_wallet = { path = "../../wallet", features = ["file_store"] }
11-
bdk_esplora = { version = "0.22.0", features = ["blocking"] }
11+
bdk_esplora = { version = "0.22.0", features = ["blocking"], path = "../../../bdk/crates/esplora" }
1212
anyhow = "1"

examples/example_wallet_rpc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77

88
[dependencies]
99
bdk_wallet = { path = "../../wallet", features = ["file_store"] }
10-
bdk_bitcoind_rpc = { version = "0.20.0" }
10+
bdk_bitcoind_rpc = { version = "0.20.0", path = "../../../bdk/crates/bitcoind_rpc" }
1111

1212
anyhow = "1"
1313
clap = { version = "4.5.17", features = ["derive", "env"] }

wallet/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ miniscript = { version = "12.3.1", features = [ "serde" ], default-features = fa
2121
bitcoin = { version = "0.32.4", features = [ "serde", "base64" ], default-features = false }
2222
serde = { version = "^1.0", features = ["derive"] }
2323
serde_json = { version = "^1.0" }
24-
bdk_chain = { version = "0.23.0", features = [ "miniscript", "serde" ], default-features = false }
24+
bdk_chain = { version = "0.23.0", features = [ "miniscript", "serde" ], default-features = false, path = "../../bdk/crates/chain" }
2525

2626
# Optional dependencies
2727
bip39 = { version = "2.0", optional = true }
28-
bdk_file_store = { version = "0.21.0", optional = true }
28+
bdk_file_store = { version = "0.21.0", optional = true, path = "../../bdk/crates/file_store" }
2929

3030
[features]
3131
default = ["std"]
@@ -40,12 +40,12 @@ test-utils = ["std"]
4040
[dev-dependencies]
4141
assert_matches = "1.5.0"
4242
tempfile = "3"
43-
bdk_chain = { version = "0.23.0", features = ["rusqlite"] }
43+
bdk_chain = { version = "0.23.0", features = ["rusqlite"], path = "../../bdk/crates/chain" }
4444
bdk_wallet = { path = ".", features = ["rusqlite", "file_store", "test-utils"] }
4545
anyhow = "1"
4646
rand = "^0.8"
47-
bdk_testenv = "0.13"
48-
bdk_bitcoind_rpc = { version = "0.20" }
47+
bdk_testenv = { version = "0.13", path = "../../bdk/crates/testenv" }
48+
bdk_bitcoind_rpc = { version = "0.20", path = "../../bdk/crates/bitcoind_rpc" }
4949

5050
[package.metadata.docs.rs]
5151
all-features = true

wallet/examples/intent_tracker.rs

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
1-
use std::{str::FromStr, sync::Arc, time::Duration};
1+
use std::{ops::Deref, str::FromStr};
22

33
use anyhow::Context;
4+
use bdk_bitcoind_rpc::Emitter;
45
use bdk_testenv::{bitcoincore_rpc::RpcApi, TestEnv};
56
use bdk_wallet::{KeychainKind, SignOptions, Wallet};
67
use bitcoin::{Amount, Network, Txid};
78

89
const DESCRIPTOR: &str = bdk_testenv::utils::DESCRIPTORS[3];
910

11+
fn sync_to_tip<C>(wallet: &mut Wallet, emitter: &mut Emitter<C>) -> anyhow::Result<()>
12+
where
13+
C: Deref,
14+
C::Target: RpcApi,
15+
{
16+
while let Some(block_event) = emitter.next_block()? {
17+
wallet.apply_block(&block_event.block, block_event.block_height())?;
18+
}
19+
Ok(())
20+
}
21+
22+
fn sync_mempool<C>(wallet: &mut Wallet, emitter: &mut Emitter<C>) -> anyhow::Result<()>
23+
where
24+
C: Deref,
25+
C::Target: RpcApi,
26+
{
27+
let event = emitter.mempool()?;
28+
wallet.apply_unconfirmed_txs(event.update);
29+
wallet.apply_evicted_txs(event.evicted);
30+
Ok(())
31+
}
32+
33+
/// Receive an unconfirmed tx, spend from it, and the unconfirmed tx get's RBF'ed.
34+
/// Our API should be able to recognise that the outgoing tx became evicted and allow the caller
35+
/// to respond accordingly.
1036
fn main() -> anyhow::Result<()> {
1137
let env = TestEnv::new().context("failed to start testenv")?;
1238
env.mine_blocks(101, None)
@@ -23,60 +49,53 @@ fn main() -> anyhow::Result<()> {
2349
0,
2450
wallet
2551
.transactions()
26-
.filter(|tx| tx.chain_position.is_unconfirmed())
27-
.map(|tx| tx.tx_node.txid),
52+
.filter(|tx| tx.chain_position.is_unconfirmed()),
2853
);
29-
while let Some(block_event) = emitter.next_block()? {
30-
wallet.apply_block(&block_event.block, block_event.block_height())?;
31-
}
3254

33-
// Receive an unconfirmed tx, spend from it, and the unconfirmed tx get's RBF'ed.
34-
// Our API should be able to recognise that the outgoing tx became evicted and allow the caller
35-
// to respond accordingly.
3655
let wallet_addr = wallet.next_unused_address(KeychainKind::External).address;
3756
let remote_addr = env
3857
.rpc_client()
3958
.get_new_address(None, None)?
40-
.assume_checked();
41-
let incoming_txid = env.send(&wallet_addr, Amount::ONE_BTC)?;
59+
.require_network(Network::Regtest)?;
4260

43-
let mempool_event = emitter.mempool()?;
44-
wallet.apply_evicted_txs(mempool_event.evicted_ats());
45-
wallet.apply_unconfirmed_txs(mempool_event.new_txs);
61+
sync_to_tip(&mut wallet, &mut emitter)?;
62+
63+
// [INCOMING TX] : Create, broadcast & sync
64+
let incoming_txid = env.send(&wallet_addr, Amount::ONE_BTC)?;
65+
sync_mempool(&mut wallet, &mut emitter)?;
4666
assert_eq!(wallet.balance().total(), Amount::ONE_BTC);
4767

48-
// Create & broadcast outgoing tx.
49-
let mut tx_builder = wallet.build_tx();
50-
tx_builder.add_recipient(remote_addr, Amount::ONE_BTC / 2);
51-
let mut psbt = tx_builder.finish()?;
52-
assert!(wallet.sign(&mut psbt, SignOptions::default())?);
53-
let outgoing_tx = psbt.extract_tx()?;
68+
// [OUTGOING TX] : Create & track
69+
let outgoing_tx = {
70+
let mut tx_builder = wallet.build_tx();
71+
tx_builder.add_recipient(remote_addr, Amount::ONE_BTC / 2);
72+
let mut psbt = tx_builder.finish()?;
73+
assert!(wallet.sign(&mut psbt, SignOptions::default())?);
74+
psbt.extract_tx()?
75+
};
5476
wallet.track_tx(outgoing_tx.clone());
5577
assert_eq!(wallet.uncanonical_txs().count(), 1);
5678

57-
// Sync.
58-
let outgoing_txid = env.rpc_client().send_raw_transaction(&outgoing_tx)?;
59-
env.wait_until_electrum_sees_txid(outgoing_txid, Duration::from_secs(5))?;
60-
let mempool_event = emitter.mempool()?;
61-
// TODO: Why is `outgoing_txid` not emitted?
62-
println!("mempool_event: {mempool_event:#?}");
63-
wallet.apply_evicted_txs(mempool_event.evicted_ats());
64-
wallet.apply_unconfirmed_txs(mempool_event.new_txs);
65-
let tx = wallet
66-
.canonical_txs()
67-
.find(|tx| tx.tx_node.txid == outgoing_txid)
68-
.expect("must find outgoing tx");
69-
assert_eq!(wallet.uncanonical_txs().count(), 0);
79+
// let outgoing_txid = env.rpc_client().send_raw_transaction(&outgoing_tx)?;
80+
// env.wait_until_electrum_sees_txid(outgoing_txid, Duration::from_secs(5))?;
81+
// let mempool_event = emitter.mempool()?;
82+
// println!("mempool_event: {mempool_event:?}");
83+
// wallet.apply_unconfirmed_txs(mempool_event.update);
84+
// wallet.apply_evicted_txs(mempool_event.evicted);
85+
// let tx = wallet
86+
// .canonical_txs()
87+
// .find(|tx| tx.tx_node.txid == outgoing_txid)
88+
// .expect("must find outgoing tx");
89+
// assert_eq!(wallet.uncanonical_txs().count(), 0);
7090

7191
// RBF incoming tx.
72-
let res = env
73-
.rpc_client()
74-
.call::<serde_json::Value>("bumpfee", &[incoming_txid.to_string().into()])?;
75-
let incoming_replacement_txid = Txid::from_str(res.get("txid").unwrap().as_str().unwrap())?;
76-
77-
let mempool_event = emitter.mempool()?;
78-
wallet.apply_evicted_txs(mempool_event.evicted_ats());
79-
wallet.apply_unconfirmed_txs(mempool_event.new_txs);
92+
let incoming_rbf_tx = {
93+
let res = env
94+
.rpc_client()
95+
.call::<serde_json::Value>("bumpfee", &[incoming_txid.to_string().into()])?;
96+
Txid::from_str(res.get("txid").unwrap().as_str().unwrap())?
97+
};
98+
sync_mempool(&mut wallet, &mut emitter)?;
8099

81100
for uncanonical_tx in wallet.uncanonical_txs() {}
82101

0 commit comments

Comments
 (0)