Skip to content

Commit 8f1ac3d

Browse files
committed
add esplora_backend example.
1 parent 4503dc5 commit 8f1ac3d

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-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

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

0 commit comments

Comments
 (0)