Skip to content

Commit

Permalink
Implement Rpc namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Sozin committed Jun 29, 2024
1 parent 7b47529 commit 17a5ddf
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ alloy-rpc-types-beacon = { version = "0.1", path = "crates/rpc-types-beacon", de
alloy-rpc-types-engine = { version = "0.1", path = "crates/rpc-types-engine", default-features = false }
alloy-rpc-types-eth = { version = "0.1", path = "crates/rpc-types-eth", default-features = false }
alloy-rpc-types-mev = { version = "0.1", path = "crates/rpc-types-mev", default-features = false }
alloy-rpc-types-rpc = { version = "0.1", path = "crates/rpc-types-rpc", default-features = false }
alloy-rpc-types-trace = { version = "0.1", path = "crates/rpc-types-trace", default-features = false }
alloy-rpc-types-txpool = { version = "0.1", path = "crates/rpc-types-txpool", default-features = false }
alloy-rpc-types = { version = "0.1", path = "crates/rpc-types", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ alloy-rpc-types-admin = { workspace = true, optional = true }
alloy-rpc-types-anvil = { workspace = true, optional = true }
alloy-rpc-types-eth.workspace = true
alloy-rpc-types-trace = { workspace = true, optional = true }
alloy-rpc-types-rpc = { workspace = true, optional = true }
alloy-rpc-types-txpool = { workspace = true, optional = true }
alloy-rpc-types-engine = { workspace = true, optional = true }
alloy-transport-http = { workspace = true, optional = true }
Expand Down Expand Up @@ -98,4 +99,5 @@ debug-api = ["dep:alloy-rpc-types-trace"]
engine-api = ["dep:alloy-rpc-types-engine"]
net-api = []
trace-api = ["dep:alloy-rpc-types-trace"]
rpc-api = ["dep:alloy-rpc-types-rpc"]
txpool-api = ["dep:alloy-rpc-types-txpool"]
5 changes: 5 additions & 0 deletions crates/provider/src/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ mod trace;
#[cfg(feature = "trace-api")]
pub use trace::{TraceApi, TraceCallList};

#[cfg(feature = "rpc-api")]
mod rpc;
#[cfg(feature = "rpc-api")]
pub use rpc::RpcApi;

#[cfg(feature = "txpool-api")]
mod txpool;
#[cfg(feature = "txpool-api")]
Expand Down
27 changes: 27 additions & 0 deletions crates/provider/src/ext/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! This module extends the Ethereum JSON-RPC provider with the Rpc namespace's RPC methods.
use crate::Provider;
use alloy_network::Network;
use alloy_rpc_types_rpc::RpcModules;
use alloy_transport::{Transport, TransportResult};

/// The rpc API provides methods to get information about the RPC server itself, such as the enabled
/// namespaces.
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait RpcApi<N, T>: Send + Sync {
/// Lists the enabled RPC namespaces and the versions of each.
async fn rpc_modules(&self) -> TransportResult<RpcModules>;
}

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl<N, T, P> RpcApi<N, T> for P
where
N: Network,
T: Transport + Clone,
P: Provider<T, N>,
{
async fn rpc_modules(&self) -> TransportResult<RpcModules> {
self.client().request("rpc_modules", ()).await
}
}
28 changes: 28 additions & 0 deletions crates/rpc-types-rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "alloy-rpc-types-rpc"
description = "Ethereum RPC rpc types"

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

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lints]
workspace = true

[dependencies]
serde = { workspace = true, features = ["derive", "std"]}

[dev-dependencies]
serde_json.workspace = true



3 changes: 3 additions & 0 deletions crates/rpc-types-rpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# alloy-rpc-types-rpc

Types for the `rpc` Ethereum JSON-RPC namespace.
10 changes: 10 additions & 0 deletions crates/rpc-types-rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg",
html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico"
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

mod rpc;
pub use rpc::*;
42 changes: 42 additions & 0 deletions crates/rpc-types-rpc/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Types for the `rpc` API.
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Represents the `rpc_modules` response, which returns the
/// list of all available modules on that transport and their version
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(transparent)]
pub struct RpcModules {
module_map: HashMap<String, String>,
}

impl RpcModules {
/// Create a new instance of `RPCModules`
pub const fn new(module_map: HashMap<String, String>) -> Self {
Self { module_map }
}

/// Consumes self and returns the inner hashmap mapping module names to their versions
pub fn into_modules(self) -> HashMap<String, String> {
self.module_map
}
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_module_versions_roundtrip() {
let s = r#"{"txpool":"1.0","trace":"1.0","eth":"1.0","web3":"1.0","net":"1.0"}"#;
let module_map = HashMap::from([
("txpool".to_owned(), "1.0".to_owned()),
("trace".to_owned(), "1.0".to_owned()),
("eth".to_owned(), "1.0".to_owned()),
("web3".to_owned(), "1.0".to_owned()),
("net".to_owned(), "1.0".to_owned()),
]);
let m = RpcModules::new(module_map);
let de_serialized: RpcModules = serde_json::from_str(s).unwrap();
assert_eq!(de_serialized, m);
}
}
2 changes: 2 additions & 0 deletions crates/rpc-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ alloy-rpc-types-engine = { workspace = true, optional = true }
alloy-rpc-types-eth = { workspace = true, optional = true }
alloy-rpc-types-mev = { workspace = true, optional = true }
alloy-rpc-types-trace = { workspace = true, optional = true }
alloy-rpc-types-rpc = { workspace = true, optional = true }
alloy-rpc-types-txpool = { workspace = true, optional = true }

[features]
Expand All @@ -38,6 +39,7 @@ engine = ["dep:alloy-rpc-types-engine"]
eth = ["dep:alloy-rpc-types-eth"]
mev = ["dep:alloy-rpc-types-mev"]
trace = ["dep:alloy-rpc-types-trace"]
rpc = ["dep:alloy-rpc-types-rpc"]
txpool = ["dep:alloy-rpc-types-txpool"]

arbitrary = ["alloy-rpc-types-eth?/arbitrary", "alloy-serde/arbitrary"]
Expand Down
3 changes: 3 additions & 0 deletions crates/rpc-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ pub use alloy_rpc_types_mev as mev;
#[cfg(feature = "trace")]
pub use alloy_rpc_types_trace as trace;

#[cfg(feature = "rpc")]
pub use alloy_rpc_types_rpc as rpc;

#[cfg(feature = "txpool")]
pub use alloy_rpc_types_txpool as txpool;

0 comments on commit 17a5ddf

Please sign in to comment.