Skip to content

Commit

Permalink
fix: fix coinmarketcap client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antomor committed Oct 18, 2023
1 parent 461df4a commit fa9ef7b
Showing 1 changed file with 55 additions and 7 deletions.
62 changes: 55 additions & 7 deletions core/bin/zksync_api/src/bin/providers/dev_price_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use actix_web::{web, HttpRequest, HttpResponse, Result};
use bigdecimal::BigDecimal;
use chrono::Utc;
use chrono::{Utc, SecondsFormat};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{collections::HashMap, fs::read_to_string, path::Path};
Expand Down Expand Up @@ -50,6 +50,46 @@ macro_rules! make_sloppy {
}};
}

async fn handle_coinmarketcap_token_price_query(
query: web::Query<CoinMarketCapTokenQuery>,
_data: web::Data<Vec<TokenData>>,
) -> Result<HttpResponse> {
let symbol = query.symbol.clone();
let base_price = match symbol.as_str() {
"RBTC" => BigDecimal::from(1800),
"wBTC" => BigDecimal::from(9000),
// Even though these tokens have their base price equal to
// the default one, we still keep them here so that in the future it would
// be easier to change the default price without affecting the important tokens
"DAI" => BigDecimal::from(1),
"tGLM" => BigDecimal::from(1),
"GLM" => BigDecimal::from(1),

"RIF" => BigDecimal::try_from(0.053533).unwrap(),
_ => BigDecimal::from(1),
};
let random_multiplier = thread_rng().gen_range(0.9, 1.1);

let price = base_price * BigDecimal::try_from(random_multiplier).unwrap();

let last_updated = Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true);
let resp = json!({
"data": {
symbol: {
"quote": {
"USD": {
"price": price.to_string(),
"last_updated": last_updated
}
}
}
}
});
vlog::info!("1.0 {} = {} USD", query.symbol, price);
Ok(HttpResponse::Ok().json(resp))
}


#[derive(Debug, Deserialize)]
struct Token {
pub address: Address,
Expand Down Expand Up @@ -140,22 +180,30 @@ pub fn create_price_service(sloppy_mode: bool) -> actix_web::Scope {
.chain(testnet_tokens.into_iter())
.collect();
if sloppy_mode {
web::scope(API_PATH)
web::scope("")
.app_data(web::Data::new(data))
.route(
"/coins/list",
"/cryptocurrency/quotes/latest",
web::get().to(make_sloppy!(handle_coinmarketcap_token_price_query)),
)
.route(
format!("{}/coins/list", API_PATH).as_str(),
web::get().to(make_sloppy!(handle_coingecko_token_list)),
)
.route(
"/coins/{coin_id}/market_chart",
format!("{}/coins/{{coin_id}}/market_chart", API_PATH).as_str(),
web::get().to(make_sloppy!(handle_coingecko_token_price_query)),
)
} else {
web::scope(API_PATH)
web::scope("")
.app_data(web::Data::new(data))
.route("/coins/list", web::get().to(handle_coingecko_token_list))
.route(
"/coins/{coin_id}/market_chart",
"/cryptocurrency/quotes/latest",
web::get().to(handle_coinmarketcap_token_price_query),
)
.route(format!("{}/coins/list", API_PATH).as_str(), web::get().to(handle_coingecko_token_list))
.route(
format!("{}/coins/{{coin_id}}/market_chart", API_PATH).as_str(),
web::get().to(handle_coingecko_token_price_query),
)
}
Expand Down

0 comments on commit fa9ef7b

Please sign in to comment.