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

Add a RPC syncing example. #79

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ members = [
"bdk_esplora_example",
"bdk_electrum_example",
"bdk_tmp_plan",
"bdk_coin_select"
"bdk_coin_select",
"bdk_rpc_wallet_example",
]
1 change: 1 addition & 0 deletions bdk_rpc_wallet_example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
17 changes: 17 additions & 0 deletions bdk_rpc_wallet_example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "bdk_rpc_wallet_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"}

# Electrum
bitcoincore-rpc = "0.16"

# Auxiliary
serde = { version = "1", features = ["derive"] }
serde_json = { version = "^1.0" }
82 changes: 82 additions & 0 deletions bdk_rpc_wallet_example/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
mod rpc;

use bdk_cli::{
anyhow::{self, Context},
clap::{self, Subcommand},
};
use bitcoincore_rpc::RpcApi;

use bdk_chain::{
bitcoin::{consensus::deserialize, Transaction},
keychain::KeychainChangeSet,
};

use rpc::{RpcClient, RpcConfig};

#[derive(Subcommand, Debug, Clone)]
enum RpcCommands {
/// Scans for transactions related spks in the tracker
Scan,
}

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

let rpc_url = "127.0.0.1:18443".to_string();
let rpc_auth = ("user".to_string(), "password".to_string());
let config = RpcConfig::new(rpc_url, rpc_auth, args.network);
let client = RpcClient::init_for_tracker(&config, &tracker.lock().unwrap().txout_index)?;

let rpc_cmd = match args.command {
bdk_cli::Commands::ChainSpecific(rpc_cmd) => rpc_cmd,
general_command => {
return bdk_cli::handle_commands(
general_command,
client,
&mut tracker,
&mut db,
args.network,
&keymap,
)
}
};

match rpc_cmd {
RpcCommands::Scan => {
let mut keychain_changeset = KeychainChangeSet::default();

let mut tracker = tracker.lock().unwrap();

let chain_update = client.wallet_scan(tracker.chain().checkpoints())?;

let sparsechain_changeset = tracker.chain().determine_changeset(&chain_update)?;

let new_txids = tracker
.chain()
.changeset_additions(&sparsechain_changeset)
.collect::<Vec<_>>();

let new_txs = new_txids
.iter()
.map(|txid| {
let tx_data = client.get_transaction(&txid, Some(true))?.hex;
let tx: Transaction = deserialize(&tx_data)?;
Ok(tx)
})
.collect::<Result<Vec<_>, anyhow::Error>>()?;

let chaingraph_changeset = tracker
.chain_graph()
.inflate_changeset(sparsechain_changeset, new_txs)
.context("inflating changeset")?;

keychain_changeset.chain_graph = chaingraph_changeset;

let mut db = db.lock().unwrap();

db.append_changeset(&keychain_changeset)?;
tracker.apply_changeset(keychain_changeset);
}
}
Ok(())
}
Loading