Skip to content

Commit

Permalink
Merge pull request #17 from hexresearch/overview-page-2
Browse files Browse the repository at this point in the history
Overview page api, templates and querying
  • Loading branch information
Aminion authored May 4, 2022
2 parents 1767654 + c88593b commit 7903e0b
Show file tree
Hide file tree
Showing 13 changed files with 5,355 additions and 8 deletions.
1 change: 1 addition & 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 hexstody-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tokio = { version = "1", features = [ "full" ] }
ecdsa = { version = "0.13.4", features = [ "serde", "pem" ] }
p256 = "0.10.1"
uuid = { version = "0.8", features = [ "serde", "v4" ] }
schemars = "0.8.8"

[dev-dependencies]
maplit = "1.0.2"
Expand Down
5 changes: 3 additions & 2 deletions hexstody-db/src/domain/currency.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// A currency that custody understands. Can be extended in future.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Currency {
BTC,
ETH,
ERC20(Erc20Token),
}

/// Description of ERC20 token that allows to distinguish them between each other
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Erc20Token {
/// Short name of the token like USDT or WBTC
pub ticker: String,
Expand Down
3 changes: 2 additions & 1 deletion hexstody-hot/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod types;
pub mod operator;
pub mod public;
pub mod operator;
2 changes: 1 addition & 1 deletion hexstody-hot/src/api/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub async fn serve_operator_api(
pool: Pool,
state: Arc<Mutex<State>>,
state_notify: Arc<Notify>,
port: u16
port: u16,
) -> Result<(), rocket::Error> {
let figment = rocket::Config::figment().merge(("port", port));
rocket::custom(figment)
Expand Down
59 changes: 55 additions & 4 deletions hexstody-hot/src/api/public.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{Mutex, Notify};

use rocket::fs::{relative, FileServer};
use rocket::response::content;
use rocket::{get};
use rocket::{get, routes};
use rocket_dyn_templates::Template;
use rocket_okapi::{openapi, openapi_get_routes, swagger_ui::*};
use std::sync::Arc;
use tokio::sync::{Mutex, Notify};

use super::types::*;
use hexstody_db::domain::currency::Currency;
use hexstody_db::state::State;
use hexstody_db::Pool;

Expand All @@ -15,6 +19,52 @@ fn ping() -> content::Json<()> {
content::Json(())
}

#[openapi(tag = "get_balance")]
#[get("/get_balance")]
fn get_balance() -> Json<Balance> {
let x = Balance {
balances: vec![BalanceItem {
currency: Currency::BTC,
value: 100,
}],
};

Json(x)
}

#[openapi(tag = "get_history")]
#[get("/get_history")]
fn get_history() -> Json<History> {
let x = History {
history_items: vec![
HistoryItem::Deposit(DepositHistoryItem {
currency: Currency::BTC,
value: 100,
}),
HistoryItem::Withdrawal(WithdrawalHistoryItem {
currency: Currency::ETH,
value: 300,
}),
],
};

Json(x)
}

#[openapi(skip)]
#[get("/")]
fn index() -> Template {
let context = HashMap::from([("title", "Index"), ("parent", "base")]);
Template::render("index", context)
}

#[openapi(skip)]
#[get("/overview")]
fn overview() -> Template {
let context = HashMap::from([("title", "Overview"), ("parent", "base")]);
Template::render("overview", context)
}

pub async fn serve_public_api(
pool: Pool,
state: Arc<Mutex<State>>,
Expand All @@ -24,7 +74,8 @@ pub async fn serve_public_api(
let figment = rocket::Config::figment().merge(("port", port));
rocket::custom(figment)
.mount("/static", FileServer::from(relative!("static/")))
.mount("/", openapi_get_routes![ping])
.mount("/", openapi_get_routes![ping, get_balance, get_history])
.mount("/", routes![index, overview])
.mount(
"/swagger/",
make_swagger_ui(&SwaggerUIConfig {
Expand Down
41 changes: 41 additions & 0 deletions hexstody-hot/src/api/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use rocket::serde::json::Json;
use rocket_okapi::okapi::schemars;
use rocket_okapi::okapi::schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use hexstody_db::domain::currency::Currency;

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct BalanceItem {
pub currency: Currency,
pub value: u64,
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct DepositHistoryItem {
pub currency: Currency,
pub value: u64,
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct WithdrawalHistoryItem {
pub currency: Currency,
pub value: u64,
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
#[serde(rename_all = "camelCase")]
pub enum HistoryItem {
Deposit(DepositHistoryItem),
Withdrawal(WithdrawalHistoryItem),
}
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct Balance {
pub balances: Vec<BalanceItem>,
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct History {
pub history_items: Vec<HistoryItem>,
}
Loading

0 comments on commit 7903e0b

Please sign in to comment.