Skip to content

Commit 156e6fc

Browse files
committed
Implement batch call for script subscribe
1 parent 84d6860 commit 156e6fc

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/api.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ pub trait ElectrumApi {
106106
/// already subscribed to the script.
107107
fn script_subscribe(&self, script: &Script) -> Result<Option<ScriptStatus>, Error>;
108108

109+
/// Batch version of [`script_subscribe`](#method.script_subscribe).
110+
///
111+
/// Takes a list of scripts and returns a list of script status responses.
112+
fn batch_script_subscribe<'s, I>(&self, scripts: I) -> Result<Vec<Option<ScriptStatus>>, Error>
113+
where
114+
I: IntoIterator<Item = &'s Script> + Clone;
115+
109116
/// Subscribes to notifications for activity on a specific *scriptPubKey*.
110117
///
111118
/// Returns a `bool` with the server response when successful.

src/batch.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ impl Batch {
4343
.push((String::from("blockchain.scripthash.get_balance"), params));
4444
}
4545

46+
/// Add one `blockchain.scripthash.listunspent` request to the batch queue
47+
pub fn script_subscribe(&mut self, script: &Script) {
48+
let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
49+
self.calls
50+
.push((String::from("blockchain.scripthash.subscribe"), params));
51+
}
52+
4653
/// Add one `blockchain.transaction.get` request to the batch queue
4754
pub fn transaction_get(&mut self, tx_hash: &Txid) {
4855
let params = vec![Param::String(tx_hash.to_hex())];

src/client.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ impl ElectrumApi for Client {
222222
impl_inner_call!(self, script_subscribe, script)
223223
}
224224

225+
#[inline]
226+
fn batch_script_subscribe<'s, I>(&self, scripts: I) -> Result<Vec<Option<ScriptStatus>>, Error>
227+
where
228+
I: IntoIterator<Item = &'s Script> + Clone,
229+
{
230+
impl_inner_call!(self, batch_script_subscribe, scripts.clone())
231+
}
232+
225233
#[inline]
226234
fn script_unsubscribe(&self, script: &Script) -> Result<bool, Error> {
227235
impl_inner_call!(self, script_unsubscribe, script)

src/raw_client.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,24 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
848848
Ok(serde_json::from_value(value)?)
849849
}
850850

851+
fn batch_script_subscribe<'s, I>(&self, scripts: I) -> Result<Vec<Option<ScriptStatus>>, Error>
852+
where
853+
I: IntoIterator<Item = &'s Script> + Clone,
854+
{
855+
{
856+
let mut script_notifications = self.script_notifications.lock()?;
857+
858+
for script in scripts.clone().into_iter() {
859+
let script_hash = script.to_electrum_scripthash();
860+
if script_notifications.contains_key(&script_hash) {
861+
return Err(Error::AlreadySubscribed(script_hash));
862+
}
863+
script_notifications.insert(script_hash, VecDeque::new());
864+
}
865+
}
866+
impl_batch_call!(self, scripts, script_subscribe)
867+
}
868+
851869
fn script_unsubscribe(&self, script: &Script) -> Result<bool, Error> {
852870
let script_hash = script.to_electrum_scripthash();
853871
let mut script_notifications = self.script_notifications.lock()?;

0 commit comments

Comments
 (0)