add uncle related missing api (#385)

Co-authored-by: Alex <flashlee2@qq.com>
This commit is contained in:
Alex Lee 2021-08-19 22:04:11 +08:00 committed by GitHub
parent ad779c867f
commit f6412f87d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 0 deletions

View File

@ -333,6 +333,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,

View File

@ -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,