From 903f777a674f111082408b2db41f9a633ae2ac54 Mon Sep 17 00:00:00 2001 From: Felipe Rosa Date: Wed, 13 Dec 2023 10:44:10 -0300 Subject: [PATCH] chore: Add fetch block and fetch block range examples --- .../examples/fetch_block.rs | 37 +++++++++++++++++ .../examples/fetch_block_range.rs | 41 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 hermes/crates/cardano-chain-follower/examples/fetch_block.rs create mode 100644 hermes/crates/cardano-chain-follower/examples/fetch_block_range.rs diff --git a/hermes/crates/cardano-chain-follower/examples/fetch_block.rs b/hermes/crates/cardano-chain-follower/examples/fetch_block.rs new file mode 100644 index 000000000..626d2e9df --- /dev/null +++ b/hermes/crates/cardano-chain-follower/examples/fetch_block.rs @@ -0,0 +1,37 @@ +//! This example shows how to use the chain follower to download arbitrary blocks +//! from the chain. + +use std::error::Error; + +use cardano_chain_follower::{ConfigBuilder, Follower, Network, Point}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let config = ConfigBuilder::default().build(); + + let mut follower = Follower::connect( + "relays-new.cardano-mainnet.iohk.io:3001", + Network::Mainnet, + config, + ) + .await?; + + let data = follower + .fetch_block(Point::Specific( + 110908236, + hex::decode("ad3798a1db2b6097c71f35609399e4b2ff834f0f45939803d563bf9d660df2f2")?, + )) + .await?; + + let block = data.decode()?; + + let total_fee = block + .txs() + .iter() + .map(|tx| tx.fee().unwrap_or_default()) + .sum::(); + + println!("Total fee: {total_fee}"); + + Ok(()) +} diff --git a/hermes/crates/cardano-chain-follower/examples/fetch_block_range.rs b/hermes/crates/cardano-chain-follower/examples/fetch_block_range.rs new file mode 100644 index 000000000..25acc1dda --- /dev/null +++ b/hermes/crates/cardano-chain-follower/examples/fetch_block_range.rs @@ -0,0 +1,41 @@ +//! This example shows how to use the chain follower to download arbitrary blocks +//! from the chain. + +use std::error::Error; + +use cardano_chain_follower::{ConfigBuilder, Follower, Network, Point}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let config = ConfigBuilder::default().build(); + + let mut follower = Follower::connect( + "relays-new.cardano-mainnet.iohk.io:3001", + Network::Mainnet, + config, + ) + .await?; + + let datas = follower + .fetch_block_range( + Point::Specific( + 110908236, + hex::decode("ad3798a1db2b6097c71f35609399e4b2ff834f0f45939803d563bf9d660df2f2")?, + ), + Point::Specific( + 110908582, + hex::decode("16e97a73e866280582ee1201a5e1815993978eede956af1869b0733bedc131f2")?, + ), + ) + .await?; + + let mut total_txs = 0; + for data in datas { + let block = data.decode()?; + total_txs = block.tx_count(); + } + + println!("Total transactions: {total_txs}"); + + Ok(()) +}