// Copyright (C) 2024 Riften Labs AS // // This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later. // A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html use rocket::http::Status; use rocket::{get, response::status::Custom, serde::json::Json, State}; use serde_json::json; use serde_json::Value; use crate::bcmr::parsedbcmr::ParsedBCMR; use crate::db::DB; use crate::rpc; use rayon::prelude::*; #[get("/tokens/list_by_volume?&")] pub fn list_by_volume( duration: Option, limit: Option, db: &State, ) -> Result>, Custom> { let thirty_days = 24 * 60 * 60 * 30; let duration = duration.unwrap_or(thirty_days); let limit = limit.unwrap_or(50); let limit = std::cmp::max(limit, 1000); #[allow(clippy::type_complexity)] let list: Vec<(String, u64, u64, u64, u64, u64, u64, Option)> = rpc::list_tokens_by_volume( &db.cauldron_r.get().unwrap(), &db.bcmr_r.get().unwrap(), duration, limit, ) .map_err(|e| Custom(Status::InternalServerError, format!("Error: {}", e)))?; let result: Vec = list .into_par_iter() .map( |( token_id, trade_volume, trade_count, tvl_sats, tvl_token, best_contract_sats, best_contract_tokens, bcmr, )| { json!({ "token_id": token_id, "trade_volume": trade_volume, "trade_count": trade_count, "tvl_sats": tvl_sats, "tvl_tokens": tvl_token, "best_contract_sats": best_contract_sats, "best_contract_tokens": best_contract_tokens, "bcmr": bcmr }) }, ) .collect(); Ok(Json(result)) }