Add the wallet_esplora example
This commit is contained in:
parent
d267517dbd
commit
b082932268
@ -8,3 +8,5 @@ publish = false
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bdk = { path = "../../crates/bdk" }
|
bdk = { path = "../../crates/bdk" }
|
||||||
|
bdk_esplora = { path = "../../crates/esplora" }
|
||||||
|
bdk_file_store = { path = "../../crates/file_store" }
|
||||||
|
@ -1,3 +1,73 @@
|
|||||||
fn main() {
|
use bdk::{
|
||||||
println!("Hello, world!");
|
bitcoin::{Address, Network},
|
||||||
|
wallet::AddressIndex,
|
||||||
|
SignOptions, Wallet,
|
||||||
|
};
|
||||||
|
use bdk_esplora::esplora_client;
|
||||||
|
use bdk_esplora::EsploraExt;
|
||||||
|
use bdk_file_store::KeychainStore;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
const STOP_GAP: usize = 50;
|
||||||
|
const PARALLEL_REQUESTS: usize = 5;
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let db = KeychainStore::new_from_path("/tmp/bdk-esplora-example")?;
|
||||||
|
let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/0/*)";
|
||||||
|
let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/1/*)";
|
||||||
|
|
||||||
|
let mut wallet = Wallet::new(
|
||||||
|
external_descriptor,
|
||||||
|
Some(internal_descriptor),
|
||||||
|
db,
|
||||||
|
Network::Testnet,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let address = wallet.get_address(AddressIndex::New);
|
||||||
|
println!("Generated Address: {}", address);
|
||||||
|
|
||||||
|
let balance = wallet.get_balance();
|
||||||
|
println!("Wallet balance before syncing: {} sats", balance.total());
|
||||||
|
|
||||||
|
println!("Syncing...");
|
||||||
|
// Scanning the chain...
|
||||||
|
let esplora_url = "https://mempool.space/testnet/api";
|
||||||
|
let client = esplora_client::Builder::new(esplora_url).build_blocking()?;
|
||||||
|
let spks = wallet.spks_of_all_keychains();
|
||||||
|
let checkpoints = wallet.checkpoints();
|
||||||
|
let update = client.scan(
|
||||||
|
checkpoints,
|
||||||
|
spks,
|
||||||
|
core::iter::empty(),
|
||||||
|
core::iter::empty(),
|
||||||
|
STOP_GAP,
|
||||||
|
PARALLEL_REQUESTS,
|
||||||
|
)?;
|
||||||
|
wallet.apply_update(update)?;
|
||||||
|
wallet.commit()?;
|
||||||
|
|
||||||
|
let balance = wallet.get_balance();
|
||||||
|
println!("Wallet balance after syncing: {} sats", balance.total());
|
||||||
|
|
||||||
|
if balance.total() == 0 {
|
||||||
|
println!("Please send some coins to the receiving address");
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")?;
|
||||||
|
|
||||||
|
let mut tx_builder = wallet.build_tx();
|
||||||
|
tx_builder
|
||||||
|
.add_recipient(faucet_address.script_pubkey(), (balance.total()) / 5)
|
||||||
|
.enable_rbf();
|
||||||
|
|
||||||
|
let (mut psbt, _) = tx_builder.finish()?;
|
||||||
|
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
|
||||||
|
assert!(finalized);
|
||||||
|
|
||||||
|
let tx = psbt.extract_tx();
|
||||||
|
client.broadcast(&tx)?;
|
||||||
|
println!("Tx broadcasted! Txid: {}", tx.txid());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user