Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transactions): send_(legacy|eip1559|eip4844)_tx #77

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ This repository contains the following examples:
- [x] [Transfer ERC20 token](./examples/transactions/examples/transfer_erc20.rs)
- [x] [Transfer ETH](./examples/transactions/examples/transfer_eth.rs)
- [x] [Sign and send a raw transaction](./examples/transactions/examples/sign_transaction.rs)
- [x] [Send EIP-1559 transaction](./examples/transactions/examples/send_eip1559_tx.rs)
- [x] [Send legacy transaction](./examples/transactions/examples/send_legacy_tx.rs)
- [x] [Send EIP-4844 transaction](./examples/transactions/examples/send_eip4844_tx.rs)
- [x] [Send transaction with access list](./examples/transactions/examples/with_access_list.rs)
- [x] [Send 4844 transaction](./examples/transactions/examples/send_4844.rs)
- [x] Wallets
- [x] [AWS signer](./examples/wallets/examples/aws_signer.rs)
- [x] [Ledger signer](./examples/wallets/examples/ledger_signer.rs)
Expand Down
42 changes: 0 additions & 42 deletions examples/transactions/examples/send_4844.rs

This file was deleted.

54 changes: 54 additions & 0 deletions examples/transactions/examples/send_eip1559_tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Example showing how to send an [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) transaction.

use alloy::{
network::TransactionBuilder,
node_bindings::Anvil,
primitives::U256,
providers::{Provider, ProviderBuilder},
rpc::types::eth::TransactionRequest,
};
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()?;

// Create a provider.
let rpc_url = anvil.endpoint().parse()?;
let provider = ProviderBuilder::new().on_http(rpc_url);

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

// Build a transaction to send 100 wei from Alice to Bob.
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(bob)
.with_nonce(0)
.with_chain_id(anvil.chain_id())
.with_value(U256::from(100))
.with_gas_limit(21_000)
.with_max_priority_fee_per_gas(1_000_000_000)
.with_max_fee_per_gas(20_000_000_000);

// Send the transaction and wait for the receipt.
let pending_tx = provider.send_transaction(tx).await?;

println!("Pending transaction...{:?}", pending_tx.tx_hash());
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved

// Wait for the transaction to be included.
let receipt = pending_tx.get_receipt().await?;

println!(
"Transaction included in block: {:?}",
receipt.block_number.expect("Failed to get block number").to_string()
);
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved

assert_eq!(receipt.from, alice);
assert_eq!(receipt.to, Some(bob));

Ok(())
}
61 changes: 61 additions & 0 deletions examples/transactions/examples/send_eip4844_tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! Example showing how to send an [EIP-4844](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-4844.md) transaction.

use alloy::{
consensus::{SidecarBuilder, SimpleCoder},
eips::eip4844::DATA_GAS_PER_BLOB,
network::TransactionBuilder,
node_bindings::Anvil,
providers::{Provider, ProviderBuilder},
rpc::types::eth::TransactionRequest,
};
use eyre::Result;

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

// Create a provider.
let provider = ProviderBuilder::new().on_builtin(&anvil.endpoint()).await?;

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

// Create a sidecar with some data.
let sidecar: SidecarBuilder<SimpleCoder> =
SidecarBuilder::from_slice("Blobs are fun!".as_bytes());
let sidecar = sidecar.build()?;

// Build a transaction to send the sidecar from Alice to Bob.
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)
.with_max_fee_per_gas(eip1559_est.max_fee_per_gas)
.with_max_priority_fee_per_gas(eip1559_est.max_priority_fee_per_gas)
.with_blob_sidecar(sidecar);

// Send the transaction and wait for the receipt.
let pending_tx = provider.send_transaction(tx).await?;

println!("Pending transaction...{:?}", pending_tx.tx_hash());
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved

// Wait for the transaction to be included.
let receipt = pending_tx.get_receipt().await?;

println!(
"Transaction included in block: {:?}",
receipt.block_number.expect("Failed to get block number").to_string()
);

assert_eq!(receipt.from, alice);
assert_eq!(receipt.to, Some(bob));
assert_eq!(receipt.blob_gas_used.unwrap(), DATA_GAS_PER_BLOB as u128);

Ok(())
}
52 changes: 52 additions & 0 deletions examples/transactions/examples/send_legacy_tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//! Example showing how to send a legacy transaction.

use alloy::{
network::TransactionBuilder,
node_bindings::Anvil,
primitives::U256,
providers::{Provider, ProviderBuilder},
rpc::types::eth::TransactionRequest,
};
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()?;

// Create a provider.
let rpc_url = anvil.endpoint().parse()?;
let provider = ProviderBuilder::new().on_http(rpc_url);

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

// Build a transaction to send 100 wei from Alice to Bob.
let tx = TransactionRequest::default()
.with_from(alice)
.with_to(bob)
.with_nonce(0)
.with_value(U256::from(100))
.with_gas_price(20_000_000_000)
.with_gas_limit(21_000);

// Send the transaction and wait for the receipt.
let pending_tx = provider.send_transaction(tx).await?;

println!("Pending transaction...{:?}", pending_tx.tx_hash());
zerosnacks marked this conversation as resolved.
Show resolved Hide resolved

// Wait for the transaction to be included.
let receipt = pending_tx.get_receipt().await?;

println!(
"Transaction included in block: {:?}",
receipt.block_number.expect("Failed to get block number").to_string()
);

assert_eq!(receipt.from, alice);
assert_eq!(receipt.to, Some(bob));

Ok(())
}
Loading