2020-05-26 11:00:56 +00:00
|
|
|
use anyhow::Result;
|
2020-05-24 15:55:46 +00:00
|
|
|
use ethers::{
|
2020-05-31 16:01:34 +00:00
|
|
|
core::{
|
|
|
|
types::{BlockNumber, TransactionRequest},
|
|
|
|
utils::GanacheBuilder,
|
|
|
|
},
|
2020-05-27 15:43:43 +00:00
|
|
|
providers::{networks::Any, HttpProvider},
|
2020-05-24 15:55:46 +00:00
|
|
|
};
|
2020-05-22 18:37:21 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
#[tokio::main]
|
2020-05-26 11:00:56 +00:00
|
|
|
async fn main() -> Result<()> {
|
2020-05-30 14:11:51 +00:00
|
|
|
let port = 8546u64;
|
|
|
|
let url = format!("http://localhost:{}", port).to_string();
|
|
|
|
let _ganache = GanacheBuilder::new().port(port).spawn();
|
|
|
|
|
2020-05-24 16:14:27 +00:00
|
|
|
// connect to the network
|
2020-05-30 14:11:51 +00:00
|
|
|
let provider = HttpProvider::<Any>::try_from(url.as_str())?;
|
2020-05-24 17:26:41 +00:00
|
|
|
let accounts = provider.get_accounts().await?;
|
|
|
|
let from = accounts[0];
|
2020-05-22 18:37:21 +00:00
|
|
|
|
2020-05-24 16:14:27 +00:00
|
|
|
// craft the tx
|
2020-05-24 18:34:56 +00:00
|
|
|
let tx = TransactionRequest::new()
|
|
|
|
.send_to_str("9A7e5d4bcA656182e66e33340d776D1542143006")?
|
|
|
|
.value(1000)
|
|
|
|
.from(from); // specify the `from` field so that the client knows which account to use
|
2020-05-24 16:14:27 +00:00
|
|
|
|
2020-05-24 17:26:41 +00:00
|
|
|
let balance_before = provider.get_balance(from, None).await?;
|
|
|
|
|
2020-05-24 16:14:27 +00:00
|
|
|
// 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);
|
|
|
|
|
2020-05-24 17:26:41 +00:00
|
|
|
let balance_after = provider.get_balance(from, None).await?;
|
|
|
|
assert!(balance_after < balance_before);
|
|
|
|
|
|
|
|
println!("Balance before {}", balance_before);
|
|
|
|
println!("Balance after {}", balance_after);
|
|
|
|
|
2020-05-23 00:01:20 +00:00
|
|
|
Ok(())
|
2020-05-22 18:37:21 +00:00
|
|
|
}
|