Skip to content

Commit

Permalink
09/24/2023 01:24:44 PM 💻
Browse files Browse the repository at this point in the history
  • Loading branch information
alloc33 committed Sep 24, 2023
1 parent c1a16f6 commit 244c879
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion market/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::Serialize;
use thiserror::Error as ThisError;
use tracing::error;

use crate::broker_client::BrokerError;
use crate::clients::BrokerError;

pub const INTERNAL_SERVER_ERROR: &str = "Internal server error occurred...";
pub const PAYLOAD_TOO_LARGE: &str = "Request payload too large...";
Expand Down
22 changes: 8 additions & 14 deletions market/src/api/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tracing::error;
use super::{alert::TradeSignal, error::ApiError, objects::Account, Response};
use crate::{
alert::WebhookAlertData,
broker_client::BrokerClient,
clients::ExchangeClient,
strategy_manager::{process_trade_signal, Broker},
App,
};
Expand Down Expand Up @@ -75,8 +75,8 @@ pub async fn receive_webhook_alert(
}

#[derive(Debug, Deserialize)]
pub struct BrokerQuery {
broker: Broker,
pub struct ExchangeQuery {
exchange: Broker,
}

pub async fn check_health(State(_app): State<Arc<App>>) -> Response<()> {
Expand All @@ -85,40 +85,34 @@ pub async fn check_health(State(_app): State<Arc<App>>) -> Response<()> {

pub async fn get_account(
State(app): State<Arc<App>>,
Query(query): Query<BrokerQuery>,
Query(query): Query<ExchangeQuery>,
) -> Response<Account> {
println!("abx");

let client = match query.broker {
let client = match query.exchange {
Broker::Alpaca => &app.clients.alpaca,
};

println!("reached");

let account = client.get_account().await?;

println!("reached 1");

Ok((StatusCode::OK, Json(account)))
}

pub async fn get_assets(
State(app): State<Arc<App>>,
Query(query): Query<BrokerQuery>,
Query(query): Query<ExchangeQuery>,
) -> Response<()> {
Ok((StatusCode::OK, Json::default()))
}

pub async fn get_orders(
State(app): State<Arc<App>>,
Query(query): Query<BrokerQuery>,
Query(query): Query<ExchangeQuery>,
) -> Response<()> {
Ok((StatusCode::OK, Json::default()))
}

pub async fn get_positions(
State(app): State<Arc<App>>,
Query(query): Query<BrokerQuery>,
Query(query): Query<ExchangeQuery>,
) -> Response<()> {
Ok((StatusCode::OK, Json::default()))
}
7 changes: 6 additions & 1 deletion market/src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pub struct Database {
pub url: String,
}

#[derive(Debug, Deserialize, Clone)]
pub struct Exchanges {
pub alpaca: Alpaca,
}

#[derive(Debug, Deserialize, Clone)]
pub struct Alpaca {
pub apca_api_key_id: String,
Expand All @@ -21,7 +26,7 @@ pub struct Alpaca {
pub struct AppConfig {
pub api_key: String,
pub database: Database,
pub alpaca: Alpaca,
pub exchanges: Exchanges,
pub strategies: Vec<Strategy>,
}

Expand Down
4 changes: 2 additions & 2 deletions market/src/broker_client.rs → market/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum BrokerError {
}

#[axum::async_trait]
pub trait BrokerClient: Send + Sync {
pub trait ExchangeClient: Send + Sync {
async fn get_account(&self) -> Result<Account, BrokerError>;
async fn get_positions(&self) -> Result<(), BrokerError>;
async fn get_orders(&self) -> Result<(), BrokerError>;
Expand All @@ -38,7 +38,7 @@ pub trait BrokerClient: Send + Sync {
}

#[axum::async_trait]
impl BrokerClient for Arc<AlpacaClient> {
impl ExchangeClient for Arc<AlpacaClient> {
async fn get_account(&self) -> Result<Account, BrokerError> {
let result = self
.issue::<account::Get>(&())
Expand Down
10 changes: 5 additions & 5 deletions market/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod api;
pub mod app_config;
pub mod broker_client;
pub mod clients;
pub mod middleware;
pub mod strategy;
pub mod strategy_manager;
Expand All @@ -15,7 +15,7 @@ use axum::{
routing::{get, post},
Router,
};
use broker_client::Clients;
use clients::Clients;
use sqlx::{postgres::PgConnectOptions, Error as SqlxError, PgPool};
use tower::ServiceBuilder;

Expand Down Expand Up @@ -55,9 +55,9 @@ pub async fn build_state(config: AppConfig, clients: Arc<Clients>) -> Result<App

pub fn build_broker_clients(config: &AppConfig) -> Result<Arc<Clients>, Box<dyn Error>> {
let alpaca = AlpacaClient::new(ApiInfo::from_parts(
&config.alpaca.apca_api_base_url,
&config.alpaca.apca_api_key_id,
&config.alpaca.apca_api_secret_key,
&config.exchanges.alpaca.apca_api_base_url,
&config.exchanges.alpaca.apca_api_key_id,
&config.exchanges.alpaca.apca_api_secret_key,
)?);

Ok(Arc::new(Clients {
Expand Down
4 changes: 2 additions & 2 deletions market/src/strategy_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ use uuid7::uuid7;

use crate::{
api::alert::{AlertType, TradeSignal, WebhookAlertData},
broker_client::{BrokerClient, Clients},
clients::{ExchangeClient, Clients},
App,
};

pub async fn process_trade_signal(
client: impl BrokerClient,
client: impl ExchangeClient,
signal: TradeSignal,
) -> Result<(), ()> {
Ok(())
Expand Down

0 comments on commit 244c879

Please sign in to comment.