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

feat: support web3_sha3 provider function #996

Merged
merged 11 commits into from
Jul 1, 2024
1 change: 1 addition & 0 deletions crates/alloy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ provider-anvil-node = [
"alloy-provider?/anvil-node",
"node-bindings",
]
provider-web3-api = ["providers", "alloy-provider?/web3-api"]

# pubsub
pubsub = [
Expand Down
1 change: 1 addition & 0 deletions crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ engine-api = ["dep:alloy-rpc-types-engine"]
net-api = []
trace-api = ["dep:alloy-rpc-types-trace"]
txpool-api = ["dep:alloy-rpc-types-txpool"]
web3-api = []
5 changes: 5 additions & 0 deletions crates/provider/src/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ pub use trace::{TraceApi, TraceCallList};
mod txpool;
#[cfg(feature = "txpool-api")]
pub use txpool::TxPoolApi;

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

/// Web3 namespace rpc interface that provides access to web3 information of the node.
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait Web3Api<N, T>: Send + Sync {
/// Gets the client version of the chain client.
async fn web3_client_version(&self) -> TransportResult<String>;
/// Gets the Keccak-256 hash of the given data.
async fn web3_sha3(&self, data: &[u8]) -> TransportResult<String>;
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl<N, T, P> Web3Api<N, T> for P
where
N: Network,
T: Transport + Clone,
P: Provider<T, N>,
{
async fn web3_client_version(&self) -> TransportResult<String> {
self.client().request("web3_clientVersion", ()).await
}

async fn web3_sha3(&self, data: &[u8]) -> TransportResult<String> {
self.client().request("web3_sha3", (data,)).await
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[cfg(test)]
mod tests {
use crate::ProviderBuilder;

use super::*;

#[tokio::test]
async fn test_web3_client_version() {
let provider = ProviderBuilder::new().on_anvil();
let version = provider.web3_client_version().await.unwrap();
assert!(!version.is_empty());
}

#[tokio::test]
async fn test_web3_sha3() {
let provider = ProviderBuilder::new().on_anvil();
let data = "alloy".as_bytes();
let hash = provider.web3_sha3(data).await.unwrap();
assert!(!hash.is_empty());
}
}