Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix coinmarketcap client tests #108

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("")
franciscotobar marked this conversation as resolved.
Show resolved Hide resolved
.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
Loading