Refactored types

This commit is contained in:
2026-07-25 04:08:48 +02:00
parent cde2abf5f1
commit 6b6085ef25
8 changed files with 158 additions and 160 deletions
+97
View File
@@ -0,0 +1,97 @@
use pulse_macros::pwp;
use crate::PulseWire;
#[pwp]
pub enum MarketTrend {
Bullish,
Bearish,
Neutral,
}
#[pwp]
pub enum Volatility {
Low,
Medium,
High,
}
#[pwp]
pub enum Direction {
Buy,
Sell,
}
#[pwp]
pub struct Signal {
pub symbol: String,
pub kind: Direction,
pub confidence: f32,
pub size: f64,
pub price: f64,
pub take_profit: f64,
pub take_stop_loss: f64,
}
#[pwp]
pub enum LogKind {
Info,
Warn,
Err,
Debug,
}
#[pwp]
pub struct EventLog {
pub kind: LogKind,
pub name: String,
pub message: String,
}
#[pwp]
pub struct Position {
pub symbol: String,
pub size: f64,
pub entry_price: f64,
pub profit: f64,
}
impl std::fmt::Display for Direction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Buy => write!(f, "BUY"),
Self::Sell => write!(f, "SELL"),
}
}
}
impl std::fmt::Display for MarketTrend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Bullish => write!(f, "Bullish"),
Self::Bearish => write!(f, "Bearish"),
Self::Neutral => write!(f, "Neutral"),
}
}
}
impl std::fmt::Display for Volatility {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Low => write!(f, "Low"),
Self::Medium => write!(f, "Medium"),
Self::High => write!(f, "High"),
}
}
}
impl std::fmt::Display for LogKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Info => write!(f, "INFO"),
Self::Warn => write!(f, "WARN"),
Self::Err => write!(f, "ERR"),
Self::Debug => write!(f, "DEBUG"),
}
}
}
+1
View File
@@ -2,6 +2,7 @@
use std::path::PathBuf; use std::path::PathBuf;
pub mod terminal; pub mod terminal;
pub mod general;
pub fn server_path() -> PathBuf { pub fn server_path() -> PathBuf {
PathBuf::from("/tmp/pulse-engine.sock") PathBuf::from("/tmp/pulse-engine.sock")
+35 -137
View File
@@ -1,13 +1,16 @@
use crate::PulseWire; use crate::{
PulseWire,
general::{EventLog, MarketTrend, Position, Signal, Volatility},
};
use pulse_macros::pwp; use pulse_macros::pwp;
#[pwp] #[pwp]
pub enum TerminalServerMessage { pub enum TerminalServerMessage {
// WatchList // WatchList
WatchListUpdated(Vec<WatchListItem>), WatchListUpdated(Vec<MarketItem>),
// Positions // Positions
PositionsUpdated(Vec<ActivePosition>), PositionsUpdated(Vec<Position>),
// Overview // Overview
OverviewUpdated(MarketOverview), OverviewUpdated(MarketOverview),
@@ -32,95 +35,19 @@ pub enum TerminalClientMessage {
} }
#[pwp] #[pwp]
pub struct WatchListItem { pub struct MarketItem {
symbol: String,
price: f64,
trend: f64,
volume_24h: f64,
}
#[pwp]
pub struct ActivePosition {
pub symbol: String, pub symbol: String,
pub size: f64, pub price: f64,
pub entry_price: f64, pub trend: f64,
pub profit: f64, pub volume_24h: f64,
}
#[pwp]
pub enum MarketTrend {
Bullish,
Bearish,
Neutral,
}
#[pwp]
pub enum Volatility {
Low,
Medium,
High,
} }
#[pwp] #[pwp]
pub struct MarketOverview { pub struct MarketOverview {
trend: MarketTrend, pub trend: MarketTrend,
volatility: Volatility, pub volatility: Volatility,
pressure: f64, pub pressure: f64,
pub alerts: Vec<Alert>,
alerts: Vec<Alert>,
}
#[pwp]
pub enum Feed {
Connected,
Disconnected,
Connecting,
Failed,
}
#[pwp]
pub struct Status {
feed: Feed,
exchange: String,
dex: String,
latency: u16,
}
#[pwp]
pub enum SignalKind {
Buy,
Sell,
}
#[pwp]
pub enum SignalParameter {
Lim,
Stl,
Tap,
Chk,
}
#[pwp]
pub struct Signal {
kind: SignalKind,
symbol: String,
param: SignalParameter,
price: f64,
}
#[pwp]
pub enum LogKind {
Info,
Warn,
Err,
Debug,
}
#[pwp]
pub struct EventLog {
kind: LogKind,
name: String,
message: String,
} }
#[pwp] #[pwp]
@@ -129,13 +56,19 @@ pub enum AlertLevel {
Medium, Medium,
Low, Low,
} }
#[pwp] #[pwp]
pub struct Alert { pub struct Alert {
level: AlertLevel, level: AlertLevel,
message: String, message: String,
} }
#[pwp]
pub struct Balance {
pub asset: String,
pub amount: f64,
pub value: f64,
}
#[pwp] #[pwp]
pub enum InspectTarget { pub enum InspectTarget {
None, None,
@@ -150,14 +83,20 @@ pub enum InspectItem {
F64(f64), F64(f64),
} }
impl std::fmt::Display for MarketTrend { #[pwp]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { pub struct Status {
match self { feed: Feed,
Self::Bullish => write!(f, "Bullish"), exchange: String,
Self::Bearish => write!(f, "Bearish"), dex: String,
Self::Neutral => write!(f, "Neutral"), latency: u16,
} }
}
#[pwp]
pub enum Feed {
Connected,
Disconnected,
Connecting,
Failed,
} }
impl std::fmt::Display for AlertLevel { impl std::fmt::Display for AlertLevel {
@@ -169,44 +108,3 @@ impl std::fmt::Display for AlertLevel {
} }
} }
} }
impl std::fmt::Display for Volatility {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Low => write!(f, "Low"),
Self::Medium => write!(f, "Medium"),
Self::High => write!(f, "High"),
}
}
}
impl std::fmt::Display for LogKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Info => write!(f, "INFO"),
Self::Warn => write!(f, "WARN"),
Self::Err => write!(f, "ERR"),
Self::Debug => write!(f, "DEBUG"),
}
}
}
impl std::fmt::Display for SignalKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Buy => write!(f, "BUY"),
Self::Sell => write!(f, "SELL"),
}
}
}
impl std::fmt::Display for SignalParameter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Lim => write!(f, "LIM"),
Self::Stl => write!(f, "STL"),
Self::Tap => write!(f, "TAP"),
Self::Chk => write!(f, "CHK"),
}
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use pulse_wire::terminal::ActivePosition; use pulse_wire::general::Position;
use tokio::{sync::Mutex, task::JoinHandle}; use tokio::{sync::Mutex, task::JoinHandle};
use crate::{ use crate::{
@@ -90,7 +90,7 @@ impl Engine {
state state
.asset_positions .asset_positions
.into_iter() .into_iter()
.map(|position| ActivePosition { .map(|position| Position {
symbol: position.position.coin, symbol: position.position.coin,
size: position.position.szi.as_f64(), size: position.position.szi.as_f64(),
entry_price: position entry_price: position
+3 -3
View File
@@ -1,6 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use pulse_wire::terminal::WatchListItem; use pulse_wire::terminal::MarketItem;
use serde_json::Value; use serde_json::Value;
fn number(value: &Value, field: &str) -> Result<f64, String> { fn number(value: &Value, field: &str) -> Result<f64, String> {
@@ -15,7 +15,7 @@ fn number(value: &Value, field: &str) -> Result<f64, String> {
pub async fn fetch_watch_list( pub async fn fetch_watch_list(
client: &hypersdk::hypercore::HttpClient, client: &hypersdk::hypercore::HttpClient,
symbols: &[String], symbols: &[String],
) -> Result<Vec<WatchListItem>, String> { ) -> Result<Vec<MarketItem>, String> {
let response = client let response = client
.meta_and_asset_ctxs(None) .meta_and_asset_ctxs(None)
.await .await
@@ -63,7 +63,7 @@ pub async fn fetch_watch_list(
by_symbol.insert( by_symbol.insert(
symbol, symbol,
WatchListItem { MarketItem {
symbol: symbol.to_owned(), symbol: symbol.to_owned(),
price, price,
trend: ((price / previous_day_price) - 1.0) * 100.0, trend: ((price / previous_day_price) - 1.0) * 100.0,
+1 -4
View File
@@ -3,10 +3,7 @@ use std::{
sync::{Arc, Weak}, sync::{Arc, Weak},
}; };
use pulse_wire::{ use pulse_wire::{PulseWire, general::{EventLog, LogKind}, terminal::TerminalClientMessage};
PulseWire,
terminal::{EventLog, LogKind, TerminalClientMessage},
};
use tokio::{ use tokio::{
io::{AsyncReadExt, AsyncWriteExt}, io::{AsyncReadExt, AsyncWriteExt},
net::{ net::{
+6 -6
View File
@@ -1,8 +1,9 @@
use pulse_wire::terminal::{ use pulse_wire::terminal::{
ActivePosition, Alert, AlertLevel, EventLog, InspectItem, InspectTarget, LogKind, Alert, AlertLevel, InspectItem, InspectTarget, MarketItem, MarketOverview, Status,
MarketOverview, Signal, SignalKind, Status, WatchListItem,
}; };
use pulse_wire::general::{Direction, EventLog, LogKind, Position, Signal};
pub trait Formatted { pub trait Formatted {
fn get_formatted(&self) -> Vec<String>; fn get_formatted(&self) -> Vec<String>;
} }
@@ -64,19 +65,18 @@ impl Formatted for EventLog {
impl Formatted for Signal { impl Formatted for Signal {
fn get_formatted(&self) -> Vec<String> { fn get_formatted(&self) -> Vec<String> {
vec![ vec![
if matches!(self.kind, SignalKind::Buy) { if matches!(self.kind, Direction::Buy) {
format!("\x1b[32m{}\x1b[0m", self.kind) format!("\x1b[32m{}\x1b[0m", self.kind)
} else { } else {
format!("\x1b[31m{}\x1b[0m", self.kind) format!("\x1b[31m{}\x1b[0m", self.kind)
}, },
format!("\x1b[35m{}\x1b[0m", self.symbol), format!("\x1b[35m{}\x1b[0m", self.symbol),
format!("\x1b[34m{}\x1b[0m", self.param),
format_f64(self.price), format_f64(self.price),
] ]
} }
} }
impl Formatted for WatchListItem { impl Formatted for MarketItem {
fn get_formatted(&self) -> Vec<String> { fn get_formatted(&self) -> Vec<String> {
vec![ vec![
format!("\x1b[35m{}\x1b[0m", self.symbol), format!("\x1b[35m{}\x1b[0m", self.symbol),
@@ -95,7 +95,7 @@ impl Formatted for WatchListItem {
} }
} }
impl Formatted for ActivePosition { impl Formatted for Position {
fn get_formatted(&self) -> Vec<String> { fn get_formatted(&self) -> Vec<String> {
vec![ vec![
format!("\x1b[35m{}\x1b[0m", self.symbol), format!("\x1b[35m{}\x1b[0m", self.symbol),
+13 -8
View File
@@ -21,8 +21,9 @@ use pulse_ui::{
use crate::formatting::{Formatted, apply_padding}; use crate::formatting::{Formatted, apply_padding};
use pulse_wire::terminal::{ use pulse_wire::{
ActivePosition, EventLog, InspectTarget, MarketOverview, Signal, Status, WatchListItem, general::{EventLog, Position, Signal},
terminal::{InspectTarget, MarketItem, MarketOverview, Status},
}; };
pub struct PulseTradeApp { pub struct PulseTradeApp {
@@ -34,8 +35,8 @@ pub struct PulseTradeApp {
market_overview: State<Option<MarketOverview>>, market_overview: State<Option<MarketOverview>>,
status: State<Option<Status>>, status: State<Option<Status>>,
watch_list: State<Vec<WatchListItem>>, watch_list: State<Vec<MarketItem>>,
active_positions: State<Vec<ActivePosition>>, active_positions: State<Vec<Position>>,
logs: State<Vec<EventLog>>, logs: State<Vec<EventLog>>,
signals: State<Vec<Signal>>, signals: State<Vec<Signal>>,
inspect: State<InspectTarget>, inspect: State<InspectTarget>,
@@ -130,15 +131,19 @@ impl App for PulseTradeApp {
( (
LayoutItem::Widget(Size::Flex(1)), LayoutItem::Widget(Size::Flex(1)),
Box::new( Box::new(
advanced_draw(&self.scroll, 1, "ACTIVE POSITIONS", &self.active_positions) advanced_draw(&self.scroll, 1, "POSITIONS", &self.active_positions).await,
.await,
), ),
), ),
( (
LayoutItem::Widget(Size::Flex(1)), LayoutItem::Widget(Size::Flex(1)),
Box::new( Box::new(
advanced_option_draw(&self.scroll, 2, "MARKET OVERVIEW", &self.market_overview) advanced_option_draw(
.await, &self.scroll,
2,
"MARKET OVERVIEW",
&self.market_overview,
)
.await,
), ),
), ),
]), ]),