Skip to content

Commit

Permalink
Adding an example to sign a transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
MBerguer committed Apr 19, 2024
1 parent 096abf0 commit ca4ce4f
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions examples/transactions/examples/transfer_sign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use alloy::network::{eip2718::Encodable2718, TransactionBuilder};
use alloy::primitives::utils::parse_units;
use alloy::providers::Provider;
use alloy::{
network::EthereumSigner, node_bindings::Anvil, primitives::U256, providers::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();
let destination: LocalWallet = anvil.keys()[1].clone().into();

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

let tx = TransactionRequest {
from: Some(signer.address()),
to: Some(destination.address()),
nonce: Some(provider.get_transaction_count(signer.address(), 0.into()).await?),
chain_id: Some(provider.get_chain_id().await?),
value: Some(U256::from(42)),
gas: Some(21000),
max_priority_fee_per_gas: Some(parse_units("0.1", "gwei").unwrap().get_absolute().to()),
max_fee_per_gas: Some(parse_units("30", "gwei").unwrap().get_absolute().to()),
..Default::default()
};

let to_send = tx.build(&EthereumSigner::from(signer)).await?;

let out = to_send.encoded_2718();

let receipt = provider.send_raw_transaction(&out).await?.get_receipt().await?;

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

Ok(())
}

0 comments on commit ca4ce4f

Please sign in to comment.