-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add fetch block and fetch block range examples
- Loading branch information
Felipe Rosa
committed
Dec 13, 2023
1 parent
dfd6d80
commit 903f777
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
hermes/crates/cardano-chain-follower/examples/fetch_block.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
hermes/crates/cardano-chain-follower/examples/fetch_block_range.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |