Skip to content

Commit 975635c

Browse files
committed
chain/ethereum: migrate load_block_ptrs_by_numbers_rpc call to use alloy
1 parent 31a73d3 commit 975635c

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

chain/ethereum/src/ethereum_adapter.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -837,10 +837,10 @@ impl EthereumAdapter {
837837
logger: Logger,
838838
numbers: Vec<BlockNumber>,
839839
) -> impl futures03::Stream<Item = Result<Arc<ExtendedBlockPtr>, Error>> + Send {
840-
let web3 = self.web3.clone();
840+
let alloy = self.alloy.clone();
841841

842842
futures03::stream::iter(numbers.into_iter().map(move |number| {
843-
let web3 = web3.clone();
843+
let alloy = alloy.clone();
844844
let logger = logger.clone();
845845

846846
async move {
@@ -849,21 +849,22 @@ impl EthereumAdapter {
849849
.limit(ENV_VARS.request_retries)
850850
.timeout_secs(ENV_VARS.json_rpc_timeout.as_secs())
851851
.run(move || {
852-
let web3 = web3.clone();
852+
let alloy = alloy.cheap_clone();
853853

854854
async move {
855-
let block_result = web3
856-
.eth()
857-
.block(BlockId::Number(Web3BlockNumber::Number(number.into())))
855+
let block_result = alloy
856+
.get_block_by_number(alloy_rpc_types::BlockNumberOrTag::Number(
857+
number as u64,
858+
))
858859
.await;
859860

860861
match block_result {
861862
Ok(Some(block)) => {
862863
let ptr = ExtendedBlockPtr::try_from((
863-
block.hash,
864-
block.number,
865-
block.parent_hash,
866-
block.timestamp,
864+
block.header.hash,
865+
block.header.number as i32,
866+
block.header.parent_hash,
867+
block.header.timestamp,
867868
))
868869
.map_err(|e| {
869870
anyhow::anyhow!("Failed to convert block: {}", e)

graph/src/blockchain/types.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ impl From<Vec<u8>> for BlockHash {
9595
}
9696
}
9797

98+
impl From<alloy::primitives::B256> for BlockHash {
99+
fn from(hash: alloy::primitives::B256) -> Self {
100+
BlockHash(hash.0.to_vec().into())
101+
}
102+
}
103+
98104
impl TryFrom<&str> for BlockHash {
99105
type Error = anyhow::Error;
100106

@@ -510,6 +516,28 @@ impl TryFrom<(H256, i32, H256, U256)> for ExtendedBlockPtr {
510516
})
511517
}
512518
}
519+
520+
impl TryFrom<(alloy::primitives::B256, i32, alloy::primitives::B256, u64)> for ExtendedBlockPtr {
521+
type Error = anyhow::Error;
522+
523+
fn try_from(
524+
tuple: (alloy::primitives::B256, i32, alloy::primitives::B256, u64),
525+
) -> Result<Self, Self::Error> {
526+
let (hash, block_number, parent_hash, timestamp) = tuple;
527+
528+
// Convert timestamp to `BlockTime`
529+
let secs =
530+
i64::try_from(timestamp).map_err(|_| anyhow!("Timestamp out of range for i64"))?;
531+
let block_time = BlockTime::since_epoch(secs, 0);
532+
533+
Ok(ExtendedBlockPtr {
534+
hash: hash.into(),
535+
number: block_number,
536+
parent_hash: parent_hash.into(),
537+
timestamp: block_time,
538+
})
539+
}
540+
}
513541
impl From<ExtendedBlockPtr> for H256 {
514542
fn from(ptr: ExtendedBlockPtr) -> Self {
515543
ptr.hash_as_h256()

0 commit comments

Comments
 (0)