Skip to content

Commit 95e9b7f

Browse files
committed
add esplora_backend example.
1 parent 4c8d4d0 commit 95e9b7f

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ name = "electrum_backend"
121121
path = "examples/electrum_backend.rs"
122122
required-features = ["electrum"]
123123

124+
[[example]]
125+
name = "esplora_backend"
126+
path = "examples/esplora_backend.rs"
127+
required-features = ["use-esplora-reqwest", "reqwest-default-tls"]
128+
124129
[workspace]
125130
members = ["macros"]
126131
[package.metadata.docs.rs]

examples/esplora_backend.rs

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
pub fn create_wallet(network: &Network, xpriv: &ExtendedPrivKey) -> Wallet<MemoryDatabase> {
42+
let wallet = Wallet::new(
43+
Bip84(*xpriv, KeychainKind::External),
44+
Some(Bip84(*xpriv, KeychainKind::Internal)),
45+
*network,
46+
MemoryDatabase::default(),
47+
)
48+
.unwrap();
49+
50+
wallet
51+
}
52+
pub fn run(network: &Network, esplora_url: &str, xpriv: &str) {
53+
let xpriv = bip32::ExtendedPrivKey::from_str(xpriv).unwrap();
54+
55+
let blockchain = EsploraBlockchain::new(esplora_url, 20);
56+
57+
let wallet = create_wallet(&network, &xpriv);
58+
59+
wallet.sync(&blockchain, SyncOptions::default()).unwrap();
60+
61+
let address = wallet.get_address(AddressIndex::New).unwrap().address;
62+
63+
println!("address: {}", address);
64+
65+
let balance = wallet.get_balance().unwrap();
66+
67+
println!("balance: {}", balance);
68+
69+
if balance > 10500 {
70+
// the wallet sends the amount to itself.
71+
let recipient_address = wallet
72+
.get_address(AddressIndex::New)
73+
.unwrap()
74+
.address
75+
.to_string();
76+
77+
let amount = 9359;
78+
79+
let tx = build_signed_tx(&wallet, &recipient_address, amount);
80+
81+
blockchain.broadcast(&tx).unwrap();
82+
83+
println!("tx id: {}", tx.txid().to_string());
84+
} else {
85+
println!("Insufficient Funds. Fund the wallet with the address above");
86+
}
87+
88+
let export = FullyNodedExport::export_wallet(&wallet, "exported wallet", true)
89+
.map_err(ToString::to_string)
90+
.map_err(bdk::Error::Generic)
91+
.unwrap();
92+
93+
println!("------\nWallet Backup: {}", export.to_string());
94+
}

0 commit comments

Comments
 (0)