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

add uncle related missing api #385

Merged
merged 1 commit into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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