forked from alloy-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer_eth.rs
More file actions
33 lines (26 loc) · 1.02 KB
/
Copy pathtransfer_eth.rs
File metadata and controls
33 lines (26 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! Example of how to transfer ETH from one account to another.
use alloy::{
network::TransactionBuilder,
primitives::U256,
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
};
use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let provider = ProviderBuilder::new().connect_anvil_with_wallet();
// Create two users, Alice and Bob.
let accounts = provider.get_accounts().await?;
let alice = accounts[0];
let bob = accounts[1];
// Build a transaction to send 100 wei from Alice to Bob.
// The `from` field is automatically filled to the first signer's address (Alice).
let tx =
TransactionRequest::default().with_from(alice).with_to(bob).with_value(U256::from(100));
// Send the transaction and listen for the transaction to be included.
let tx_hash = provider.send_transaction(tx).await?.watch().await?;
println!("Sent transaction: {tx_hash}");
Ok(())
}