Skip to content

Commit 8d432a6

Browse files
committed
chain/ethereum: migrate load_block_ptrs_by_numbers_rpc call to use alloy
1 parent ed10f44 commit 8d432a6

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
@@ -822,10 +822,10 @@ impl EthereumAdapter {
822822
logger: Logger,
823823
numbers: Vec<BlockNumber>,
824824
) -> impl futures03::Stream<Item = Result<Arc<ExtendedBlockPtr>, Error>> + Send {
825-
let web3 = self.web3.clone();
825+
let alloy = self.alloy.clone();
826826

827827
futures03::stream::iter(numbers.into_iter().map(move |number| {
828-
let web3 = web3.clone();
828+
let alloy = alloy.clone();
829829
let logger = logger.clone();
830830

831831
async move {
@@ -834,21 +834,22 @@ impl EthereumAdapter {
834834
.limit(ENV_VARS.request_retries)
835835
.timeout_secs(ENV_VARS.json_rpc_timeout.as_secs())
836836
.run(move || {
837-
let web3 = web3.clone();
837+
let alloy = alloy.cheap_clone();
838838

839839
async move {
840-
let block_result = web3
841-
.eth()
842-
.block(BlockId::Number(Web3BlockNumber::Number(number.into())))
840+
let block_result = alloy
841+
.get_block_by_number(alloy_rpc_types::BlockNumberOrTag::Number(
842+
number as u64,
843+
))
843844
.await;
844845

845846
match block_result {
846847
Ok(Some(block)) => {
847848
let ptr = ExtendedBlockPtr::try_from((
848-
block.hash,
849-
block.number,
850-
block.parent_hash,
851-
block.timestamp,
849+
block.header.hash,
850+
block.header.number as i32,
851+
block.header.parent_hash,
852+
block.header.timestamp,
852853
))
853854
.map_err(|e| {
854855
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)