Sitelet https://github.com/njelich/examples/commit/2fb3d2aa52c50bc0648469a287a091b6a02144aa
Skip to content

Commit 2fb3d2a

Browse files
authored
chore(clean up): update use of Anvil instances (alloy-rs#143)
* clean up * update more examples * clean up * clean up
1 parent 57284fd commit 2fb3d2a

44 files changed

Lines changed: 151 additions & 280 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎examples/advanced/examples/any_network.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,22 @@ async fn main() -> Result<()> {
4747
let signer: PrivateKeySigner = "<PRIVATE_KEY>".parse().expect("should parse private key");
4848
let wallet = EthereumWallet::from(signer);
4949

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

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

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

65+
// Fetch the `gasUsedForL1` and `l1BlockNumber` fields from the receipt.
6266
let arb_fields: ArbOtherFields = receipt.other.deserialize_into()?;
6367
let l1_gas = arb_fields.gas_used_for_l1.to::<u128>();
6468
let l1_block_number = arb_fields.l1_block_number.to::<u64>();

‎examples/advanced/examples/encoding_dyn_abi.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! [EIP712](https://eips.ethereum.org/EIPS/eip-712) encoding and decoding via `dyn_abi`
1+
//! Example of [EIP712](https://eips.ethereum.org/EIPS/eip-712) encoding and decoding via `dyn_abi`.
22
33
use alloy::{
44
dyn_abi::{DynSolType, DynSolValue},

‎examples/advanced/examples/encoding_sol_static.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Example for static encoding calldata via `sol!`
1+
//! Example for static encoding calldata via `sol!`.
22
33
use std::str::FromStr;
44

@@ -9,7 +9,7 @@ use alloy::{
99
sol_types::SolCall,
1010
};
1111

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

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

4242
let swap_data =
4343
swapExactTokensForTokensCall::new((amount_in, amount_out_min, path, to, deadline));

‎examples/contracts/examples/deploy_from_artifact.rs‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
//! Example of deploying a contract from an artifact using the `sol!` macro to Anvil and interacting
22
//! with it.
33
4-
use alloy::{
5-
network::EthereumWallet, node_bindings::Anvil, primitives::U256, providers::ProviderBuilder,
6-
signers::local::PrivateKeySigner, sol,
7-
};
4+
use alloy::{primitives::U256, providers::ProviderBuilder, sol};
85
use eyre::Result;
96

107
// Codegen from artifact.
@@ -19,18 +16,7 @@ sol!(
1916
async fn main() -> Result<()> {
2017
// Spin up a local Anvil node.
2118
// Ensure `anvil` is available in $PATH.
22-
let anvil = Anvil::new().try_spawn()?;
23-
24-
// Set up signer from the first default Anvil account (Alice).
25-
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
26-
let wallet = EthereumWallet::from(signer);
27-
28-
// Create a provider with the wallet.
29-
let rpc_url = anvil.endpoint().parse()?;
30-
let provider =
31-
ProviderBuilder::new().with_recommended_fillers().wallet(wallet).on_http(rpc_url);
32-
33-
println!("Anvil running at `{}`", anvil.endpoint());
19+
let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil_with_wallet();
3420

3521
// Deploy the `Counter` contract.
3622
let contract = Counter::deploy(&provider).await?;

‎examples/contracts/examples/deploy_from_bytecode.rs‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
44
use alloy::{
55
hex,
6-
network::{EthereumWallet, ReceiptResponse, TransactionBuilder},
7-
node_bindings::Anvil,
6+
network::{ReceiptResponse, TransactionBuilder},
87
primitives::U256,
98
providers::{Provider, ProviderBuilder},
109
rpc::types::TransactionRequest,
11-
signers::local::PrivateKeySigner,
1210
sol,
1311
};
1412
use eyre::Result;
@@ -36,18 +34,7 @@ sol! {
3634
async fn main() -> Result<()> {
3735
// Spin up a local Anvil node.
3836
// Ensure `anvil` is available in $PATH.
39-
let anvil = Anvil::new().try_spawn()?;
40-
41-
// Set up signer from the first default Anvil account (Alice).
42-
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
43-
let wallet = EthereumWallet::from(signer);
44-
45-
println!("Anvil running at `{}`", anvil.endpoint());
46-
47-
// Create a provider with the wallet.
48-
let rpc_url = anvil.endpoint().parse()?;
49-
let provider =
50-
ProviderBuilder::new().with_recommended_fillers().wallet(wallet).on_http(rpc_url);
37+
let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil_with_wallet();
5138

5239
// Deploy the `Counter` contract from bytecode at runtime.
5340
let bytecode = hex::decode(

‎examples/contracts/examples/deploy_from_contract.rs‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
//! Example of deploying a contract from Solidity code using the `sol!` macro to Anvil and
22
//! interacting with it.
33
4-
use alloy::{
5-
network::EthereumWallet, node_bindings::Anvil, primitives::U256, providers::ProviderBuilder,
6-
signers::local::PrivateKeySigner, sol,
7-
};
4+
use alloy::{primitives::U256, providers::ProviderBuilder, sol};
85
use eyre::Result;
96

107
// Codegen from embedded Solidity code and precompiled bytecode.
@@ -29,18 +26,7 @@ sol! {
2926
async fn main() -> Result<()> {
3027
// Spin up a local Anvil node.
3128
// Ensure `anvil` is available in $PATH.
32-
let anvil = Anvil::new().try_spawn()?;
33-
34-
// Set up signer from the first default Anvil account (Alice).
35-
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
36-
let wallet = EthereumWallet::from(signer);
37-
38-
// Create a provider with the wallet.
39-
let rpc_url = anvil.endpoint().parse()?;
40-
let provider =
41-
ProviderBuilder::new().with_recommended_fillers().wallet(wallet).on_http(rpc_url);
42-
43-
println!("Anvil running at `{}`", anvil.endpoint());
29+
let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil_with_wallet();
4430

4531
// Deploy the `Counter` contract.
4632
let contract = Counter::deploy(&provider).await?;

‎examples/contracts/examples/interact_with_abi.rs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Example of generating code from ABI file using the `sol!` macro to interact with the contract.
22
3-
use alloy::{node_bindings::Anvil, primitives::address, providers::ProviderBuilder, sol};
3+
use alloy::{primitives::address, providers::ProviderBuilder, sol};
44
use eyre::Result;
55

66
// Codegen from ABI file to interact with the contract.
@@ -15,11 +15,9 @@ sol!(
1515
async fn main() -> Result<()> {
1616
// Spin up a forked Anvil node.
1717
// Ensure `anvil` is available in $PATH.
18-
let anvil = Anvil::new().fork("https://eth.merkle.io").try_spawn()?;
19-
20-
// Create a provider.
21-
let rpc_url = anvil.endpoint().parse()?;
22-
let provider = ProviderBuilder::new().on_http(rpc_url);
18+
let rpc_url = "https://eth.merkle.io";
19+
let provider =
20+
ProviderBuilder::new().on_anvil_with_wallet_and_config(|anvil| anvil.fork(rpc_url));
2321

2422
// Create a contract instance.
2523
let contract = IWETH9::new(address!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"), provider);

‎examples/contracts/examples/interact_with_contract_instance.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use eyre::Result;
1414

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

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

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

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

‎examples/contracts/examples/unknown_return_types.rs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ use eyre::Result;
1414

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

19-
let from = provider.get_accounts().await?[0];
21+
// Get the first account from the wallet, Alice.
22+
let alice = provider.get_accounts().await?[0];
2023

2124
let bytecode = hex::decode(
2225
// contract Colors {
@@ -44,7 +47,7 @@ async fn main() -> Result<()> {
4447
// }
4548
"6080604052348015600f57600080fd5b506105fb8061001f6000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c8063610c76f01461005157806384b5e5961461006d57806399efff171461009d578063befdb4f6146100cf575b600080fd5b61006b60048036038101906100669190610435565b610101565b005b610087600480360381019061008291906104e6565b6101ce565b6040516100949190610564565b60405180910390f35b6100b760048036038101906100b291906104e6565b61027d565b6040516100c69392919061058e565b60405180910390f35b6100e960048036038101906100e491906104e6565b61037c565b6040516100f89392919061058e565b60405180910390f35b60405180606001604052808460ff1681526020018360ff1681526020018260ff168152506000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008201518160000160006101000a81548160ff021916908360ff16021790555060208201518160000160016101000a81548160ff021916908360ff16021790555060408201518160000160026101000a81548160ff021916908360ff160217905550905050505050565b6101d66103cd565b6000808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206040518060600160405290816000820160009054906101000a900460ff1660ff1660ff1681526020016000820160019054906101000a900460ff1660ff1660ff1681526020016000820160029054906101000a900460ff1660ff1660ff16815250509050919050565b60008060008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060000160009054906101000a900460ff166000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060000160019054906101000a900460ff166000808773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060000160029054906101000a900460ff169250925092509193909250565b60006020528060005260406000206000915090508060000160009054906101000a900460ff16908060000160019054906101000a900460ff16908060000160029054906101000a900460ff16905083565b6040518060600160405280600060ff168152602001600060ff168152602001600060ff1681525090565b600080fd5b600060ff82169050919050565b610412816103fc565b811461041d57600080fd5b50565b60008135905061042f81610409565b92915050565b60008060006060848603121561044e5761044d6103f7565b5b600061045c86828701610420565b935050602061046d86828701610420565b925050604061047e86828701610420565b9150509250925092565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006104b382610488565b9050919050565b6104c3816104a8565b81146104ce57600080fd5b50565b6000813590506104e0816104ba565b92915050565b6000602082840312156104fc576104fb6103f7565b5b600061050a848285016104d1565b91505092915050565b61051c816103fc565b82525050565b6060820160008201516105386000850182610513565b50602082015161054b6020850182610513565b50604082015161055e6040850182610513565b50505050565b60006060820190506105796000830184610522565b92915050565b610588816103fc565b82525050565b60006060820190506105a3600083018661057f565b6105b0602083018561057f565b6105bd604083018461057f565b94935050505056fea2646970667358221220ce426adf2fbf80a861f23a5eb1e99a281bb07e427b9beed059e09c285f16db6c64736f6c634300081a0033"
4649
)?;
47-
let deploy_tx = TransactionRequest::default().from(from).with_deploy_code(bytecode);
50+
let deploy_tx = TransactionRequest::default().from(alice).with_deploy_code(bytecode);
4851

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

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

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

8992
// Get the color as tuple.
9093
let get_color_tuple =
91-
counter_instance.function("getColorAsTuple", &[DynSolValue::Address(from)])?;
94+
counter_instance.function("getColorAsTuple", &[DynSolValue::Address(alice)])?;
9295
let get_color_tuple_result = get_color_tuple.call().await?;
9396

9497
// The `r`, `g`, `b` are returned as a solidity tuple and hence represented as individual

‎examples/fillers/examples/gas_filler.rs‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
11
//! Example of using the `GasFiller` in the provider.
22
33
use alloy::{
4-
network::{EthereumWallet, TransactionBuilder},
5-
node_bindings::Anvil,
4+
network::TransactionBuilder,
65
primitives::{address, U256},
76
providers::{Provider, ProviderBuilder},
87
rpc::types::request::TransactionRequest,
9-
signers::local::PrivateKeySigner,
108
};
119
use eyre::Result;
1210

1311
#[tokio::main]
1412
async fn main() -> Result<()> {
1513
// Spin up a local Anvil node.
1614
// Ensure `anvil` is available in $PATH.
17-
let anvil = Anvil::new().try_spawn()?;
18-
19-
// Set up signer from the first default Anvil account (Alice).
20-
let signer: PrivateKeySigner = anvil.keys()[0].clone().into();
21-
let wallet = EthereumWallet::from(signer);
22-
23-
// Create a provider with the wallet.
24-
let rpc_url = anvil.endpoint().parse()?;
2515
let provider = ProviderBuilder::new()
2616
// Add the `GasFiller` to the provider.
2717
// It is generally recommended to use the `.with_recommended_fillers()` method, which
2818
// includes the `GasFiller`.
2919
.with_gas_estimation()
30-
.wallet(wallet)
31-
.on_http(rpc_url);
20+
.on_anvil_with_wallet();
3221

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

4332
// Send the transaction, the nonce (0) is automatically managed by the provider.
4433
let builder = provider.send_transaction(tx.clone()).await?;

0 commit comments

Comments
 (0)