2020-05-26 11:00:56 +00:00
|
|
|
use anyhow::Result;
|
2020-06-11 06:45:14 +00:00
|
|
|
use ethers::{prelude::*, utils::Ganache};
|
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<()> {
|
2020-06-22 13:42:34 +00:00
|
|
|
let ganache = Ganache::new().spawn();
|
2020-06-02 10:58:48 +00:00
|
|
|
|
2020-10-02 08:41:16 +00:00
|
|
|
let wallet: LocalWallet = ganache.keys()[0].clone().into();
|
|
|
|
let wallet2: LocalWallet = ganache.keys()[1].clone().into();
|
2020-05-30 14:11:51 +00:00
|
|
|
|
2020-05-24 16:14:27 +00:00
|
|
|
// connect to the network
|
2020-06-22 13:42:34 +00:00
|
|
|
let provider = Provider::<Http>::try_from(ganache.endpoint())?;
|
2020-05-24 16:14:27 +00:00
|
|
|
|
2020-05-30 14:11:51 +00:00
|
|
|
// connect the wallet to the provider
|
2020-10-08 15:56:36 +00:00
|
|
|
let client = SignerMiddleware::new(provider, wallet);
|
2020-05-24 14:41:12 +00:00
|
|
|
|
2020-05-24 16:14:27 +00:00
|
|
|
// craft the transaction
|
2020-06-22 13:42:34 +00:00
|
|
|
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!
|
2020-12-17 11:26:01 +00:00
|
|
|
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
|
2021-07-06 08:06:18 +00:00
|
|
|
let receipt = pending_tx
|
|
|
|
.await?
|
|
|
|
.ok_or_else(|| anyhow::format_err!("tx dropped from mempool"))?;
|
2020-06-15 12:40:06 +00:00
|
|
|
let tx = client.get_transaction(receipt.transaction_hash).await?;
|
2020-05-24 20:27:51 +00:00
|
|
|
|
2020-05-31 16:20:08 +00:00
|
|
|
println!("Sent tx: {}\n", serde_json::to_string(&tx)?);
|
2020-05-30 14:11:51 +00:00
|
|
|
println!("Tx receipt: {}", serde_json::to_string(&receipt)?);
|
2020-05-23 00:01:20 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|