From ae37c02adcdca940d08ddba9dca5b65906550dcc Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 24 Jul 2026 16:50:36 +0200 Subject: [PATCH] Using config in engine --- src/engine/config.rs | 4 ++-- src/engine/engine.rs | 21 +++++++++++++-------- src/engine/fetch.rs | 4 ++-- src/engine/main.rs | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/engine/config.rs b/src/engine/config.rs index 3380131..8555d75 100644 --- a/src/engine/config.rs +++ b/src/engine/config.rs @@ -16,12 +16,12 @@ pub fn pulse_config_directory() -> tokio::io::Result { #[derive(Debug, Default, serde::Serialize, serde::Deserialize)] pub struct WatchList { - symbols: Vec, + pub symbols: Vec, } #[derive(Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Config { - watchlist: WatchList, + pub watchlist: WatchList, } impl Config { diff --git a/src/engine/engine.rs b/src/engine/engine.rs index a309542..e1dd561 100644 --- a/src/engine/engine.rs +++ b/src/engine/engine.rs @@ -1,18 +1,20 @@ use std::sync::Arc; -use crate::terminal::TerminalServer; +use tokio::sync::Mutex; -const WATCH_LIST_SYMBOLS: &[&str] = &["BTC", "ETH", "SOL", "XRP"]; +use crate::{config::Config, terminal::TerminalServer}; pub struct Engine { pub terminal_server: Arc, + pub config: Arc>, } impl Engine { - pub fn new() -> Self { - Self { + pub async fn new() -> tokio::io::Result { + Ok(Self { terminal_server: TerminalServer::new(), - } + config: Arc::new(Mutex::new(Config::new().await?)), + }) } pub fn spawn_terminal_server(&self) { @@ -26,8 +28,9 @@ impl Engine { }); } - pub fn spawn_broadcaster(&mut self) { + pub fn spawn_broadcaster(&self) { let terminal_server = self.terminal_server.clone(); + let config = self.config.clone(); tokio::spawn(async move { let mut refresh = tokio::time::interval(tokio::time::Duration::from_secs(5)); @@ -35,7 +38,9 @@ impl Engine { loop { refresh.tick().await; - match crate::fetch::fetch_watch_list(WATCH_LIST_SYMBOLS).await { + let watch_list = &config.lock().await.watchlist.symbols; + + match crate::fetch::fetch_watch_list(watch_list).await { Ok(watch_list) => { if let Err(error) = terminal_server .broadcast( @@ -54,7 +59,7 @@ impl Engine { }); } - pub async fn run_engine(&mut self) -> tokio::io::Result<()> { + pub async fn run_engine(&self) -> tokio::io::Result<()> { loop { tokio::time::sleep(tokio::time::Duration::from_millis(5000)).await; } diff --git a/src/engine/fetch.rs b/src/engine/fetch.rs index 1c6ae18..9f7b271 100644 --- a/src/engine/fetch.rs +++ b/src/engine/fetch.rs @@ -12,7 +12,7 @@ fn number(value: &Value, field: &str) -> Result { .map_err(|error| format!("could not parse asset context field {field} ({raw}): {error}")) } -pub async fn fetch_watch_list(symbols: &[&str]) -> Result, String> { +pub async fn fetch_watch_list(symbols: &[String]) -> Result, String> { let response = hypersdk::hypercore::mainnet() .meta_and_asset_ctxs(None) .await @@ -73,7 +73,7 @@ pub async fn fetch_watch_list(symbols: &[&str]) -> Result, St .iter() .map(|symbol| { by_symbol - .remove(*symbol) + .remove(symbol.as_str()) .ok_or_else(|| format!("{symbol} is not in the Hyperliquid perpetual universe")) }) .collect() diff --git a/src/engine/main.rs b/src/engine/main.rs index b1a7454..1b50670 100644 --- a/src/engine/main.rs +++ b/src/engine/main.rs @@ -5,7 +5,7 @@ pub mod terminal; #[tokio::main] async fn main() -> tokio::io::Result<()> { - let mut engine = engine::Engine::new(); + let engine = engine::Engine::new().await?; engine.spawn_terminal_server();