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 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
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
8 changes: 4 additions & 4 deletions examples/anvil/examples/deploy_contract_anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ async fn main() -> Result<()> {
// Deploy the contract.
let contract = Counter::deploy(&provider).await?;

println!("Deployed contract at address: {:?}", contract.address());
println!("Deployed contract at address: {}", contract.address());

// 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);
println!("Set number to 42: {}", receipt.transaction_hash);

// Increment the number to 43.
let builder = contract.increment();
let receipt = builder.send().await?.get_receipt().await?;

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

// Retrieve the number, which should be 43.
let Counter::numberReturn { _0 } = contract.number().call().await?;

println!("Retrieved number: {:?}", _0.to_string());
println!("Retrieved number: {_0}");

Ok(())
}
8 changes: 4 additions & 4 deletions examples/contracts/examples/deploy_from_artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ async fn main() -> Result<()> {
// Deploy the contract.
let contract = Counter::deploy(&provider).await?;

println!("Deployed contract at address: {:?}", contract.address());
println!("Deployed contract at address: {}", contract.address());

let builder = contract.setNumber(U256::from(42));
let receipt = builder.send().await?.get_receipt().await?;

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

// Increment the number to 43.
let builder = contract.increment();
let receipt = builder.send().await?.get_receipt().await?;

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

// Retrieve the number, which should be 43.
let Counter::numberReturn { _0 } = contract.number().call().await?;

println!("Retrieved number: {:?}", _0.to_string());
println!("Retrieved number: {_0}");

Ok(())
}
8 changes: 4 additions & 4 deletions examples/contracts/examples/deploy_from_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ async fn main() -> Result<()> {
// Deploy the contract.
let contract = Counter::deploy(&provider).await?;

println!("Deployed contract at address: {:?}", contract.address());
println!("Deployed contract at address: {}", contract.address());

let builder = contract.setNumber(U256::from(42));
let receipt = builder.send().await?.get_receipt().await?;

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

// Increment the number to 43.
let builder = contract.increment();
let receipt = builder.send().await?.get_receipt().await?;

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

// Retrieve the number, which should be 43.
let Counter::numberReturn { _0 } = contract.number().call().await?;

println!("Retrieved number: {:?}", _0.to_string());
println!("Retrieved number: {_0}");

Ok(())
}
6 changes: 3 additions & 3 deletions examples/providers/examples/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ async fn main() -> Result<()> {
// Send the transaction and wait for the receipt.
let pending_tx = provider.send_transaction(tx).await?;

println!("Pending transaction...{:?}", pending_tx.tx_hash());
println!("Pending transaction... {}", pending_tx.tx_hash());

// 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()
"Transaction included in block {}",
receipt.block_number.expect("Failed to get block number")
);

assert_eq!(receipt.from, alice);
Expand Down
2 changes: 1 addition & 1 deletion examples/providers/examples/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async fn main() -> Result<()> {

let handle = tokio::spawn(async move {
while let Some(block) = stream.next().await {
println!("{:?}", block.header.number);
println!("{}", block.header.number.unwrap());
}
});

Expand Down
2 changes: 1 addition & 1 deletion examples/providers/examples/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn main() -> Result<()> {
// Take the stream and print the block number upon receiving a new block.
let handle = tokio::spawn(async move {
while let Some(block) = stream.next().await {
println!("Latest block number: {:?}", block.header.number.unwrap().to_string());
println!("Latest block number: {}", block.header.number.unwrap());
}
});

Expand Down
7 changes: 2 additions & 5 deletions examples/providers/examples/ws_with_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ async fn main() -> Result<()> {
// Take the basic stream and print the block number upon receiving a new block.
let basic_handle = tokio::spawn(async move {
while let Some(block) = stream_basic.next().await {
println!("Latest block number (basic): {:?}", block.header.number.unwrap().to_string());
println!("Latest block number (basic): {}", block.header.number.unwrap());
}
});

// Take the bearer stream and print the block number upon receiving a new block.
let bearer_handle = tokio::spawn(async move {
while let Some(block) = stream_bearer.next().await {
println!(
"Latest block number (bearer): {:?}",
block.header.number.unwrap().to_string()
);
println!("Latest block number (bearer): {}", block.header.number.unwrap());
}
});

Expand Down
2 changes: 1 addition & 1 deletion examples/subscriptions/examples/event_multiplexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async fn main() -> Result<()> {
// Deploy the `EventExample` contract.
let contract = EventMultiplexer::deploy(provider).await?;

println!("Deployed contract at: {:?}", contract.address());
println!("Deployed contract at: {}", contract.address());

// Create filters for each event.
let add_filter = contract.Add_filter().watch().await?;
Expand Down
2 changes: 1 addition & 1 deletion examples/subscriptions/examples/poll_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn main() -> Result<()> {
// Deploy the `Counter` contract.
let contract = Counter::deploy(provider.clone()).await?;

println!("Deployed contract at: {:?}", contract.address());
println!("Deployed contract at: {}", contract.address());

// Create filters for each event.
let increment_filter = contract.Increment_filter().watch().await?;
Expand Down
2 changes: 1 addition & 1 deletion examples/subscriptions/examples/subscribe_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn main() -> Result<()> {
let mut stream = subscription.into_stream().take(2);

while let Some(block) = stream.next().await {
println!("Received block number: {:?}", block.header.number.unwrap().to_string());
println!("Received block number: {}", block.header.number.unwrap());
}

// Poll for block headers.
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());

// 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")
);

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());

// 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")
);

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());

// 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")
);

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

Ok(())
}
Loading
Loading