2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-07-16 18:22:45 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-07-16 18:22:45 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-07-16 18:22:45 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-07-16 18:22:45 +02:00
|
|
|
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
|
|
|
|
//! A simple client to get the current ETH price using an external API.
|
|
|
|
|
2019-06-27 11:29:24 +02:00
|
|
|
use std::{cmp, fmt, io, str};
|
2017-08-04 13:06:01 +02:00
|
|
|
use fetch::{Client as FetchClient, Fetch};
|
2018-03-14 13:40:54 +01:00
|
|
|
use futures::{Future, Stream};
|
2019-06-27 11:29:24 +02:00
|
|
|
use log::warn;
|
2018-10-22 09:40:50 +02:00
|
|
|
use parity_runtime::Executor;
|
2019-06-27 11:29:24 +02:00
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
pub use fetch;
|
2017-08-04 13:06:01 +02:00
|
|
|
|
|
|
|
/// Current ETH price information.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct PriceInfo {
|
|
|
|
/// Current ETH price in USD.
|
|
|
|
pub ethusd: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A client to get the current ETH price using an external API.
|
|
|
|
pub struct Client<F = FetchClient> {
|
2018-10-22 09:40:50 +02:00
|
|
|
pool: Executor,
|
2017-08-04 13:06:01 +02:00
|
|
|
api_endpoint: String,
|
|
|
|
fetch: F,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> fmt::Debug for Client<F> {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt.debug_struct("price_info::Client")
|
|
|
|
.field("api_endpoint", &self.api_endpoint)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> cmp::PartialEq for Client<F> {
|
|
|
|
fn eq(&self, other: &Client<F>) -> bool {
|
|
|
|
self.api_endpoint == other.api_endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: Fetch> Client<F> {
|
|
|
|
/// Creates a new instance of the `Client` given a `fetch::Client`.
|
2018-10-22 09:40:50 +02:00
|
|
|
pub fn new(fetch: F, pool: Executor) -> Client<F> {
|
2018-01-09 12:17:01 +01:00
|
|
|
let api_endpoint = "https://api.etherscan.io/api?module=stats&action=ethprice".to_owned();
|
2018-03-14 13:40:54 +01:00
|
|
|
Client { pool, api_endpoint, fetch }
|
2017-08-04 13:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the current ETH price and calls `set_price` with the result.
|
2018-04-13 17:34:27 +02:00
|
|
|
pub fn get<G: FnOnce(PriceInfo) + Sync + Send + 'static>(&self, set_price: G) {
|
2018-04-09 16:14:33 +02:00
|
|
|
let future = self.fetch.get(&self.api_endpoint, fetch::Abort::default())
|
2019-06-27 11:29:24 +02:00
|
|
|
.and_then(|response| response.concat2())
|
2018-10-22 09:40:50 +02:00
|
|
|
.and_then(move |body| {
|
2018-03-14 13:40:54 +01:00
|
|
|
let body_str = str::from_utf8(&body).ok();
|
|
|
|
let value: Option<Value> = body_str.and_then(|s| serde_json::from_str(s).ok());
|
2017-08-04 13:06:01 +02:00
|
|
|
|
2017-08-04 13:39:57 +02:00
|
|
|
let ethusd = value
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|value| value.pointer("/result/ethusd"))
|
|
|
|
.and_then(|obj| obj.as_str())
|
|
|
|
.and_then(|s| s.parse().ok());
|
|
|
|
|
|
|
|
match ethusd {
|
|
|
|
Some(ethusd) => {
|
|
|
|
set_price(PriceInfo { ethusd });
|
|
|
|
Ok(())
|
|
|
|
},
|
2019-06-27 11:29:24 +02:00
|
|
|
None => {
|
|
|
|
let msg = format!("Unexpected response: {}", body_str.unwrap_or_default());
|
|
|
|
let err = io::Error::new(io::ErrorKind::Other, msg);
|
|
|
|
Err(fetch::Error::Io(err))
|
|
|
|
}
|
2017-08-04 13:39:57 +02:00
|
|
|
}
|
2017-08-04 13:06:01 +02:00
|
|
|
})
|
|
|
|
.map_err(|err| {
|
|
|
|
warn!("Failed to auto-update latest ETH price: {:?}", err);
|
2018-03-14 13:40:54 +01:00
|
|
|
});
|
2018-10-22 09:40:50 +02:00
|
|
|
self.pool.spawn(future)
|
2017-08-04 13:06:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2019-06-27 11:29:24 +02:00
|
|
|
use std::sync::{
|
|
|
|
Arc, atomic::{AtomicBool, Ordering}
|
|
|
|
};
|
2018-04-11 11:59:04 +02:00
|
|
|
use fake_fetch::FakeFetch;
|
2019-06-27 11:29:24 +02:00
|
|
|
use parity_runtime::{Runtime, Executor};
|
|
|
|
use super::Client;
|
2017-08-04 13:06:01 +02:00
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
fn price_info_ok(response: &str, executor: Executor) -> Client<FakeFetch<String>> {
|
|
|
|
Client::new(FakeFetch::new(Some(response.to_owned())), executor)
|
2017-08-04 13:06:01 +02:00
|
|
|
}
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
fn price_info_not_found(executor: Executor) -> Client<FakeFetch<String>> {
|
|
|
|
Client::new(FakeFetch::new(None::<String>), executor)
|
2017-08-04 13:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_get_price_info() {
|
2018-10-22 09:40:50 +02:00
|
|
|
let runtime = Runtime::with_thread_count(1);
|
|
|
|
|
2017-08-04 13:06:01 +02:00
|
|
|
// given
|
|
|
|
let response = r#"{
|
|
|
|
"status": "1",
|
|
|
|
"message": "OK",
|
|
|
|
"result": {
|
|
|
|
"ethbtc": "0.0891",
|
|
|
|
"ethbtc_timestamp": "1499894236",
|
|
|
|
"ethusd": "209.55",
|
|
|
|
"ethusd_timestamp": "1499894229"
|
|
|
|
}
|
|
|
|
}"#;
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
let price_info = price_info_ok(response, runtime.executor());
|
2017-08-04 13:06:01 +02:00
|
|
|
|
|
|
|
// when
|
|
|
|
price_info.get(|price| {
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert_eq!(price.ethusd, 209.55);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_call_set_price_if_response_is_malformed() {
|
2018-10-22 09:40:50 +02:00
|
|
|
let runtime = Runtime::with_thread_count(1);
|
|
|
|
|
2017-08-04 13:06:01 +02:00
|
|
|
// given
|
|
|
|
let response = "{}";
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
let price_info = price_info_ok(response, runtime.executor());
|
2017-08-04 13:06:01 +02:00
|
|
|
let b = Arc::new(AtomicBool::new(false));
|
|
|
|
|
|
|
|
// when
|
|
|
|
let bb = b.clone();
|
|
|
|
price_info.get(move |_| {
|
|
|
|
bb.store(true, Ordering::Relaxed);
|
|
|
|
});
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert_eq!(b.load(Ordering::Relaxed), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_call_set_price_if_response_is_invalid() {
|
2018-10-22 09:40:50 +02:00
|
|
|
let runtime = Runtime::with_thread_count(1);
|
|
|
|
|
2017-08-04 13:06:01 +02:00
|
|
|
// given
|
2018-10-22 09:40:50 +02:00
|
|
|
let price_info = price_info_not_found(runtime.executor());
|
2017-08-04 13:06:01 +02:00
|
|
|
let b = Arc::new(AtomicBool::new(false));
|
|
|
|
|
|
|
|
// when
|
|
|
|
let bb = b.clone();
|
|
|
|
price_info.get(move |_| {
|
|
|
|
bb.store(true, Ordering::Relaxed);
|
|
|
|
});
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert_eq!(b.load(Ordering::Relaxed), false);
|
|
|
|
}
|
|
|
|
}
|