Skip to content

Commit

Permalink
feat(TempProvider): raw_request (#45)
Browse files Browse the repository at this point in the history
* feat(): raw_request

* chore: remove unneeded async block
  • Loading branch information
Evalir authored and onbjerg committed Nov 25, 2023
1 parent 8ad7b7e commit 0702253
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions crates/providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use alloy_transport::{BoxTransport, Transport, TransportErrorKind, TransportResu
use alloy_transport_http::Http;
use auto_impl::auto_impl;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use thiserror::Error;

#[derive(Debug, Error, Serialize, Deserialize)]
Expand Down Expand Up @@ -238,6 +238,16 @@ pub trait TempProvider: Send + Sync {
) -> TransportResult<Vec<LocalizedTransactionTrace>>
where
Self: Sync;

async fn raw_request<P, R>(
&self,
method: &'static str,
params: P,
) -> TransportResult<R>
where
P: Serialize + Send + Sync + Clone,
R: Serialize + DeserializeOwned + Send + Sync + Unpin + 'static,
Self: Sync;
}

impl<T: Transport + Clone + Send + Sync> Provider<T> {
Expand Down Expand Up @@ -663,6 +673,22 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner.prepare("trace_block", block).await
}

/// Sends a raw request with the methods and params specified to the internal connection,
/// and returns the result.
async fn raw_request<P, R>(
&self,
method: &'static str,
params: P,
) -> TransportResult<R>
where
P: Serialize + Send + Sync + Clone,
R: Serialize + DeserializeOwned + Send + Sync + Unpin + 'static,
Self: Sync
{
let res: R = self.inner.prepare(method, &params).await?;
Ok(res)
}

#[cfg(feature = "anvil")]
async fn set_code(&self, address: Address, code: &'static str) -> TransportResult<()>
where
Expand Down Expand Up @@ -708,7 +734,7 @@ mod providers_test {
utils,
};
use alloy_primitives::{address, b256, U256, U64};
use alloy_rpc_types::{BlockNumberOrTag, Filter};
use alloy_rpc_types::{BlockNumberOrTag, Filter, Block};
use ethers_core::utils::Anvil;

#[tokio::test]
Expand All @@ -719,6 +745,14 @@ mod providers_test {
assert_eq!(0, num)
}

#[tokio::test]
async fn gets_block_number_with_raw_req() {
let anvil = Anvil::new().spawn();
let provider = Provider::try_from(&anvil.endpoint()).unwrap();
let num: U64 = provider.raw_request("eth_blockNumber", ()).await.unwrap();
assert_eq!(0, num.to::<u64>())
}

#[tokio::test]
async fn gets_transaction_count() {
let anvil = Anvil::new().spawn();
Expand All @@ -745,6 +779,18 @@ mod providers_test {
assert_eq!(block.header.hash.unwrap(), hash);
}

#[tokio::test]
async fn gets_block_by_hash_with_raw_req() {
let anvil = Anvil::new().spawn();
let provider = Provider::try_from(&anvil.endpoint()).unwrap();
let num = 0;
let tag: BlockNumberOrTag = num.into();
let block = provider.get_block_by_number(tag, true).await.unwrap().unwrap();
let hash = block.header.hash.unwrap();
let block: Block = provider.raw_request::<(alloy_primitives::FixedBytes<32>, bool), Block>("eth_getBlockByHash", (hash, true)).await.unwrap();
assert_eq!(block.header.hash.unwrap(), hash);
}

#[tokio::test]
async fn gets_block_by_number_full() {
let anvil = Anvil::new().spawn();
Expand Down

0 comments on commit 0702253

Please sign in to comment.