use bitcoin::{Script, Txid};
use types::{Call, Param, ToElectrumScriptHash};
pub struct Batch {
calls: Vec<Call>,
}
impl Batch {
pub fn script_list_unspent(&mut self, script: &Script) {
let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
self.calls
.push((String::from("blockchain.scripthash.listunspent"), params));
}
pub fn script_get_history(&mut self, script: &Script) {
let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
self.calls
.push((String::from("blockchain.scripthash.get_history"), params));
}
pub fn script_get_balance(&mut self, script: &Script) {
let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
self.calls
.push((String::from("blockchain.scripthash.get_balance"), params));
}
pub fn script_subscribe(&mut self, script: &Script) {
let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
self.calls
.push((String::from("blockchain.scripthash.subscribe"), params));
}
pub fn transaction_get(&mut self, tx_hash: &Txid) {
let params = vec![Param::String(format!("{:x}", tx_hash))];
self.calls
.push((String::from("blockchain.transaction.get"), params));
}
pub fn estimate_fee(&mut self, number: usize) {
let params = vec![Param::Usize(number)];
self.calls
.push((String::from("blockchain.estimatefee"), params));
}
pub fn block_header(&mut self, height: u32) {
let params = vec![Param::U32(height)];
self.calls
.push((String::from("blockchain.block.header"), params));
}
pub fn iter(&self) -> BatchIter {
BatchIter {
batch: self,
index: 0,
}
}
}
impl std::iter::IntoIterator for Batch {
type Item = (String, Vec<Param>);
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.calls.into_iter()
}
}
pub struct BatchIter<'a> {
batch: &'a Batch,
index: usize,
}
impl<'a> std::iter::Iterator for BatchIter<'a> {
type Item = &'a (String, Vec<Param>);
fn next(&mut self) -> Option<Self::Item> {
let val = self.batch.calls.get(self.index);
self.index += 1;
val
}
}
impl std::default::Default for Batch {
fn default() -> Self {
Batch { calls: Vec::new() }
}
}