Skip to content

Commit

Permalink
chore: Add fetch block and fetch block range examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Rosa committed Dec 13, 2023
1 parent dfd6d80 commit 903f777
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
37 changes: 37 additions & 0 deletions hermes/crates/cardano-chain-follower/examples/fetch_block.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
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::<u64>();

println!("Total fee: {total_fee}");

Ok(())
}
41 changes: 41 additions & 0 deletions hermes/crates/cardano-chain-follower/examples/fetch_block_range.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
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(())
}

0 comments on commit 903f777

Please sign in to comment.