Skip to content

Commit

Permalink
feat: add RPC types + Add temporary bare Provider (alloy-rs#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evalir authored Nov 1, 2023
1 parent 0ebb433 commit 36b4a50
Show file tree
Hide file tree
Showing 44 changed files with 5,384 additions and 13 deletions.
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ rustdoc-args = ["--cfg", "docsrs"]
alloy-json-rpc = { version = "0.1.0", path = "crates/json-rpc" }
alloy-transports = { version = "0.1.0", path = "crates/transports" }
alloy-networks = { version = "0.1.0", path = "crates/networks" }
alloy-rpc-types = { version = "0.1.0", path = "crates/rpc-types" }

alloy-primitives = { version = "0.3.0", features = ["serde"] }
alloy-primitives = { version = "0.4.2", features = ["serde"] }
alloy-rlp = "0.3.0"

# futures
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

0 comments on commit 36b4a50

Please sign in to comment.