use unc_jsonrpc_client::methods; mod utils; pub fn specify_block_reference() -> std::io::Result { println!("=========[Block Reference]========="); let block_reference = utils::select( || { println!(" [1] final \x1b[38;5;244m(alias: f, fin)\x1b[0m"); println!(" [2] optimistic \x1b[38;5;244m(alias: o, opt)\x1b[0m"); println!(" [3] block hash \x1b[38;5;244m(alias: s, hash)\x1b[0m"); println!(" [4] block height \x1b[38;5;244m(alias: h, height)\x1b[0m"); }, "\x1b[33m(enter a selection)\x1b[0m> ", |selection| match (selection, selection.parse()) { ("f" | "fin" | "final", _) | (_, Ok(1)) => { Some(unc_primitives::types::BlockReference::Finality( unc_primitives::types::Finality::Final, )) } ("o" | "opt" | "optimistic", _) | (_, Ok(2)) => { Some(unc_primitives::types::BlockReference::Finality( unc_primitives::types::Finality::None, )) } ("s" | "hash" | "block hash", _) | (_, Ok(3)) => loop { match utils::input("What block hash should we query? ") .unwrap() .parse() { Ok(block_hash) => { break Some(unc_primitives::types::BlockReference::BlockId( unc_primitives::types::BlockId::Hash(block_hash), )) } _ => println!("(i) Invalid block hash, please reenter!"), } }, ("h" | "height" | "block height", _) | (_, Ok(4)) => loop { match utils::input("What block height should we query? ") .unwrap() .parse() { Ok(block_height) => { break Some(unc_primitives::types::BlockReference::BlockId( unc_primitives::types::BlockId::Height(block_height), )) } _ => println!("(i) Invalid block height, please reenter!"), } }, _ => None, }, )?; println!("==================================="); Ok(block_reference) } #[tokio::main] async fn main() -> Result<(), Box> { env_logger::init(); let client = utils::select_network()?; // tolerate only 3 retries for _ in 1..=3 { let block_reference = specify_block_reference()?; match client .call(methods::block::RpcBlockRequest { block_reference }) .await { Ok(block_details) => println!("{:#?}", block_details), Err(err) => match err.handler_error() { Some(methods::block::RpcBlockError::UnknownBlock { .. }) => { println!("(i) Unknown block!"); continue; } Some(err) => { println!("(i) An error occurred `{:#?}`", err); continue; } _ => println!("(i) A non-handler error ocurred `{:#?}`", err), }, }; break; } Ok(()) }