-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement Rpc namespace * Review comment * Dependency fix --------- Co-authored-by: Mikhail Sozin <[email protected]>
- Loading branch information
Showing
6 changed files
with
83 additions
and
0 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
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,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::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 | ||
} | ||
} |
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
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,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); | ||
} | ||
} |