Using config in engine
This commit is contained in:
@@ -16,12 +16,12 @@ pub fn pulse_config_directory() -> tokio::io::Result<PathBuf> {
|
||||
|
||||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
|
||||
pub struct WatchList {
|
||||
symbols: Vec<String>,
|
||||
pub symbols: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Config {
|
||||
watchlist: WatchList,
|
||||
pub watchlist: WatchList,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
|
||||
+13
-8
@@ -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<TerminalServer>,
|
||||
pub config: Arc<Mutex<Config>>,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
pub async fn new() -> tokio::io::Result<Self> {
|
||||
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;
|
||||
}
|
||||
|
||||
+2
-2
@@ -12,7 +12,7 @@ fn number(value: &Value, field: &str) -> Result<f64, String> {
|
||||
.map_err(|error| format!("could not parse asset context field {field} ({raw}): {error}"))
|
||||
}
|
||||
|
||||
pub async fn fetch_watch_list(symbols: &[&str]) -> Result<Vec<WatchListItem>, String> {
|
||||
pub async fn fetch_watch_list(symbols: &[String]) -> Result<Vec<WatchListItem>, 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<Vec<WatchListItem>, 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()
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user