Skip to content

Commit

Permalink
add ticker api to operators
Browse files Browse the repository at this point in the history
  • Loading branch information
LeMarwin committed Sep 24, 2022
1 parent 48d3c93 commit 80527cf
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 1 deletion.
4 changes: 4 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 hexstody-hot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ hexstody-public = { path = "../hexstody-public" }
hexstody-operator = { path = "../hexstody-operator" }
hexstody-btc-test = { path = "../hexstody-btc-test" }
hexstody-ticker-provider = { path = "../hexstody-ticker-provider" }
hexstody-runtime-db = { path = "../hexstody-runtime-db" }
log = "0.4.14"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
10 changes: 10 additions & 0 deletions hexstody-hot/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use figment::Figment;
use futures::future::{join3, AbortHandle, AbortRegistration, Abortable, Aborted};
use futures::Future;
use hexstody_eth_client::client::EthClient;
use hexstody_runtime_db::RuntimeState;
use hexstody_ticker_provider::client::TickerClient;
use log::*;
use p256::pkcs8::DecodePublicKey;
Expand Down Expand Up @@ -174,6 +175,7 @@ async fn serve_abortable<F, Fut, Out>(
async fn serve_api(
pool: Pool,
state_mx: Arc<Mutex<State>>,
runtime_state_mx: Arc<Mutex<RuntimeState>>,
state_notify: Arc<Notify>,
start_notify: Arc<Notify>,
update_sender: mpsc::Sender<StateUpdate>,
Expand All @@ -198,6 +200,7 @@ async fn serve_api(
hexstody_public::api::serve_api(
pool.clone(),
state_mx.clone(),
runtime_state_mx.clone(),
state_notify.clone(),
start_notify.clone(),
update_sender.clone(),
Expand All @@ -215,11 +218,13 @@ async fn serve_api(
hexstody_operator::api::serve_api(
pool.clone(),
state_mx.clone(),
runtime_state_mx.clone(),
state_notify.clone(),
start_notify.clone(),
update_sender.clone(),
btc_client,
eth_client,
ticker_client,
api_config,
)
})
Expand All @@ -231,6 +236,7 @@ async fn serve_api(
pub async fn serve_apis(
pool: Pool,
state_mx: Arc<Mutex<State>>,
runtime_state_mx: Arc<Mutex<RuntimeState>>,
state_notify: Arc<Notify>,
start_notify: Arc<Notify>,
api_config: ApiConfig,
Expand All @@ -249,6 +255,7 @@ pub async fn serve_apis(
let public_api_fut = serve_api(
pool.clone(),
state_mx.clone(),
runtime_state_mx.clone(),
state_notify.clone(),
public_start.clone(),
update_sender.clone(),
Expand All @@ -264,6 +271,7 @@ pub async fn serve_apis(
let operator_api_fut = serve_api(
pool,
state_mx,
runtime_state_mx.clone(),
state_notify,
operator_start.clone(),
update_sender.clone(),
Expand Down Expand Up @@ -316,6 +324,7 @@ pub async fn run_hot_wallet(
info!("Reconstructing state from database");
let state = query_state(args.network, &pool).await?;
let state_mx = Arc::new(Mutex::new(state));
let runtime_state_mx = Arc::new(Mutex::new(RuntimeState::new()));
let state_notify = Arc::new(Notify::new());
let (update_sender, update_receiver) = mpsc::channel(1000);
let (update_resp_sender, update_resp_receiver) = mpsc::channel(1000);
Expand Down Expand Up @@ -356,6 +365,7 @@ pub async fn run_hot_wallet(
if let Err(Aborted) = serve_apis(
pool,
state_mx,
runtime_state_mx,
state_notify,
start_notify,
api_config,
Expand Down
3 changes: 3 additions & 0 deletions hexstody-operator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ hexstody-btc-api = { path = "../hexstody-btc-api" }
hexstody-btc-client = { path = "../hexstody-btc-client" }
hexstody-eth-client = { path = "../hexstody-eth-client" }
hexstody-sig = { path = "../hexstody-sig" }
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
9 changes: 9 additions & 0 deletions hexstody-operator/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use figment::Figment;
use hexstody_runtime_db::RuntimeState;
use hexstody_ticker::api::ticker_api;
use hexstody_ticker_provider::client::TickerClient;
use rocket::{
fairing::AdHoc,
fs::{FileServer, NamedFile},
Expand Down Expand Up @@ -549,11 +552,13 @@ async fn get_exchange_address(
pub async fn serve_api(
pool: Pool,
state: Arc<Mutex<HexstodyState>>,
runtime_state: Arc<Mutex<RuntimeState>>,
_state_notify: Arc<Notify>,
start_notify: Arc<Notify>,
update_sender: mpsc::Sender<StateUpdate>,
btc_client: BtcClient,
eth_client: EthClient,
ticker_client: TickerClient,
api_config: Figment,
) -> Result<(), rocket::Error> {
let on_ready = AdHoc::on_liftoff("API Start!", |_| {
Expand All @@ -562,6 +567,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("/", routes![index])
Expand All @@ -587,6 +593,7 @@ pub async fn serve_api(
get_user_info, // GET: /user/info/<user_id>
],
)
.mount("/ticker/", ticker_api)
.mount(
"/swagger/",
make_swagger_ui(&SwaggerUIConfig {
Expand All @@ -595,10 +602,12 @@ pub async fn serve_api(
}),
)
.manage(state)
.manage(runtime_state)
.manage(pool)
.manage(update_sender)
.manage(btc_client)
.manage(eth_client)
.manage(ticker_client)
.manage(static_path)
.attach(AdHoc::config::<SignatureVerificationConfig>())
.attach(on_ready)
Expand Down
7 changes: 7 additions & 0 deletions hexstody-operator/static/scripts/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,11 @@ export async function getInvites(privateKeyJwk, publicKeyDer) {

export async function getUserInfo(privateKeyJwk, publicKeyDer, userId) {
return await makeSignedRequest(privateKeyJwk, publicKeyDer, null, `user/info/${userId}`, "GET")
}

export async function getTicker(currency){
return await fetch("/ticker/ticker", {
method: "POST",
body: JSON.stringify(currency)
})
}
2 changes: 1 addition & 1 deletion hexstody-public/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ static_path: &State<StaticPath>,) -> Result<Template, Redirect> {
pub async fn serve_api(
pool: Pool,
state: Arc<Mutex<DbState>>,
runtime_state: Arc<Mutex<RuntimeState>>,
_state_notify: Arc<Notify>,
start_notify: Arc<Notify>,
update_sender: mpsc::Sender<StateUpdate>,
Expand All @@ -406,7 +407,6 @@ pub async fn serve_api(
api_config: Figment,
is_test: bool,
) -> Result<(), rocket::Error> {
let runtime_state = Arc::new(Mutex::new(RuntimeState::new()));
let on_ready = AdHoc::on_liftoff("API Start!", |_| {
Box::pin(async move {
start_notify.notify_one();
Expand Down
3 changes: 3 additions & 0 deletions hexstody-public/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use futures_util::future::TryFutureExt;
use hexstody_client::client::HexstodyClient;
use rocket::fs::relative;
use std::panic::AssertUnwindSafe;
use hexstody_runtime_db::RuntimeState;

const SERVICE_TEST_PORT: u16 = 8000;
const SERVICE_TEST_HOST: &str = "127.0.0.1";
Expand All @@ -36,6 +37,7 @@ where
let btc_client = BtcClient::new("127.0.0.1");
let eth_client = EthClient::new("http://127.0.0.1");
let ticker_client = TickerClient::new("https://min-api.cryptocompare.com");
let runtime_state = Arc::new(Mutex::new(RuntimeState::new()));
let api_config = rocket::Config::figment()
.merge(("port", SERVICE_TEST_PORT))
.merge(("static_path", relative!("static")))
Expand All @@ -51,6 +53,7 @@ where
let serve_task = serve_api(
pool,
state,
runtime_state,
state_notify,
start_notify,
update_sender,
Expand Down

0 comments on commit 80527cf

Please sign in to comment.