From ff0a45ee30bb727031f4ebe9af0f639d834f7a65 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Tue, 9 Apr 2024 07:45:24 +0000 Subject: [PATCH 1/8] upgrade to cd5a2c7 --- Cargo.toml | 2 +- README.md | 6 ++--- .../anvil/examples/deploy_contract_anvil.rs | 3 +-- .../examples/deploy_from_artifact.rs | 3 +-- .../examples/deploy_from_contract.rs | 3 +-- .../contracts/examples/interact_with_abi.rs | 2 +- ...ce_layer.rs => nonce_management_filler.rs} | 20 ++++++++-------- ...mended_layers.rs => recommended_filler.rs} | 13 ++++------- examples/layers/examples/signer_layer.rs | 10 ++++---- examples/providers/examples/builder.rs | 16 +++++-------- examples/providers/examples/http.rs | 2 +- .../examples/query_deployed_bytecode.rs | 2 +- examples/queries/examples/query_logs.rs | 2 +- .../transactions/examples/gas_price_usd.rs | 6 ++--- examples/transactions/examples/trace_call.rs | 2 +- .../examples/trace_transaction.rs | 2 +- .../transactions/examples/transfer_erc20.rs | 2 +- .../transactions/examples/transfer_eth.rs | 2 +- examples/wallets/examples/keystore_signer.rs | 23 ++++++++----------- examples/wallets/examples/ledger_signer.rs | 18 +++++++-------- .../wallets/examples/private_key_signer.rs | 22 ++++++++---------- examples/wallets/examples/trezor_signer.rs | 20 ++++++++-------- examples/wallets/examples/yubi_signer.rs | 22 ++++++++---------- 23 files changed, 89 insertions(+), 114 deletions(-) rename examples/layers/examples/{nonce_layer.rs => nonce_management_filler.rs} (80%) rename examples/layers/examples/{recommended_layers.rs => recommended_filler.rs} (81%) diff --git a/Cargo.toml b/Cargo.toml index 6a97c8ee..5e40177a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -81,7 +81,7 @@ unnecessary_struct_initialization = "allow" use_self = "allow" [workspace.dependencies] -alloy = { git = "https://github.com/alloy-rs/alloy", rev = "bfd0fda", features = [ +alloy = { git = "https://github.com/alloy-rs/alloy", rev = "cd5a2c7", features = [ "contract", "network", "node-bindings", diff --git a/README.md b/README.md index 7b06345e..bcc48734 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,9 @@ This repository contains the following examples: - [x] [Deploy from contract](./examples/contracts/examples/deploy_from_contract.rs) - [x] [Interact with ABI](./examples/contracts/examples/interact_with_abi.rs) - [x] Layers (Middleware) - - [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] [Nonce management filler](./examples/layers/examples/nonce_layer.rs) + - [x] [Recommended filler](./examples/layers/examples/recommended_layers.rs) + - [x] [Signer management layer](./examples/layers/examples/signer_layer.rs) - [x] Subscriptions - [x] [Subscribe and watch blocks](./examples/subscriptions/examples/subscribe_blocks.rs) - [x] [Subscribe to contract events and watch logs](./examples/subscriptions/examples/watch_contract_event.rs) diff --git a/examples/anvil/examples/deploy_contract_anvil.rs b/examples/anvil/examples/deploy_contract_anvil.rs index 678c7d98..d673b9eb 100644 --- a/examples/anvil/examples/deploy_contract_anvil.rs +++ b/examples/anvil/examples/deploy_contract_anvil.rs @@ -39,8 +39,7 @@ async fn main() -> Result<()> { // Create a provider with a signer and the network. let rpc_url = anvil.endpoint().parse()?; - let provider = - ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_http(rpc_url)?; println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/contracts/examples/deploy_from_artifact.rs b/examples/contracts/examples/deploy_from_artifact.rs index 12f23344..fea47b60 100644 --- a/examples/contracts/examples/deploy_from_artifact.rs +++ b/examples/contracts/examples/deploy_from_artifact.rs @@ -29,8 +29,7 @@ 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_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_http(rpc_url)?; println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/contracts/examples/deploy_from_contract.rs b/examples/contracts/examples/deploy_from_contract.rs index 0064b4bc..536f6864 100644 --- a/examples/contracts/examples/deploy_from_contract.rs +++ b/examples/contracts/examples/deploy_from_contract.rs @@ -39,8 +39,7 @@ 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_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_http(rpc_url)?; println!("Anvil running at `{}`", anvil.endpoint()); diff --git a/examples/contracts/examples/interact_with_abi.rs b/examples/contracts/examples/interact_with_abi.rs index 35f182d5..13bac73e 100644 --- a/examples/contracts/examples/interact_with_abi.rs +++ b/examples/contracts/examples/interact_with_abi.rs @@ -19,7 +19,7 @@ async fn main() -> Result<()> { // Create a provider. let rpc_url = anvil.endpoint().parse()?; - let provider = ProviderBuilder::new().on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().on_http(rpc_url)?; // Create a contract instance. let contract = IERC20::new("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2".parse()?, provider); diff --git a/examples/layers/examples/nonce_layer.rs b/examples/layers/examples/nonce_management_filler.rs similarity index 80% rename from examples/layers/examples/nonce_layer.rs rename to examples/layers/examples/nonce_management_filler.rs index 01b0f360..739e44f2 100644 --- a/examples/layers/examples/nonce_layer.rs +++ b/examples/layers/examples/nonce_management_filler.rs @@ -1,10 +1,10 @@ -//! Example of using the `NonceManagerLayer` in the provider. +//! Example of using the `NonceFiller` in the provider. use alloy::{ network::{EthereumSigner, TransactionBuilder}, node_bindings::Anvil, primitives::{address, U256}, - providers::{layers::NonceManagerLayer, Provider, ProviderBuilder}, + providers::{Provider, ProviderBuilder}, rpc::types::eth::request::TransactionRequest, signers::wallet::LocalWallet, }; @@ -35,22 +35,20 @@ async fn main() -> Result<()> { // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() - // Add the `NonceManagerLayer` to the provider. - // It is generally recommended to use the `.with_recommended_layers()` method, which - // includes the `NonceManagerLayer`. - .layer(NonceManagerLayer) + // Add the `NonceFiller` to the provider. + .with_nonce_management() .signer(EthereumSigner::from(signer)) - .on_reqwest_http(rpc_url)?; + .on_http(rpc_url)?; // Create an EIP-1559 type transaction. let tx = TransactionRequest::default() .with_from(alice) .with_to(vitalik.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)) + // Notice that without the `GasFiller`, you need to set the gas related fields. + .with_gas_limit(21_000) + .with_max_fee_per_gas(2_000_0000_000) + .with_max_priority_fee_per_gas(1_000_000_000) // It is required to set the chain_id for EIP-1559 transactions. .with_chain_id(anvil.chain_id()); diff --git a/examples/layers/examples/recommended_layers.rs b/examples/layers/examples/recommended_filler.rs similarity index 81% rename from examples/layers/examples/recommended_layers.rs rename to examples/layers/examples/recommended_filler.rs index c6a6d9a4..2a0b1965 100644 --- a/examples/layers/examples/recommended_layers.rs +++ b/examples/layers/examples/recommended_filler.rs @@ -26,17 +26,14 @@ async fn main() -> Result<()> { // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() - // Adds the `GasEstimatorLayer` and the `NonceManagerLayer` layers. - .with_recommended_layers() - // Alternatively, you can add the layers individually: - // .with_gas_estimation() - // .with_nonce_management() + // Adds the `ChainIdFiller`, GasFiller` and the `NonceFiller` layers. + .with_recommended_fillers() .signer(EthereumSigner::from(signer)) - .on_reqwest_http(rpc_url)?; + .on_http(rpc_url)?; // Create an EIP-1559 type transaction. - // Notice that the `nonce` field is set by the `NonceManagerLayer`. - // Notice that without the `GasEstimatorLayer`, you need to set the gas related fields. + // Notice that the `nonce` field is set by the `NonceFiller`. + // Notice that without the `GasFiller`, you need to set the gas related fields. let tx = TransactionRequest::default() .with_from(alice) .with_to(vitalik.into()) diff --git a/examples/layers/examples/signer_layer.rs b/examples/layers/examples/signer_layer.rs index 44188ece..1ea3b3dc 100644 --- a/examples/layers/examples/signer_layer.rs +++ b/examples/layers/examples/signer_layer.rs @@ -28,18 +28,18 @@ async fn main() -> Result<()> { let provider = ProviderBuilder::new() // Add the `SignerLayer` to the provider .signer(EthereumSigner::from(signer)) - .on_reqwest_http(rpc_url)?; + .on_http(rpc_url)?; // Create a legacy type transaction. let tx = TransactionRequest::default() .with_from(alice) - // Notice that without the `NonceManagerLayer`, you need to manually set the nonce field. + // Notice that without the `NonceFiller`, you need to manually set the nonce field. .with_nonce(0) .with_to(vitalik.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)); + // Notice that without the `GasFiller`, you need to set the gas related fields. + .with_gas_price(2_000_0000_000) + .with_gas_limit(1_000_000_000); let builder = provider.send_transaction(tx).await?; let node_hash = *builder.tx_hash(); diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs index 4d5c42b8..358c705e 100644 --- a/examples/providers/examples/builder.rs +++ b/examples/providers/examples/builder.rs @@ -1,7 +1,7 @@ //! Example of using the `ProviderBuilder` to create a provider with a signer and network. use alloy::{ - network::{EthereumSigner, TransactionBuilder}, + network::EthereumSigner, node_bindings::Anvil, primitives::U256, providers::{Provider, ProviderBuilder}, @@ -25,17 +25,13 @@ async fn main() -> Result<()> { // Set up the HTTP provider with the `reqwest` crate. let rpc_url = anvil.endpoint().parse()?; - let provider = - ProviderBuilder::new().signer(EthereumSigner::from(wallet)).on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new() + .with_recommended_fillers() + .signer(EthereumSigner::from(wallet)) + .on_http(rpc_url)?; // Create a transaction. - let mut tx = TransactionRequest::default() - .to(Some(bob)) - .value(U256::from(100)) - .nonce(0) - .gas_limit(U256::from(21000)); - - tx.set_gas_price(U256::from(20e9)); + let tx = TransactionRequest::default().to(Some(bob)).value(U256::from(100)); // Send the transaction and wait for the receipt. let pending_tx = provider.send_transaction(tx).await?; diff --git a/examples/providers/examples/http.rs b/examples/providers/examples/http.rs index c379ba47..486e5b8c 100644 --- a/examples/providers/examples/http.rs +++ b/examples/providers/examples/http.rs @@ -9,7 +9,7 @@ async fn main() -> Result<()> { let rpc_url = "https://eth.merkle.io".parse()?; // Create a provider with the HTTP transport using the `reqwest` crate. - let provider = ProviderBuilder::new().on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().on_http(rpc_url)?; // Get latest block number. let latest_block = provider.get_block_number().await?; diff --git a/examples/queries/examples/query_deployed_bytecode.rs b/examples/queries/examples/query_deployed_bytecode.rs index 4711b06d..547fa8c5 100644 --- a/examples/queries/examples/query_deployed_bytecode.rs +++ b/examples/queries/examples/query_deployed_bytecode.rs @@ -11,7 +11,7 @@ use eyre::Result; async fn main() -> Result<()> { // Create a provider. let rpc_url = "https://eth.merkle.io".parse()?; - let provider = ProviderBuilder::new().on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().on_http(rpc_url)?; // Get the bytecode of the Uniswap V3 USDC-ETH pool on Ethereum mainnet. let pool_address = address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"); diff --git a/examples/queries/examples/query_logs.rs b/examples/queries/examples/query_logs.rs index 08b84ad0..1f594c9d 100644 --- a/examples/queries/examples/query_logs.rs +++ b/examples/queries/examples/query_logs.rs @@ -11,7 +11,7 @@ use eyre::Result; async fn main() -> Result<()> { // Create a provider. let rpc_url = "https://eth.merkle.io".parse()?; - let provider = ProviderBuilder::new().on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().on_http(rpc_url)?; // Get logs from the latest block let latest_block = provider.get_block_number().await?; diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index 9a62d23a..15edc614 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -30,7 +30,7 @@ async fn main() -> Result<()> { // Create a provider. let rpc_url = anvil.endpoint().parse()?; - let provider = ProviderBuilder::new().on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().on_http(rpc_url)?; // Create a call to get the latest answer from the Chainlink ETH/USD feed. let call = latestAnswerCall {}.abi_encode(); @@ -54,9 +54,9 @@ async fn main() -> Result<()> { Ok(()) } -fn get_usd_value(amount: U256, price_usd: U256) -> Result { +fn get_usd_value(amount: u128, price_usd: U256) -> Result { let base = U256::from(10).pow(U256::from(ETH_DECIMALS)); - let value = amount * price_usd / base; + let value = U256::from(amount) * price_usd / base; let formatted = format_units(value, ETH_USD_FEED_DECIMALS)?.parse::()?; Ok(formatted) diff --git a/examples/transactions/examples/trace_call.rs b/examples/transactions/examples/trace_call.rs index 9d8e9e99..069333f2 100644 --- a/examples/transactions/examples/trace_call.rs +++ b/examples/transactions/examples/trace_call.rs @@ -14,7 +14,7 @@ use eyre::Result; async fn main() -> Result<()> { // Create a provider. let rpc_url = "https://eth.merkle.io".parse()?; - let provider = ProviderBuilder::new().on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().on_http(rpc_url)?; // Create two users, Alice and Bob. let alice = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"); diff --git a/examples/transactions/examples/trace_transaction.rs b/examples/transactions/examples/trace_transaction.rs index c21f63b5..6355b546 100644 --- a/examples/transactions/examples/trace_transaction.rs +++ b/examples/transactions/examples/trace_transaction.rs @@ -19,7 +19,7 @@ async fn main() -> Result<()> { // Create a provider. let rpc_url = anvil.endpoint().parse()?; - let provider = ProviderBuilder::new().on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().on_http(rpc_url)?; // Hash of the tx we want to trace let hash = b256!("97a02abf405d36939e5b232a5d4ef5206980c5a6661845436058f30600c52df7"); diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index 79029c1e..0dcdb03c 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -26,7 +26,7 @@ async fn main() -> Result<()> { // Create a provider. let rpc_url = anvil.endpoint().parse()?; - let provider = ProviderBuilder::new().on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().on_http(rpc_url)?; // Create two users, Alice and Bob. let alice = anvil.addresses()[0]; diff --git a/examples/transactions/examples/transfer_eth.rs b/examples/transactions/examples/transfer_eth.rs index d3d03600..a00b2688 100644 --- a/examples/transactions/examples/transfer_eth.rs +++ b/examples/transactions/examples/transfer_eth.rs @@ -16,7 +16,7 @@ async fn main() -> Result<()> { // Create a provider. let rpc_url = anvil.endpoint().parse()?; - let provider = ProviderBuilder::new().on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new().on_http(rpc_url)?; // Create two users, Alice and Bob. let alice = anvil.addresses()[0]; diff --git a/examples/wallets/examples/keystore_signer.rs b/examples/wallets/examples/keystore_signer.rs index 67672cf2..18b32441 100644 --- a/examples/wallets/examples/keystore_signer.rs +++ b/examples/wallets/examples/keystore_signer.rs @@ -1,7 +1,7 @@ //! Example of using a keystore wallet to sign and send a transaction. use alloy::{ - network::EthereumSigner, + network::{EthereumSigner, TransactionBuilder}, node_bindings::Anvil, primitives::{address, U256}, providers::{Provider, ProviderBuilder}, @@ -27,18 +27,15 @@ async fn main() -> Result<()> { // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; - let provider = - ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_reqwest_http(rpc_url)?; - - // Create a transaction. - let tx = TransactionRequest { - value: Some(U256::from(100)), - to: address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into(), - nonce: Some(0), - gas_price: Some(U256::from(20e9)), - gas: Some(U256::from(21000)), - ..Default::default() - }; + let provider = ProviderBuilder::new() + .with_recommended_fillers() + .signer(EthereumSigner::from(signer)) + .on_http(rpc_url)?; + + // Build a transaction. + let tx = TransactionRequest::default() + .with_value(U256::from(100)) + .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); // Send the transaction and wait for the receipt. let receipt = provider.send_transaction(tx).await?.get_receipt().await?; diff --git a/examples/wallets/examples/ledger_signer.rs b/examples/wallets/examples/ledger_signer.rs index 39fa04fd..16e84da4 100644 --- a/examples/wallets/examples/ledger_signer.rs +++ b/examples/wallets/examples/ledger_signer.rs @@ -1,7 +1,7 @@ //! Example of signing and sending a transaction using a Ledger device. use alloy::{ - network::EthereumSigner, + network::{EthereumSigner, TransactionBuilder}, primitives::{address, U256}, providers::{Provider, ProviderBuilder}, rpc::types::eth::request::TransactionRequest, @@ -16,17 +16,15 @@ async fn main() -> Result<()> { // Create a provider with the signer. let rpc_url = "http://localhost:8545".parse()?; - let provider = - ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new() + .with_recommended_fillers() + .signer(EthereumSigner::from(signer)) + .on_http(rpc_url)?; // Create a transaction. - let tx = TransactionRequest { - value: Some(U256::from(100)), - to: address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into(), - gas_price: Some(U256::from(20e9)), - gas: Some(U256::from(21000)), - ..Default::default() - }; + let tx = TransactionRequest::default() + .with_value(U256::from(100)) + .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); // Send the transaction and wait for the receipt. let receipt = diff --git a/examples/wallets/examples/private_key_signer.rs b/examples/wallets/examples/private_key_signer.rs index 18dcd476..5d21cfb4 100644 --- a/examples/wallets/examples/private_key_signer.rs +++ b/examples/wallets/examples/private_key_signer.rs @@ -1,7 +1,7 @@ //! Example of using a local wallet to sign and send a transaction. use alloy::{ - network::EthereumSigner, + network::{EthereumSigner, TransactionBuilder}, node_bindings::Anvil, primitives::{address, U256}, providers::{Provider, ProviderBuilder}, @@ -20,18 +20,14 @@ async fn main() -> Result<()> { // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; - let provider = - ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_reqwest_http(rpc_url)?; - - // Create a transaction. - let tx = TransactionRequest { - value: Some(U256::from(100)), - to: address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into(), - nonce: Some(0), - gas_price: Some(U256::from(20e9)), - gas: Some(U256::from(21000)), - ..Default::default() - }; + let provider = ProviderBuilder::new() + .with_recommended_fillers() + .signer(EthereumSigner::from(signer)) + .on_http(rpc_url)?; + // Build a transaction. + let tx = TransactionRequest::default() + .with_value(U256::from(100)) + .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); // Send the transaction and wait for the receipt. let receipt = provider.send_transaction(tx).await?.get_receipt().await?; diff --git a/examples/wallets/examples/trezor_signer.rs b/examples/wallets/examples/trezor_signer.rs index 61e50210..f011fd5e 100644 --- a/examples/wallets/examples/trezor_signer.rs +++ b/examples/wallets/examples/trezor_signer.rs @@ -1,7 +1,7 @@ //! Example of signing and sending a transaction using a Trezor device. use alloy::{ - network::EthereumSigner, + network::{EthereumSigner, TransactionBuilder}, primitives::{address, U256}, providers::{Provider, ProviderBuilder}, rpc::types::eth::request::TransactionRequest, @@ -16,17 +16,15 @@ async fn main() -> Result<()> { // Create a provider with the signer. let rpc_url = "http://localhost:8545".parse()?; - let provider = - ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_reqwest_http(rpc_url)?; + let provider = ProviderBuilder::new() + .with_recommended_fillers() + .signer(EthereumSigner::from(signer)) + .on_http(rpc_url)?; - // Create a transaction. - let tx = TransactionRequest { - value: Some(U256::from(100)), - to: address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into(), - gas_price: Some(U256::from(20e9)), - gas: Some(U256::from(21000)), - ..Default::default() - }; + // Build a transaction. + let tx = TransactionRequest::default() + .with_value(U256::from(100)) + .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); // Send the transaction and wait for the receipt. let receipt = diff --git a/examples/wallets/examples/yubi_signer.rs b/examples/wallets/examples/yubi_signer.rs index f8c6462f..76f14be7 100644 --- a/examples/wallets/examples/yubi_signer.rs +++ b/examples/wallets/examples/yubi_signer.rs @@ -1,7 +1,7 @@ //! Example of signing and sending a transaction using a Yubi device. use alloy::{ - network::EthereumSigner, + network::{EthereumSigner, TransactionBuilder}, primitives::{address, U256}, providers::{Provider, ProviderBuilder}, rpc::types::eth::request::TransactionRequest, @@ -25,17 +25,15 @@ async fn main() -> Result<()> { // Create a provider with the signer. let rpc_url = "http://localhost:8545".parse()?; - let provider = - ProviderBuilder::new().signer(EthereumSigner::from(signer)).on_reqwest_http(rpc_url)?; - - // Create a transaction. - let tx = TransactionRequest { - value: Some(U256::from(100)), - to: address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into(), - gas_price: Some(U256::from(20e9)), - gas: Some(U256::from(21000)), - ..Default::default() - }; + let provider = ProviderBuilder::new() + .with_recommended_fillers() + .signer(EthereumSigner::from(signer)) + .on_http(rpc_url)?; + + // Build a transaction. + let tx = TransactionRequest::default() + .with_value(U256::from(100)) + .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); // Send the transaction and wait for the receipt. let receipt = From df0dd73950af6547252f880b6a39945d994ec128 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Tue, 9 Apr 2024 07:57:44 +0000 Subject: [PATCH 2/8] start updating examples --- .../anvil/examples/deploy_contract_anvil.rs | 32 +++++++------------ .../layers/examples/recommended_filler.rs | 4 +-- examples/layers/examples/signer_layer.rs | 2 +- examples/transactions/examples/trace_call.rs | 13 ++++---- .../transactions/examples/transfer_erc20.rs | 10 ++---- .../transactions/examples/transfer_eth.rs | 4 +-- examples/wallets/examples/keystore_signer.rs | 2 +- examples/wallets/examples/ledger_signer.rs | 2 +- .../wallets/examples/private_key_signer.rs | 3 +- examples/wallets/examples/trezor_signer.rs | 2 +- examples/wallets/examples/yubi_signer.rs | 2 +- 11 files changed, 31 insertions(+), 45 deletions(-) diff --git a/examples/anvil/examples/deploy_contract_anvil.rs b/examples/anvil/examples/deploy_contract_anvil.rs index d673b9eb..a35858a6 100644 --- a/examples/anvil/examples/deploy_contract_anvil.rs +++ b/examples/anvil/examples/deploy_contract_anvil.rs @@ -1,12 +1,8 @@ //! Example of deploying a contract 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; @@ -39,32 +35,26 @@ async fn main() -> Result<()> { // Create a provider with a signer and the network. 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); + // 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); diff --git a/examples/layers/examples/recommended_filler.rs b/examples/layers/examples/recommended_filler.rs index 2a0b1965..ebca2c3c 100644 --- a/examples/layers/examples/recommended_filler.rs +++ b/examples/layers/examples/recommended_filler.rs @@ -1,4 +1,4 @@ -//! Example of using the `.with_recommended_layers()` method in the provider. +//! Example of using the `.with_recommended_fillers()` method in the provider. use alloy::{ network::{EthereumSigner, TransactionBuilder}, @@ -31,7 +31,7 @@ async fn main() -> Result<()> { .signer(EthereumSigner::from(signer)) .on_http(rpc_url)?; - // Create an EIP-1559 type transaction. + // Build a EIP-1559 type transaction. // Notice that the `nonce` field is set by the `NonceFiller`. // Notice that without the `GasFiller`, you need to set the gas related fields. let tx = TransactionRequest::default() diff --git a/examples/layers/examples/signer_layer.rs b/examples/layers/examples/signer_layer.rs index 1ea3b3dc..fa9a74ae 100644 --- a/examples/layers/examples/signer_layer.rs +++ b/examples/layers/examples/signer_layer.rs @@ -30,7 +30,7 @@ async fn main() -> Result<()> { .signer(EthereumSigner::from(signer)) .on_http(rpc_url)?; - // Create a legacy type transaction. + // Build a legacy type transaction to send 100 wei to Vitalik. let tx = TransactionRequest::default() .with_from(alice) // Notice that without the `NonceFiller`, you need to manually set the nonce field. diff --git a/examples/transactions/examples/trace_call.rs b/examples/transactions/examples/trace_call.rs index 069333f2..a1bc90ce 100644 --- a/examples/transactions/examples/trace_call.rs +++ b/examples/transactions/examples/trace_call.rs @@ -1,6 +1,7 @@ //! Example of how to trace a transaction using `trace_call`. use alloy::{ + network::TransactionBuilder, primitives::{address, U256}, providers::{Provider, ProviderBuilder}, rpc::types::{ @@ -20,13 +21,11 @@ async fn main() -> Result<()> { let alice = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"); let bob = address!("70997970C51812dc3A010C7d01b50e0d17dc79C8"); - // Create a transaction to send 100 wei from Alice to Bob. - let tx = TransactionRequest { - from: Some(alice), - to: Some(bob), - value: Some(U256::from(100)), - ..Default::default() - }; + // Build a transaction to send 100 wei from Alice to Bob. + let tx = TransactionRequest::default() + .with_value(U256::from(100)) + .with_from(alice.into()) + .with_to(bob.into()); // Trace the transaction on top of the latest block. let trace_type = [TraceType::Trace]; diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index 0dcdb03c..65c152f2 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -1,7 +1,7 @@ //! Example of how to transfer ERC20 tokens from one account to another. use alloy::{ - network::Ethereum, + network::{Ethereum, TransactionBuilder}, node_bindings::Anvil, primitives::{Address, Bytes, U256}, providers::{Provider, ProviderBuilder, ReqwestProvider}, @@ -97,12 +97,8 @@ async fn balance_of( let call = ERC20Example::balanceOfCall { account }.abi_encode(); let input = Bytes::from(call); - // Create a transaction. - let tx = TransactionRequest { - to: Some(contract_address), - input: Some(input).into(), - ..Default::default() - }; + // Build a transaction to call the contract with the input. + let tx = TransactionRequest::default().with_to(contract_address.into()).with_input(input); // Call the contract. let result = provider.call(&tx, None).await?; diff --git a/examples/transactions/examples/transfer_eth.rs b/examples/transactions/examples/transfer_eth.rs index a00b2688..a785d1ba 100644 --- a/examples/transactions/examples/transfer_eth.rs +++ b/examples/transactions/examples/transfer_eth.rs @@ -22,8 +22,8 @@ async fn main() -> Result<()> { let alice = anvil.addresses()[0]; let bob = anvil.addresses()[1]; - // Create a transaction to transfer 1 wei from Alice to Bob. - let tx = TransactionRequest::default().from(alice).value(U256::from(1)).to(Some(bob)); + // Build a transaction to send 100 wei from Alice to Bob. + let tx = TransactionRequest::default().from(alice).value(U256::from(100)).to(Some(bob)); // Send the transaction and wait for the receipt. let pending_tx = provider.send_transaction(tx).await?; diff --git a/examples/wallets/examples/keystore_signer.rs b/examples/wallets/examples/keystore_signer.rs index 18b32441..5943c488 100644 --- a/examples/wallets/examples/keystore_signer.rs +++ b/examples/wallets/examples/keystore_signer.rs @@ -32,7 +32,7 @@ async fn main() -> Result<()> { .signer(EthereumSigner::from(signer)) .on_http(rpc_url)?; - // Build a transaction. + // Build a transaction to send 100 wei to Vitalik. let tx = TransactionRequest::default() .with_value(U256::from(100)) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); diff --git a/examples/wallets/examples/ledger_signer.rs b/examples/wallets/examples/ledger_signer.rs index 16e84da4..4d853f37 100644 --- a/examples/wallets/examples/ledger_signer.rs +++ b/examples/wallets/examples/ledger_signer.rs @@ -21,7 +21,7 @@ async fn main() -> Result<()> { .signer(EthereumSigner::from(signer)) .on_http(rpc_url)?; - // Create a transaction. + // Build a transaction to send 100 wei to Vitalik. let tx = TransactionRequest::default() .with_value(U256::from(100)) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); diff --git a/examples/wallets/examples/private_key_signer.rs b/examples/wallets/examples/private_key_signer.rs index 5d21cfb4..3a66aebe 100644 --- a/examples/wallets/examples/private_key_signer.rs +++ b/examples/wallets/examples/private_key_signer.rs @@ -24,7 +24,8 @@ async fn main() -> Result<()> { .with_recommended_fillers() .signer(EthereumSigner::from(signer)) .on_http(rpc_url)?; - // Build a transaction. + + // Build a transaction to send 100 wei to Vitalik. let tx = TransactionRequest::default() .with_value(U256::from(100)) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); diff --git a/examples/wallets/examples/trezor_signer.rs b/examples/wallets/examples/trezor_signer.rs index f011fd5e..5721f1ff 100644 --- a/examples/wallets/examples/trezor_signer.rs +++ b/examples/wallets/examples/trezor_signer.rs @@ -21,7 +21,7 @@ async fn main() -> Result<()> { .signer(EthereumSigner::from(signer)) .on_http(rpc_url)?; - // Build a transaction. + // Build a transaction to send 100 wei to Vitalik. let tx = TransactionRequest::default() .with_value(U256::from(100)) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); diff --git a/examples/wallets/examples/yubi_signer.rs b/examples/wallets/examples/yubi_signer.rs index 76f14be7..74d25955 100644 --- a/examples/wallets/examples/yubi_signer.rs +++ b/examples/wallets/examples/yubi_signer.rs @@ -30,7 +30,7 @@ async fn main() -> Result<()> { .signer(EthereumSigner::from(signer)) .on_http(rpc_url)?; - // Build a transaction. + // Build a transaction to send 100 wei to Vitalik. let tx = TransactionRequest::default() .with_value(U256::from(100)) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); From e3575e8d18b3d76c5fe4eb965d7cd962fb17be85 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Tue, 9 Apr 2024 18:55:58 +0000 Subject: [PATCH 3/8] fix examples --- examples/providers/examples/builder.rs | 7 +++++-- examples/transactions/examples/gas_price_usd.rs | 4 +++- examples/transactions/examples/trace_call.rs | 6 +++--- examples/transactions/examples/transfer_eth.rs | 6 +++++- examples/wallets/examples/keystore_signer.rs | 8 +++++--- examples/wallets/examples/ledger_signer.rs | 11 ++++++++--- examples/wallets/examples/private_key_signer.rs | 2 ++ examples/wallets/examples/trezor_signer.rs | 11 ++++++++--- examples/wallets/examples/yubi_signer.rs | 15 ++++++++++----- 9 files changed, 49 insertions(+), 21 deletions(-) diff --git a/examples/providers/examples/builder.rs b/examples/providers/examples/builder.rs index 358c705e..645db9d4 100644 --- a/examples/providers/examples/builder.rs +++ b/examples/providers/examples/builder.rs @@ -1,7 +1,7 @@ //! Example of using the `ProviderBuilder` to create a provider with a signer and network. use alloy::{ - network::EthereumSigner, + network::{EthereumSigner, TransactionBuilder}, node_bindings::Anvil, primitives::U256, providers::{Provider, ProviderBuilder}, @@ -31,7 +31,10 @@ async fn main() -> Result<()> { .on_http(rpc_url)?; // Create a transaction. - let tx = TransactionRequest::default().to(Some(bob)).value(U256::from(100)); + let tx = TransactionRequest::default() + .with_from(alice) + .with_to(bob.into()) + .with_value(U256::from(100)); // Send the transaction and wait for the receipt. let pending_tx = provider.send_transaction(tx).await?; diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index 15edc614..4081e5df 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -1,6 +1,7 @@ //! Example of how to get the gas price in USD using the Chainlink ETH/USD feed. use alloy::{ + network::TransactionBuilder, node_bindings::Anvil, primitives::{address, utils::format_units, Address, Bytes, U256}, providers::{Provider, ProviderBuilder}, @@ -37,7 +38,8 @@ async fn main() -> Result<()> { let input = Bytes::from(call); // Call the Chainlink ETH/USD feed contract. - let tx = TransactionRequest::default().to(Some(ETH_USD_FEED)).input(Some(input).into()); + let tx = TransactionRequest::default().with_to(ETH_USD_FEED.into()).with_input(input); + let response = provider.call(&tx, None).await?; let result = U256::from_str(&response.to_string())?; diff --git a/examples/transactions/examples/trace_call.rs b/examples/transactions/examples/trace_call.rs index a1bc90ce..3b2f4326 100644 --- a/examples/transactions/examples/trace_call.rs +++ b/examples/transactions/examples/trace_call.rs @@ -23,9 +23,9 @@ async fn main() -> Result<()> { // Build a transaction to send 100 wei from Alice to Bob. let tx = TransactionRequest::default() - .with_value(U256::from(100)) - .with_from(alice.into()) - .with_to(bob.into()); + .with_from(alice) + .with_to(bob.into()) + .with_value(U256::from(100)); // Trace the transaction on top of the latest block. let trace_type = [TraceType::Trace]; diff --git a/examples/transactions/examples/transfer_eth.rs b/examples/transactions/examples/transfer_eth.rs index a785d1ba..4936f3dd 100644 --- a/examples/transactions/examples/transfer_eth.rs +++ b/examples/transactions/examples/transfer_eth.rs @@ -1,6 +1,7 @@ //! Example of how to transfer ETH from one account to another. use alloy::{ + network::TransactionBuilder, node_bindings::Anvil, primitives::U256, providers::{Provider, ProviderBuilder}, @@ -23,7 +24,10 @@ async fn main() -> Result<()> { let bob = anvil.addresses()[1]; // Build a transaction to send 100 wei from Alice to Bob. - let tx = TransactionRequest::default().from(alice).value(U256::from(100)).to(Some(bob)); + let tx = TransactionRequest::default() + .with_from(alice) + .with_to(bob.into()) + .with_value(U256::from(100)); // Send the transaction and wait for the receipt. let pending_tx = provider.send_transaction(tx).await?; diff --git a/examples/wallets/examples/keystore_signer.rs b/examples/wallets/examples/keystore_signer.rs index 5943c488..2aa4f880 100644 --- a/examples/wallets/examples/keystore_signer.rs +++ b/examples/wallets/examples/keystore_signer.rs @@ -24,6 +24,7 @@ async fn main() -> Result<()> { let keystore_file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("examples/keystore/alice.json"); let signer = Wallet::decrypt_keystore(keystore_file_path, password)?; + let alice = signer.address(); // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; @@ -32,10 +33,11 @@ async fn main() -> Result<()> { .signer(EthereumSigner::from(signer)) .on_http(rpc_url)?; - // Build a transaction to send 100 wei to Vitalik. + // Build a transaction to send 100 wei to Vitalik from Alice. let tx = TransactionRequest::default() - .with_value(U256::from(100)) - .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); + .with_from(alice) + .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) + .with_value(U256::from(100)); // Send the transaction and wait for the receipt. let receipt = provider.send_transaction(tx).await?.get_receipt().await?; diff --git a/examples/wallets/examples/ledger_signer.rs b/examples/wallets/examples/ledger_signer.rs index 4d853f37..3fefa2b5 100644 --- a/examples/wallets/examples/ledger_signer.rs +++ b/examples/wallets/examples/ledger_signer.rs @@ -5,7 +5,10 @@ use alloy::{ primitives::{address, U256}, providers::{Provider, ProviderBuilder}, rpc::types::eth::request::TransactionRequest, - signers::ledger::{HDPath, LedgerSigner}, + signers::{ + ledger::{HDPath, LedgerSigner}, + Signer, + }, }; use eyre::Result; @@ -13,6 +16,7 @@ use eyre::Result; async fn main() -> Result<()> { // Instantiate the application by acquiring a lock on the Ledger device. let signer = LedgerSigner::new(HDPath::LedgerLive(0), Some(1)).await?; + let from = signer.address(); // Create a provider with the signer. let rpc_url = "http://localhost:8545".parse()?; @@ -23,8 +27,9 @@ async fn main() -> Result<()> { // Build a transaction to send 100 wei to Vitalik. let tx = TransactionRequest::default() - .with_value(U256::from(100)) - .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); + .with_from(from) + .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) + .with_value(U256::from(100)); // Send the transaction and wait for the receipt. let receipt = diff --git a/examples/wallets/examples/private_key_signer.rs b/examples/wallets/examples/private_key_signer.rs index 3a66aebe..ceaecc9b 100644 --- a/examples/wallets/examples/private_key_signer.rs +++ b/examples/wallets/examples/private_key_signer.rs @@ -17,6 +17,7 @@ async fn main() -> Result<()> { // Set up signer from the first default Anvil account (Alice). let signer: LocalWallet = anvil.keys()[0].clone().into(); + let alice = signer.address(); // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; @@ -27,6 +28,7 @@ async fn main() -> Result<()> { // Build a transaction to send 100 wei to Vitalik. let tx = TransactionRequest::default() + .with_from(alice) .with_value(U256::from(100)) .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); diff --git a/examples/wallets/examples/trezor_signer.rs b/examples/wallets/examples/trezor_signer.rs index 5721f1ff..7403ebaf 100644 --- a/examples/wallets/examples/trezor_signer.rs +++ b/examples/wallets/examples/trezor_signer.rs @@ -5,7 +5,10 @@ use alloy::{ primitives::{address, U256}, providers::{Provider, ProviderBuilder}, rpc::types::eth::request::TransactionRequest, - signers::trezor::{HDPath, TrezorSigner}, + signers::{ + trezor::{HDPath, TrezorSigner}, + Signer, + }, }; use eyre::Result; @@ -13,6 +16,7 @@ use eyre::Result; async fn main() -> Result<()> { // Instantiate the application by acquiring a lock on the Trezor device. let signer = TrezorSigner::new(HDPath::TrezorLive(0), Some(1)).await?; + let from = signer.address(); // Create a provider with the signer. let rpc_url = "http://localhost:8545".parse()?; @@ -23,8 +27,9 @@ async fn main() -> Result<()> { // Build a transaction to send 100 wei to Vitalik. let tx = TransactionRequest::default() - .with_value(U256::from(100)) - .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); + .with_from(from) + .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) + .with_value(U256::from(100)); // Send the transaction and wait for the receipt. let receipt = diff --git a/examples/wallets/examples/yubi_signer.rs b/examples/wallets/examples/yubi_signer.rs index 74d25955..a01dcb8e 100644 --- a/examples/wallets/examples/yubi_signer.rs +++ b/examples/wallets/examples/yubi_signer.rs @@ -5,9 +5,12 @@ use alloy::{ primitives::{address, U256}, providers::{Provider, ProviderBuilder}, rpc::types::eth::request::TransactionRequest, - signers::wallet::{ - yubihsm::{Connector, Credentials, UsbConfig}, - YubiWallet, + signers::{ + wallet::{ + yubihsm::{Connector, Credentials, UsbConfig}, + YubiWallet, + }, + Signer, }, }; use eyre::Result; @@ -22,6 +25,7 @@ async fn main() -> Result<()> { // `from_key` method to upload a key you already have, or the `new` method // to generate a new keypair. let signer = YubiWallet::connect(connector, Credentials::default(), 0); + let from = signer.address(); // Create a provider with the signer. let rpc_url = "http://localhost:8545".parse()?; @@ -32,8 +36,9 @@ async fn main() -> Result<()> { // Build a transaction to send 100 wei to Vitalik. let tx = TransactionRequest::default() - .with_value(U256::from(100)) - .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); + .with_from(from) + .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) + .with_value(U256::from(100)); // Send the transaction and wait for the receipt. let receipt = From 4de6d72321dd7869c1b3d3fbc7e814ff4668f67a Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Tue, 9 Apr 2024 18:58:11 +0000 Subject: [PATCH 4/8] fix clippy and ordering --- examples/layers/examples/nonce_management_filler.rs | 2 +- examples/layers/examples/signer_layer.rs | 2 +- examples/wallets/examples/private_key_signer.rs | 4 ++-- examples/wallets/examples/yubi_signer.rs | 9 +++------ 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/examples/layers/examples/nonce_management_filler.rs b/examples/layers/examples/nonce_management_filler.rs index 739e44f2..b1485728 100644 --- a/examples/layers/examples/nonce_management_filler.rs +++ b/examples/layers/examples/nonce_management_filler.rs @@ -47,7 +47,7 @@ async fn main() -> Result<()> { .with_value(U256::from(100)) // Notice that without the `GasFiller`, you need to set the gas related fields. .with_gas_limit(21_000) - .with_max_fee_per_gas(2_000_0000_000) + .with_max_fee_per_gas(20_000_000_000) .with_max_priority_fee_per_gas(1_000_000_000) // It is required to set the chain_id for EIP-1559 transactions. .with_chain_id(anvil.chain_id()); diff --git a/examples/layers/examples/signer_layer.rs b/examples/layers/examples/signer_layer.rs index fa9a74ae..99c1a994 100644 --- a/examples/layers/examples/signer_layer.rs +++ b/examples/layers/examples/signer_layer.rs @@ -38,7 +38,7 @@ async fn main() -> Result<()> { .with_to(vitalik.into()) .with_value(U256::from(100)) // Notice that without the `GasFiller`, you need to set the gas related fields. - .with_gas_price(2_000_0000_000) + .with_gas_price(20_000_000_000) .with_gas_limit(1_000_000_000); let builder = provider.send_transaction(tx).await?; diff --git a/examples/wallets/examples/private_key_signer.rs b/examples/wallets/examples/private_key_signer.rs index ceaecc9b..00e043da 100644 --- a/examples/wallets/examples/private_key_signer.rs +++ b/examples/wallets/examples/private_key_signer.rs @@ -29,8 +29,8 @@ async fn main() -> Result<()> { // Build a transaction to send 100 wei to Vitalik. let tx = TransactionRequest::default() .with_from(alice) - .with_value(U256::from(100)) - .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()); + .with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045").into()) + .with_value(U256::from(100)); // Send the transaction and wait for the receipt. let receipt = provider.send_transaction(tx).await?.get_receipt().await?; diff --git a/examples/wallets/examples/yubi_signer.rs b/examples/wallets/examples/yubi_signer.rs index a01dcb8e..d4578b9c 100644 --- a/examples/wallets/examples/yubi_signer.rs +++ b/examples/wallets/examples/yubi_signer.rs @@ -5,12 +5,9 @@ use alloy::{ primitives::{address, U256}, providers::{Provider, ProviderBuilder}, rpc::types::eth::request::TransactionRequest, - signers::{ - wallet::{ - yubihsm::{Connector, Credentials, UsbConfig}, - YubiWallet, - }, - Signer, + signers::wallet::{ + yubihsm::{Connector, Credentials, UsbConfig}, + YubiWallet, }, }; use eyre::Result; From e75a6ef783fc01be8ca46bf2b5de9f9091a741ff Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Tue, 9 Apr 2024 19:08:56 +0000 Subject: [PATCH 5/8] requires signer and from, not sure why it is not inheriting from the provider --- examples/anvil/examples/deploy_contract_anvil.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/anvil/examples/deploy_contract_anvil.rs b/examples/anvil/examples/deploy_contract_anvil.rs index a35858a6..46654855 100644 --- a/examples/anvil/examples/deploy_contract_anvil.rs +++ b/examples/anvil/examples/deploy_contract_anvil.rs @@ -43,7 +43,9 @@ async fn main() -> Result<()> { println!("Anvil running at `{}`", anvil.endpoint()); // Deploy the contract. - let contract = Counter::deploy(provider).await?; + let contract_builder = Counter::deploy_builder(&provider); + let contract_address = contract_builder.deploy().await?; + let contract = Counter::new(contract_address, provider); println!("Deployed contract at address: {:?}", contract.address()); From 4b799263eb24d4f44b5cdecd3cf1d0f6562cfea4 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Tue, 16 Apr 2024 11:57:31 +0000 Subject: [PATCH 6/8] upgrade to cb95183 --- Cargo.toml | 2 +- examples/queries/examples/query_contract_storage.rs | 3 ++- examples/transactions/examples/gas_price_usd.rs | 4 ++-- examples/transactions/examples/trace_call.rs | 6 ++---- examples/transactions/examples/trace_transaction.rs | 2 +- examples/transactions/examples/transfer_erc20.rs | 4 ++-- 6 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5e40177a..044c09bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -81,7 +81,7 @@ unnecessary_struct_initialization = "allow" use_self = "allow" [workspace.dependencies] -alloy = { git = "https://github.com/alloy-rs/alloy", rev = "cd5a2c7", features = [ +alloy = { git = "https://github.com/alloy-rs/alloy", rev = "cb95183", features = [ "contract", "network", "node-bindings", diff --git a/examples/queries/examples/query_contract_storage.rs b/examples/queries/examples/query_contract_storage.rs index 2122499a..817e692d 100644 --- a/examples/queries/examples/query_contract_storage.rs +++ b/examples/queries/examples/query_contract_storage.rs @@ -3,6 +3,7 @@ use alloy::{ primitives::{address, U256}, providers::{Provider, ProviderBuilder}, + rpc::types::eth::BlockId, }; use eyre::Result; @@ -15,7 +16,7 @@ async fn main() -> Result<()> { // Get storage slot 0 from the Uniswap V3 USDC-ETH pool on Ethereum mainnet. let pool_address = address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"); let storage_slot = U256::from(0); - let storage = provider.get_storage_at(pool_address, storage_slot, None).await?; + let storage = provider.get_storage_at(pool_address, storage_slot, BlockId::latest()).await?; println!("Slot 0: {storage:?}"); diff --git a/examples/transactions/examples/gas_price_usd.rs b/examples/transactions/examples/gas_price_usd.rs index 4081e5df..00ed9633 100644 --- a/examples/transactions/examples/gas_price_usd.rs +++ b/examples/transactions/examples/gas_price_usd.rs @@ -5,7 +5,7 @@ use alloy::{ node_bindings::Anvil, primitives::{address, utils::format_units, Address, Bytes, U256}, providers::{Provider, ProviderBuilder}, - rpc::types::eth::TransactionRequest, + rpc::types::eth::{BlockId, TransactionRequest}, sol, sol_types::SolCall, }; @@ -40,7 +40,7 @@ async fn main() -> Result<()> { // Call the Chainlink ETH/USD feed contract. let tx = TransactionRequest::default().with_to(ETH_USD_FEED.into()).with_input(input); - let response = provider.call(&tx, None).await?; + let response = provider.call(&tx, BlockId::latest()).await?; let result = U256::from_str(&response.to_string())?; // Get the gas price of the network. diff --git a/examples/transactions/examples/trace_call.rs b/examples/transactions/examples/trace_call.rs index 3b2f4326..fd288311 100644 --- a/examples/transactions/examples/trace_call.rs +++ b/examples/transactions/examples/trace_call.rs @@ -5,7 +5,7 @@ use alloy::{ primitives::{address, U256}, providers::{Provider, ProviderBuilder}, rpc::types::{ - eth::{BlockId, BlockNumberOrTag, TransactionRequest}, + eth::{BlockId, TransactionRequest}, trace::parity::TraceType, }, }; @@ -29,9 +29,7 @@ async fn main() -> Result<()> { // Trace the transaction on top of the latest block. let trace_type = [TraceType::Trace]; - let result = provider - .trace_call(&tx, &trace_type, Some(BlockId::Number(BlockNumberOrTag::Latest))) - .await?; + let result = provider.trace_call(&tx, &trace_type, BlockId::latest()).await?; println!("{:?}", result.trace); diff --git a/examples/transactions/examples/trace_transaction.rs b/examples/transactions/examples/trace_transaction.rs index 6355b546..ae6d8977 100644 --- a/examples/transactions/examples/trace_transaction.rs +++ b/examples/transactions/examples/trace_transaction.rs @@ -3,7 +3,7 @@ use alloy::{ node_bindings::Anvil, primitives::b256, - providers::{Provider, ProviderBuilder}, + providers::{debug::DebugApi, ProviderBuilder}, rpc::types::trace::geth::{ GethDebugBuiltInTracerType, GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions, diff --git a/examples/transactions/examples/transfer_erc20.rs b/examples/transactions/examples/transfer_erc20.rs index 65c152f2..4b9a464c 100644 --- a/examples/transactions/examples/transfer_erc20.rs +++ b/examples/transactions/examples/transfer_erc20.rs @@ -5,7 +5,7 @@ use alloy::{ node_bindings::Anvil, primitives::{Address, Bytes, U256}, providers::{Provider, ProviderBuilder, ReqwestProvider}, - rpc::types::eth::TransactionRequest, + rpc::types::eth::{BlockId, TransactionRequest}, sol, sol_types::SolCall, }; @@ -101,7 +101,7 @@ async fn balance_of( let tx = TransactionRequest::default().with_to(contract_address.into()).with_input(input); // Call the contract. - let result = provider.call(&tx, None).await?; + let result = provider.call(&tx, BlockId::latest()).await?; // Decode the result. let result = ERC20Example::balanceOfCall::abi_decode_returns(&result, true)?._0; From 1cdacdbfaa2f275393a63d2e1421dc9e7d73f970 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Tue, 16 Apr 2024 12:05:57 +0000 Subject: [PATCH 7/8] fix broken examples --- examples/anvil/examples/deploy_contract_anvil.rs | 7 ++++--- examples/layers/examples/signer_layer.rs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/anvil/examples/deploy_contract_anvil.rs b/examples/anvil/examples/deploy_contract_anvil.rs index 46654855..60479790 100644 --- a/examples/anvil/examples/deploy_contract_anvil.rs +++ b/examples/anvil/examples/deploy_contract_anvil.rs @@ -32,6 +32,7 @@ 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()?; @@ -43,20 +44,20 @@ async fn main() -> Result<()> { println!("Anvil running at `{}`", anvil.endpoint()); // Deploy the contract. - let contract_builder = Counter::deploy_builder(&provider); + let contract_builder = Counter::deploy_builder(&provider).from(from); let contract_address = contract_builder.deploy().await?; 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 builder = contract.setNumber(U256::from(42)).from(from); 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(); + let builder = contract.increment().from(from); let receipt = builder.send().await?.get_receipt().await?; println!("Incremented number: {:?}", receipt.transaction_hash); diff --git a/examples/layers/examples/signer_layer.rs b/examples/layers/examples/signer_layer.rs index 99c1a994..2c513add 100644 --- a/examples/layers/examples/signer_layer.rs +++ b/examples/layers/examples/signer_layer.rs @@ -39,7 +39,7 @@ async fn main() -> Result<()> { .with_value(U256::from(100)) // Notice that without the `GasFiller`, you need to set the gas related fields. .with_gas_price(20_000_000_000) - .with_gas_limit(1_000_000_000); + .with_gas_limit(21_000); let builder = provider.send_transaction(tx).await?; let node_hash = *builder.tx_hash(); From 076025a36759c50ab7d8763fb90bcb23b6f14f59 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Tue, 16 Apr 2024 12:27:34 +0000 Subject: [PATCH 8/8] update layer -> singer, add gas_signer and improve documentation --- README.md | 9 +-- examples/{layers => fillers}/Cargo.toml | 0 examples/fillers/examples/gas_filler.rs | 66 +++++++++++++++++++ .../examples/nonce_management_filler.rs | 4 +- .../examples/recommended_fillers.rs} | 8 +-- .../examples/signer_filler.rs} | 4 +- 6 files changed, 80 insertions(+), 11 deletions(-) rename examples/{layers => fillers}/Cargo.toml (100%) create mode 100644 examples/fillers/examples/gas_filler.rs rename examples/{layers => fillers}/examples/nonce_management_filler.rs (92%) rename examples/{layers/examples/recommended_filler.rs => fillers/examples/recommended_fillers.rs} (88%) rename examples/{layers/examples/signer_layer.rs => fillers/examples/signer_filler.rs} (95%) diff --git a/README.md b/README.md index bcc48734..038007f4 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,11 @@ This repository contains the following examples: - [x] [Deploy from artifact](./examples/contracts/examples/deploy_from_artifact.rs) - [x] [Deploy from contract](./examples/contracts/examples/deploy_from_contract.rs) - [x] [Interact with ABI](./examples/contracts/examples/interact_with_abi.rs) -- [x] Layers (Middleware) - - [x] [Nonce management filler](./examples/layers/examples/nonce_layer.rs) - - [x] [Recommended filler](./examples/layers/examples/recommended_layers.rs) - - [x] [Signer management layer](./examples/layers/examples/signer_layer.rs) +- [x] Fillers (Middleware) + - [x] [Gas filler](./examples/fillers/examples/gas_filler.rs) + - [x] [Nonce management filler](./examples/fillers/examples/nonce_filler.rs) + - [x] [Recommended fillers](./examples/fillers/examples/recommended_fillers.rs) + - [x] [Signer management filler](./examples/fillers/examples/signer_filler.rs) - [x] Subscriptions - [x] [Subscribe and watch blocks](./examples/subscriptions/examples/subscribe_blocks.rs) - [x] [Subscribe to contract events and watch logs](./examples/subscriptions/examples/watch_contract_event.rs) diff --git a/examples/layers/Cargo.toml b/examples/fillers/Cargo.toml similarity index 100% rename from examples/layers/Cargo.toml rename to examples/fillers/Cargo.toml diff --git a/examples/fillers/examples/gas_filler.rs b/examples/fillers/examples/gas_filler.rs new file mode 100644 index 00000000..cbaba2f9 --- /dev/null +++ b/examples/fillers/examples/gas_filler.rs @@ -0,0 +1,66 @@ +//! Example of using the `GasFiller` in the provider. + +use alloy::{ + network::{EthereumSigner, TransactionBuilder}, + node_bindings::Anvil, + primitives::{address, U256}, + providers::{Provider, ProviderBuilder}, + rpc::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 signer from the first default Anvil account (Alice). + let signer: LocalWallet = anvil.keys()[0].clone().into(); + + // Create two users, Alice and Vitalik. + let alice = signer.address(); + let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"); + + // Create a provider with the signer. + let rpc_url = anvil.endpoint().parse()?; + let provider = ProviderBuilder::new() + // Add the `GasFiller` to the provider. + // It is generally recommended to use the `.with_recommended_fillers()` method, which + // includes the `GasFiller`. + .with_gas_estimation() + .signer(EthereumSigner::from(signer)) + .on_http(rpc_url)?; + + // Create an EIP-1559 type transaction. + let tx = TransactionRequest::default() + .with_from(alice) + .with_to(vitalik.into()) + .with_value(U256::from(100)) + // Notice that without the `NonceFiller`, you need to set `nonce` field. + .with_nonce(0) + // Notice that without the `ChainIdFiller`, you need to set the `chain_id` field. + .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_tx = provider.get_transaction_by_hash(node_hash).await?; + assert_eq!(pending_tx.nonce, 0); + + println!("Transaction sent with nonce: {}", pending_tx.nonce); + + // Update the nonce and send the transaction again. + let tx = tx.with_nonce(1); + + // 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_tx = provider.get_transaction_by_hash(node_hash).await?; + assert_eq!(pending_tx.nonce, 1); + + println!("Transaction sent with nonce: {}", pending_tx.nonce); + + Ok(()) +} diff --git a/examples/layers/examples/nonce_management_filler.rs b/examples/fillers/examples/nonce_management_filler.rs similarity index 92% rename from examples/layers/examples/nonce_management_filler.rs rename to examples/fillers/examples/nonce_management_filler.rs index b1485728..343bc7ba 100644 --- a/examples/layers/examples/nonce_management_filler.rs +++ b/examples/fillers/examples/nonce_management_filler.rs @@ -36,6 +36,8 @@ async fn main() -> Result<()> { let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() // Add the `NonceFiller` to the provider. + // It is generally recommended to use the `.with_recommended_fillers()` method, which + // includes the `NonceFiller`. .with_nonce_management() .signer(EthereumSigner::from(signer)) .on_http(rpc_url)?; @@ -49,7 +51,7 @@ async fn main() -> Result<()> { .with_gas_limit(21_000) .with_max_fee_per_gas(20_000_000_000) .with_max_priority_fee_per_gas(1_000_000_000) - // It is required to set the chain_id for EIP-1559 transactions. + // Notice that without the `ChainIdFiller`, you need to set the `chain_id` field. .with_chain_id(anvil.chain_id()); // Send the transaction, the nonce (0) is automatically managed by the provider. diff --git a/examples/layers/examples/recommended_filler.rs b/examples/fillers/examples/recommended_fillers.rs similarity index 88% rename from examples/layers/examples/recommended_filler.rs rename to examples/fillers/examples/recommended_fillers.rs index ebca2c3c..005f0b83 100644 --- a/examples/layers/examples/recommended_filler.rs +++ b/examples/fillers/examples/recommended_fillers.rs @@ -26,19 +26,19 @@ async fn main() -> Result<()> { // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() - // Adds the `ChainIdFiller`, GasFiller` and the `NonceFiller` layers. + // Adds the `ChainIdFiller`, `GasFiller` and the `NonceFiller` layers. .with_recommended_fillers() .signer(EthereumSigner::from(signer)) .on_http(rpc_url)?; // Build a EIP-1559 type transaction. // Notice that the `nonce` field is set by the `NonceFiller`. - // Notice that without the `GasFiller`, you need to set the gas related fields. + // Notice that the gas related fields are set by the `GasFiller`. + // Notice that the `chain_id` field is set by the `ChainIdFiller`. let tx = TransactionRequest::default() .with_from(alice) .with_to(vitalik.into()) - .with_value(U256::from(100)) - .with_chain_id(anvil.chain_id()); + .with_value(U256::from(100)); // Send the transaction, the nonce (0) is automatically managed by the provider. let builder = provider.send_transaction(tx.clone()).await?; diff --git a/examples/layers/examples/signer_layer.rs b/examples/fillers/examples/signer_filler.rs similarity index 95% rename from examples/layers/examples/signer_layer.rs rename to examples/fillers/examples/signer_filler.rs index 2c513add..eb4b9652 100644 --- a/examples/layers/examples/signer_layer.rs +++ b/examples/fillers/examples/signer_filler.rs @@ -1,4 +1,4 @@ -//! Example of using the `SignerLayer` in the provider. +//! Example of using the `SignerFiller` in the provider. use alloy::{ network::{EthereumSigner, TransactionBuilder}, @@ -26,7 +26,7 @@ async fn main() -> Result<()> { // Create a provider with the signer. let rpc_url = anvil.endpoint().parse()?; let provider = ProviderBuilder::new() - // Add the `SignerLayer` to the provider + // Add the `SignerFiller` to the provider .signer(EthereumSigner::from(signer)) .on_http(rpc_url)?;