Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

RPC chain example #157

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Cargo.lock
out.perf-folded
perf.data
rust-perf.svg

.bdk_example_db
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"bdk_cli_lib",
"bdk_esplora_example",
"bdk_electrum_example",
"bdk_rpc_example",
"bdk_tmp_plan",
"bdk_coin_select"
]
2 changes: 1 addition & 1 deletion bdk_chain/src/spk_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<I: Clone + Ord> SpkTxOutIndex<I> {
///
/// [`ForEachTxout`]: crate::ForEachTxout
pub fn scan(&mut self, txouts: &impl ForEachTxout) {
txouts.for_each_txout(&mut |(op, txout)| self.scan_txout(op, txout))
txouts.for_each_txout(&mut |(op, txout)| self.scan_txout(op, txout));
}

/// Scan a single `TxOut` for a matching script pubkey
Expand Down
2 changes: 2 additions & 0 deletions bdk_cli_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ anyhow = "1"
serde = { version = "1", features = ["derive"] }
thiserror = "1.0.37"
serde_json = { version = "^1.0" }
log = "^0.4"
env_logger = "0.7"
12 changes: 8 additions & 4 deletions bdk_cli_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ use bdk_chain::{
use bdk_coin_select::{coin_select_bnb, CoinSelector, CoinSelectorOpt, WeightedValue};
pub use clap;
use clap::{Parser, Subcommand};
pub use log;
use std::{cmp::Reverse, collections::HashMap, fmt::Debug, path::PathBuf, time::Duration};

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
#[clap(propagate_version = true)]
pub struct Args<C: clap::Subcommand> {
pub struct Args<A: clap::Args, C: clap::Subcommand> {
#[clap(env = "DESCRIPTOR")]
pub descriptor: String,
#[clap(env = "CHANGE_DESCRIPTOR")]
Expand All @@ -39,6 +40,9 @@ pub struct Args<C: clap::Subcommand> {
#[clap(env = "BDK_CP_LIMIT", long, default_value = "20")]
pub cp_limit: usize,

#[clap(flatten)]
pub chain_args: A,

#[clap(subcommand)]
pub command: Commands<C>,
}
Expand Down Expand Up @@ -509,8 +513,8 @@ where
Ok(())
}

pub fn init<C: clap::Subcommand, P>() -> anyhow::Result<(
Args<C>,
pub fn init<A: clap::Args, C: clap::Subcommand, P>() -> anyhow::Result<(
Args<A, C>,
KeyMap,
KeychainTracker<Keychain, P>,
KeychainStore<Keychain, P>,
Expand All @@ -519,7 +523,7 @@ where
P: sparse_chain::ChainPosition,
KeychainChangeSet<Keychain, P>: serde::Serialize + serde::de::DeserializeOwned,
{
let args = Args::<C>::parse();
let args = Args::<A, C>::parse();
let secp = Secp256k1::default();
let (descriptor, mut keymap) =
Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, &args.descriptor)?;
Expand Down
7 changes: 5 additions & 2 deletions bdk_electrum_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ mod electrum;
use bdk_chain::{bitcoin::Network, keychain::KeychainChangeSet};
use bdk_cli::{
anyhow::{self, Context},
clap::{self, Parser, Subcommand},
clap::{self, Args, Parser, Subcommand},
};
use electrum::ElectrumClient;
use std::{fmt::Debug, io, io::Write};

use electrum_client::{Client, ConfigBuilder, ElectrumApi};

#[derive(Args, Debug, Clone)]
struct ElectrumArgs {}

#[derive(Subcommand, Debug, Clone)]
enum ElectrumCommands {
/// Scans the addresses in the wallet using esplora API.
Expand Down Expand Up @@ -43,7 +46,7 @@ pub struct ScanOption {
}

fn main() -> anyhow::Result<()> {
let (args, keymap, mut tracker, mut db) = bdk_cli::init::<ElectrumCommands, _>()?;
let (args, keymap, mut tracker, mut db) = bdk_cli::init::<ElectrumArgs, ElectrumCommands, _>()?;

let electrum_url = match args.network {
Network::Bitcoin => "ssl://electrum.blockstream.info:50002",
Expand Down
25 changes: 18 additions & 7 deletions bdk_esplora_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ use std::io::{self, Write};
const DEFAULT_PARALLEL_REQUESTS: u8 = 5;
use bdk_cli::{
anyhow::{self, Context},
clap::{self, Subcommand},
clap::{self, Args, Subcommand},
};

#[derive(Args, Debug, Clone)]
struct EsploraUrlArgs {
#[clap(env = "ESPLORA_URL", long)]
url: Option<String>,
}

#[derive(Subcommand, Debug, Clone)]
enum EsploraCommands {
/// Scans the addresses in the wallet using esplora API.
Expand All @@ -33,12 +39,17 @@ enum EsploraCommands {
}

fn main() -> anyhow::Result<()> {
let (args, keymap, mut keychain_tracker, mut db) = bdk_cli::init::<EsploraCommands, _>()?;
let esplora_url = match args.network {
Network::Bitcoin => "https://mempool.space/api",
Network::Testnet => "https://mempool.space/testnet/api",
Network::Regtest => "http://localhost:3000",
Network::Signet => "https://mempool.space/signet/api",
let (args, keymap, mut keychain_tracker, mut db) =
bdk_cli::init::<EsploraUrlArgs, EsploraCommands, _>()?;

let esplora_url = match &args.chain_args.url {
Some(url) => url.as_str(),
None => match args.network {
Network::Bitcoin => "https://mempool.space/api",
Network::Testnet => "https://mempool.space/testnet/api",
Network::Regtest => "http://localhost:3000",
Network::Signet => "https://mempool.space/signet/api",
},
};

let client = Client::new(esplora_url, DEFAULT_PARALLEL_REQUESTS)?;
Expand Down
17 changes: 17 additions & 0 deletions bdk_rpc_example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "bdk_rpc_example"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# BDK Core
bdk_chain = { path = "../bdk_chain", features = ["serde"] }
bdk_cli = { path = "../bdk_cli_lib"}

# Rpc
bitcoincore-rpc = "0.16.0"

# Helpers
ctrlc = "3.2.4"
Loading