Skip to content

Commit

Permalink
feat(alloy-providers): additional missing methods (#184)
Browse files Browse the repository at this point in the history
* feat(providers): additional missing methods

* comments
  • Loading branch information
Evalir committed Feb 5, 2024
1 parent 2be6214 commit 6d457a5
Showing 1 changed file with 32 additions and 0 deletions.
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

0 comments on commit 6d457a5

Please sign in to comment.