[go: nahoru, domu]

Skip to content

Commit

Permalink
feat(rpc): add getHeader by number+hash (#2701)
Browse files Browse the repository at this point in the history
  • Loading branch information
alrevuelta committed Feb 12, 2024
1 parent 40eaa41 commit b249da3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ethers-providers/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ pub trait Middleware: Sync + Send + Debug {
self.inner().get_block_number().await.map_err(MiddlewareError::from_err)
}

/// Get the block header by number or hash
async fn get_header<T: Into<BlockId> + Send + Sync>(
&self,
block_hash_or_number: T,
) -> Result<Option<Block<Transaction>>, Self::Error> {
self.inner().get_header(block_hash_or_number).await.map_err(MiddlewareError::from_err)
}

/// Sends the transaction to the entire Ethereum network and returns the
/// transaction's hash. This will consume gas from the account that signed
/// the transaction. This call will fail if no signer is available, and the
Expand Down
17 changes: 17 additions & 0 deletions ethers-providers/src/rpc/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,23 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
self.get_block_gen(block_hash_or_number.into(), false).await
}

async fn get_header<T: Into<BlockId> + Send + Sync>(
&self,
block_hash_or_number: T,
) -> Result<Option<Block<Transaction>>, Self::Error> {
let id = block_hash_or_number.into();
Ok(match id {
BlockId::Number(num) => {
let num = utils::serialize(&num);
self.request("eth_getHeaderByNumber", [num]).await?
}
BlockId::Hash(hash) => {
let hash = utils::serialize(&hash);
self.request("eth_getHeaderByHash", [hash]).await?
}
})
}

async fn get_block_with_txs<T: Into<BlockId> + Send + Sync>(
&self,
block_hash_or_number: T,
Expand Down

0 comments on commit b249da3

Please sign in to comment.