ethers-rs/examples/local_signer.rs

41 lines
1.2 KiB
Rust
Raw Normal View History

2020-05-24 17:17:46 +00:00
use ethers::{types::TransactionRequest, HttpProvider, MainnetWallet};
2020-05-23 00:01:20 +00:00
use std::convert::TryFrom;
use std::str::FromStr;
#[tokio::main]
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 16:14:27 +00:00
// create a wallet and connect it to the provider
2020-05-24 14:41:12 +00:00
let client = MainnetWallet::from_str(
2020-05-24 17:17:46 +00:00
"15c42bf2987d5a8a73804a8ea72fb4149f88adf73e98fc3f8a8ce9f24fcb7774",
2020-05-23 00:01:20 +00:00
)?
.connect(&provider);
// get the account's nonce (we abuse the Deref to access the provider's functions)
let nonce = client.get_transaction_count(client.address(), None).await?;
2020-05-24 17:17:46 +00:00
dbg!(nonce);
2020-05-24 14:41:12 +00:00
2020-05-24 16:14:27 +00:00
// craft the transaction
2020-05-24 17:17:46 +00:00
let tx = TransactionRequest {
from: None,
2020-05-23 00:01:20 +00:00
to: Some("986eE0C8B91A58e490Ee59718Cca41056Cf55f24".parse().unwrap()),
2020-05-24 17:17:46 +00:00
gas: Some(21000.into()),
gas_price: Some(100_000.into()),
value: Some(10000.into()),
data: Some(vec![].into()),
nonce: Some(nonce),
2020-05-23 00:01:20 +00:00
};
2020-05-24 16:14:27 +00:00
// send it!
let tx = client.sign_and_send_transaction(tx).await?;
2020-05-23 00:01:20 +00:00
2020-05-24 16:14:27 +00:00
// get the mined tx
2020-05-24 14:41:12 +00:00
let tx = client.get_transaction(tx.hash).await?;
2020-05-23 00:01:20 +00:00
println!("{}", serde_json::to_string(&tx)?);
Ok(())
}