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(alloy-providers): additional missing methods #184

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions crates/providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ pub trait TempProvider: Send + Sync {
full: bool,
) -> TransportResult<Option<Block>>;

/// Gets the client version of the chain client.
async fn get_client_version(&self) -> TransportResult<String>;

/// Gets the chain ID.
async fn get_chain_id(&self) -> TransportResult<U64>;

/// Gets the network ID. Same as `eth_chainId`.
async fn get_net_version(&self) -> TransportResult<U64>;

/// Gets the specified storage value from [Address].
async fn get_storage_at(
&self,
Expand Down Expand Up @@ -276,11 +282,20 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner.prepare("eth_getBlockByNumber", (number, full)).await
}

/// Gets the client version of the chain client.
async fn get_client_version(&self) -> TransportResult<String> {
self.inner.prepare("web3_clientVersion", ()).await
}

/// Gets the chain ID.
async fn get_chain_id(&self) -> TransportResult<U64> {
self.inner.prepare("eth_chainId", ()).await
}

async fn get_net_version(&self) -> TransportResult<U64> {
self.inner.prepare("net_version", ()).await
}

/// Gets the specified storage value from [Address].
async fn get_storage_at(
&self,
Expand Down Expand Up @@ -613,6 +628,14 @@ mod tests {
assert_eq!(block.header.number.unwrap(), U256::from(num));
}

#[tokio::test]
async fn gets_client_version() {
let anvil = Anvil::new().spawn();
let provider = Provider::try_from(&anvil.endpoint()).unwrap();
let version = provider.get_client_version().await.unwrap();
assert!(version.contains("anvil"));
}

#[tokio::test]
async fn gets_chain_id() {
let chain_id: u64 = 13371337;
Expand All @@ -622,6 +645,15 @@ mod tests {
assert_eq!(chain_id, U64::from(chain_id));
}

#[tokio::test]
async fn gets_network_id() {
let chain_id: u64 = 13371337;
let anvil = Anvil::new().args(["--chain-id", chain_id.to_string().as_str()]).spawn();
let provider = Provider::try_from(&anvil.endpoint()).unwrap();
let chain_id = provider.get_net_version().await.unwrap();
assert_eq!(chain_id, U64::from(chain_id));
}

#[tokio::test]
#[cfg(feature = "anvil")]
async fn gets_code_at() {
Expand Down
Loading