ethers-rs/examples/local_signer.rs

33 lines
1.0 KiB
Rust
Raw Permalink Normal View History

use ethers::{prelude::*, utils::Anvil};
use eyre::Result;
2020-05-23 00:01:20 +00:00
use std::convert::TryFrom;
#[tokio::main]
2020-05-26 11:00:56 +00:00
async fn main() -> Result<()> {
let anvil = Anvil::new().spawn();
2020-06-02 10:58:48 +00:00
let wallet: LocalWallet = anvil.keys()[0].clone().into();
let wallet2: LocalWallet = anvil.keys()[1].clone().into();
2020-05-24 16:14:27 +00:00
// connect to the network
let provider = Provider::<Http>::try_from(anvil.endpoint())?;
2020-05-24 16:14:27 +00:00
// connect the wallet to the provider
2022-08-30 16:45:36 +00:00
let client = SignerMiddleware::new(provider, wallet.with_chain_id(anvil.chain_id()));
2020-05-24 14:41:12 +00:00
2020-05-24 16:14:27 +00:00
// craft the transaction
let tx = TransactionRequest::new().to(wallet2.address()).value(10000);
2020-05-23 00:01:20 +00:00
2020-05-24 16:14:27 +00:00
// send it!
let pending_tx = client.send_transaction(tx, None).await?;
2020-05-23 00:01:20 +00:00
2020-05-24 16:14:27 +00:00
// get the mined tx
let receipt = pending_tx.await?.ok_or_else(|| eyre::format_err!("tx dropped from mempool"))?;
let tx = client.get_transaction(receipt.transaction_hash).await?;
2020-05-24 20:27:51 +00:00
println!("Sent tx: {}\n", serde_json::to_string(&tx)?);
println!("Tx receipt: {}", serde_json::to_string(&receipt)?);
2020-05-23 00:01:20 +00:00
Ok(())
}