Skip to content

Commit

Permalink
chore: simplify cli usage and output
Browse files Browse the repository at this point in the history
  • Loading branch information
oleonardolima committed Jul 6, 2022
1 parent a0106af commit 3f8bd14
Showing 1 changed file with 6 additions and 39 deletions.
45 changes: 6 additions & 39 deletions src/bin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::str::FromStr;

use anyhow::Ok;
use bitcoin::{Address, BlockHash, Network};
use block_events::api::BlockEvent;
use clap::{ArgGroup, Parser, Subcommand};
use futures_util::{pin_mut, StreamExt};
use serde::{Deserialize, Serialize};
Expand All @@ -11,16 +9,15 @@ use serde::{Deserialize, Serialize};
#[clap(author = "Leonardo Souza <[email protected]>, LLFourn <[email protected]>")]
#[clap(version = "0.1.0")]
#[clap(
long_about = "A CLI interface and tool to use with the block-events library.\n
This a work in progress project for Summer of Bitcoin 2022."
long_about = "A CLI interface and tool to use with the block-events library. A work in progress project for Summer of Bitcoin 2022."
)]

struct Cli {
#[clap(subcommand)]
command: Commands,

#[clap(short, long, default_value = "testnet")]
network: Network,
#[clap(short, long, default_value = "mempool.space/testnet/api/v1")]
base_url: String,
}

#[derive(Debug, Subcommand)]
Expand Down Expand Up @@ -66,43 +63,13 @@ async fn main() -> anyhow::Result<()> {

let cli = Cli::parse();

// build the url by the network argument
let base_url = &match cli.network {
Network::Bitcoin => "mempool.space/api/v1".to_string(),
Network::Regtest => "127.0.0.1:8999/api/v1".to_string(),
network => format!("mempool.space/{}/api/v1", network),
};

// async fetch the data stream through the lib
let block_events = block_events::subscribe_to_blocks(
base_url.as_str(),
// Some((
// 102,
// BlockHash::from_str("3d3ab0efd0f8f0eb047d9e77f7ad7c6b6791c896bd8a21437da555670f799e08")
// .unwrap(),
// )),
None,
)
.await?;
let block_events = block_events::subscribe_to_blocks(cli.base_url.as_str(), None).await?;

// consume and execute the code (current matching and printing) in async manner for each new block-event
pin_mut!(block_events);
while let Some(block_event) = block_events.next().await {
println!("BlockExtended: {:#?}", block_event)
// match block_event {
// BlockEvent::Connected(block) => {
// println!("Connected BlockEvent: {:#?}", block);
// }
// BlockEvent::Disconnected((height, block_hash)) => {
// println!(
// "Disconnected BlockEvent: [height {:#?}] [block_hash: {:#?}]",
// height, block_hash
// );
// }
// BlockEvent::Error() => {
// eprint!("ERROR BlockEvent: received an error from the block-events stream");
// }
// }
println!("{:#?}", block_event);
}
Ok(())
}

0 comments on commit 3f8bd14

Please sign in to comment.