2020-06-21 07:17:11 +00:00
|
|
|
#![allow(unused_braces)]
|
2020-08-18 18:47:56 +00:00
|
|
|
use ethers::providers::{
|
|
|
|
gas_oracle::{EthGasStation, Etherchain, Etherscan, GasCategory, GasNow, GasOracle},
|
|
|
|
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-09-17 11:06:56 +00:00
|
|
|
types::{BlockId, TransactionRequest, H256},
|
2020-06-17 09:22:01 +00:00
|
|
|
utils::{parse_ether, Ganache},
|
|
|
|
};
|
2020-06-21 07:17:11 +00:00
|
|
|
|
2020-09-17 11:06:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn non_existing_data_works() {
|
|
|
|
let provider = Provider::<Http>::try_from(
|
|
|
|
"https://rinkeby.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(provider
|
|
|
|
.get_transaction(H256::zero())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
|
|
|
assert!(provider
|
|
|
|
.get_transaction_receipt(H256::zero())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
|
|
|
assert!(provider
|
|
|
|
.get_block(BlockId::Hash(H256::zero()))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
|
|
|
assert!(provider
|
|
|
|
.get_block_with_txs(BlockId::Hash(H256::zero()))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-18 18:47:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn gas_oracle() {
|
|
|
|
// initialize and fetch gas estimates from EthGasStation
|
|
|
|
let eth_gas_station_oracle = EthGasStation::new(None);
|
|
|
|
let data_1 = eth_gas_station_oracle.fetch().await;
|
|
|
|
assert!(data_1.is_ok());
|
|
|
|
|
2020-09-17 11:06:56 +00:00
|
|
|
let api_key = std::env::var("ETHERSCAN_API_KEY").unwrap();
|
|
|
|
let api_key = Some(api_key.as_str());
|
|
|
|
|
2020-08-18 18:47:56 +00:00
|
|
|
// initialize and fetch gas estimates from Etherscan
|
|
|
|
// since etherscan does not support `fastest` category, we expect an error
|
2020-09-17 11:06:56 +00:00
|
|
|
let etherscan_oracle = Etherscan::new(api_key).category(GasCategory::Fastest);
|
2020-08-18 18:47:56 +00:00
|
|
|
let data_2 = etherscan_oracle.fetch().await;
|
|
|
|
assert!(data_2.is_err());
|
|
|
|
|
|
|
|
// but fetching the `standard` gas price should work fine
|
2020-09-17 11:06:56 +00:00
|
|
|
let etherscan_oracle_2 = Etherscan::new(api_key).category(GasCategory::SafeLow);
|
2020-08-18 18:47:56 +00:00
|
|
|
|
|
|
|
let data_3 = etherscan_oracle_2.fetch().await;
|
|
|
|
assert!(data_3.is_ok());
|
|
|
|
|
|
|
|
// initialize and fetch gas estimates from Etherchain
|
|
|
|
let etherchain_oracle = Etherchain::new().category(GasCategory::Fast);
|
|
|
|
let data_4 = etherchain_oracle.fetch().await;
|
|
|
|
assert!(data_4.is_ok());
|
|
|
|
|
2020-09-17 11:06:56 +00:00
|
|
|
// initialize and fetch gas estimates from SparkPool
|
|
|
|
let gas_now_oracle = GasNow::new().category(GasCategory::Fastest);
|
|
|
|
let data_5 = gas_now_oracle.fetch().await;
|
|
|
|
assert!(data_5.is_ok());
|
2020-08-18 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 08:09:19 +00:00
|
|
|
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();
|
|
|
|
|
2020-08-12 08:35:33 +00:00
|
|
|
let tx_hash = "c8496681d0ade783322980cce00c89419fce4b484635d9e09c79787a0f75d450"
|
2020-06-17 09:22:01 +00:00
|
|
|
.parse::<H256>()
|
|
|
|
.unwrap();
|
2020-09-17 11:06:56 +00:00
|
|
|
let tx = provider.get_transaction(tx_hash).await.unwrap().unwrap();
|
2020-06-17 09:22:01 +00:00
|
|
|
assert!(tx.gateway_fee_recipient.is_none());
|
|
|
|
assert_eq!(tx.gateway_fee.unwrap(), 0.into());
|
|
|
|
assert_eq!(tx.hash, tx_hash);
|
2020-08-12 08:35:33 +00:00
|
|
|
assert_eq!(tx.block_number.unwrap(), 447181.into())
|
2020-06-17 09:22:01 +00:00
|
|
|
}
|
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();
|
|
|
|
|
2020-09-17 11:06:56 +00:00
|
|
|
let block = provider.get_block(447254).await.unwrap().unwrap();
|
2020-07-02 13:05:27 +00:00
|
|
|
assert_eq!(
|
|
|
|
block.randomness,
|
|
|
|
Randomness {
|
2020-08-12 08:35:33 +00:00
|
|
|
committed: "003e12deb86292844274493e9ab6e57ed1e276202c16799d97af723eb0d3253f"
|
2020-07-02 13:05:27 +00:00
|
|
|
.from_hex::<Vec<u8>>()
|
|
|
|
.unwrap()
|
|
|
|
.into(),
|
2020-08-12 08:35:33 +00:00
|
|
|
revealed: "1333b3b45e0385da48a01b4459aeda7607867ef6a41167cfdeefa49b9fdce6d7"
|
2020-07-02 13:05:27 +00:00
|
|
|
.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
|
|
|
}
|