2020-06-21 07:17:11 +00:00
|
|
|
#![allow(unused_braces)]
|
2020-06-17 09:22:01 +00:00
|
|
|
use ethers::providers::{Http, Provider};
|
2020-06-22 08:44:08 +00:00
|
|
|
use std::{convert::TryFrom, time::Duration};
|
2020-06-17 08:02:03 +00:00
|
|
|
|
2020-06-17 09:22:01 +00:00
|
|
|
#[cfg(not(feature = "celo"))]
|
2020-06-21 07:17:11 +00:00
|
|
|
mod eth_tests {
|
|
|
|
use super::*;
|
2020-06-17 09:22:01 +00:00
|
|
|
use ethers::{
|
2020-06-21 08:09:19 +00:00
|
|
|
providers::JsonRpcClient,
|
2020-06-17 09:22:01 +00:00
|
|
|
types::TransactionRequest,
|
|
|
|
utils::{parse_ether, Ganache},
|
|
|
|
};
|
2020-06-21 07:17:11 +00:00
|
|
|
|
|
|
|
// Without TLS this would error with "TLS Support not compiled in"
|
|
|
|
#[test]
|
|
|
|
#[cfg(any(feature = "async-std-tls", feature = "tokio-tls"))]
|
|
|
|
fn ssl_websocket() {
|
|
|
|
// this is extremely ugly but I couldn't figure out a better way of having
|
|
|
|
// a shared async test for both runtimes
|
|
|
|
#[cfg(feature = "async-std-tls")]
|
|
|
|
let block_on = async_std::task::block_on;
|
|
|
|
#[cfg(feature = "tokio-tls")]
|
|
|
|
let mut runtime = tokio::runtime::Runtime::new().unwrap();
|
|
|
|
#[cfg(feature = "tokio-tls")]
|
|
|
|
let mut block_on = |x| runtime.block_on(x);
|
|
|
|
|
|
|
|
use ethers::providers::Ws;
|
|
|
|
block_on(async move {
|
|
|
|
let ws = Ws::connect("wss://rinkeby.infura.io/ws/v3/c60b0bb42f8a4c6481ecd229eddaca27")
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let provider = Provider::new(ws);
|
|
|
|
let _number = provider.get_block_number().await.unwrap();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(feature = "tokio-runtime")]
|
|
|
|
async fn watch_blocks_websocket() {
|
|
|
|
use ethers::{
|
|
|
|
providers::{FilterStream, StreamExt, Ws},
|
|
|
|
types::H256,
|
|
|
|
};
|
2020-06-17 09:22:01 +00:00
|
|
|
|
2020-06-22 13:42:34 +00:00
|
|
|
let ganache = Ganache::new().block_time(2u64).spawn();
|
|
|
|
let (ws, _) = async_tungstenite::tokio::connect_async(ganache.ws_endpoint())
|
2020-06-21 07:17:11 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-06-22 08:44:08 +00:00
|
|
|
let provider = Provider::new(Ws::new(ws)).interval(Duration::from_millis(500u64));
|
2020-06-21 07:17:11 +00:00
|
|
|
|
2020-06-22 08:44:08 +00:00
|
|
|
let stream = provider.watch_blocks().await.unwrap().stream();
|
2020-06-21 07:17:11 +00:00
|
|
|
|
|
|
|
let _blocks = stream.take(3usize).collect::<Vec<H256>>().await;
|
|
|
|
let _number = provider.get_block_number().await.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn pending_txs_with_confirmations_ganache() {
|
2020-06-22 13:42:34 +00:00
|
|
|
let ganache = Ganache::new().block_time(2u64).spawn();
|
|
|
|
let provider = Provider::<Http>::try_from(ganache.endpoint())
|
2020-06-22 08:44:08 +00:00
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(500u64));
|
2020-06-21 08:09:19 +00:00
|
|
|
generic_pending_txs_test(provider).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(any(feature = "tokio-runtime", feature = "tokio-tls"))]
|
|
|
|
async fn websocket_pending_txs_with_confirmations_ganache() {
|
|
|
|
use ethers::providers::Ws;
|
2020-06-22 13:42:34 +00:00
|
|
|
let ganache = Ganache::new().block_time(2u64).spawn();
|
|
|
|
let ws = Ws::connect(ganache.ws_endpoint()).await.unwrap();
|
2020-06-21 08:09:19 +00:00
|
|
|
let provider = Provider::new(ws);
|
|
|
|
generic_pending_txs_test(provider).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn generic_pending_txs_test<P: JsonRpcClient>(provider: Provider<P>) {
|
2020-06-21 07:17:11 +00:00
|
|
|
let accounts = provider.get_accounts().await.unwrap();
|
2020-06-17 08:02:03 +00:00
|
|
|
|
2020-06-21 08:09:19 +00:00
|
|
|
let tx = TransactionRequest::pay(accounts[0], parse_ether(1u64).unwrap()).from(accounts[0]);
|
2020-06-22 08:44:08 +00:00
|
|
|
let tx_hash = provider.send_transaction(tx).await.unwrap();
|
|
|
|
let pending_tx = provider.pending_transaction(tx_hash);
|
|
|
|
let receipt = pending_tx.confirmations(5).await.unwrap();
|
2020-06-17 08:02:03 +00:00
|
|
|
|
2020-06-21 07:17:11 +00:00
|
|
|
// got the correct receipt
|
2020-06-22 08:44:08 +00:00
|
|
|
assert_eq!(receipt.transaction_hash, tx_hash);
|
2020-06-21 07:17:11 +00:00
|
|
|
}
|
2020-06-17 08:02:03 +00:00
|
|
|
}
|
2020-06-17 09:22:01 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "celo")]
|
|
|
|
mod celo_tests {
|
|
|
|
use super::*;
|
2020-07-02 13:05:27 +00:00
|
|
|
use ethers::{
|
|
|
|
providers::FilterStream,
|
|
|
|
types::{Randomness, H256},
|
|
|
|
};
|
2020-06-17 13:09:41 +00:00
|
|
|
use futures_util::stream::StreamExt;
|
2020-07-02 13:05:27 +00:00
|
|
|
use rustc_hex::FromHex;
|
2020-06-17 09:22:01 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
// https://alfajores-blockscout.celo-testnet.org/tx/0x544ea96cddb16aeeaedaf90885c1e02be4905f3eb43d6db3f28cac4dbe76a625/internal_transactions
|
|
|
|
async fn get_transaction() {
|
|
|
|
let provider =
|
|
|
|
Provider::<Http>::try_from("https://alfajores-forno.celo-testnet.org").unwrap();
|
|
|
|
|
|
|
|
let tx_hash = "544ea96cddb16aeeaedaf90885c1e02be4905f3eb43d6db3f28cac4dbe76a625"
|
|
|
|
.parse::<H256>()
|
|
|
|
.unwrap();
|
|
|
|
let tx = provider.get_transaction(tx_hash).await.unwrap();
|
|
|
|
assert!(tx.gateway_fee_recipient.is_none());
|
|
|
|
assert_eq!(tx.gateway_fee.unwrap(), 0.into());
|
|
|
|
assert_eq!(tx.hash, tx_hash);
|
|
|
|
assert_eq!(tx.block_number.unwrap(), 1100845.into())
|
|
|
|
}
|
2020-06-17 13:09:41 +00:00
|
|
|
|
2020-07-02 13:05:27 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn get_block() {
|
|
|
|
let provider =
|
|
|
|
Provider::<Http>::try_from("https://alfajores-forno.celo-testnet.org").unwrap();
|
|
|
|
|
|
|
|
let block = provider.get_block(1342561).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
block.randomness,
|
|
|
|
Randomness {
|
|
|
|
committed: "a3a64b7a29bb4ddd49b7d9e3cf3dd14ecbb7f0321061706c634d14b15425dd30"
|
|
|
|
.from_hex::<Vec<u8>>()
|
|
|
|
.unwrap()
|
|
|
|
.into(),
|
|
|
|
revealed: "3c5f2f71941783cbe7f2dbd387c35503ca0470b300e1613866b988a1db8902a3"
|
|
|
|
.from_hex::<Vec<u8>>()
|
|
|
|
.unwrap()
|
|
|
|
.into(),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-17 13:09:41 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn watch_blocks() {
|
2020-06-22 08:44:08 +00:00
|
|
|
let provider = Provider::<Http>::try_from("https://alfajores-forno.celo-testnet.org")
|
2020-06-17 13:09:41 +00:00
|
|
|
.unwrap()
|
2020-06-22 08:44:08 +00:00
|
|
|
.interval(Duration::from_millis(2000u64));
|
|
|
|
|
|
|
|
let stream = provider.watch_blocks().await.unwrap().stream();
|
2020-06-17 13:09:41 +00:00
|
|
|
|
|
|
|
let _blocks = stream.take(3usize).collect::<Vec<H256>>().await;
|
|
|
|
}
|
2020-06-17 09:22:01 +00:00
|
|
|
}
|