Skip to content

Commit

Permalink
chore: upgrade to 188c4f8 and remove temporary workarounds (#55)
Browse files Browse the repository at this point in the history
* upgrade to 188c4f8, which includes alloy-rs/alloy@3faa1fc

* use with_recommended_fillers(), remove manual gas estimation and nonce management

* use deploy method on sol! artifact

* clean up redundant artifacts

* greatly simplify contract interaction

* make test not depend on exact amounts, show common before/after balance check

* simplify contract interaction
  • Loading branch information
zerosnacks authored Apr 17, 2024
1 parent af505ee commit 096abf0
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 662 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ unnecessary_struct_initialization = "allow"
use_self = "allow"

[workspace.dependencies]
alloy = { git = "https://github.com/alloy-rs/alloy", rev = "44c905d", features = [
alloy = { git = "https://github.com/alloy-rs/alloy", rev = "188c4f8", features = [
"contract",
"network",
"node-bindings",
Expand Down
9 changes: 3 additions & 6 deletions examples/anvil/examples/deploy_contract_anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ async fn main() -> Result<()> {

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

// Create a provider with a signer and the network.
let rpc_url = anvil.endpoint().parse()?;
Expand All @@ -44,20 +43,18 @@ async fn main() -> Result<()> {
println!("Anvil running at `{}`", anvil.endpoint());

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

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

// Set the number to 42.
let builder = contract.setNumber(U256::from(42)).from(from);
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 builder = contract.increment().from(from);
let builder = contract.increment();
let receipt = builder.send().await?.get_receipt().await?;

println!("Incremented number: {:?}", receipt.transaction_hash);
Expand Down
35 changes: 0 additions & 35 deletions examples/contracts/examples/abi/Counter.json

This file was deleted.

31 changes: 10 additions & 21 deletions examples/contracts/examples/deploy_from_artifact.rs
Original file line number Diff line number Diff line change
@@ -1,12 +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},
signers::wallet::LocalWallet,
sol,
network::EthereumSigner, node_bindings::Anvil, primitives::U256, providers::ProviderBuilder,
signers::wallet::LocalWallet, sol,
};
use eyre::Result;

Expand All @@ -29,32 +25,25 @@ async fn main() -> Result<()> {

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

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);
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
33 changes: 12 additions & 21 deletions examples/contracts/examples/deploy_from_contract.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
//! Example of deploying a contract from Solidity code to Anvil and interacting with it.
use alloy::{
network::EthereumSigner,
node_bindings::Anvil,
primitives::U256,
providers::{Provider, ProviderBuilder},
signers::wallet::LocalWallet,
sol,
network::EthereumSigner, node_bindings::Anvil, primitives::U256, providers::ProviderBuilder,
signers::wallet::LocalWallet, sol,
};
use eyre::Result;

Expand Down Expand Up @@ -37,34 +33,29 @@ async fn main() -> Result<()> {
// Set up signer from the first default Anvil account (Alice).
let signer: LocalWallet = anvil.keys()[0].clone().into();

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

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

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);
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
4 changes: 2 additions & 2 deletions examples/queries/examples/query_contract_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
// Create a provider.
let rpc_url = "https://eth.merkle.io";
let provider = ProviderBuilder::new().on_builtin(rpc_url).await?;
let rpc_url = "https://eth.merkle.io".parse()?;
let provider = ProviderBuilder::new().on_http(rpc_url)?;

// Get storage slot 0 from the Uniswap V3 USDC-ETH pool on Ethereum mainnet.
let pool_address = address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640");
Expand Down
Loading

0 comments on commit 096abf0

Please sign in to comment.