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

providers: Add temporary bare Provider #14

Merged
merged 19 commits into from
Nov 1, 2023
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
30 changes: 22 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,29 @@ jobs:
matrix:
rust: ["stable", "beta", "nightly", "1.65"] # MSRV
flags: ["--no-default-features", "", "--all-features"]
exclude:
# Skip because some features have highest MSRV.
- rust: "1.65" # MSRV
flags: "--all-features"
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- uses: Swatinem/rust-cache@v2
- name: test
run: cargo test --workspace ${{ matrix.flags }}

- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- name: Install Anvil
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
# Only run tests on latest stable and above
- name: build
if: ${{ matrix.rust == '1.65' }} # MSRV
run: cargo build --workspace ${{ matrix.flags }}
- name: test
if: ${{ matrix.rust != '1.65' }} # MSRV
run: cargo test --workspace ${{ matrix.flags }}
wasm:
name: check WASM
runs-on: ubuntu-latest
Expand Down
60 changes: 58 additions & 2 deletions crates/json-rpc/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,69 @@ impl RequestPacket {
}

/// A [`ResponsePacket`] is a [`Response`] or a batch of responses.
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
#[derive(Debug, Clone)]
pub enum ResponsePacket<Payload = Box<RawValue>, ErrData = Box<RawValue>> {
Single(Response<Payload, ErrData>),
Batch(Vec<Response<Payload, ErrData>>),
}

use serde::de::{self, Deserializer, MapAccess, SeqAccess, Visitor};
use std::fmt;
use std::marker::PhantomData;

impl<'de, Payload, ErrData> Deserialize<'de> for ResponsePacket<Payload, ErrData>
where
Payload: Deserialize<'de>,
ErrData: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct ResponsePacketVisitor<Payload, ErrData> {
marker: PhantomData<fn() -> ResponsePacket<Payload, ErrData>>,
}

impl<'de, Payload, ErrData> Visitor<'de> for ResponsePacketVisitor<Payload, ErrData>
where
Payload: Deserialize<'de>,
ErrData: Deserialize<'de>,
{
type Value = ResponsePacket<Payload, ErrData>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a single response or a batch of responses")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut responses = Vec::new();

while let Some(response) = seq.next_element()? {
responses.push(response);
}

Ok(ResponsePacket::Batch(responses))
}

fn visit_map<M>(self, map: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let response =
Deserialize::deserialize(de::value::MapAccessDeserializer::new(map))?;
Ok(ResponsePacket::Single(response))
}
}

deserializer.deserialize_any(ResponsePacketVisitor {
marker: PhantomData,
})
}
}

/// A [`BorrowedResponsePacket`] is a [`ResponsePacket`] that has been partially
/// deserialized, borrowing its contents from the deserializer. This is used
/// primarily for intermediate deserialization. Most users will not require it.
Expand Down
2 changes: 1 addition & 1 deletion crates/json-rpc/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where

/// Serialize the request, including the request parameters.
pub fn serialize(self) -> serde_json::Result<SerializedRequest> {
let request = serde_json::to_string(&self.params)?;
let request = serde_json::to_string(&self)?;
Ok(SerializedRequest {
meta: self.meta,
request: RawValue::from_string(request)?,
Expand Down
13 changes: 13 additions & 0 deletions crates/providers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ alloy-json-rpc.workspace = true
alloy-networks.workspace = true
alloy-primitives.workspace = true
alloy-transports.workspace = true
alloy-rpc-types.workspace = true
async-trait = "0.1.73"
futures-util = "0.3.28"
serde_json = { workspace = true, features = ["raw_value"] }
serde = { workspace = true, features = ["derive"] }
thiserror = "1.0"
once_cell = "1.17"
reqwest = "0.11.22"
serial_test = "2.0.0"

[dev-dependencies]
tokio = { version = "1.33.0", features = ["macros"] }
ethers-core = "2.0.10"

[features]
anvil = []
3 changes: 3 additions & 0 deletions crates/providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use alloy_primitives::Address;
use alloy_transports::{BoxTransport, RpcClient, Transport, TransportError};
use serde_json::value::RawValue;

pub mod provider;
pub mod utils;

use std::{borrow::Cow, marker::PhantomData};

/// A network-wrapped RPC client.
Expand Down
Loading