ethers-rs/ethers/examples/local_signer.rs

42 lines
1.3 KiB
Rust
Raw Normal View History

2020-05-26 11:00:56 +00:00
use anyhow::Result;
use ethers::{prelude::*, utils::GanacheBuilder};
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 port = 8545u64;
let url = format!("http://localhost:{}", port).to_string();
let _ganache = GanacheBuilder::new()
.port(port)
.mnemonic("abstract vacuum mammal awkward pudding scene penalty purchase dinner depart evoke puzzle")
.spawn();
2020-06-02 10:58:48 +00:00
// this private key belongs to the above mnemonic
2020-06-02 10:58:48 +00:00
let wallet: Wallet =
"380eb0f3d505f087e438eca80bc4df9a7faa24f868e69fc0440261a0fc0567dc".parse()?;
2020-05-24 16:14:27 +00:00
// connect to the network
2020-06-02 10:58:48 +00:00
let provider = Provider::<Http>::try_from(url.as_str())?;
2020-05-24 16:14:27 +00:00
// connect the wallet to the provider
2020-06-02 10:58:48 +00:00
let client = wallet.connect(provider);
2020-05-24 14:41:12 +00:00
2020-05-24 16:14:27 +00:00
// craft the transaction
2020-05-24 18:34:56 +00:00
let tx = TransactionRequest::new()
.send_to_str("986eE0C8B91A58e490Ee59718Cca41056Cf55f24")?
.value(10000);
2020-05-23 00:01:20 +00:00
2020-05-24 16:14:27 +00:00
// send it!
2020-05-25 15:35:38 +00:00
let hash = 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
2020-05-25 15:35:38 +00:00
let tx = client.get_transaction(hash).await?;
2020-05-23 00:01:20 +00:00
2020-05-24 20:27:51 +00:00
let receipt = client.get_transaction_receipt(tx.hash).await?;
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(())
}