Skip to content

Commit

Permalink
chore: replace rpc urls with generic ones (#2199)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Feb 27, 2023
1 parent 5d15031 commit 319b86a
Show file tree
Hide file tree
Showing 25 changed files with 48 additions and 72 deletions.
4 changes: 1 addition & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ documentation test:
/// use ethers::providers::{JsonRpcClient, Provider, Http};
/// use std::convert::TryFrom;
///
/// let provider = Provider::<Http>::try_from(
/// "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27"
/// ).expect("could not instantiate HTTP Provider");
/// let provider = Provider::<Http>::try_from("https://eth.llamarpc.com").expect("could not instantiate HTTP Provider");
///
/// # async fn foo<P: JsonRpcClient>(provider: &Provider<P>) -> Result<(), Box<dyn std::error::Error>> {
/// let block = provider.get_block(100u64).await?;
Expand Down
22 changes: 11 additions & 11 deletions book/getting-started/connect_to_an_ethereum_node.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ Ethers-rs allows application to connect the blockchain using web3 providers. Pro

Some common actions you can perform using a provider include:

* Getting the current block number
* Getting the balance of an Ethereum address
* Sending a transaction to the blockchain
* Calling a smart contract function
* Subscribe logs and smart contract events
* Getting the transaction history of an address
- Getting the current block number
- Getting the balance of an Ethereum address
- Sending a transaction to the blockchain
- Calling a smart contract function
- Subscribe logs and smart contract events
- Getting the transaction history of an address

Providers are an important part of web3 libraries because they allow you to easily interact with the Ethereum blockchain without having to manage the underlying connection to the node yourself.

Code below shows a basic setup to connect a provider to a node:

```rust
// The `prelude` module provides a convenient way to import a number
// of common dependencies at once. This can be useful if you are working
// with multiple parts of the library and want to avoid having
// The `prelude` module provides a convenient way to import a number
// of common dependencies at once. This can be useful if you are working
// with multiple parts of the library and want to avoid having
// to import each dependency individually.
use ethers::prelude::*;

const RPC_URL: &str = "https://mainnet.infura.io/v3/your-project-id";
const RPC_URL: &str = "https://eth.llamarpc.com";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let provider = Provider::<Http>::try_from(RPC_URL)?;
let block_number: U64 = provider.get_block_number().await?;
println!("{block_number}");
Expand Down
6 changes: 3 additions & 3 deletions book/providers/advanced_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::sync::Arc;

#[tokio::main]
async fn main() -> eyre::Result<()> {
let rpc_url = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
let rpc_url = "https://eth.llamarpc.com";
let provider: Arc<Provider<Http>> = Arc::new(Provider::<Http>::try_from(rpc_url)?);

let from_adr: H160 = "0x6fC21092DA55B392b045eD78F4732bff3C580e2c".parse()?;
Expand Down Expand Up @@ -50,7 +50,7 @@ use std::sync::Arc;

#[tokio::main]
async fn main() -> eyre::Result<()> {
let rpc_url = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
let rpc_url = "https://eth.llamarpc.com";
let provider: Arc<Provider<Http>> = Arc::new(Provider::<Http>::try_from(rpc_url)?);

let from_adr: H160 = "0x6fC21092DA55B392b045eD78F4732bff3C580e2c".parse()?;
Expand Down Expand Up @@ -85,7 +85,7 @@ use std::sync::Arc;

#[tokio::main]
async fn main() -> eyre::Result<()> {
let rpc_url = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
let rpc_url = "https://eth.llamarpc.com";
let provider: Arc<Provider<Http>> = Arc::new(Provider::<Http>::try_from(rpc_url)?);

let from_adr: H160 = "0x6fC21092DA55B392b045eD78F4732bff3C580e2c".parse()?;
Expand Down
11 changes: 5 additions & 6 deletions book/providers/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ethers::providers::{Http, Middleware, Provider};
#[tokio::main]
async fn main() -> eyre::Result<()> {
// Initialize a new Http provider
let rpc_url = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
let rpc_url = "https://eth.llamarpc.com";
let provider = Provider::try_from(rpc_url)?;

Ok(())
Expand Down Expand Up @@ -60,7 +60,7 @@ use ethers::providers::{Http, Middleware, Provider};

#[tokio::main]
async fn main() -> eyre::Result<()> {
let rpc_url = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
let rpc_url = "https://eth.llamarpc.com";
let provider = Provider::try_from(rpc_url)?;

let chain_id = provider.get_chainid().await?;
Expand Down Expand Up @@ -88,7 +88,7 @@ abigen!(

#[tokio::main]
async fn main() -> eyre::Result<()> {
let rpc_url = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
let rpc_url = "https://eth.llamarpc.com";
let provider = Arc::new(Provider::try_from(rpc_url)?);

// Initialize a new instance of the Weth/Dai Uniswap V2 pair contract
Expand All @@ -108,13 +108,12 @@ This example is a little more complicated, so let's walk through what is going o
It is very common to wrap a provider in an `Arc` to share the provider across threads. Let's look at another example where the provider is used asynchronously across two tokio threads. In the next example, a new provider is initialized and used to asynchronously fetch the number of Ommer blocks from the most recent block, as well as the previous block.

```rust
use std::sync::Arc;

use ethers::providers::{Http, Middleware, Provider};
use std::sync::Arc;

#[tokio::main]
async fn main() -> eyre::Result<()> {
let rpc_url = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
let rpc_url = "https://eth.llamarpc.com";
let provider = Arc::new(Provider::try_from(rpc_url)?);

let current_block_number = provider.get_block_number().await?;
Expand Down
2 changes: 1 addition & 1 deletion ethers-contract/src/multicall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl MulticallVersion {
/// let abi: Abi = serde_json::from_str(r#"[{"inputs":[{"internalType":"string","name":"value","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"author","type":"address"},{"indexed":true,"internalType":"address","name":"oldAuthor","type":"address"},{"indexed":false,"internalType":"string","name":"oldValue","type":"string"},{"indexed":false,"internalType":"string","name":"newValue","type":"string"}],"name":"ValueChanged","type":"event"},{"inputs":[],"name":"getValue","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lastSender","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"value","type":"string"}],"name":"setValue","outputs":[],"stateMutability":"nonpayable","type":"function"}]"#)?;
///
/// // connect to the network
/// let client = Provider::<Http>::try_from("https://goerli.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27")?;
/// let client = Provider::<Http>::try_from("http://localhost:8545")?;
///
/// // create the contract object. This will be used to construct the calls for multicall
/// let client = Arc::new(client);
Expand Down
15 changes: 3 additions & 12 deletions ethers-providers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@ and can be overriden with the [`ens`](./struct.Provider.html#method.ens) method
# use ethers_core::types::Address;
# use ethers_providers::{Provider, Http, Middleware, Ws};
# async fn foo() -> Result<(), Box<dyn std::error::Error>> {
// HTTP Provider
let provider = Provider::<Http>::try_from(
"https://mainnet.infura.io/v3/YOUR_API_KEY"
)?;
// Websocket Provider
let provider = Provider::<Ws>::connect(
"wss://mainnet.infura.io/v3/YOUR_API_KEY"
).await?;
let provider = Provider::<Http>::try_from("https://eth.llamarpc.com")?;
let block = provider.get_block(100u64).await?;
println!("Got block: {}", serde_json::to_string(&block)?);
Expand All @@ -68,9 +60,8 @@ Using ENS:
```rust,no_run
# use ethers_providers::{Provider, Http, Middleware};
# async fn foo() -> Result<(), Box<dyn std::error::Error>> {
# let provider = Provider::<Http>::try_from(
# "https://mainnet.infura.io/v3/YOUR_API_KEY"
# )?;
let provider = Provider::<Http>::try_from("https://eth.llamarpc.com")?;
// Resolve ENS name to Address
let name = "vitalik.eth";
let address = provider.resolve_name(name).await?;
Expand Down
4 changes: 2 additions & 2 deletions ethers-providers/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub trait Middleware: Sync + Send + Debug {
/// # use std::convert::TryFrom;
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// # let provider = Provider::<HttpProvider>::try_from("https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27").unwrap();
/// # let provider = Provider::<HttpProvider>::try_from("https://eth.llamarpc.com").unwrap();
/// let avatar = provider.resolve_avatar("parishilton.eth").await.unwrap();
/// assert_eq!(avatar.to_string(), "https://i.imgur.com/YW3Hzph.jpg");
/// # }
Expand All @@ -264,7 +264,7 @@ pub trait Middleware: Sync + Send + Debug {
/// # use std::{str::FromStr, convert::TryFrom};
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// # let provider = Provider::<HttpProvider>::try_from("https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27").unwrap();
/// # let provider = Provider::<HttpProvider>::try_from("https://eth.llamarpc.com").unwrap();
/// let token = ethers_providers::erc::ERCNFT::from_str("erc721:0xc92ceddfb8dd984a89fb494c376f9a48b999aafc/9018").unwrap();
/// let token_image = provider.resolve_nft(token).await.unwrap();
/// assert_eq!(token_image.to_string(), "https://creature.mypinata.cloud/ipfs/QmNwj3aUzXfG4twV3no7hJRYxLLAWNPk6RrfQaqJ6nVJFa/9018.jpg");
Expand Down
10 changes: 5 additions & 5 deletions ethers-providers/src/rpc/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl FromStr for NodeClient {
/// use std::convert::TryFrom;
///
/// let provider = Provider::<Http>::try_from(
/// "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27"
/// "https://eth.llamarpc.com"
/// ).expect("could not instantiate HTTP Provider");
///
/// let block = provider.get_block(100u64).await?;
Expand Down Expand Up @@ -1387,20 +1387,20 @@ mod sealed {
///
/// Note that this will send an RPC to retrieve the chain id.
///
/// ```
/// ```no_run
/// # use ethers_providers::{Http, Provider, ProviderExt};
/// # async fn t() {
/// let http_provider = Provider::<Http>::connect("https://eth-mainnet.alchemyapi.io/v2/API_KEY").await;
/// let http_provider = Provider::<Http>::connect("https://eth.llamarpc.com").await;
/// # }
/// ```
///
/// This is essentially short for
///
/// ```
/// ```no_run
/// use std::convert::TryFrom;
/// use ethers_core::types::Chain;
/// use ethers_providers::{Http, Provider, ProviderExt};
/// let http_provider = Provider::<Http>::try_from("https://eth-mainnet.alchemyapi.io/v2/API_KEY").unwrap().set_chain(Chain::Mainnet);
/// let http_provider = Provider::<Http>::try_from("https://eth.llamarpc.com").unwrap().set_chain(Chain::Mainnet);
/// ```
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
Expand Down
3 changes: 1 addition & 2 deletions examples/anvil/examples/anvil_fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
// ensure `anvil` is available in $PATH
let anvil =
Anvil::new().fork("https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27").spawn();
let anvil = Anvil::new().fork("https://eth.llamarpc.com").spawn();

println!("Anvil running at `{}`", anvil.endpoint());

Expand Down
2 changes: 1 addition & 1 deletion examples/contracts/examples/abigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async fn rust_inline_generation() -> Result<()> {
]"#,
);

const RPC_URL: &str = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
const RPC_URL: &str = "https://eth.llamarpc.com";
const WETH_ADDRESS: &str = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";

let provider = Provider::<Http>::try_from(RPC_URL)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/contracts/examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abigen!(
]"#,
);

const WSS_URL: &str = "wss://mainnet.infura.io/ws/v3/c60b0bb42f8a4c6481ecd229eddaca27";
const WSS_URL: &str = "wss://eth.llamarpc.com";
const WETH_ADDRESS: &str = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";

#[tokio::main]
Expand Down
4 changes: 1 addition & 3 deletions examples/contracts/examples/events_with_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ abigen!(

#[tokio::main]
async fn main() -> Result<()> {
let client =
Provider::<Ws>::connect("wss://mainnet.infura.io/ws/v3/c60b0bb42f8a4c6481ecd229eddaca27")
.await?;
let client = Provider::<Ws>::connect("wss://eth.llamarpc.com").await?;

let client = Arc::new(client);

Expand Down
2 changes: 1 addition & 1 deletion examples/events/examples/filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ethers::{
use eyre::Result;
use std::sync::Arc;

const HTTP_URL: &str = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
const HTTP_URL: &str = "https://eth.llamarpc.com";
const V3FACTORY_ADDRESS: &str = "0x1F98431c8aD98523631AE4a59f267346ea31F984";
const DAI_ADDRESS: &str = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
const USDC_ADDRESS: &str = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
Expand Down
2 changes: 1 addition & 1 deletion examples/middleware/examples/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ethers::{
};
use std::convert::TryFrom;

const RPC_URL: &str = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
const RPC_URL: &str = "https://eth.llamarpc.com";
const SIGNING_KEY: &str = "fdb33e2105f08abe41a8ee3b758726a31abdd57b7a443f470f23efce853af169";

/// In ethers-rs, middleware is a way to customize the behavior of certain aspects of the library by
Expand Down
2 changes: 1 addition & 1 deletion examples/middleware/examples/gas_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async fn polygon() {
}

async fn provider_oracle() {
const RPC_URL: &str = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
const RPC_URL: &str = "https://eth.llamarpc.com";
let provider = Provider::<Http>::try_from(RPC_URL).unwrap();
let oracle = ProviderOracle::new(provider);
match oracle.fetch().await {
Expand Down
2 changes: 1 addition & 1 deletion examples/providers/examples/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ethers::prelude::*;
use reqwest::header::{HeaderMap, HeaderValue};
use std::sync::Arc;

const RPC_URL: &str = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
const RPC_URL: &str = "https://eth.llamarpc.com";

#[tokio::main]
async fn main() -> eyre::Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion examples/providers/examples/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ethers::prelude::*;
use reqwest::Url;
use std::time::Duration;

const RPC_URL: &str = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
const RPC_URL: &str = "https://eth.llamarpc.com";

#[tokio::main]
async fn main() -> eyre::Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion examples/providers/examples/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use ethers::prelude::*;

const WSS_URL: &str = "wss://mainnet.infura.io/ws/v3/c60b0bb42f8a4c6481ecd229eddaca27";
const WSS_URL: &str = "wss://eth.llamarpc.com";

#[tokio::main]
async fn main() -> eyre::Result<()> {
Expand Down
4 changes: 1 addition & 3 deletions examples/queries/examples/paginated_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<()> {
let client: Provider<Ws> =
Provider::<Ws>::connect("wss://mainnet.infura.io/ws/v3/c60b0bb42f8a4c6481ecd229eddaca27")
.await?;
let client: Provider<Ws> = Provider::<Ws>::connect("wss://eth.llamarpc.com").await?;
let client = Arc::new(client);

let last_block = client.get_block(BlockNumber::Latest).await?.unwrap().number.unwrap();
Expand Down
4 changes: 1 addition & 3 deletions examples/queries/examples/uniswapv2_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ abigen!(

#[tokio::main]
async fn main() -> Result<()> {
let client = Provider::<Http>::try_from(
"https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27",
)?;
let client = Provider::<Http>::try_from("https://eth.llamarpc.com")?;
let client = Arc::new(client);

// ETH/USDT pair on Uniswap V2
Expand Down
4 changes: 1 addition & 3 deletions examples/subscriptions/examples/subscribe_events_by_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,5 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

async fn get_client() -> Provider<Ws> {
Provider::<Ws>::connect("wss://mainnet.infura.io/ws/v3/c60b0bb42f8a4c6481ecd229eddaca27")
.await
.unwrap()
Provider::<Ws>::connect("wss://eth.llamarpc.com").await.unwrap()
}
4 changes: 1 addition & 3 deletions examples/subscriptions/examples/subscribe_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<()> {
let client =
Provider::<Ws>::connect("wss://mainnet.infura.io/ws/v3/c60b0bb42f8a4c6481ecd229eddaca27")
.await?;
let client = Provider::<Ws>::connect("wss://eth.llamarpc.com").await?;
let client = Arc::new(client);

let last_block = client.get_block(BlockNumber::Latest).await?.unwrap().number.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/subscriptions/examples/watch_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::Duration;

#[tokio::main]
async fn main() -> Result<()> {
let ws_endpoint = "wss://mainnet.infura.io/ws/v3/c60b0bb42f8a4c6481ecd229eddaca27";
let ws_endpoint = "wss://eth.llamarpc.com";
let ws = Ws::connect(ws_endpoint).await?;
let provider = Provider::new(ws).interval(Duration::from_millis(2000));
let mut stream = provider.watch_blocks().await?.take(1);
Expand Down
3 changes: 1 addition & 2 deletions examples/transactions/examples/ens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
// fork mainnet
let anvil =
Anvil::new().fork("https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27").spawn();
let anvil = Anvil::new().fork("https://eth.llamarpc.com").spawn();
let from = anvil.addresses()[0];
// connect to the network
let provider = Provider::<Http>::try_from(anvil.endpoint()).unwrap().with_sender(from);
Expand Down
2 changes: 1 addition & 1 deletion examples/transactions/examples/gas_price_usd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abigen!(
const ETH_DECIMALS: u32 = 18;
const USD_PRICE_DECIMALS: u32 = 8;
const ETH_USD_FEED: &str = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
const RPC_URI: &str = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27";
const RPC_URI: &str = "https://eth.llamarpc.com";

/// Retrieves the USD amount per gas unit, using a Chainlink price oracle.
/// Function gets the amount of `wei` to be spent per gas unit then multiplies
Expand Down

0 comments on commit 319b86a

Please sign in to comment.