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

Commit

Permalink
add uncle related missing api
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlee85 committed Aug 19, 2021
1 parent 635236f commit 578ad0f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ethers-providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,27 @@ pub trait Middleware: Sync + Send + Debug {
.map_err(FromErr::from)
}

async fn get_uncle_count<T: Into<BlockId> + Send + Sync>(
&self,
block_hash_or_number: T,
) -> Result<U256, Self::Error> {
self.inner()
.get_uncle_count(block_hash_or_number)
.await
.map_err(FromErr::from)
}

async fn get_uncle<T: Into<BlockId> + Send + Sync>(
&self,
block_hash_or_number: T,
idx: U64,
) -> Result<Option<Block<H256>>, Self::Error> {
self.inner()
.get_uncle(block_hash_or_number, idx)
.await
.map_err(FromErr::from)
}

async fn get_transaction_count<T: Into<NameOrAddress> + Send + Sync>(
&self,
from: T,
Expand Down
41 changes: 41 additions & 0 deletions ethers-providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,47 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
self.get_block_gen(block_hash_or_number.into(), true).await
}

/// Gets the block uncle count at `block_hash_or_number`
async fn get_uncle_count<T: Into<BlockId> + Send + Sync>(
&self,
block_hash_or_number: T,
) -> Result<U256, Self::Error> {
let id = block_hash_or_number.into();
Ok(match id {
BlockId::Hash(hash) => {
let hash = utils::serialize(&hash);
self.request("eth_getUncleCountByBlockHash", [hash]).await?
}
BlockId::Number(num) => {
let num = utils::serialize(&num);
self.request("eth_getUncleCountByBlockNumber", [num])
.await?
}
})
}

/// Gets the block uncle at `block_hash_or_number` and `idx`
async fn get_uncle<T: Into<BlockId> + Send + Sync>(
&self,
block_hash_or_number: T,
idx: U64,
) -> Result<Option<Block<H256>>, ProviderError> {
let blk_id = block_hash_or_number.into();
let idx = utils::serialize(&idx);
Ok(match blk_id {
BlockId::Hash(hash) => {
let hash = utils::serialize(&hash);
self.request("eth_getUncleByBlockHashAndIndex", [hash, idx])
.await?
}
BlockId::Number(num) => {
let num = utils::serialize(&num);
self.request("eth_getUncleByBlockNumberAndIndex", [num, idx])
.await?
}
})
}

/// Gets the transaction with `transaction_hash`
async fn get_transaction<T: Send + Sync + Into<TxHash>>(
&self,
Expand Down

0 comments on commit 578ad0f

Please sign in to comment.