Skip to content

Commit

Permalink
feat: add sync to esplora client
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Jan 18, 2024
1 parent 7319aea commit e3bd99e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
38 changes: 14 additions & 24 deletions bdk-ffi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion bdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ path = "uniffi-bindgen.rs"
default = ["uniffi/cli"]

[dependencies]
bdk = { version = "1.0.0-alpha.3", features = ["all-keys", "keys-bip39"] }
bdk = { git = "https://github.com/notmandatory/bdk.git", branch = "feat/wallet_start_update" }
# bdk = { version = "1.0.0-alpha.3", features = ["all-keys", "keys-bip39"] }
bdk_esplora = { version = "0.5.0", default-features = false, features = ["std", "blocking"] }

# TODO 22: The bdk_esplora crate uses esplora_client which uses reqwest for async. By default it uses the system
Expand Down
4 changes: 4 additions & 0 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ impl From<NewError<std::io::Error>> for Alpha3Error {
}
}

impl From<bdk_esplora::esplora_client::Error> for Alpha3Error {
fn from(err: bdk_esplora::esplora_client::Error) -> Self { Alpha3Error::Generic }
}

impl From<CreateTxError<std::io::Error>> for Alpha3Error {
fn from(_: CreateTxError<std::io::Error>) -> Self {
Alpha3Error::Generic
Expand Down
37 changes: 36 additions & 1 deletion bdk-ffi/src/esplora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,42 @@ impl EsploraClient {
Ok(Arc::new(Update(update)))
}

// pub fn sync();
pub fn sync(
&self,
wallet: Arc<Wallet>,
parallel_requests: u64,
) -> Result<Arc<Update>, Alpha3Error> {
let wallet = wallet.get_wallet();

// 1. Get data required to do a wallet sync
let sync_request = wallet.sync_request(true);

// 2. Sync wallet SPKs (addresses), unconfirmed transactions, and UTXOs
let graph_update = self.0.sync(
sync_request.spks,
sync_request.txids,
sync_request.outpoints,
parallel_requests as usize,
).map_err(Alpha3Error::from)?;

// 3. From wallet transactions determine missing blockchain heights
let missing_heights = graph_update.missing_heights(wallet.local_chain());

// 4. Get blockchain update from the original request checkpoint and missing heights
let chain_update = self.0.update_local_chain(
sync_request.checkpoint,
missing_heights,
).map_err(Alpha3Error::from)?;

// 5. Create and return the wallet update
let update = BdkUpdate {
graph: graph_update,
chain: Some(chain_update),
..BdkUpdate::default()
};

Ok(Arc::new(Update(update)))
}

pub fn broadcast(&self, transaction: &Transaction) -> Result<(), Alpha3Error> {
let bdk_transaction: BdkTransaction = transaction.into();
Expand Down

0 comments on commit e3bd99e

Please sign in to comment.