-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from alloy-rs/prestwich/transports
Add a transports crate & initial Network abstraction
- Loading branch information
Showing
28 changed files
with
2,099 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
# alloy-next | ||
|
||
### Layout | ||
|
||
- alloy-json-rpc | ||
- Core data types for JSON-RPC 2.0 | ||
- alloy-transports | ||
- Transports and RPC call futures. | ||
- alloy-networks | ||
- Network abstraction for RPC types. Allows capturing different RPC param and response types on a per-network basis. | ||
- alloy-provider | ||
- Based on ethers::middleware::Middleware, but abstract over <N>, and object-safe. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "alloy-json-rpc" | ||
version.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
exclude.workspace = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
serde = { workspace = true, features = ["derive"] } | ||
serde_json = { version = "1.0.103", features = ["raw_value"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// A JSON-RPC 2.0 ID object. This may be a number, string, or null. | ||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] | ||
#[serde(untagged)] | ||
pub enum Id { | ||
Number(u64), | ||
String(String), | ||
None, | ||
} | ||
|
||
impl Id { | ||
/// Returns `true` if the ID is a number. | ||
pub fn is_number(&self) -> bool { | ||
matches!(self, Id::Number(_)) | ||
} | ||
|
||
/// Returns `true` if the ID is a string. | ||
pub fn is_string(&self) -> bool { | ||
matches!(self, Id::String(_)) | ||
} | ||
|
||
/// Returns `true` if the ID is `None`. | ||
pub fn is_none(&self) -> bool { | ||
matches!(self, Id::None) | ||
} | ||
|
||
/// Returns the ID as a number, if it is one. | ||
pub fn as_number(&self) -> Option<u64> { | ||
match self { | ||
Id::Number(n) => Some(*n), | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Returns the ID as a string, if it is one. | ||
pub fn as_string(&self) -> Option<&str> { | ||
match self { | ||
Id::String(s) => Some(s), | ||
_ => None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//! Alloy JSON-RPC data types. | ||
//! | ||
//! This crate provides data types for use with the JSON-RPC 2.0 protocol. It | ||
//! does not provide any functionality for actually sending or receiving | ||
//! JSON-RPC data. | ||
//! | ||
//! This crate is aimed at simplifying client implementations. It is not | ||
//! well-suited to in-server applications. We do not support borrowing data from | ||
//! deserializers, for example. This choice prevents complex lifetime | ||
//! propagation in user code, at the expense of copying data | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
|
||
mod request; | ||
pub use request::JsonRpcRequest; | ||
|
||
mod response; | ||
pub use response::{ErrorPayload, JsonRpcResponse, ResponsePayload}; | ||
|
||
mod common; | ||
pub use common::Id; | ||
|
||
mod result; | ||
pub use result::RpcResult; | ||
|
||
/// An object that can be used as a JSON-RPC parameter. | ||
pub trait RpcParam: Serialize + Clone + Send + Sync + Unpin {} | ||
impl<T> RpcParam for T where T: Serialize + Clone + Send + Sync + Unpin {} | ||
|
||
/// An object that can be used as a JSON-RPC return value. | ||
// Note: we add `'static` here to indicate that the Resp is wholly owned. It | ||
// may not borrow. | ||
pub trait RpcReturn: DeserializeOwned + Send + Sync + Unpin + 'static {} | ||
impl<T> RpcReturn for T where T: DeserializeOwned + Send + Sync + Unpin + 'static {} | ||
|
||
/// An object that can be used as a JSON-RPC parameter and return value. | ||
pub trait RpcObject: RpcParam + RpcReturn {} | ||
impl<T> RpcObject for T where T: RpcParam + RpcReturn {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::{common::Id, RpcParam}; | ||
|
||
use serde::{ser::SerializeMap, Deserialize, Serialize}; | ||
|
||
/// A JSON-RPC 2.0 request object. | ||
/// | ||
/// This is a generic type that can be used to represent any JSON-RPC request. | ||
/// The `Params` type parameter is used to represent the parameters of the | ||
/// request, and the `method` field is used to represent the method name. | ||
/// | ||
/// ### Note | ||
/// | ||
/// The value of `method` must be known at compile time. | ||
#[derive(Debug, Deserialize, Clone)] | ||
pub struct JsonRpcRequest<Params> { | ||
pub method: &'static str, | ||
pub params: Params, | ||
pub id: Id, | ||
} | ||
|
||
impl<Params> Serialize for JsonRpcRequest<Params> | ||
where | ||
Params: RpcParam, | ||
{ | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
let mut map = serializer.serialize_map(Some(4))?; | ||
map.serialize_entry("method", self.method)?; | ||
map.serialize_entry("params", &self.params)?; | ||
map.serialize_entry("id", &self.id)?; | ||
map.serialize_entry("jsonrpc", "2.0")?; | ||
map.end() | ||
} | ||
} |
Oops, something went wrong.