Skip to content

Commit

Permalink
use `deploy() on Counter directly
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosnacks committed Mar 25, 2024
1 parent bce6f58 commit fca2d11
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
29 changes: 8 additions & 21 deletions examples/contracts/examples/deploy_from_artifact.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
//! Example of deploying a contract from an artifact to Anvil and interacting with it.
use alloy::{
network::EthereumSigner,
node_bindings::Anvil,
primitives::U256,
providers::{Provider, ProviderBuilder},
rpc::client::RpcClient,
signers::wallet::LocalWallet,
sol,
network::EthereumSigner, node_bindings::Anvil, primitives::U256, providers::ProviderBuilder,
rpc::client::RpcClient, signers::wallet::LocalWallet, sol,
};
use eyre::Result;

Expand All @@ -30,33 +25,25 @@ async fn main() -> Result<()> {
// Create a provider with a signer.
let http = anvil.endpoint().parse()?;
let provider = ProviderBuilder::new()
.with_recommended_layers()
.signer(EthereumSigner::from(wallet))
.on_client(RpcClient::new_http(http));

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

// Get the base fee for the block.
let base_fee = provider.get_gas_price().await?;

// Deploy the contract.
let contract_builder = Counter::deploy_builder(&provider);
let estimate = contract_builder.estimate_gas().await?;
let contract_address =
contract_builder.gas(estimate).gas_price(base_fee).nonce(0).deploy().await?;

println!("Deployed contract at address: {:?}", contract_address);
let contract = Counter::deploy(&provider).await?;

let contract = Counter::new(contract_address, &provider);
println!("Deployed contract at address: {:?}", contract.address());

let estimate = contract.setNumber(U256::from(42)).estimate_gas().await?;
let builder = contract.setNumber(U256::from(42)).nonce(1).gas(estimate).gas_price(base_fee);
// Set the number to 42.
let builder = contract.setNumber(U256::from(42));
let receipt = builder.send().await?.get_receipt().await?;

println!("Set number to 42: {:?}", receipt.transaction_hash);

// Increment the number to 43.
let estimate = contract.increment().estimate_gas().await?;
let builder = contract.increment().nonce(2).gas(estimate).gas_price(base_fee);
let builder = contract.increment();
let receipt = builder.send().await?.get_receipt().await?;

println!("Incremented number: {:?}", receipt.transaction_hash);
Expand Down
8 changes: 3 additions & 5 deletions examples/contracts/examples/deploy_from_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ async fn main() -> Result<()> {
println!("Anvil running at `{}`", anvil.endpoint());

// Deploy the contract.
let contract_builder = Counter::deploy_builder(&provider);
let contract_address = contract_builder.deploy().await?;
let contract = Counter::deploy(&provider).await?;

println!("Deployed contract at address: {:?}", contract_address);

let contract = Counter::new(contract_address, &provider);
println!("Deployed contract at address: {:?}", contract.address());

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

Expand Down

0 comments on commit fca2d11

Please sign in to comment.