Skip to content

Commit

Permalink
fix: fix coinmarketcap client tests (#108)
Browse files Browse the repository at this point in the history
* fix: fix coinmarketcap client tests

* style: apply fmt

* test: fix parallel tests

---------

Co-authored-by: Francisco Tobar <[email protected]>
  • Loading branch information
antomor and franciscotobar committed Oct 19, 2023
1 parent 0bad4cf commit ff1536d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
64 changes: 57 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::{SecondsFormat, Utc};
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,45 @@ 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 +179,33 @@ 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
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ services:
dev-ticker:
image: "rsksmart/rollup-dev-ticker:1.0.0-beta"
env_file:
- ./etc/env/${ZKSYNC_ENV-dev}.env
- ./etc/env/${ENV_OVERRIDE-deploy}.env
ports:
- "9876:9876"
Expand Down
1 change: 1 addition & 0 deletions docker/environment/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ENV RUSTUP_HOME=/usr/local/rustup \
PATH=/usr/local/cargo/bin:$PATH
RUN curl https://sh.rustup.rs -sSf | bash -s -- -y
RUN rustup install 1.69.0
RUN rustup override set 1.69.0
RUN cargo install diesel_cli --version=1.4.0 --no-default-features --features postgres
RUN cargo install --version=0.5.6 sqlx-cli
RUN cargo install wasm-pack --git https://github.com/d3lm/wasm-pack --rev 713868b204f151acd1989c3f29ff9d3bc944c306
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/zk/src/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function rustCryptoTests() {
}

export async function serverRust() {
await utils.spawn(`${CARGO_FLAGS} cargo test --release`);
await utils.spawn(`${CARGO_FLAGS} cargo test --release -- --test-threads=1`);
await db(true);
await rustApi(true);
await prover();
Expand Down

0 comments on commit ff1536d

Please sign in to comment.