Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat(rpc): add getHeader by number+hash #2701

Merged
merged 1 commit into from
Feb 12, 2024
Merged

Conversation

alrevuelta
Copy link
Contributor

Add support for eth_getHeaderByNumber and eth_getHeaderByHash endpoints. To be used as follows:

use ethers_core::types::{BlockId, BlockNumber};
use ethers_providers::Middleware;
use ethers_providers::{Http, Provider};

async fn mytest() {
    let provider = Provider::<Http>::try_from("http://localhost:8545").unwrap();
    let by_num = BlockId::Number(BlockNumber::Number(18826806u64.into()));
    let by_hash = BlockId::Hash(
        "0xac45643453aba125a85a779163e7cfa40e4f8898639f1d5249de1c1481817510".parse().unwrap(),
    );
    // Use by_num or by_hash
    let block = provider.get_header(by_hash).await.unwrap();
    println!("block: {:?}", block);
}

Note that the same type Block is used, but non-header fields are left empty.

@DaniPopes DaniPopes requested a review from mattsse February 12, 2024 21:11
async fn get_header<T: Into<BlockId> + Send + Sync>(
&self,
block_hash_or_number: T,
) -> Result<Option<Block<Transaction>>, Self::Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while this is not quite right, because this returns a header, this will still succeed because the transactions will be empty.

since we don't have a header type I'm okay with this

@mattsse mattsse merged commit b249da3 into gakonst:master Feb 12, 2024
17 of 19 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants