Skip to content

Commit

Permalink
Add ticker api to public server
Browse files Browse the repository at this point in the history
  • Loading branch information
LeMarwin committed Sep 24, 2022
1 parent 3117432 commit 48d3c93
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 118 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ members = [
"hexstody-hot",
"hexstody-sig",
"hexstody-ticker-provider",
"hexstody-ticker",
"hexstody-runtime-db",
]
1 change: 1 addition & 0 deletions hexstody-public/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ hexstody-btc-client = { path = "../hexstody-btc-client" }
hexstody-eth-client = { path = "../hexstody-eth-client" }
hexstody-runtime-db = { path = "../hexstody-runtime-db" }
hexstody-ticker-provider = { path = "../hexstody-ticker-provider" }
hexstody-ticker = { path = "../hexstody-ticker" }
log = "0.4.14"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
4 changes: 3 additions & 1 deletion hexstody-public/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod wallet;
use base64;
use figment::Figment;
use hexstody_runtime_db::RuntimeState;
use hexstody_ticker::api::ticker_api;
use hexstody_ticker_provider::client::TickerClient;
use qrcode_generator::QrCodeEcc;
use std::fs::File;
Expand Down Expand Up @@ -412,6 +413,7 @@ pub async fn serve_api(
})
});
let static_path: PathBuf = api_config.extract_inner("static_path").unwrap();
let ticker_api = ticker_api();
let _ = rocket::custom(api_config)
.mount("/", FileServer::from(static_path.clone()))
.mount(
Expand All @@ -420,7 +422,6 @@ pub async fn serve_api(
ping,
get_balance,
get_balance_by_currency,
ticker,
get_user_data,
ethfee,
btcfee,
Expand Down Expand Up @@ -451,6 +452,7 @@ pub async fn serve_api(
list_my_orders
],
)
.mount("/ticker/", ticker_api)
.mount(
"/",
routes![
Expand Down
2 changes: 1 addition & 1 deletion hexstody-public/static/scripts/page/overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function getHistory(skip, take, filter) {
}

async function getCourseForCurrency(currency) {
return await fetch("/ticker",
return await fetch("/ticker/ticker",
{
method: "POST",
body: JSON.stringify(currency)
Expand Down
15 changes: 13 additions & 2 deletions hexstody-ticker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
[package]
name = "hexstody-ticker-provider"
name = "hexstody-ticker"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hexstody-api = { path = "../hexstody-api" }
hexstody-ticker-provider = { path = "../hexstody-ticker-provider" }
hexstody-runtime-db = { path = "../hexstody-runtime-db" }
schemars = { version = "0.8.8", features = ["chrono", "uuid"] }
reqwest = { version = "0.11", features = [ "json" ] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"
log = "0.4.14"
log = "0.4.14"
rocket = { version = "=0.5.0-rc.2", default-features = false, features = [
"json",
"uuid",
] }
rocket_okapi = { git = "https://github.com/GREsau/okapi", rev = "ddb07a709129b24ed8e106b0fbf576b6ded615ac", features = [
"rapidoc",
"swagger",
] }
tokio = { version = "1", features = ["full"] }
64 changes: 64 additions & 0 deletions hexstody-ticker/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::sync::Arc;

use hexstody_api::{
types::TickerETH,
domain::{Currency, Fiat}};
use hexstody_api::error;
use hexstody_runtime_db::RuntimeState;
use hexstody_ticker_provider::client::TickerClient;
use rocket::{post, State, Route, serde::json::Json};
use rocket_okapi::{openapi, openapi_get_routes, JsonSchema};
use serde::{Serialize, Deserialize};
use tokio::sync::Mutex;

pub fn ticker_api() -> Vec<Route> {
openapi_get_routes![
ticker,
ticker_pair
]
}

#[openapi(tag = "wallet")]
#[post("/ticker", data = "<currency>")]
pub async fn ticker(
rstate: &State<Arc<Mutex<RuntimeState>>>,
ticker_client: &State<TickerClient>,
currency: Json<Currency>,
) -> error::Result<Json<TickerETH>> {
let currency = currency.into_inner();
let mut rstate = rstate.lock().await;
let ticker: TickerETH = rstate
.get_multifiat_ticker(ticker_client, currency, vec![Fiat::USD, Fiat::RUB])
.await
.map_err(|e| error::Error::GenericError(e.to_string()))?;
Ok(Json(ticker))
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct CurrencyPair {
from: Currency,
to: Currency
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct CurrencyPairResponse{
from: Currency,
to: Currency,
rate: f64
}

#[openapi(tag = "wallet")]
#[post("/pair", data = "<currency_pair>")]
pub async fn ticker_pair(
rstate: &State<Arc<Mutex<RuntimeState>>>,
ticker_client: &State<TickerClient>,
currency_pair: Json<CurrencyPair>,
) -> error::Result<Json<CurrencyPairResponse>> {
let CurrencyPair{ from, to } = currency_pair.into_inner();
let mut rstate = rstate.lock().await;
let rate = rstate
.get_pair_ticker(ticker_client, from.clone(), to.clone())
.await
.map_err(|e| error::Error::GenericError(e.to_string()))?;
Ok(Json(CurrencyPairResponse{ from, to, rate }))
}
113 changes: 0 additions & 113 deletions hexstody-ticker/src/client.rs

This file was deleted.

3 changes: 2 additions & 1 deletion hexstody-ticker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod client;
pub mod worker;
pub mod api;
3 changes: 3 additions & 0 deletions hexstody-ticker/src/worker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

pub async fn run_ticker_worker(){
}

0 comments on commit 48d3c93

Please sign in to comment.