]> Untitled Git - bdk-cli/commitdiff
Make the blockchain interface async again on wasm32-unknown-unknown
authorAlekos Filini <alekos.filini@gmail.com>
Mon, 20 Jul 2020 13:51:57 +0000 (15:51 +0200)
committerAlekos Filini <alekos.filini@gmail.com>
Mon, 20 Jul 2020 18:02:24 +0000 (20:02 +0200)
The procedural macro `#[maybe_async]` makes a method or every method of a trait
"async" whenever the target_arch is `wasm32`, and leaves them untouched on
every other platform.

The macro `maybe_await!($e:expr)` can be used to call `maybe_async` methods on
multi-platform code: it expands to `$e` on non-wasm32 platforms and to
`$e.await` on wasm32.

The macro `await_or_block!($e:expr)` can be used to contain async code as much
as possible: it expands to `tokio::runtime::Runtime::new().unwrap().block_on($e)`
on non-wasm32 platforms, and to `$e.await` on wasm32.

Cargo.toml
src/cli.rs

index 65be202de702f0e58d0be94cf3480cd079c90e2e..c2b6f29a9ee06f8cebe45704117c468c081f38ba 100644 (file)
@@ -5,6 +5,7 @@ edition = "2018"
 authors = ["Riccardo Casatta <riccardo@casatta.it>", "Alekos Filini <alekos.filini@gmail.com>"]
 
 [dependencies]
+magical-macros = { path = "./macros" }
 log = "^0.4"
 bitcoin = { version = "0.23", features = ["use-serde"] }
 miniscript = { version = "1.0" }
@@ -15,17 +16,23 @@ serde_json = { version = "^1.0" }
 sled = { version = "0.31.0", optional = true }
 electrum-client = { version = "0.2.0-beta.1", optional = true }
 reqwest = { version = "0.10", optional = true, features = ["json"] }
-tokio = { version = "0.2", optional = true, features = ["rt-core"] }
 futures = { version = "0.3", optional = true }
 clap = { version = "2.33", optional = true }
 base64 = { version = "^0.11", optional = true }
 
+# Platform-specific dependencies
+[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
+tokio = { version = "0.2", features = ["rt-core"] }
+
+[target.'cfg(target_arch = "wasm32")'.dependencies]
+async-trait = "0.1"
+
 [features]
 minimal = []
 compiler = ["miniscript/compiler"]
 default = ["key-value-db", "electrum"]
 electrum = ["electrum-client"]
-esplora = ["reqwest", "futures", "tokio"]
+esplora = ["reqwest", "futures"]
 key-value-db = ["sled"]
 cli-utils = ["clap", "base64"]
 
index 90c0704d00dcef87eba0e97930a098e208ae6fd1..8ea39ed84aa31109ff16c024b85d6237f4e0682c 100644 (file)
@@ -276,6 +276,7 @@ pub fn add_global_flags<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
     .subcommand(SubCommand::with_name("repl").about("Opens an interactive shell"))
 }
 
+#[maybe_async]
 pub fn handle_matches<C, D>(
     wallet: &Wallet<C, D>,
     matches: ArgMatches<'_>,
@@ -287,7 +288,7 @@ where
     if let Some(_sub_matches) = matches.subcommand_matches("get_new_address") {
         Ok(Some(format!("{}", wallet.get_new_address()?)))
     } else if let Some(_sub_matches) = matches.subcommand_matches("sync") {
-        wallet.sync(None, None)?;
+        maybe_await!(wallet.sync(None, None))?;
         Ok(None)
     } else if let Some(_sub_matches) = matches.subcommand_matches("list_unspent") {
         let mut res = String::new();
@@ -382,7 +383,7 @@ where
             panic!("Missing `psbt` and `tx` option");
         };
 
-        let txid = wallet.broadcast(tx)?;
+        let txid = maybe_await!(wallet.broadcast(tx))?;
 
         Ok(Some(format!("TXID: {}", txid)))
     } else if let Some(sub_matches) = matches.subcommand_matches("extract_psbt") {