From fa9ef7b898607db934e207a476c5c4bca33d3772 Mon Sep 17 00:00:00 2001 From: Antonio Morrone Date: Wed, 18 Oct 2023 13:07:09 +0200 Subject: [PATCH] fix: fix coinmarketcap client tests --- .../src/bin/providers/dev_price_provider.rs | 62 ++++++++++++++++--- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/core/bin/zksync_api/src/bin/providers/dev_price_provider.rs b/core/bin/zksync_api/src/bin/providers/dev_price_provider.rs index 0195d9147..41c84b4ee 100644 --- a/core/bin/zksync_api/src/bin/providers/dev_price_provider.rs +++ b/core/bin/zksync_api/src/bin/providers/dev_price_provider.rs @@ -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}; @@ -50,6 +50,46 @@ macro_rules! make_sloppy { }}; } +async fn handle_coinmarketcap_token_price_query( + query: web::Query, + _data: web::Data>, +) -> Result { + 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, @@ -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), ) }