2020-06-17 09:22:01 +00:00
|
|
|
use ethers::{
|
|
|
|
providers::{Http, Provider},
|
|
|
|
signers::Wallet,
|
|
|
|
types::TransactionRequest,
|
|
|
|
};
|
2020-06-22 08:44:08 +00:00
|
|
|
use std::{convert::TryFrom, time::Duration};
|
2020-06-17 09:22:01 +00:00
|
|
|
|
|
|
|
#[cfg(not(feature = "celo"))]
|
|
|
|
mod eth_tests {
|
|
|
|
use super::*;
|
2020-06-17 13:36:09 +00:00
|
|
|
use ethers::{
|
|
|
|
types::BlockNumber,
|
|
|
|
utils::{parse_ether, Ganache},
|
|
|
|
};
|
2020-06-17 09:22:01 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn pending_txs_with_confirmations_rinkeby_infura() {
|
|
|
|
let provider = Provider::<Http>::try_from(
|
|
|
|
"https://rinkeby.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27",
|
|
|
|
)
|
2020-06-22 08:44:08 +00:00
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(2000u64));
|
2020-06-17 09:22:01 +00:00
|
|
|
|
|
|
|
// pls do not drain this key :)
|
|
|
|
// note: this works even if there's no EIP-155 configured!
|
|
|
|
let client = "FF7F80C6E9941865266ED1F481263D780169F1D98269C51167D20C630A5FDC8A"
|
|
|
|
.parse::<Wallet>()
|
|
|
|
.unwrap()
|
|
|
|
.connect(provider);
|
|
|
|
|
|
|
|
let tx = TransactionRequest::pay(client.address(), parse_ether(1u64).unwrap());
|
2020-06-22 08:44:08 +00:00
|
|
|
let tx_hash = client
|
2020-06-17 13:36:09 +00:00
|
|
|
.send_transaction(tx, Some(BlockNumber::Pending))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-06-22 08:44:08 +00:00
|
|
|
let receipt = client
|
|
|
|
.pending_transaction(tx_hash)
|
|
|
|
.confirmations(3)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-06-17 09:22:01 +00:00
|
|
|
|
|
|
|
// got the correct receipt
|
2020-06-22 08:44:08 +00:00
|
|
|
assert_eq!(receipt.transaction_hash, tx_hash);
|
2020-06-17 09:22:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn send_eth() {
|
2020-06-22 13:42:34 +00:00
|
|
|
let ganache = Ganache::new().spawn();
|
2020-06-17 09:22:01 +00:00
|
|
|
|
|
|
|
// this private key belongs to the above mnemonic
|
2020-06-22 13:42:34 +00:00
|
|
|
let wallet: Wallet = ganache.keys()[0].clone().into();
|
|
|
|
let wallet2: Wallet = ganache.keys()[1].clone().into();
|
2020-06-17 09:22:01 +00:00
|
|
|
|
|
|
|
// connect to the network
|
2020-06-22 13:42:34 +00:00
|
|
|
let provider = Provider::<Http>::try_from(ganache.endpoint())
|
2020-06-22 08:44:08 +00:00
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(10u64));
|
2020-06-17 09:22:01 +00:00
|
|
|
|
|
|
|
// connect the wallet to the provider
|
|
|
|
let client = wallet.connect(provider);
|
|
|
|
|
|
|
|
// craft the transaction
|
2020-06-22 13:42:34 +00:00
|
|
|
let tx = TransactionRequest::new().to(wallet2.address()).value(10000);
|
2020-06-17 09:22:01 +00:00
|
|
|
|
|
|
|
let balance_before = client.get_balance(client.address(), None).await.unwrap();
|
|
|
|
|
|
|
|
// send it!
|
|
|
|
client.send_transaction(tx, None).await.unwrap();
|
|
|
|
|
|
|
|
let balance_after = client.get_balance(client.address(), None).await.unwrap();
|
|
|
|
|
|
|
|
assert!(balance_before > balance_after);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "celo")]
|
|
|
|
mod celo_tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_send_transaction() {
|
|
|
|
// Celo testnet
|
2020-06-22 08:44:08 +00:00
|
|
|
let provider = Provider::<Http>::try_from("https://alfajores-forno.celo-testnet.org")
|
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(3000u64));
|
2020-06-17 09:22:01 +00:00
|
|
|
|
|
|
|
// Funded with https://celo.org/developers/faucet
|
|
|
|
// Please do not drain this account :)
|
|
|
|
let client = "d652abb81e8c686edba621a895531b1f291289b63b5ef09a94f686a5ecdd5db1"
|
|
|
|
.parse::<Wallet>()
|
|
|
|
.unwrap()
|
|
|
|
.connect(provider);
|
|
|
|
|
|
|
|
let balance_before = client.get_balance(client.address(), None).await.unwrap();
|
|
|
|
let tx = TransactionRequest::pay(client.address(), 100);
|
2020-06-22 08:44:08 +00:00
|
|
|
let tx_hash = client.send_transaction(tx, None).await.unwrap();
|
|
|
|
let _receipt = client
|
|
|
|
.pending_transaction(tx_hash)
|
|
|
|
.confirmations(3)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-06-17 09:22:01 +00:00
|
|
|
let balance_after = client.get_balance(client.address(), None).await.unwrap();
|
|
|
|
assert!(balance_before > balance_after);
|
|
|
|
}
|
|
|
|
}
|