forked from matter-labs/zksync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Movchan
committed
Aug 28, 2020
1 parent
37468a2
commit 97ddd97
Showing
7 changed files
with
256 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
use crate::fee_ticker::ticker_api::TokenPriceAPI; | ||
use async_trait::async_trait; | ||
use chrono::{DateTime, NaiveDateTime, Utc}; | ||
use failure::Error; | ||
use models::node::TokenPrice; | ||
use models::primitives::UnsignedRatioSerializeAsDecimal; | ||
use num::rational::Ratio; | ||
use num::BigUint; | ||
use reqwest::Url; | ||
use std::collections::HashMap; | ||
use std::str::FromStr; | ||
use std::time::Duration; | ||
|
||
/// The limit of time we are willing to wait for response. | ||
const REQUEST_TIMEOUT: Duration = Duration::from_millis(5000); | ||
|
||
const COINGECKO_API_BASE_URL: &str = "https://api.coingecko.com/api/v3/"; | ||
|
||
pub struct CoinGeckoAPI { | ||
client: reqwest::Client, | ||
token_ids: HashMap<String, String>, | ||
} | ||
|
||
impl CoinGeckoAPI { | ||
pub fn new(client: reqwest::Client) -> Result<Self, Error> { | ||
let token_list_url = Url::from_str(COINGECKO_API_BASE_URL) | ||
.unwrap() | ||
.join("coins/list") | ||
.expect("failed to join URL path"); | ||
|
||
let token_list = reqwest::blocking::get(token_list_url) | ||
.map_err(|err| failure::format_err!("CoinGecko API request failed: {}", err))? | ||
.json::<CoinGeckoTokenList>()?; | ||
|
||
let mut token_ids = HashMap::new(); | ||
for token in token_list.0 { | ||
token_ids.insert(token.symbol, token.id); | ||
} | ||
|
||
Ok(Self { client, token_ids }) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl TokenPriceAPI for CoinGeckoAPI { | ||
async fn get_price(&self, token_symbol: &str) -> Result<TokenPrice, Error> { | ||
let token_id = self | ||
.token_ids | ||
.get(&token_symbol.to_lowercase()) | ||
.ok_or_else(|| { | ||
failure::format_err!("Token '{}' is not listed on CoinGecko", token_symbol) | ||
})?; | ||
|
||
let market_chart_url = Url::from_str(COINGECKO_API_BASE_URL) | ||
.unwrap() | ||
.join(format!("coins/{}/market_chart", token_id).as_str()) | ||
.expect("failed to join URL path"); | ||
|
||
let request = self | ||
.client | ||
.get(market_chart_url) | ||
.query(&[("vs_currency", "usd"), ("days", "1")]); | ||
|
||
let api_request_future = tokio::time::timeout(REQUEST_TIMEOUT, request.send()); | ||
|
||
let market_chart = api_request_future | ||
.await | ||
.map_err(|_| failure::format_err!("CoinGecko API request timeout"))? | ||
.map_err(|err| failure::format_err!("CoinGecko API request failed: {}", err))? | ||
.json::<CoinGeckoMarketChart>() | ||
.await?; | ||
|
||
let last_updated_timestamp_ms = market_chart | ||
.prices | ||
.last() | ||
.ok_or_else(|| failure::format_err!("CoinGecko returned empty price data"))? | ||
.0; | ||
|
||
let usd_price = market_chart | ||
.prices | ||
.into_iter() | ||
.map(|token_price| token_price.1) | ||
.min() | ||
.ok_or_else(|| failure::format_err!("CoinGecko returned empty price data"))?; | ||
|
||
let naive_last_updated = NaiveDateTime::from_timestamp( | ||
last_updated_timestamp_ms / 1_000, // ms to s | ||
(last_updated_timestamp_ms % 1_000) as u32 * 1_000_000, // ms to ns | ||
); | ||
let last_updated = DateTime::<Utc>::from_utc(naive_last_updated, Utc); | ||
|
||
Ok(TokenPrice { | ||
usd_price, | ||
last_updated, | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
struct CoinGeckoTokenInfo { | ||
id: String, | ||
symbol: String, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
struct CoinGeckoTokenList(Vec<CoinGeckoTokenInfo>); | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
struct CoinGeckoTokenPrice( | ||
pub i64, // timestamp (milliseconds) | ||
#[serde(with = "UnsignedRatioSerializeAsDecimal")] pub Ratio<BigUint>, // price | ||
); | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
struct CoinGeckoMarketChart { | ||
prices: Vec<CoinGeckoTokenPrice>, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_coingecko_api() { | ||
let mut runtime = tokio::runtime::Builder::new() | ||
.basic_scheduler() | ||
.enable_all() | ||
.build() | ||
.expect("tokio runtime"); | ||
let client = reqwest::Client::new(); | ||
let api = CoinGeckoAPI::new(client).expect("coingecko init"); | ||
runtime | ||
.block_on(api.get_price("ETH")) | ||
.expect("Failed to get data from ticker"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.