Skip to content

Commit

Permalink
chore(clean up): update use of Anvil instances (#143)
Browse files Browse the repository at this point in the history
* clean up

* update more examples

* clean up

* clean up
  • Loading branch information
zerosnacks authored Sep 19, 2024
1 parent 57284fd commit 2fb3d2a
Show file tree
Hide file tree
Showing 44 changed files with 151 additions and 280 deletions.
4 changes: 4 additions & 0 deletions examples/advanced/examples/any_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,22 @@ async fn main() -> Result<()> {
let signer: PrivateKeySigner = "<PRIVATE_KEY>".parse().expect("should parse private key");
let wallet = EthereumWallet::from(signer);

// Create a provider with the Arbitrum Sepolia network and the wallet.
let rpc_url = "https://sepolia-rollup.arbitrum.io/rpc".parse()?;
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.network::<AnyNetwork>()
.wallet(wallet)
.on_http(rpc_url);

// Create a contract instance.
let contract = Counter::new(COUNTER_CONTRACT_ADDRESS, &provider);

// Set the number to 42.
let builder = contract.setNumber(U256::from(42));
let receipt = builder.send().await?.get_receipt().await?;

// Fetch the `gasUsedForL1` and `l1BlockNumber` fields from the receipt.
let arb_fields: ArbOtherFields = receipt.other.deserialize_into()?;
let l1_gas = arb_fields.gas_used_for_l1.to::<u128>();
let l1_block_number = arb_fields.l1_block_number.to::<u64>();
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/examples/encoding_dyn_abi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! [EIP712](https://eips.ethereum.org/EIPS/eip-712) encoding and decoding via `dyn_abi`
//! Example of [EIP712](https://eips.ethereum.org/EIPS/eip-712) encoding and decoding via `dyn_abi`.

use alloy::{
dyn_abi::{DynSolType, DynSolValue},
Expand Down
6 changes: 3 additions & 3 deletions examples/advanced/examples/encoding_sol_static.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Example for static encoding calldata via `sol!`
//! Example for static encoding calldata via `sol!`.

use std::str::FromStr;

Expand All @@ -9,7 +9,7 @@ use alloy::{
sol_types::SolCall,
};

// Using UniswapV2 `swapExactTokensForTokens()` method for this example
// Using UniswapV2 `swapExactTokensForTokens()` method for this example.
// See: https://docs.uniswap.org/contracts/v2/reference/smart-contracts/router-02#swapexacttokensfortokens
sol!(
#[allow(missing_docs)]
Expand Down Expand Up @@ -37,7 +37,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let to = Address::from_str("0x742d35Cc6634C0532925a3b844Bc454e4438f44e")?;

// Unix timestamp after which the transaction will revert.
let deadline = U256::from(1690000000u64); // random timestamp
let deadline = U256::from(1690000000u64); // Random timestamp

let swap_data =
swapExactTokensForTokensCall::new((amount_in, amount_out_min, path, to, deadline));
Expand Down
18 changes: 2 additions & 16 deletions examples/contracts/examples/deploy_from_artifact.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Example of deploying a contract from an artifact using the `sol!` macro to Anvil and interacting
//! with it.

use alloy::{
network::EthereumWallet, node_bindings::Anvil, primitives::U256, providers::ProviderBuilder,
signers::local::PrivateKeySigner, sol,
};
use alloy::{primitives::U256, providers::ProviderBuilder, sol};
use eyre::Result;

// Codegen from artifact.
Expand All @@ -19,18 +16,7 @@ sol!(
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let anvil = Anvil::new().try_spawn()?;

// Set up signer from the first default Anvil account (Alice).
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let rpc_url = anvil.endpoint().parse()?;
let provider =
ProviderBuilder::new().with_recommended_fillers().wallet(wallet).on_http(rpc_url);

println!("Anvil running at `{}`", anvil.endpoint());
let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil_with_wallet();

// Deploy the `Counter` contract.
let contract = Counter::deploy(&provider).await?;
Expand Down
17 changes: 2 additions & 15 deletions examples/contracts/examples/deploy_from_bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

use alloy::{
hex,
network::{EthereumWallet, ReceiptResponse, TransactionBuilder},
node_bindings::Anvil,
network::{ReceiptResponse, TransactionBuilder},
primitives::U256,
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
signers::local::PrivateKeySigner,
sol,
};
use eyre::Result;
Expand Down Expand Up @@ -36,18 +34,7 @@ sol! {
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let anvil = Anvil::new().try_spawn()?;

// Set up signer from the first default Anvil account (Alice).
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
let wallet = EthereumWallet::from(signer);

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

// Create a provider with the wallet.
let rpc_url = anvil.endpoint().parse()?;
let provider =
ProviderBuilder::new().with_recommended_fillers().wallet(wallet).on_http(rpc_url);
let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil_with_wallet();

// Deploy the `Counter` contract from bytecode at runtime.
let bytecode = hex::decode(
Expand Down
18 changes: 2 additions & 16 deletions examples/contracts/examples/deploy_from_contract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Example of deploying a contract from Solidity code using the `sol!` macro to Anvil and
//! interacting with it.

use alloy::{
network::EthereumWallet, node_bindings::Anvil, primitives::U256, providers::ProviderBuilder,
signers::local::PrivateKeySigner, sol,
};
use alloy::{primitives::U256, providers::ProviderBuilder, sol};
use eyre::Result;

// Codegen from embedded Solidity code and precompiled bytecode.
Expand All @@ -29,18 +26,7 @@ sol! {
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let anvil = Anvil::new().try_spawn()?;

// Set up signer from the first default Anvil account (Alice).
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let rpc_url = anvil.endpoint().parse()?;
let provider =
ProviderBuilder::new().with_recommended_fillers().wallet(wallet).on_http(rpc_url);

println!("Anvil running at `{}`", anvil.endpoint());
let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil_with_wallet();

// Deploy the `Counter` contract.
let contract = Counter::deploy(&provider).await?;
Expand Down
10 changes: 4 additions & 6 deletions examples/contracts/examples/interact_with_abi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Example of generating code from ABI file using the `sol!` macro to interact with the contract.

use alloy::{node_bindings::Anvil, primitives::address, providers::ProviderBuilder, sol};
use alloy::{primitives::address, providers::ProviderBuilder, sol};
use eyre::Result;

// Codegen from ABI file to interact with the contract.
Expand All @@ -15,11 +15,9 @@ sol!(
async fn main() -> Result<()> {
// Spin up a forked Anvil node.
// Ensure `anvil` is available in $PATH.
let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?;

// Create a provider.
let rpc_url = anvil.endpoint().parse()?;
let provider = ProviderBuilder::new().on_http(rpc_url);
let rpc_url = "https://eth.merkle.io";
let provider =
ProviderBuilder::new().on_anvil_with_wallet_and_config(|anvil| anvil.fork(rpc_url));

// Create a contract instance.
let contract = IWETH9::new(address!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"), provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil_with_wallet();

// Deploy the `Counter` contract from bytecode at runtime.
Expand Down Expand Up @@ -76,7 +78,7 @@ async fn main() -> Result<()> {

println!("Retrieved number: {number}");

// Try calling a function that does not exist
// Try calling a function that does not exist.
let unknown_function = contract.function("decrement", &[]).unwrap_err();
assert!(unknown_function.to_string().contains("function decrement does not exist"));

Expand Down
11 changes: 7 additions & 4 deletions examples/contracts/examples/unknown_return_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil_with_wallet();

let from = provider.get_accounts().await?[0];
// Get the first account from the wallet, Alice.
let alice = provider.get_accounts().await?[0];

let bytecode = hex::decode(
// contract Colors {
Expand Down Expand Up @@ -44,7 +47,7 @@ async fn main() -> Result<()> {
// }
"6080604052348015600f57600080fd5b506105fb8061001f6000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c8063610c76f01461005157806384b5e5961461006d57806399efff171461009d578063befdb4f6146100cf575b600080fd5b61006b60048036038101906100669190610435565b610101565b005b610087600480360381019061008291906104e6565b6101ce565b6040516100949190610564565b60405180910390f35b6100b760048036038101906100b291906104e6565b61027d565b6040516100c69392919061058e565b60405180910390f35b6100e960048036038101906100e491906104e6565b61037c565b6040516100f89392919061058e565b60405180910390f35b60405180606001604052808460ff1681526020018360ff1681526020018260ff168152506000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008201518160000160006101000a81548160ff021916908360ff16021790555060208201518160000160016101000a81548160ff021916908360ff16021790555060408201518160000160026101000a81548160ff021916908360ff160217905550905050505050565b6101d66103cd565b6000808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206040518060600160405290816000820160009054906101000a900460ff1660ff1660ff1681526020016000820160019054906101000a900460ff1660ff1660ff1681526020016000820160029054906101000a900460ff1660ff1660ff16815250509050919050565b60008060008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060000160009054906101000a900460ff166000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060000160019054906101000a900460ff166000808773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060000160029054906101000a900460ff169250925092509193909250565b60006020528060005260406000206000915090508060000160009054906101000a900460ff16908060000160019054906101000a900460ff16908060000160029054906101000a900460ff16905083565b6040518060600160405280600060ff168152602001600060ff168152602001600060ff1681525090565b600080fd5b600060ff82169050919050565b610412816103fc565b811461041d57600080fd5b50565b60008135905061042f81610409565b92915050565b60008060006060848603121561044e5761044d6103f7565b5b600061045c86828701610420565b935050602061046d86828701610420565b925050604061047e86828701610420565b9150509250925092565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006104b382610488565b9050919050565b6104c3816104a8565b81146104ce57600080fd5b50565b6000813590506104e0816104ba565b92915050565b6000602082840312156104fc576104fb6103f7565b5b600061050a848285016104d1565b91505092915050565b61051c816103fc565b82525050565b6060820160008201516105386000850182610513565b50602082015161054b6020850182610513565b50604082015161055e6040850182610513565b50505050565b60006060820190506105796000830184610522565b92915050565b610588816103fc565b82525050565b60006060820190506105a3600083018661057f565b6105b0602083018561057f565b6105bd604083018461057f565b94935050505056fea2646970667358221220ce426adf2fbf80a861f23a5eb1e99a281bb07e427b9beed059e09c285f16db6c64736f6c634300081a0033"
)?;
let deploy_tx = TransactionRequest::default().from(from).with_deploy_code(bytecode);
let deploy_tx = TransactionRequest::default().from(alice).with_deploy_code(bytecode);

let contract_address = provider
.send_transaction(deploy_tx)
Expand Down Expand Up @@ -75,7 +78,7 @@ async fn main() -> Result<()> {
assert!(set_color_receipt.status());

// Get the color.
let get_color_func = counter_instance.function("getColor", &[DynSolValue::Address(from)])?;
let get_color_func = counter_instance.function("getColor", &[DynSolValue::Address(alice)])?;
let get_color_result = get_color_func.call().await?;

// The `r`, `g`, `b` values in the `Color` struct get converted to a `DynSolValue::Tuple`.
Expand All @@ -88,7 +91,7 @@ async fn main() -> Result<()> {

// Get the color as tuple.
let get_color_tuple =
counter_instance.function("getColorAsTuple", &[DynSolValue::Address(from)])?;
counter_instance.function("getColorAsTuple", &[DynSolValue::Address(alice)])?;
let get_color_tuple_result = get_color_tuple.call().await?;

// The `r`, `g`, `b` are returned as a solidity tuple and hence represented as individual
Expand Down
17 changes: 3 additions & 14 deletions examples/fillers/examples/gas_filler.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
//! Example of using the `GasFiller` in the provider.

use alloy::{
network::{EthereumWallet, TransactionBuilder},
node_bindings::Anvil,
network::TransactionBuilder,
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::request::TransactionRequest,
signers::local::PrivateKeySigner,
};
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let anvil = Anvil::new().try_spawn()?;

// Set up signer from the first default Anvil account (Alice).
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let rpc_url = anvil.endpoint().parse()?;
let provider = ProviderBuilder::new()
// Add the `GasFiller` to the provider.
// It is generally recommended to use the `.with_recommended_fillers()` method, which
// includes the `GasFiller`.
.with_gas_estimation()
.wallet(wallet)
.on_http(rpc_url);
.on_anvil_with_wallet();

// Build an EIP-1559 type transaction to send 100 wei to Vitalik.
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
Expand All @@ -38,7 +27,7 @@ async fn main() -> Result<()> {
// Notice that without the `NonceFiller`, you need to set `nonce` field.
.with_nonce(0)
// Notice that without the `ChainIdFiller`, you need to set the `chain_id` field.
.with_chain_id(anvil.chain_id());
.with_chain_id(provider.get_chain_id().await?);

// Send the transaction, the nonce (0) is automatically managed by the provider.
let builder = provider.send_transaction(tx.clone()).await?;
Expand Down
17 changes: 3 additions & 14 deletions examples/fillers/examples/nonce_filler.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! Example of using the `NonceFiller` in the provider.

use alloy::{
network::{EthereumWallet, TransactionBuilder},
node_bindings::Anvil,
network::TransactionBuilder,
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::request::TransactionRequest,
signers::local::PrivateKeySigner,
};
use eyre::Result;

Expand All @@ -23,14 +21,6 @@ use eyre::Result;
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let anvil = Anvil::new().try_spawn()?;

// Set up signer from the first default Anvil account (Alice).
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let rpc_url = anvil.endpoint().parse()?;
let provider = ProviderBuilder::new()
// Add the `NonceFiller` to the provider.
// It is generally recommended to use the `.with_recommended_fillers()` method, which
Expand All @@ -42,8 +32,7 @@ async fn main() -> Result<()> {
// reorganizations.
.with_cached_nonce_management()
// .with_simple_nonce_management()
.wallet(wallet)
.on_http(rpc_url);
.on_anvil_with_wallet();

// Build an EIP-1559 type transaction to send 100 wei to Vitalik.
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
Expand All @@ -55,7 +44,7 @@ async fn main() -> Result<()> {
.with_max_fee_per_gas(20_000_000_000)
.with_max_priority_fee_per_gas(1_000_000_000)
// Notice that without the `ChainIdFiller`, you need to set the `chain_id` field.
.with_chain_id(anvil.chain_id());
.with_chain_id(provider.get_chain_id().await?);

// Send the transaction, the nonce (0) is automatically managed by the provider.
let builder = provider.send_transaction(tx.clone()).await?;
Expand Down
16 changes: 3 additions & 13 deletions examples/fillers/examples/recommended_fillers.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
//! Example of using the `.with_recommended_fillers()` method in the provider.

use alloy::{
network::{EthereumWallet, TransactionBuilder},
node_bindings::Anvil,
network::TransactionBuilder,
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::request::TransactionRequest,
signers::local::PrivateKeySigner,
};
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let anvil = Anvil::new().try_spawn()?;

// Set up signer from the first default Anvil account (Alice).
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let rpc_url = anvil.endpoint().parse()?;
let provider = ProviderBuilder::new()
// Adds the `ChainIdFiller`, `GasFiller` and the `NonceFiller` layers.
// This is the recommended way to set up the provider.
.with_recommended_fillers()
.wallet(wallet)
.on_http(rpc_url);
.on_anvil_with_wallet();

// Build an EIP-1559 type transaction to send 100 wei to Vitalik.
// Notice that the `nonce` field is set by the `NonceFiller`.
Expand Down
2 changes: 1 addition & 1 deletion examples/fillers/examples/wallet_filler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn main() -> Result<()> {
let wallet = EthereumWallet::from(signer);

// Create a provider with the wallet.
let rpc_url = anvil.endpoint().parse()?;
let rpc_url = anvil.endpoint_url();
let provider = ProviderBuilder::new()
// Add the `WalletFiller` to the provider
.wallet(wallet)
Expand Down
10 changes: 7 additions & 3 deletions examples/layers/examples/logging_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,23 @@ where

Box::pin(async move {
let res = fut.await;

println!("Response: {res:?}");

res
})
}
}

#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let anvil = Anvil::new().spawn();
let client = ClientBuilder::default().layer(LoggingLayer).http(anvil.endpoint_url());

// Create a new client with the logging layer.
let rpc_url = anvil.endpoint_url();
let client = ClientBuilder::default().layer(LoggingLayer).http(rpc_url);

// Create a new provider with the client.
let provider = ProviderBuilder::new().on_client(client);

for _ in 0..10 {
Expand Down
Loading

0 comments on commit 2fb3d2a

Please sign in to comment.