Skip to content

Commit c7e1247

Browse files
committed
add esplora_backend example.
1 parent 8215494 commit c7e1247

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ name = "electrum_backend"
124124
path = "examples/electrum_backend.rs"
125125
required-features = ["electrum"]
126126

127+
[[example]]
128+
name = "esplora_backend"
129+
path = "examples/esplora_backend.rs"
130+
required-features = ["use-esplora-reqwest", "reqwest-default-tls"]
131+
127132
[workspace]
128133
members = ["macros"]
129134
[package.metadata.docs.rs]

examples/esplora_backend.rs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::str::FromStr;
2+
3+
use bdk::blockchain::Blockchain;
4+
use bdk::{
5+
blockchain::esplora::EsploraBlockchain,
6+
database::MemoryDatabase,
7+
template::Bip84,
8+
wallet::{export::FullyNodedExport, AddressIndex},
9+
KeychainKind, SyncOptions, Wallet,
10+
};
11+
use bitcoin::{
12+
util::bip32::{self, ExtendedPrivKey},
13+
Network,
14+
};
15+
16+
pub mod utils;
17+
18+
use crate::utils::tx::build_signed_tx;
19+
20+
/// This will create a wallet from an xpriv and get the balance by connecting to an Esplora server.
21+
/// If enough amount is available, this will send a transaction to an address.
22+
/// Otherwise, this will display a wallet address to receive funds.
23+
///
24+
/// This can be run with `cargo run --features="use-esplora-reqwest, reqwest-default-tls" --example esplora_backend`
25+
/// in the root folder.
26+
///
27+
/// Note: The configuration above uses asynchronous HTTP calls.
28+
/// To run it in a synchronous way, the user should change `required-features` to
29+
/// `required-features = ["use-esplora-ureq"] in `esplora_backend` target in Cargo.toml and run
30+
/// `cargo run --features="use-esplora-ureq" --example esplora_backend`
31+
fn main() {
32+
let network = Network::Signet;
33+
34+
let xpriv = "tprv8ZgxMBicQKsPcx5nBGsR63Pe8KnRUqmbJNENAfGftF3yuXoMMoVJJcYeUw5eVkm9WBPjWYt6HMWYJNesB5HaNVBaFc1M6dRjWSYnmewUMYy";
35+
36+
let esplora_url = "https://explorer.bc-2.jp/api";
37+
38+
run(&network, esplora_url, xpriv);
39+
}
40+
41+
fn create_wallet(network: &Network, xpriv: &ExtendedPrivKey) -> Wallet<MemoryDatabase> {
42+
Wallet::new(
43+
Bip84(*xpriv, KeychainKind::External),
44+
Some(Bip84(*xpriv, KeychainKind::Internal)),
45+
*network,
46+
MemoryDatabase::default(),
47+
)
48+
.unwrap()
49+
}
50+
51+
fn run(network: &Network, esplora_url: &str, xpriv: &str) {
52+
let xpriv = bip32::ExtendedPrivKey::from_str(xpriv).unwrap();
53+
54+
let blockchain = EsploraBlockchain::new(esplora_url, 20);
55+
56+
let wallet = create_wallet(network, &xpriv);
57+
58+
wallet.sync(&blockchain, SyncOptions::default()).unwrap();
59+
60+
let address = wallet.get_address(AddressIndex::New).unwrap().address;
61+
62+
println!("address: {}", address);
63+
64+
let balance = wallet.get_balance().unwrap();
65+
66+
println!("Available coins in BDK wallet : {} sats", balance);
67+
68+
if balance.confirmed > 10500 {
69+
// the wallet sends the amount to itself.
70+
let recipient_address = wallet
71+
.get_address(AddressIndex::New)
72+
.unwrap()
73+
.address
74+
.to_string();
75+
76+
let amount = 9359;
77+
78+
let tx = build_signed_tx(&wallet, &recipient_address, amount);
79+
80+
blockchain.broadcast(&tx).unwrap();
81+
82+
println!("tx id: {}", tx.txid());
83+
} else {
84+
println!("Insufficient Funds. Fund the wallet with the address above");
85+
}
86+
87+
let export = FullyNodedExport::export_wallet(&wallet, "exported wallet", true)
88+
.map_err(ToString::to_string)
89+
.map_err(bdk::Error::Generic)
90+
.unwrap();
91+
92+
println!("------\nWallet Backup: {}", export.to_string());
93+
}

0 commit comments

Comments
 (0)