ethers-rs/examples/transfer_eth.rs

43 lines
1.1 KiB
Rust
Raw Normal View History

2020-05-24 15:55:46 +00:00
use ethers::{
types::{BlockNumber, TransactionRequest},
HttpProvider,
};
2020-05-22 18:37:21 +00:00
use std::convert::TryFrom;
#[tokio::main]
2020-05-23 00:01:20 +00:00
async fn main() -> Result<(), failure::Error> {
2020-05-24 16:14:27 +00:00
// connect to the network
2020-05-24 15:55:46 +00:00
let provider = HttpProvider::try_from("http://localhost:8545")?;
2020-05-24 17:17:46 +00:00
let from = "784C1bA9846aB4CE78E9CFa27884E29dd31d593A".parse()?;
2020-05-22 18:37:21 +00:00
2020-05-24 16:14:27 +00:00
// craft the tx
let tx = TransactionRequest {
2020-05-24 17:17:46 +00:00
from: Some(from),
2020-05-24 16:14:27 +00:00
to: Some("9A7e5d4bcA656182e66e33340d776D1542143006".parse()?),
value: Some(1000u64.into()),
gas: None,
gas_price: None,
data: None,
nonce: None,
};
// broadcast it via the eth_sendTransaction API
let tx_hash = provider.send_transaction(tx).await?;
2020-05-23 00:01:20 +00:00
let tx = provider.get_transaction(tx_hash).await?;
println!("{}", serde_json::to_string(&tx)?);
let nonce1 = provider
.get_transaction_count(from, Some(BlockNumber::Latest))
.await?;
let nonce2 = provider
.get_transaction_count(from, Some(BlockNumber::Number(0.into())))
.await?;
assert!(nonce2 < nonce1);
Ok(())
2020-05-22 18:37:21 +00:00
}