Skip to content

Commit 5ee09be

Browse files
authored
feat(derive): Latest BN (op-rs#521)
* feat: add latest block number to chain provider for op-rs * fix: use upstream provider types
1 parent 3050746 commit 5ee09be

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

crates/derive/src/online/alloy_providers.rs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use alloy_provider::{Provider, ReqwestProvider};
88
use alloy_rlp::{Buf, Decodable};
99
use alloy_transport::{RpcError, TransportErrorKind, TransportResult};
1010
use async_trait::async_trait;
11-
use core::{num::NonZeroUsize, str::FromStr};
11+
use core::num::NonZeroUsize;
1212
use lru::LruCache;
1313
use op_alloy_genesis::{RollupConfig, SystemConfig};
1414
use op_alloy_protocol::{BlockInfo, L2BlockInfo};
@@ -60,17 +60,14 @@ impl AlloyChainProvider {
6060
Self::new(inner)
6161
}
6262

63+
/// Returns the latest L2 block number.
64+
pub async fn latest_block_number(&mut self) -> Result<u64, RpcError<TransportErrorKind>> {
65+
self.inner.get_block_number().await
66+
}
67+
6368
/// Returns the chain ID.
6469
pub async fn chain_id(&mut self) -> Result<u64, RpcError<TransportErrorKind>> {
65-
let chain_id: TransportResult<alloc::string::String> =
66-
self.inner.raw_request("eth_chainId".into(), ()).await;
67-
let chain_id = match chain_id {
68-
Ok(s) => {
69-
U64::from_str(s.as_str()).map_err(|e| RpcError::LocalUsageError(Box::new(e)))?
70-
}
71-
Err(e) => return Err(e),
72-
};
73-
Ok(chain_id.to::<u64>())
70+
self.inner.get_chain_id().await
7471
}
7572
}
7673

@@ -294,26 +291,12 @@ impl AlloyL2ChainProvider {
294291

295292
/// Returns the chain ID.
296293
pub async fn chain_id(&mut self) -> Result<u64, RpcError<TransportErrorKind>> {
297-
let chain_id: TransportResult<alloc::string::String> =
298-
self.inner.raw_request("eth_chainId".into(), ()).await;
299-
let chain_id = match chain_id {
300-
Ok(s) => {
301-
U64::from_str(s.as_str()).map_err(|e| RpcError::LocalUsageError(Box::new(e)))?
302-
}
303-
Err(e) => return Err(e),
304-
};
305-
Ok(chain_id.to::<u64>())
294+
self.inner.get_chain_id().await
306295
}
307296

308297
/// Returns the latest L2 block number.
309298
pub async fn latest_block_number(&mut self) -> Result<u64, RpcError<TransportErrorKind>> {
310-
let s = self
311-
.inner
312-
.raw_request::<(), alloc::string::String>("eth_blockNumber".into(), ())
313-
.await?;
314-
U64::from_str(s.as_str())
315-
.map_err(|e| RpcError::LocalUsageError(Box::new(e)))
316-
.map(|u| u.to::<u64>())
299+
self.inner.get_block_number().await
317300
}
318301

319302
/// Creates a new [AlloyL2ChainProvider] from the provided [reqwest::Url].

0 commit comments

Comments
 (0)