Skip to content

Commit

Permalink
minor followup of #57
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosnacks committed Apr 22, 2024
1 parent c73b7d2 commit 0a6d00b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ This repository contains the following examples:
- [x] [Trace transaction](./examples/transactions/examples/trace_transaction.rs)
- [x] [Transfer ERC20 token](./examples/transactions/examples/transfer_erc20.rs)
- [x] [Transfer ETH](./examples/transactions/examples/transfer_eth.rs)
- [x] [Signing a transaction](./examples/transactions/examples/transaction_sign.rs)
- [x] [Sign and send a raw transaction](./examples/transactions/examples/sign_transaction.rs)
- [ ] Wallets
- [ ] AWS signer
- [ ] GCP signer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Example of how to create and sign a transaction with EthereumSigner.
//! Example of signing, encoding and sending a raw transaction using a wallet.
use alloy::{
network::{eip2718::Encodable2718, EthereumSigner, TransactionBuilder},
Expand All @@ -12,18 +12,22 @@ use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
// Spin up a forked 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 a provider.
let rpc_url = anvil.endpoint().parse()?;
let provider = ProviderBuilder::new().on_http(rpc_url)?;

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

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

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

// Build a transaction to send 100 wei from Alice to Bob.
let tx = TransactionRequest::default()
.with_from(alice)
Expand All @@ -36,15 +40,15 @@ async fn main() -> Result<()> {
.with_max_fee_per_gas(20_000_000_000);

// Build the transaction using the EthereumSigner with the provided signer.
let to_send = tx.build(&EthereumSigner::from(signer)).await?;
let tx_envelope = tx.build(&signer).await?;

// Encode the transaction using EIP-2718 encoding.
let out = to_send.encoded_2718();
let tx_encoded = tx_envelope.encoded_2718();

// Send the raw transaction and retrieve the transaction receipt.
let receipt = provider.send_raw_transaction(&out).await?.get_receipt().await?;
let receipt = provider.send_raw_transaction(&tx_encoded).await?.get_receipt().await?;

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

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

0 comments on commit 0a6d00b

Please sign in to comment.