diff --git a/nym-api/src/circulating_supply_api/mod.rs b/nym-api/src/circulating_supply_api/mod.rs index 9d0e984ab6..900d38380b 100644 --- a/nym-api/src/circulating_supply_api/mod.rs +++ b/nym-api/src/circulating_supply_api/mod.rs @@ -15,7 +15,11 @@ pub(crate) mod routes; /// Merges the routes with http information and returns it to Rocket for serving pub(crate) fn circulating_supply_routes(settings: &OpenApiSettings) -> (Vec, OpenApi) { - openapi_get_routes_spec![settings: routes::get_circulating_supply] + openapi_get_routes_spec![ + settings: routes::get_full_circulating_supply, + routes::get_total_supply, + routes::get_circulating_supply + ] } /// Spawn the circulating supply cache refresher. diff --git a/nym-api/src/circulating_supply_api/routes.rs b/nym-api/src/circulating_supply_api/routes.rs index 898248445a..da6aea1d22 100644 --- a/nym-api/src/circulating_supply_api/routes.rs +++ b/nym-api/src/circulating_supply_api/routes.rs @@ -1,15 +1,30 @@ -use rocket::http::Status; -use rocket::serde::json::Json; -use rocket::State; +// Copyright 2022-2023 - Nym Technologies SA +// SPDX-License-Identifier: Apache-2.0 use crate::circulating_supply_api::cache::CirculatingSupplyCache; use crate::node_status_api::models::ErrorResponse; use nym_api_requests::models::CirculatingSupplyResponse; +use rocket::http::Status; +use rocket::serde::json::Json; +use rocket::State; use rocket_okapi::openapi; +use validator_client::nyxd::Coin; + +// TODO: this is not the best place to put it, it should be more centralised, +// but for a quick fix, that's good enough for now... +// (for proper solution we should be managing `NymNetworkDetails` via rocket and grabbing display exponent +// value from the mix denom here. +const UNYM_RATIO: f64 = 1000000.; + +fn unym_coin_to_float_unym(coin: Coin) -> f64 { + // our total supply can't exceed 1B so an overflow here is impossible + // (if it happened, then we SHOULD crash) + coin.amount as f64 / UNYM_RATIO +} #[openapi(tag = "circulating-supply")] #[get("/circulating-supply")] -pub(crate) async fn get_circulating_supply( +pub(crate) async fn get_full_circulating_supply( cache: &State, ) -> Result, ErrorResponse> { match cache.get_circulating_supply().await { @@ -20,3 +35,43 @@ pub(crate) async fn get_circulating_supply( )), } } + +#[openapi(tag = "circulating-supply")] +#[get("/circulating-supply/total-supply-value")] +pub(crate) async fn get_total_supply( + cache: &State, +) -> Result, ErrorResponse> { + let full_circulating_supply = match cache.get_circulating_supply().await { + Some(res) => res, + None => { + return Err(ErrorResponse::new( + "unavailable", + Status::InternalServerError, + )) + } + }; + + Ok(Json(unym_coin_to_float_unym( + full_circulating_supply.total_supply.into(), + ))) +} + +#[openapi(tag = "circulating-supply")] +#[get("/circulating-supply/circulating-supply-value")] +pub(crate) async fn get_circulating_supply( + cache: &State, +) -> Result, ErrorResponse> { + let full_circulating_supply = match cache.get_circulating_supply().await { + Some(res) => res, + None => { + return Err(ErrorResponse::new( + "unavailable", + Status::InternalServerError, + )) + } + }; + + Ok(Json(unym_coin_to_float_unym( + full_circulating_supply.circulating_supply.into(), + ))) +}