Skip to content
This repository was archived by the owner on Aug 21, 2025. It is now read-only.
Merged
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
13 changes: 0 additions & 13 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ wasm-bindgen-futures = { version = "0.4.50", optional = true }
anyhow = { version = "1.0.98", default-features = false }
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
serde-wasm-bindgen = "0.6.5"
web-sys = { version = "0.3.77", default-features = false }
web-sys = { version = "0.3.77", default-features = false, features = [
"Window",
] }

# Compatibility to compile to WASM
getrandom = { version = "0.2.16", features = ["js"] }
gloo-timers = { version = "0.3.0", features = ["futures"] }

# Bitcoin dependencies
bdk_wallet = { version = "2.0.0" }
Expand Down
50 changes: 40 additions & 10 deletions src/bitcoin/esplora_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ use bdk_wallet::{
chain::spk_client::{FullScanRequest as BdkFullScanRequest, SyncRequest as BdkSyncRequest},
KeychainKind,
};
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{
prelude::{wasm_bindgen, Closure},
JsCast, JsValue,
};
use wasm_bindgen_futures::JsFuture;
use web_sys::js_sys::{Function, Promise};

use crate::{
result::JsResult,
types::{FeeEstimates, FullScanRequest, SyncRequest, Transaction, Txid, Update},
};
use std::time::Duration;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
time::Duration,
};

use bdk_esplora::esplora_client::Sleeper;
use gloo_timers::future::{sleep, TimeoutFuture};

use crate::utils::SendSyncWrapper;

#[wasm_bindgen]
pub struct EsploraClient {
Expand All @@ -27,8 +34,10 @@ pub struct EsploraClient {
#[wasm_bindgen]
impl EsploraClient {
#[wasm_bindgen(constructor)]
pub fn new(url: &str) -> JsResult<EsploraClient> {
let client = Builder::new(url).build_async_with_sleeper::<WebSleeper>()?;
pub fn new(url: &str, max_retries: usize) -> JsResult<EsploraClient> {
let client = Builder::new(url)
.max_retries(max_retries)
.build_async_with_sleeper::<WebSleeper>()?;
Ok(EsploraClient { client })
}

Expand Down Expand Up @@ -65,13 +74,34 @@ impl EsploraClient {
}
}

#[derive(Clone)]
struct WebSleep(JsFuture);

impl Future for WebSleep {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
// delegate to the inner JsFuture
Pin::new(&mut self.get_mut().0).poll(cx).map(|_| ())
}
}

// SAFETY: Wasm is single-threaded; the value is never accessed concurrently.
unsafe impl Send for WebSleep {}

#[derive(Clone, Copy)]
struct WebSleeper;

impl Sleeper for WebSleeper {
type Sleep = SendSyncWrapper<TimeoutFuture>;
type Sleep = WebSleep;

fn sleep(dur: Duration) -> Self::Sleep {
SendSyncWrapper(sleep(dur))
let ms = dur.as_millis() as i32;
let promise = Promise::new(&mut |resolve, _reject| {
let cb = Closure::once_into_js(move || resolve.call0(&JsValue::NULL).unwrap());
web_sys::window()
.unwrap()
.set_timeout_with_callback_and_timeout_and_arguments_0(cb.unchecked_ref::<Function>(), ms)
.unwrap();
});
WebSleep(JsFuture::from(promise))
}
}
24 changes: 0 additions & 24 deletions src/utils/future.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
mod descriptor;
mod future;

#[cfg(feature = "debug")]
mod panic_hook;
pub mod result;

pub use descriptor::*;
pub use future::SendSyncWrapper;

#[cfg(feature = "debug")]
pub use panic_hook::set_panic_hook;
2 changes: 1 addition & 1 deletion tests/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn test_browser() {
"wpkh(tprv8ZgxMBicQKsPe2qpAuh1K1Hig72LCoP4JgNxZM2ZRWHZYnpuw5oHoGBsQm7Qb8mLgPpRJVn3hceWgGQRNbPD6x1pp2Qme2YFRAPeYh7vmvE/84'/1'/0'/0/*)#a6kgzlgq".into(),
"wpkh(tprv8ZgxMBicQKsPe2qpAuh1K1Hig72LCoP4JgNxZM2ZRWHZYnpuw5oHoGBsQm7Qb8mLgPpRJVn3hceWgGQRNbPD6x1pp2Qme2YFRAPeYh7vmvE/84'/1'/0'/1/*)#vwnfl2cc".into(),
).expect("wallet");
let blockchain_client = EsploraClient::new("https://mutinynet.com/api").expect("esplora_client");
let blockchain_client = EsploraClient::new("https://mutinynet.com/api", 6).expect("esplora_client");

let block_height = wallet.latest_checkpoint().height();
assert_eq!(block_height, 0);
Expand Down
2 changes: 1 addition & 1 deletion tests/node/integration/esplora.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Esplora client", () => {

let feeRate: FeeRate;
let wallet: Wallet;
const esploraClient = new EsploraClient(esploraUrl);
const esploraClient = new EsploraClient(esploraUrl, 0);

it("creates a new wallet", () => {
wallet = Wallet.create(network, externalDescriptor, internalDescriptor);
Expand Down
Loading