From f6412f87d7b5d2307c312beea76ea442be440b68 Mon Sep 17 00:00:00 2001 From: Alex Lee Date: Thu, 19 Aug 2021 22:04:11 +0800 Subject: [PATCH] add uncle related missing api (#385) Co-authored-by: Alex --- ethers-providers/src/lib.rs | 21 ++++++++++++++++ ethers-providers/src/provider.rs | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/ethers-providers/src/lib.rs b/ethers-providers/src/lib.rs index 43b5f120..70638a41 100644 --- a/ethers-providers/src/lib.rs +++ b/ethers-providers/src/lib.rs @@ -333,6 +333,27 @@ pub trait Middleware: Sync + Send + Debug { .map_err(FromErr::from) } + async fn get_uncle_count + Send + Sync>( + &self, + block_hash_or_number: T, + ) -> Result { + self.inner() + .get_uncle_count(block_hash_or_number) + .await + .map_err(FromErr::from) + } + + async fn get_uncle + Send + Sync>( + &self, + block_hash_or_number: T, + idx: U64, + ) -> Result>, Self::Error> { + self.inner() + .get_uncle(block_hash_or_number, idx) + .await + .map_err(FromErr::from) + } + async fn get_transaction_count + Send + Sync>( &self, from: T, diff --git a/ethers-providers/src/provider.rs b/ethers-providers/src/provider.rs index f10d1ea6..facb3f66 100644 --- a/ethers-providers/src/provider.rs +++ b/ethers-providers/src/provider.rs @@ -227,6 +227,47 @@ impl Middleware for Provider

{ 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 + Send + Sync>( + &self, + block_hash_or_number: T, + ) -> Result { + 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 + Send + Sync>( + &self, + block_hash_or_number: T, + idx: U64, + ) -> Result>, 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>( &self,