Skip to content

Commit a9c426e

Browse files
committed
add esplora_backend example.
1 parent 7a47022 commit a9c426e

File tree

2 files changed

+88
-0
lines changed

2 files changed

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

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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-reqwest, reqwest-default-tls" --example esplora_backend`
16+
/// in the root folder.
17+
///
18+
/// Note: The configuration above uses asynchronous HTTP calls.
19+
/// To run it in a synchronous way, the user should change `required-features` to
20+
/// `required-features = ["use-esplora-ureq"] in `esplora_backend` target in Cargo.toml and run
21+
/// `cargo run --features="use-esplora-ureq" --example esplora_backend`
22+
fn main() {
23+
let network = Network::Signet;
24+
25+
let xpriv = "tprv8ZgxMBicQKsPcx5nBGsR63Pe8KnRUqmbJNENAfGftF3yuXoMMoVJJcYeUw5eVkm9WBPjWYt6HMWYJNesB5HaNVBaFc1M6dRjWSYnmewUMYy";
26+
27+
let esplora_url = "https://explorer.bc-2.jp/api";
28+
29+
run(&network, esplora_url, xpriv);
30+
}
31+
32+
pub fn create_wallet(
33+
network: &Network,
34+
xpriv: &ExtendedPrivKey
35+
) -> Wallet<MemoryDatabase> {
36+
let wallet = Wallet::new(
37+
Bip84(*xpriv, KeychainKind::External),
38+
Some(Bip84(*xpriv, KeychainKind::Internal)),
39+
*network,
40+
MemoryDatabase::default()
41+
)
42+
.unwrap();
43+
44+
wallet
45+
}
46+
pub fn run(network: &Network, esplora_url: &str, xpriv: &str) {
47+
let xpriv = bip32::ExtendedPrivKey::from_str(xpriv).unwrap();
48+
49+
let blockchain = EsploraBlockchain::new(esplora_url, 20);
50+
51+
let wallet = create_wallet(&network, &xpriv);
52+
53+
wallet.sync(&blockchain, SyncOptions::default()).unwrap();
54+
55+
let address = wallet.get_address(AddressIndex::New).unwrap().address;
56+
57+
println!("address: {}", address);
58+
59+
let balance = wallet.get_balance().unwrap();
60+
61+
println!("balance: {}", balance);
62+
63+
if balance > 10500 {
64+
// the wallet sends the amount to itself.
65+
let recipient_address = wallet.get_address(AddressIndex::New).unwrap().address.to_string();
66+
67+
let amount = 9359;
68+
69+
let tx = build_signed_tx(&wallet, &recipient_address, amount);
70+
71+
blockchain.broadcast(&tx).unwrap();
72+
73+
println!("tx id: {}", tx.txid().to_string());
74+
} else {
75+
println!("Insufficient Funds. Fund the wallet with the address above");
76+
}
77+
78+
let export = FullyNodedExport::export_wallet(&wallet, "exported wallet", true)
79+
.map_err(ToString::to_string)
80+
.map_err(bdk::Error::Generic).unwrap();
81+
82+
println!("------\nWallet Backup: {}", export.to_string());
83+
}

0 commit comments

Comments
 (0)