Skip to content

Commit

Permalink
example(wallet_esplora_async): update to use new sqlite_store::Wallet…
Browse files Browse the repository at this point in the history
…Store
  • Loading branch information
notmandatory committed Apr 18, 2024
1 parent b076fb5 commit f3d406b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion example-crates/wallet_esplora_async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ edition = "2021"
[dependencies]
bdk = { path = "../../crates/bdk" }
bdk_esplora = { path = "../../crates/esplora", features = ["async-https"] }
bdk_file_store = { path = "../../crates/file_store" }
bdk_sqlite_store = { path = "../../crates/sqlite_store", features = ["bdk"] }
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
anyhow = "1"
31 changes: 15 additions & 16 deletions example-crates/wallet_esplora_async/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
use std::{io::Write, str::FromStr};

use bdk::bitcoin::key::Secp256k1;
use bdk::bitcoin::Network::Signet;
use bdk::miniscript::Descriptor;
use bdk::{
bitcoin::{Address, Network},
wallet::{AddressIndex, Update},
SignOptions, Wallet,
};
use bdk_esplora::{esplora_client, EsploraAsyncExt};
use bdk_file_store::Store;
use bdk_sqlite_store::wallet::WalletStore;

const DB_MAGIC: &str = "bdk_wallet_esplora_async_example";
const SEND_AMOUNT: u64 = 5000;
const STOP_GAP: usize = 50;
const PARALLEL_REQUESTS: usize = 5;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let db_path = std::env::temp_dir().join("bdk-esplora-async-example");
let db = Store::<bdk::wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;
let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";

let mut wallet = Wallet::new_or_load(
external_descriptor,
Some(internal_descriptor),
db,
Network::Testnet,
)?;
let db_path = "bdk-esplora-async-example.sqlite";
let db = WalletStore::new(db_path)?;

let secp = Secp256k1::new();
let external_descriptor = Descriptor::parse_descriptor(&secp, "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)").unwrap();
let internal_descriptor = Descriptor::parse_descriptor(&secp, "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)").unwrap();

let mut wallet =
Wallet::new_or_load(external_descriptor, Some(internal_descriptor), db, Signet)?;

let address = wallet.try_get_address(AddressIndex::New)?;
println!("Generated Address: {}", address);
Expand All @@ -34,8 +34,7 @@ async fn main() -> Result<(), anyhow::Error> {
println!("Wallet balance before syncing: {} sats", balance.total());

print!("Syncing...");
let client =
esplora_client::Builder::new("https://blockstream.info/testnet/api").build_async()?;
let client = esplora_client::Builder::new("http://signet.bitcoindevkit.net").build_async()?;

let prev_tip = wallet.latest_checkpoint();
let keychain_spks = wallet
Expand Down Expand Up @@ -79,7 +78,7 @@ async fn main() -> Result<(), anyhow::Error> {
}

let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")?
.require_network(Network::Testnet)?;
.require_network(Network::Signet)?;

let mut tx_builder = wallet.build_tx();
tx_builder
Expand Down

0 comments on commit f3d406b

Please sign in to comment.