2020-09-24 21:33:09 +00:00
|
|
|
use ethers_providers::{Http, Middleware, Provider};
|
|
|
|
|
|
|
|
use ethers_core::types::TransactionRequest;
|
2020-10-08 15:56:36 +00:00
|
|
|
use ethers_middleware::signer::SignerMiddleware;
|
2020-10-02 08:41:16 +00:00
|
|
|
use ethers_signers::LocalWallet;
|
2020-09-24 21:33:09 +00:00
|
|
|
use std::{convert::TryFrom, time::Duration};
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(not(feature = "celo"))]
|
|
|
|
async fn send_eth() {
|
|
|
|
use ethers_core::utils::Ganache;
|
|
|
|
|
|
|
|
let ganache = Ganache::new().spawn();
|
|
|
|
|
|
|
|
// this private key belongs to the above mnemonic
|
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-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
// connect to the network
|
|
|
|
let provider = Provider::<Http>::try_from(ganache.endpoint())
|
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(10u64));
|
2020-10-08 15:56:36 +00:00
|
|
|
let provider = SignerMiddleware::new(provider, wallet);
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
// craft the transaction
|
|
|
|
let tx = TransactionRequest::new().to(wallet2.address()).value(10000);
|
|
|
|
|
|
|
|
let balance_before = provider
|
|
|
|
.get_balance(provider.address(), None)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// send it!
|
|
|
|
provider.send_transaction(tx, None).await.unwrap();
|
|
|
|
|
|
|
|
let balance_after = provider
|
|
|
|
.get_balance(provider.address(), None)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(balance_before > balance_after);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "celo")]
|
|
|
|
async fn test_send_transaction() {
|
|
|
|
// Celo testnet
|
|
|
|
let provider = Provider::<Http>::try_from("https://alfajores-forno.celo-testnet.org")
|
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(3000u64));
|
|
|
|
|
|
|
|
// Funded with https://celo.org/developers/faucet
|
|
|
|
// Please do not drain this account :)
|
|
|
|
let wallet = "d652abb81e8c686edba621a895531b1f291289b63b5ef09a94f686a5ecdd5db1"
|
2020-10-02 08:41:16 +00:00
|
|
|
.parse::<LocalWallet>()
|
2020-09-24 21:33:09 +00:00
|
|
|
.unwrap();
|
2020-10-08 15:56:36 +00:00
|
|
|
let client = SignerMiddleware::new(provider, wallet);
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
let balance_before = client.get_balance(client.address(), None).await.unwrap();
|
|
|
|
let tx = TransactionRequest::pay(client.address(), 100);
|
|
|
|
let tx_hash = client.send_transaction(tx, None).await.unwrap();
|
|
|
|
let _receipt = client
|
|
|
|
.pending_transaction(tx_hash)
|
|
|
|
.confirmations(3)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let balance_after = client.get_balance(client.address(), None).await.unwrap();
|
|
|
|
assert!(balance_before > balance_after);
|
|
|
|
}
|