Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TempProvider): raw_request #45

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Changes from 1 commit
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
54 changes: 52 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,26 @@ 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 = async move {
let res: R = self.inner.prepare(method, &params).await?;
Ok(res)
}.await;

res
Evalir marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(feature = "anvil")]
async fn set_code(&self, address: Address, code: &'static str) -> TransportResult<()>
where
Expand Down Expand Up @@ -708,7 +738,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 +749,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 +783,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
Loading