Skip to content

Commit

Permalink
Merge pull request #21 from alloy-rs/zerosnacks/add-recommended-layer…
Browse files Browse the repository at this point in the history
…s-example

feat(layers): add recommended layers example
  • Loading branch information
zerosnacks authored Mar 26, 2024
2 parents f2c7292 + fff6a81 commit 3a3cfe0
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ publish = false
exclude = ["examples/"]

[workspace.dependencies]
alloy = { git = "https://github.com/alloy-rs/alloy", rev = "fd8f065", features = [
alloy = { git = "https://github.com/alloy-rs/alloy", rev = "f7333c4", features = [
# "dyn-abi",
# "json-abi",
# "json",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cargo run --example mnemonic_signer
- [x] [Deploy from contract](./examples/contracts/examples/deploy_from_contract.rs)
- [x] [Generate](./examples/contracts/examples/generate.rs)
- [x] Layers
- [x] [Recommended layers](./examples/layers/examples/recommended_layers.rs)
- [x] [Nonce manager](./examples/layers/examples/nonce_layer.rs)
- [x] [Signature manager](./examples/layers/examples/signer_layer.rs)
- [x] Subscriptions
Expand Down
6 changes: 5 additions & 1 deletion examples/layers/examples/nonce_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ async fn main() -> Result<()> {
// Create a provider with the signer.
let http = anvil.endpoint().parse()?;
let provider = ProviderBuilder::new()
// Add the `ManagedNonceLayer` to the provider
// Add the `ManagedNonceLayer` to the provider.
// It is generally recommended to use the `.with_recommended_layers()` method, which
// includes the `ManagedNonceLayer`.
.layer(ManagedNonceLayer)
.signer(EthereumSigner::from(wallet))
.on_client(RpcClient::new_http(http));
Expand All @@ -42,9 +44,11 @@ async fn main() -> Result<()> {
.with_from(from)
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into())
.with_value(U256::from(100))
// Notice that without the `GasEstimatorLayer`, you need to set the gas related fields.
.with_gas_limit(U256::from(21000))
.with_max_fee_per_gas(U256::from(20e9))
.with_max_priority_fee_per_gas(U256::from(1e9))
// It is required to set the chain_id for EIP-1559 transactions.
.with_chain_id(anvil.chain_id());

// Send the transaction, the nonce (0) is automatically managed by the provider.
Expand Down
60 changes: 60 additions & 0 deletions examples/layers/examples/recommended_layers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Example of using the `ManagedNonceLayer` in the provider.
use alloy::{
network::{EthereumSigner, TransactionBuilder},
node_bindings::Anvil,
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::{client::RpcClient, types::eth::request::TransactionRequest},
signers::wallet::LocalWallet,
};
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH
let anvil = Anvil::new().try_spawn()?;

// Set up the wallets.
let wallet: LocalWallet = anvil.keys()[0].clone().into();
let from = wallet.address();

// Create a provider with the signer.
let http = anvil.endpoint().parse()?;
let provider = ProviderBuilder::new()
// Adds the `GasEstimatorLayer` and the `ManagedNonceLayer` layers.
.with_recommended_layers()
// Alternatively, you can add the layers individually:
// .with_gas_estimation()
// .with_nonce_management()
.signer(EthereumSigner::from(wallet))
.on_client(RpcClient::new_http(http));

// Create an EIP-1559 type transaction.
// Notice that the `nonce` field is set by the `ManagedNonceLayer`.
// Notice that without the `GasEstimatorLayer`, you need to set the gas related fields.
let tx = TransactionRequest::default()
.with_from(from)
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into())
.with_value(U256::from(100))
.with_chain_id(anvil.chain_id());

// Send the transaction, the nonce (0) is automatically managed by the provider.
let builder = provider.send_transaction(tx.clone()).await?;
let node_hash = *builder.tx_hash();
let pending_transaction = provider.get_transaction_by_hash(node_hash).await?;
assert_eq!(pending_transaction.nonce, 0);

println!("Transaction sent with nonce: {}", pending_transaction.nonce);

// Send the transaction, the nonce (1) is automatically managed by the provider.
let builder = provider.send_transaction(tx).await?;
let node_hash = *builder.tx_hash();
let pending_transaction = provider.get_transaction_by_hash(node_hash).await?;
assert_eq!(pending_transaction.nonce, 1);

println!("Transaction sent with nonce: {}", pending_transaction.nonce);

Ok(())
}
2 changes: 2 additions & 0 deletions examples/layers/examples/signer_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ async fn main() -> Result<()> {

// Create a legacy type transaction.
let tx = TransactionRequest::default()
// Notice that without the `ManagedNonceLayer`, you need to manually set the nonce field.
.with_nonce(0)
.with_from(from)
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into())
.with_value(U256::from(100))
// Notice that without the `GasEstimatorLayer`, you need to set the gas related fields.
.with_gas_price(U256::from(20e9))
.with_gas_limit(U256::from(21000));

Expand Down
4 changes: 2 additions & 2 deletions examples/providers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ repository.workspace = true
[dev-dependencies]
alloy.workspace = true
# Temp dependency fix to enable relevant features - Ref: https://github.com/alloy-rs/examples/pull/3#discussion_r1537842062
alloy-rpc-client = { git = "https://github.com/alloy-rs/alloy", rev = "fd8f065", features = [
alloy-rpc-client = { git = "https://github.com/alloy-rs/alloy", rev = "f7333c4", features = [
"pubsub",
] }
alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "fd8f065", features = [
alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "f7333c4", features = [
"pubsub",
"ws",
] }
Expand Down
4 changes: 2 additions & 2 deletions examples/subscriptions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ repository.workspace = true
[dev-dependencies]
alloy.workspace = true
# Temp fix for enabling features. Ref: https://github.com/alloy-rs/examples/pull/3/#discussion_r1537842062
alloy-rpc-client = { git = "https://github.com/alloy-rs/alloy", rev = "fd8f065", features = [
alloy-rpc-client = { git = "https://github.com/alloy-rs/alloy", rev = "f7333c4", features = [
"pubsub",
"ws",
] }
alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "fd8f065", features = [
alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "f7333c4", features = [
"pubsub",
] }
# alloy-contract.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion examples/transactions/examples/transfer_erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async fn main() -> Result<()> {
println!("Transfer tx: {:?}", pending_tx.tx_hash());

// Wait for confirmation
let _ = pending_tx.with_confirmations(1);
let _ = pending_tx.with_required_confirmations(1);

let to_bal = balance_of(&provider, to, contract_address).await?;
let from_bal = balance_of(&provider, from, contract_address).await?;
Expand Down
3 changes: 2 additions & 1 deletion examples/wallets/examples/ledger_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ async fn main() -> Result<()> {
};

// Broadcast the transaction and wait for the receipt.
let receipt = provider.send_transaction(tx).await?.with_confirmations(3).get_receipt().await?;
let receipt =
provider.send_transaction(tx).await?.with_required_confirmations(3).get_receipt().await?;

println!("Send transaction: {:?}", receipt.transaction_hash);

Expand Down
3 changes: 2 additions & 1 deletion examples/wallets/examples/trezor_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ async fn main() -> Result<()> {
};

// Broadcast the transaction and wait for the receipt.
let receipt = provider.send_transaction(tx).await?.with_confirmations(3).get_receipt().await?;
let receipt =
provider.send_transaction(tx).await?.with_required_confirmations(3).get_receipt().await?;

println!("Send transaction: {:?}", receipt.transaction_hash);

Expand Down
3 changes: 2 additions & 1 deletion examples/wallets/examples/yubi_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ async fn main() -> Result<()> {
};

// Broadcast the transaction and wait for the receipt.
let receipt = provider.send_transaction(tx).await?.with_confirmations(3).get_receipt().await?;
let receipt =
provider.send_transaction(tx).await?.with_required_confirmations(3).get_receipt().await?;

println!("Send transaction: {:?}", receipt.transaction_hash);

Expand Down

0 comments on commit 3a3cfe0

Please sign in to comment.