Sitelet https://github.com/SharkHand3/examples/commit/4d111af337d29bced2d4f32a8d72fedc473bb423
Skip to content

Commit 4d111af

Browse files
authored
feat(contracts): update to cb95183 and add with_recommended_fillers() to examples where appropriate (alloy-rs#50)
* upgrade to cd5a2c7 * start updating examples * fix examples * fix clippy and ordering * requires signer and from, not sure why it is not inheriting from the provider * upgrade to cb95183 * fix broken examples * update layer -> singer, add gas_signer and improve documentation
1 parent b7649b5 commit 4d111af

26 files changed

Lines changed: 226 additions & 169 deletions

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ unnecessary_struct_initialization = "allow"
8181
use_self = "allow"
8282

8383
[workspace.dependencies]
84-
alloy = { git = "https://github.com/alloy-rs/alloy", rev = "bfd0fda", features = [
84+
alloy = { git = "https://github.com/alloy-rs/alloy", rev = "cb95183", features = [
8585
"contract",
8686
"network",
8787
"node-bindings",

‎README.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ This repository contains the following examples:
3434
- [x] [Deploy from artifact](./examples/contracts/examples/deploy_from_artifact.rs)
3535
- [x] [Deploy from contract](./examples/contracts/examples/deploy_from_contract.rs)
3636
- [x] [Interact with ABI](./examples/contracts/examples/interact_with_abi.rs)
37-
- [x] Layers (Middleware)
38-
- [x] [Recommended layers](./examples/layers/examples/recommended_layers.rs)
39-
- [x] [Nonce manager](./examples/layers/examples/nonce_layer.rs)
40-
- [x] [Signature manager](./examples/layers/examples/signer_layer.rs)
37+
- [x] Fillers (Middleware)
38+
- [x] [Gas filler](./examples/fillers/examples/gas_filler.rs)
39+
- [x] [Nonce management filler](./examples/fillers/examples/nonce_filler.rs)
40+
- [x] [Recommended fillers](./examples/fillers/examples/recommended_fillers.rs)
41+
- [x] [Signer management filler](./examples/fillers/examples/signer_filler.rs)
4142
- [x] Subscriptions
4243
- [x] [Subscribe and watch blocks](./examples/subscriptions/examples/subscribe_blocks.rs)
4344
- [x] [Subscribe to contract events and watch logs](./examples/subscriptions/examples/watch_contract_event.rs)

‎examples/anvil/examples/deploy_contract_anvil.rs‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
//! Example of deploying a contract to Anvil and interacting with it.
22
33
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,
106
};
117
use eyre::Result;
128

@@ -36,36 +32,32 @@ async fn main() -> Result<()> {
3632

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

4037
// Create a provider with a signer and the network.
4138
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)?;
4443

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

47-
// Get the base fee for the block.
48-
let base_fee = provider.get_gas_price().await?;
49-
5046
// 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);
5750

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

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);
6255
let receipt = builder.send().await?.get_receipt().await?;
6356

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

6659
// 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);
6961
let receipt = builder.send().await?.get_receipt().await?;
7062

7163
println!("Incremented number: {:?}", receipt.transaction_hash);

‎examples/contracts/examples/deploy_from_artifact.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ async fn main() -> Result<()> {
2929

3030
// Create a provider with a signer.
3131
let rpc_url = anvil.endpoint().parse()?;
32-
let provider =
33-
ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_reqwest_http(rpc_url)?;
32+
let provider = ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_http(rpc_url)?;
3433

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

‎examples/contracts/examples/deploy_from_contract.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ async fn main() -> Result<()> {
3939

4040
// Create a provider with a signer.
4141
let rpc_url = anvil.endpoint().parse()?;
42-
let provider =
43-
ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_reqwest_http(rpc_url)?;
42+
let provider = ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_http(rpc_url)?;
4443

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

‎examples/contracts/examples/interact_with_abi.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async fn main() -> Result<()> {
1919

2020
// Create a provider.
2121
let rpc_url = anvil.endpoint().parse()?;
22-
let provider = ProviderBuilder::new().on_reqwest_http(rpc_url)?;
22+
let provider = ProviderBuilder::new().on_http(rpc_url)?;
2323

2424
// Create a contract instance.
2525
let contract = IERC20::new("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2".parse()?, provider);

examples/layers/examples/recommended_layers.rs renamed to examples/fillers/examples/gas_filler.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Example of using the `.with_recommended_layers()` method in the provider.
1+
//! Example of using the `GasFiller` in the provider.
22
33
use alloy::{
44
network::{EthereumSigner, TransactionBuilder},
@@ -26,21 +26,21 @@ async fn main() -> Result<()> {
2626
// Create a provider with the signer.
2727
let rpc_url = anvil.endpoint().parse()?;
2828
let provider = ProviderBuilder::new()
29-
// Adds the `GasEstimatorLayer` and the `NonceManagerLayer` layers.
30-
.with_recommended_layers()
31-
// Alternatively, you can add the layers individually:
32-
// .with_gas_estimation()
33-
// .with_nonce_management()
29+
// Add the `GasFiller` to the provider.
30+
// It is generally recommended to use the `.with_recommended_fillers()` method, which
31+
// includes the `GasFiller`.
32+
.with_gas_estimation()
3433
.signer(EthereumSigner::from(signer))
35-
.on_reqwest_http(rpc_url)?;
34+
.on_http(rpc_url)?;
3635

3736
// Create an EIP-1559 type transaction.
38-
// Notice that the `nonce` field is set by the `NonceManagerLayer`.
39-
// Notice that without the `GasEstimatorLayer`, you need to set the gas related fields.
4037
let tx = TransactionRequest::default()
4138
.with_from(alice)
4239
.with_to(vitalik.into())
4340
.with_value(U256::from(100))
41+
// Notice that without the `NonceFiller`, you need to set `nonce` field.
42+
.with_nonce(0)
43+
// Notice that without the `ChainIdFiller`, you need to set the `chain_id` field.
4444
.with_chain_id(anvil.chain_id());
4545

4646
// Send the transaction, the nonce (0) is automatically managed by the provider.
@@ -51,6 +51,9 @@ async fn main() -> Result<()> {
5151

5252
println!("Transaction sent with nonce: {}", pending_tx.nonce);
5353

54+
// Update the nonce and send the transaction again.
55+
let tx = tx.with_nonce(1);
56+
5457
// Send the transaction, the nonce (1) is automatically managed by the provider.
5558
let builder = provider.send_transaction(tx).await?;
5659
let node_hash = *builder.tx_hash();

examples/layers/examples/nonce_layer.rs renamed to examples/fillers/examples/nonce_management_filler.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//! Example of using the `NonceManagerLayer` in the provider.
1+
//! Example of using the `NonceFiller` in the provider.
22
33
use alloy::{
44
network::{EthereumSigner, TransactionBuilder},
55
node_bindings::Anvil,
66
primitives::{address, U256},
7-
providers::{layers::NonceManagerLayer, Provider, ProviderBuilder},
7+
providers::{Provider, ProviderBuilder},
88
rpc::types::eth::request::TransactionRequest,
99
signers::wallet::LocalWallet,
1010
};
@@ -35,23 +35,23 @@ async fn main() -> Result<()> {
3535
// Create a provider with the signer.
3636
let rpc_url = anvil.endpoint().parse()?;
3737
let provider = ProviderBuilder::new()
38-
// Add the `NonceManagerLayer` to the provider.
39-
// It is generally recommended to use the `.with_recommended_layers()` method, which
40-
// includes the `NonceManagerLayer`.
41-
.layer(NonceManagerLayer)
38+
// Add the `NonceFiller` to the provider.
39+
// It is generally recommended to use the `.with_recommended_fillers()` method, which
40+
// includes the `NonceFiller`.
41+
.with_nonce_management()
4242
.signer(EthereumSigner::from(signer))
43-
.on_reqwest_http(rpc_url)?;
43+
.on_http(rpc_url)?;
4444

4545
// Create an EIP-1559 type transaction.
4646
let tx = TransactionRequest::default()
4747
.with_from(alice)
4848
.with_to(vitalik.into())
4949
.with_value(U256::from(100))
50-
// Notice that without the `GasEstimatorLayer`, you need to set the gas related fields.
51-
.with_gas_limit(U256::from(21000))
52-
.with_max_fee_per_gas(U256::from(20e9))
53-
.with_max_priority_fee_per_gas(U256::from(1e9))
54-
// It is required to set the chain_id for EIP-1559 transactions.
50+
// Notice that without the `GasFiller`, you need to set the gas related fields.
51+
.with_gas_limit(21_000)
52+
.with_max_fee_per_gas(20_000_000_000)
53+
.with_max_priority_fee_per_gas(1_000_000_000)
54+
// Notice that without the `ChainIdFiller`, you need to set the `chain_id` field.
5555
.with_chain_id(anvil.chain_id());
5656

5757
// Send the transaction, the nonce (0) is automatically managed by the provider.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! Example of using the `.with_recommended_fillers()` method in the provider.
2+
3+
use alloy::{
4+
network::{EthereumSigner, TransactionBuilder},
5+
node_bindings::Anvil,
6+
primitives::{address, U256},
7+
providers::{Provider, ProviderBuilder},
8+
rpc::types::eth::request::TransactionRequest,
9+
signers::wallet::LocalWallet,
10+
};
11+
use eyre::Result;
12+
13+
#[tokio::main]
14+
async fn main() -> Result<()> {
15+
// Spin up a local Anvil node.
16+
// 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: LocalWallet = anvil.keys()[0].clone().into();
21+
22+
// Create two users, Alice and Vitalik.
23+
let alice = signer.address();
24+
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
25+
26+
// Create a provider with the signer.
27+
let rpc_url = anvil.endpoint().parse()?;
28+
let provider = ProviderBuilder::new()
29+
// Adds the `ChainIdFiller`, `GasFiller` and the `NonceFiller` layers.
30+
.with_recommended_fillers()
31+
.signer(EthereumSigner::from(signer))
32+
.on_http(rpc_url)?;
33+
34+
// Build a EIP-1559 type transaction.
35+
// Notice that the `nonce` field is set by the `NonceFiller`.
36+
// Notice that the gas related fields are set by the `GasFiller`.
37+
// Notice that the `chain_id` field is set by the `ChainIdFiller`.
38+
let tx = TransactionRequest::default()
39+
.with_from(alice)
40+
.with_to(vitalik.into())
41+
.with_value(U256::from(100));
42+
43+
// Send the transaction, the nonce (0) is automatically managed by the provider.
44+
let builder = provider.send_transaction(tx.clone()).await?;
45+
let node_hash = *builder.tx_hash();
46+
let pending_tx = provider.get_transaction_by_hash(node_hash).await?;
47+
assert_eq!(pending_tx.nonce, 0);
48+
49+
println!("Transaction sent with nonce: {}", pending_tx.nonce);
50+
51+
// Send the transaction, the nonce (1) is automatically managed by the provider.
52+
let builder = provider.send_transaction(tx).await?;
53+
let node_hash = *builder.tx_hash();
54+
let pending_tx = provider.get_transaction_by_hash(node_hash).await?;
55+
assert_eq!(pending_tx.nonce, 1);
56+
57+
println!("Transaction sent with nonce: {}", pending_tx.nonce);
58+
59+
Ok(())
60+
}

0 commit comments

Comments
 (0)