2023-02-27 20:03:17 +00:00
|
|
|
# ethers-providers
|
|
|
|
|
|
|
|
Clients for interacting with Ethereum nodes.
|
2021-11-23 19:23:12 +00:00
|
|
|
|
|
|
|
This crate provides asynchronous
|
|
|
|
[Ethereum JSON-RPC](https://github.com/ethereum/wiki/wiki/JSON-RPC) compliant
|
|
|
|
clients.
|
|
|
|
|
2023-02-27 20:03:17 +00:00
|
|
|
For more information, please refer to the [book](https://gakonst.com/ethers-rs).
|
|
|
|
|
|
|
|
## Websockets
|
|
|
|
|
|
|
|
This crate supports for WebSockets via `tokio-tungstenite`.
|
|
|
|
Please ensure that you have the `ws` feature enabled if you wish to use WebSockets:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[dependencies]
|
|
|
|
ethers-providers = { version = "1.0.2", features = ["ws"] }
|
|
|
|
```
|
|
|
|
|
|
|
|
## Interprocess Communication (IPC)
|
|
|
|
|
|
|
|
This crate supports for Interprocess Communication via Unix sockets and Windows named pipes.
|
|
|
|
Please ensure that you have the `ipc` feature enabled if you wish to use IPC:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[dependencies]
|
|
|
|
ethers-providers = { version = "1.0.2", features = ["ipc"] }
|
|
|
|
```
|
|
|
|
|
|
|
|
## Ethereum Name Service
|
|
|
|
|
|
|
|
The provider may also be used to resolve [Ethereum Name Service](https://ens.domains) (ENS) names
|
|
|
|
to addresses (and vice versa).
|
|
|
|
The default ENS address is [`0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e`][ens]
|
|
|
|
and can be overriden with the [`ens`](./struct.Provider.html#method.ens) method on the provider.
|
2021-11-23 19:23:12 +00:00
|
|
|
|
2023-02-27 20:03:17 +00:00
|
|
|
[ens]: https://etherscan.io/address/0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e
|
2021-11-23 19:23:12 +00:00
|
|
|
|
2023-02-27 20:03:17 +00:00
|
|
|
## Examples
|
2021-11-23 19:23:12 +00:00
|
|
|
|
2023-02-27 20:03:17 +00:00
|
|
|
```rust,no_run
|
|
|
|
# use ethers_core::types::Address;
|
|
|
|
# use ethers_providers::{Provider, Http, Middleware, Ws};
|
2021-11-23 19:23:12 +00:00
|
|
|
# async fn foo() -> Result<(), Box<dyn std::error::Error>> {
|
2023-02-27 22:16:33 +00:00
|
|
|
let provider = Provider::<Http>::try_from("https://eth.llamarpc.com")?;
|
2023-02-27 20:03:17 +00:00
|
|
|
|
2021-11-23 19:23:12 +00:00
|
|
|
let block = provider.get_block(100u64).await?;
|
|
|
|
println!("Got block: {}", serde_json::to_string(&block)?);
|
|
|
|
|
2022-05-02 19:47:42 +00:00
|
|
|
let addr = "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359".parse::<Address>()?;
|
|
|
|
let code = provider.get_code(addr, None).await?;
|
2021-11-23 19:23:12 +00:00
|
|
|
println!("Got code: {}", serde_json::to_string(&code)?);
|
|
|
|
# Ok(())
|
|
|
|
# }
|
|
|
|
```
|
|
|
|
|
2023-02-27 20:03:17 +00:00
|
|
|
Using ENS:
|
2021-11-23 19:23:12 +00:00
|
|
|
|
2023-02-27 20:03:17 +00:00
|
|
|
```rust,no_run
|
2021-11-23 19:23:12 +00:00
|
|
|
# use ethers_providers::{Provider, Http, Middleware};
|
|
|
|
# async fn foo() -> Result<(), Box<dyn std::error::Error>> {
|
2023-02-27 22:16:33 +00:00
|
|
|
let provider = Provider::<Http>::try_from("https://eth.llamarpc.com")?;
|
|
|
|
|
2021-11-23 19:23:12 +00:00
|
|
|
// Resolve ENS name to Address
|
|
|
|
let name = "vitalik.eth";
|
|
|
|
let address = provider.resolve_name(name).await?;
|
|
|
|
|
|
|
|
// Lookup ENS name given Address
|
|
|
|
let resolved_name = provider.lookup_address(address).await?;
|
|
|
|
assert_eq!(name, resolved_name);
|
2022-02-16 14:25:41 +00:00
|
|
|
|
|
|
|
/// Lookup ENS field
|
|
|
|
let url = "https://vitalik.ca".to_string();
|
|
|
|
let resolved_url = provider.resolve_field(name, "url").await?;
|
|
|
|
assert_eq!(url, resolved_url);
|
|
|
|
|
|
|
|
/// Lookup and resolve ENS avatar
|
|
|
|
let avatar = "https://ipfs.io/ipfs/QmSP4nq9fnN9dAiCj42ug9Wa79rqmQerZXZch82VqpiH7U/image.gif".to_string();
|
|
|
|
let resolved_avatar = provider.resolve_avatar(name).await?;
|
|
|
|
assert_eq!(avatar, resolved_avatar.to_string());
|
2021-11-23 19:23:12 +00:00
|
|
|
# Ok(())
|
|
|
|
# }
|
|
|
|
```
|