|
1 | 1 | //! Example of deploying a contract to Anvil and interacting with it. |
2 | 2 |
|
3 | 3 | use alloy::{ |
4 | | - network::EthereumSigner, |
5 | | - node_bindings::Anvil, |
6 | | - primitives::U256, |
7 | | - providers::{Provider, ProviderBuilder}, |
8 | | - signers::wallet::LocalWallet, |
9 | | - sol, |
| 4 | + network::EthereumSigner, node_bindings::Anvil, primitives::U256, providers::ProviderBuilder, |
| 5 | + signers::wallet::LocalWallet, sol, |
10 | 6 | }; |
11 | 7 | use eyre::Result; |
12 | 8 |
|
@@ -36,36 +32,32 @@ async fn main() -> Result<()> { |
36 | 32 |
|
37 | 33 | // Set up signer from the first default Anvil account (Alice). |
38 | 34 | let signer: LocalWallet = anvil.keys()[0].clone().into(); |
| 35 | + let from = signer.address(); |
39 | 36 |
|
40 | 37 | // Create a provider with a signer and the network. |
41 | 38 | let rpc_url = anvil.endpoint().parse()?; |
42 | | - let provider = |
43 | | - ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_reqwest_http(rpc_url)?; |
| 39 | + let provider = ProviderBuilder::new() |
| 40 | + .with_recommended_fillers() |
| 41 | + .signer(EthereumSigner::from(signer)) |
| 42 | + .on_http(rpc_url)?; |
44 | 43 |
|
45 | 44 | println!("Anvil running at `{}`", anvil.endpoint()); |
46 | 45 |
|
47 | | - // Get the base fee for the block. |
48 | | - let base_fee = provider.get_gas_price().await?; |
49 | | - |
50 | 46 | // Deploy the contract. |
51 | | - let contract_builder = Counter::deploy_builder(&provider); |
52 | | - let estimate = contract_builder.estimate_gas().await?; |
53 | | - let contract_address = |
54 | | - contract_builder.gas(estimate).gas_price(base_fee).nonce(0).deploy().await?; |
55 | | - |
56 | | - println!("Deployed contract at address: {contract_address:?}"); |
| 47 | + let contract_builder = Counter::deploy_builder(&provider).from(from); |
| 48 | + let contract_address = contract_builder.deploy().await?; |
| 49 | + let contract = Counter::new(contract_address, provider); |
57 | 50 |
|
58 | | - let contract = Counter::new(contract_address, &provider); |
| 51 | + println!("Deployed contract at address: {:?}", contract.address()); |
59 | 52 |
|
60 | | - let estimate = contract.setNumber(U256::from(42)).estimate_gas().await?; |
61 | | - let builder = contract.setNumber(U256::from(42)).nonce(1).gas(estimate).gas_price(base_fee); |
| 53 | + // Set the number to 42. |
| 54 | + let builder = contract.setNumber(U256::from(42)).from(from); |
62 | 55 | let receipt = builder.send().await?.get_receipt().await?; |
63 | 56 |
|
64 | 57 | println!("Set number to 42: {:?}", receipt.transaction_hash); |
65 | 58 |
|
66 | 59 | // Increment the number to 43. |
67 | | - let estimate = contract.increment().estimate_gas().await?; |
68 | | - let builder = contract.increment().nonce(2).gas(estimate).gas_price(base_fee); |
| 60 | + let builder = contract.increment().from(from); |
69 | 61 | let receipt = builder.send().await?.get_receipt().await?; |
70 | 62 |
|
71 | 63 | println!("Incremented number: {:?}", receipt.transaction_hash); |
|
0 commit comments