-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(forehose-client): add fetch beacon block example
- Loading branch information
1 parent
77fff9b
commit 32ea66e
Showing
2 changed files
with
58 additions
and
57 deletions.
There are no files selected for viewing
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,58 @@ | ||
//! # Fetch Beacon Block | ||
//! | ||
//! Demonstrates how to fetch a single block from Beacon Firehose, using the `Fetch` API. | ||
|
||
use firehose_client::{Chain, FirehoseClient}; | ||
use sf_protos::{ | ||
beacon::r#type::v1::Block as FirehoseBeaconBlock, | ||
ethereum::r#type::v2::Block as FirehoseEthBlock, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn main() { | ||
// Show matching data from execution layer and beacon chain | ||
let mut execution_layer_client = FirehoseClient::new(Chain::Ethereum); | ||
|
||
let response = execution_layer_client | ||
.fetch_block(20672593) | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
|
||
let block = FirehoseEthBlock::try_from(response.into_inner()).unwrap(); | ||
|
||
assert_eq!(block.number, 20672593); | ||
assert_eq!( | ||
format!("0x{}", hex::encode(block.hash)).as_str(), | ||
"0xea48ba1c8e38ea586239e9c5ec62949ddd79404c6006c099bb02a8b22ddd18e4" | ||
); | ||
|
||
let mut beacon_client = FirehoseClient::new(Chain::Beacon); | ||
// This is the slot number for the Beacon block we want to fetch, but right now | ||
// we don't have a way to map the block number of the execution block to the slot number | ||
// of the Beacon block. | ||
let response = beacon_client.fetch_block(9881091).await.unwrap().unwrap(); | ||
let block = FirehoseBeaconBlock::try_from(response.into_inner()).unwrap(); | ||
|
||
assert_eq!(block.slot, 9881091); | ||
|
||
let body = block.body.as_ref().unwrap(); | ||
|
||
match body { | ||
Body::Deneb(body) => { | ||
let execution_payload = body.execution_payload.as_ref().unwrap(); | ||
|
||
let block_hash = &execution_payload.block_hash; | ||
|
||
assert_eq!( | ||
format!("0x{}", hex::encode(block_hash)).as_str(), | ||
"0xea48ba1c8e38ea586239e9c5ec62949ddd79404c6006c099bb02a8b22ddd18e4" | ||
); | ||
|
||
let block_number = execution_payload.block_number; | ||
|
||
assert_eq!(block_number, 20672593); | ||
} | ||
_ => unimplemented!(), | ||
}; | ||
} |
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