Skip to content

Commit

Permalink
Adding an example to sign a transaction (#57)
Browse files Browse the repository at this point in the history
* Adding an example to sign a transaction

* PR feedback

* more pr feedback

* more suitable to use transaction

* one more
  • Loading branch information
MBerguer authored Apr 22, 2024
1 parent 56aeea0 commit c73b7d2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ 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] Fillers (Middleware)
- [x] [Gas estimation filler](./examples/fillers/examples/gas_filler.rs)
- [x] [Gas estimation 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)
Expand All @@ -60,6 +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)
- [ ] Wallets
- [ ] AWS signer
- [ ] GCP signer
Expand Down Expand Up @@ -95,4 +96,4 @@ Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in these crates by you, as defined in the Apache-2.0 license,
shall be dual licensed as above, without any additional terms or conditions.
</sub>
</sub>
53 changes: 53 additions & 0 deletions examples/transactions/examples/transaction_sign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Example of how to create and sign a transaction with EthereumSigner.
use alloy::{
network::{eip2718::Encodable2718, EthereumSigner, TransactionBuilder},
node_bindings::Anvil,
primitives::U256,
providers::{Provider, ProviderBuilder},
rpc::types::eth::TransactionRequest,
signers::wallet::LocalWallet,
};
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
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 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)
.with_to(bob.into())
.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);

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

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

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

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

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

Ok(())
}

0 comments on commit c73b7d2

Please sign in to comment.