Sitelet https://github.com/alloy-rs/examples/pull/104/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions examples/fillers/examples/gas_filler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ async fn main() -> Result<()> {
// 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()
Expand All @@ -33,9 +29,9 @@ async fn main() -> Result<()> {
.signer(EthereumSigner::from(signer))
.on_http(rpc_url);

// Create an EIP-1559 type transaction.
// Build an EIP-1559 type transaction to send 100 wei to Vitalik.
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(vitalik)
.with_value(U256::from(100))
// Notice that without the `NonceFiller`, you need to set `nonce` field.
Expand Down
8 changes: 2 additions & 6 deletions examples/fillers/examples/nonce_filler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ async fn main() -> Result<()> {
// 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()
Expand All @@ -42,9 +38,9 @@ async fn main() -> Result<()> {
.signer(EthereumSigner::from(signer))
.on_http(rpc_url);

// Create an EIP-1559 type transaction.
// Build an EIP-1559 type transaction to send 100 wei to Vitalik.
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(vitalik)
.with_value(U256::from(100))
// Notice that without the `GasFiller`, you need to set the gas related fields.
Expand Down
10 changes: 3 additions & 7 deletions examples/fillers/examples/recommended_fillers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ async fn main() -> Result<()> {
// 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()
Expand All @@ -31,12 +27,12 @@ async fn main() -> Result<()> {
.signer(EthereumSigner::from(signer))
.on_http(rpc_url);

// Build a EIP-1559 type transaction.
// Build an EIP-1559 type transaction to send 100 wei to Vitalik.
// Notice that the `nonce` field is set by the `NonceFiller`.
// 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).with_value(U256::from(100));
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let tx = TransactionRequest::default().with_to(vitalik).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?;
Expand Down
10 changes: 3 additions & 7 deletions examples/fillers/examples/signer_filler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ async fn main() -> Result<()> {
// 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()
Expand All @@ -31,12 +27,12 @@ async fn main() -> Result<()> {
.on_http(rpc_url);

// Build a legacy type transaction to send 100 wei to Vitalik.
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let tx = TransactionRequest::default()
.with_from(alice)
// Notice that without the `NonceFiller`, you need to manually set the nonce field.
.with_nonce(0)
.with_to(vitalik)
.with_value(U256::from(100))
// Notice that without the `NonceFiller`, you need to manually set the nonce field.
.with_nonce(0)
// Notice that without the `GasFiller`, you need to set the gas related fields.
.with_gas_price(20_000_000_000)
.with_gas_limit(21_000);
Expand Down
3 changes: 1 addition & 2 deletions examples/providers/examples/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ async fn main() -> Result<()> {
.on_http(rpc_url);

// Create a transaction.
let tx =
TransactionRequest::default().with_from(alice).with_to(bob).with_value(U256::from(100));
let tx = TransactionRequest::default().with_to(bob).with_value(U256::from(100));

// Send the transaction and wait for the broadcast.
let pending_tx = provider.send_transaction(tx).await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ async fn main() -> Result<()> {
let bob = anvil.addresses()[1];

// Build a transaction to send 100 wei from Alice to Bob.
// The `from` field is automatically filled to the first signer's address (Alice).
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(bob)
.with_nonce(0)
.with_chain_id(anvil.chain_id())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ async fn main() -> Result<()> {
let sidecar = sidecar.build()?;

// Build a transaction to send the sidecar from Alice to Bob.
// The `from` field is automatically filled to the first signer's address (Alice).
let gas_price = provider.get_gas_price().await?;
let eip1559_est = provider.estimate_eip1559_fees(None).await?;
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(bob)
.with_nonce(0)
.with_max_fee_per_blob_gas(gas_price)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ async fn main() -> Result<()> {
let bob = anvil.addresses()[1];

// Build a transaction to send 100 wei from Alice to Bob.
// The `from` field is automatically filled to the first signer's address (Alice).
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(bob)
.with_nonce(0)
.with_value(U256::from(100))
Expand Down
7 changes: 2 additions & 5 deletions examples/transactions/examples/send_private_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ async fn main() -> Result<()> {
// Create a signer from a random wallet.
let signer = LocalWallet::random();

// Create two users, Alice and Bob.
let alice = signer.address();
let bob = LocalWallet::random().address();

// Build a transaction to send 100 wei from Alice to Bob.
// The `from` field is automatically filled to the first signer's address (Alice).
let bob = LocalWallet::random().address();
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(bob)
.with_nonce(0)
.with_chain_id(1)
Expand Down
2 changes: 1 addition & 1 deletion examples/transactions/examples/send_raw_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ async fn main() -> Result<()> {
let bob = anvil.addresses()[1];

// Build a transaction to send 100 wei from Alice to Bob.
// The `from` field is automatically filled to the first signer's address (Alice).
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(bob)
.with_nonce(0)
.with_chain_id(anvil.chain_id())
Expand Down
10 changes: 4 additions & 6 deletions examples/transactions/examples/trace_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ async fn main() -> Result<()> {
let rpc_url = "https://eth.merkle.io".parse()?;
let provider = ProviderBuilder::new().on_http(rpc_url);

// Create two users, Alice and Bob.
let alice = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let bob = address!("70997970C51812dc3A010C7d01b50e0d17dc79C8");

// Build a transaction to send 100 wei from Alice to Bob.
// Build a transaction to send 100 wei from Alice to Vitalik.
let alice = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let tx =
TransactionRequest::default().with_from(alice).with_to(bob).with_value(U256::from(100));
TransactionRequest::default().with_from(alice).with_to(vitalik).with_value(U256::from(100));

// Trace the transaction on top of the latest block.
let trace_type = [TraceType::Trace];
Expand Down
9 changes: 3 additions & 6 deletions examples/transactions/examples/transfer_eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ async fn main() -> Result<()> {
// Set up wallet from the first default Anvil account (Alice).
let wallet: LocalWallet = anvil.keys()[0].clone().into();

// Create two users, Alice and Bob.
let alice = wallet.address();
let bob = anvil.addresses()[1];

// Create a provider with a signer and the network.
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.signer(EthereumSigner::from(wallet))
.on_http(rpc_url);

// Build a transaction to send 100 wei from Alice to Bob.
let tx =
TransactionRequest::default().with_from(alice).with_to(bob).with_value(U256::from(100));
// The `from` field is automatically filled to the first signer's address (Alice).
let bob = anvil.addresses()[1];
let tx = TransactionRequest::default().with_to(bob).with_value(U256::from(100));

// Send the transaction and listen for the transaction to be included.
let tx_hash = provider.send_transaction(tx).await?.watch().await?;
Expand Down
18 changes: 5 additions & 13 deletions examples/transactions/examples/with_access_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,20 @@ async fn main() -> Result<()> {
let provider =
ProviderBuilder::new().with_recommended_fillers().on_builtin(&anvil.endpoint()).await?;

// Create two users, Alice and Bob.
let alice = anvil.addresses()[0];
let bob = anvil.addresses()[1];

// Deploy the `SimpleStorage` contract.
let alice = anvil.addresses()[0];
let contract_address = SimpleStorage::deploy_builder(provider.clone(), "initial".to_string())
.from(alice)
.deploy()
.await?;
let contract = SimpleStorage::new(contract_address, provider.clone());

// Build a transaction to set the values.
// Build a transaction to set the values of the contract.
// The `from` field is automatically filled to the first signer's address (Alice).
let set_value_call = contract.setValues("hello".to_string(), "world".to_string());
let calldata = set_value_call.calldata().to_owned();

let eip1559_fees = provider.estimate_eip1559_fees(None).await?;
let tx = TransactionRequest::default()
.from(bob)
.to(contract_address)
.input(calldata.into())
.max_fee_per_gas(eip1559_fees.max_fee_per_gas)
.max_priority_fee_per_gas(eip1559_fees.max_priority_fee_per_gas);
let bob = anvil.addresses()[1];
let tx = TransactionRequest::default().from(bob).to(contract_address).input(calldata.into());

// Create an access list for the transaction.
let access_list_with_gas_used = provider.create_access_list(&tx).await?;
Expand Down
10 changes: 4 additions & 6 deletions examples/wallets/examples/keystore_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ 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()?;
Expand All @@ -34,11 +33,10 @@ async fn main() -> Result<()> {
.signer(EthereumSigner::from(signer))
.on_http(rpc_url);

// Build a transaction to send 100 wei to Vitalik from Alice.
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"))
.with_value(U256::from(100));
// Build a transaction to send 100 wei from Alice to Vitalik.
// The `from` field is automatically filled to the first signer's address (Alice).
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let tx = TransactionRequest::default().with_to(vitalik).with_value(U256::from(100));

// Send the transaction and wait for inclusion.
let tx_hash = provider.send_transaction(tx).await?.watch().await?;
Expand Down
15 changes: 5 additions & 10 deletions examples/wallets/examples/ledger_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use alloy::{
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
signers::{
ledger::{HDPath, LedgerSigner},
Signer,
},
signers::ledger::{HDPath, LedgerSigner},
};
use eyre::Result;

Expand All @@ -19,19 +16,17 @@ 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 provider = ProviderBuilder::new()
.with_recommended_fillers()
.signer(EthereumSigner::from(signer))
.on_http(rpc_url);

// Build a transaction to send 100 wei to Vitalik.
let tx = TransactionRequest::default()
.with_from(from)
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"))
.with_value(U256::from(100));
// Build a transaction to send 100 wei from Alice to Vitalik.
// The `from` field is automatically filled to the first signer's address (Alice).
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let tx = TransactionRequest::default().with_to(vitalik).with_value(U256::from(100));

// Send the transaction and wait for inclusion with 3 confirmations.
let tx_hash =
Expand Down
11 changes: 5 additions & 6 deletions examples/wallets/examples/mnemonic_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@ async fn main() -> Result<()> {
let index = 0u32;
let password = "TREZOR123";

// Access mnemonic phrase with password
// Child key at derivation path: m/44'/60'/0'/0/{index}
// Access mnemonic phrase with password.
// Child key at derivation path: m/44'/60'/0'/0/{index}.
let wallet = MnemonicBuilder::<English>::default()
.phrase(phrase)
.index(index)?
// Use this if your mnemonic is encrypted
// Use this if your mnemonic is encrypted.
.password(password)
.build()?;

println!("Wallet: {}", wallet.address());

// Generate a random wallet (24 word phrase) at custom derivation path
// Generate a random wallet (24 word phrase) at custom derivation path.
let wallet = MnemonicBuilder::<English>::default()
.word_count(24)
.derivation_path("m/44'/60'/0'/2/1")?
// Optionally add this if you want the generated mnemonic to be written
// to a file
// .write_to(path)
// to a file `.write_to(path)`.
.build_random()?;

println!("Random wallet: {}", wallet.address());
Expand Down
6 changes: 3 additions & 3 deletions examples/wallets/examples/private_key_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ async fn main() -> Result<()> {

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

// [RISK WARNING! Writing a private key in the code file is insecure behavior.]
// The following code is for testing only. Set up signer from private key, be aware of danger.
// let signer: LocalWallet = "<THE_PRIVATE_KEY>".parse().expect("Failed to parse private key");
let alice = signer.address();

// Create a provider with the signer.
let rpc_url = anvil.endpoint().parse()?;
Expand All @@ -30,9 +30,9 @@ 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 from Alice to Vitalik.
// The `from` field is automatically filled to the first signer's address (Alice).
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"))
.with_value(U256::from(100));

Expand Down
15 changes: 5 additions & 10 deletions examples/wallets/examples/trezor_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use alloy::{
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
signers::{
trezor::{HDPath, TrezorSigner},
Signer,
},
signers::trezor::{HDPath, TrezorSigner},
};
use eyre::Result;

Expand All @@ -19,19 +16,17 @@ 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 provider = ProviderBuilder::new()
.with_recommended_fillers()
.signer(EthereumSigner::from(signer))
.on_http(rpc_url);

// Build a transaction to send 100 wei to Vitalik.
let tx = TransactionRequest::default()
.with_from(from)
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"))
.with_value(U256::from(100));
// Build a transaction to send 100 wei from Alice to Vitalik.
// The `from` field is automatically filled to the first signer's address (Alice).
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let tx = TransactionRequest::default().with_to(vitalik).with_value(U256::from(100));

// Send the transaction and wait for inclusion with 3 confirmations.
let tx_hash =
Expand Down
Loading