Skip to content

Commit

Permalink
feat: call quotes run fn from cli (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit authored Nov 15, 2023
1 parent 36a25eb commit 07e7609
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 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 cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "The stablesats cli binary"
[dependencies]
shared = { path = "../shared", package = "stablesats-shared" }
price-server = { path = "../price-server" }
quotes-server = { path = "../quotes-server" }
user-trades = { path = "../user-trades" }
galoy-client = { path = "../galoy-client" }
okex-client = { path = "../okex-client" }
Expand Down
43 changes: 43 additions & 0 deletions cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ async fn run_cmd(
hedging,
exchanges,
bria,
quotes_server,
}: Config,
) -> anyhow::Result<()> {
println!("Stablesats - v{}", env!("CARGO_PKG_VERSION"));
Expand Down Expand Up @@ -223,6 +224,38 @@ async fn run_cmd(
}
}

if quotes_server.enabled {
println!("Starting quotes_server");

let quotes_send = send.clone();
let pool = if let Some(pool) = pool.as_ref() {
pool.clone()
} else {
crate::db::init_pool(&db).await?
};
let (snd, recv) = futures::channel::mpsc::unbounded();
checkers.insert("quotes", snd);
let price = price_recv.resubscribe();
let weights = extract_weights_for_quotes_server(&exchanges);
handles.push(tokio::spawn(async move {
let _ = quotes_send.try_send(
quotes_server::run(
pool,
recv,
quotes_server.health,
quotes_server.server,
quotes_server.fees,
price,
quotes_server.price_cache,
weights,
quotes_server.config,
)
.await
.context("Quote Server error"),
);
}));
}

if user_trades.enabled {
println!("Starting user trades process");

Expand Down Expand Up @@ -271,3 +304,13 @@ fn extract_weights(config: &hedging::ExchangesConfig) -> price_server::ExchangeW
bitfinex: None,
}
}

fn extract_weights_for_quotes_server(
config: &hedging::ExchangesConfig,
) -> quotes_server::ExchangeWeights {
quotes_server::ExchangeWeights {
okex: config.okex.as_ref().map(|c| c.weight),

bitfinex: None,
}
}
35 changes: 35 additions & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use hedging::{ExchangesConfig, HedgingAppConfig};
use price_server::{
ExchangePriceCacheConfig, FeeCalculatorConfig, PriceServerConfig, PriceServerHealthCheckConfig,
};
use quotes_server::{
QuotesConfig, QuotesExchangePriceCacheConfig, QuotesFeeCalculatorConfig, QuotesServerConfig,
QuotesServerHealthCheckConfig,
};
use user_trades::UserTradesConfig;

use super::{db::DbConfig, tracing::TracingConfig};
Expand All @@ -32,6 +36,8 @@ pub struct Config {
pub exchanges: ExchangesConfig,
#[serde(default)]
pub bria: BriaClientConfig,
#[serde(default)]
pub quotes_server: QuotesServerWrapper,
}

pub struct EnvOverride {
Expand Down Expand Up @@ -101,6 +107,35 @@ impl Default for PriceServerWrapper {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuotesServerWrapper {
#[serde(default = "bool_true")]
pub enabled: bool,
#[serde(default)]
pub health: QuotesServerHealthCheckConfig,
#[serde(default)]
pub server: QuotesServerConfig,
#[serde(default)]
pub fees: QuotesFeeCalculatorConfig,
#[serde(default)]
pub price_cache: QuotesExchangePriceCacheConfig,
#[serde(default)]
pub config: QuotesConfig,
}

impl Default for QuotesServerWrapper {
fn default() -> Self {
Self {
enabled: true,
server: QuotesServerConfig::default(),
health: QuotesServerHealthCheckConfig::default(),
fees: QuotesFeeCalculatorConfig::default(),
price_cache: QuotesExchangePriceCacheConfig::default(),
config: QuotesConfig::default(),
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct BitfinexPriceFeedConfigWrapper {
#[serde(default)]
Expand Down
2 changes: 1 addition & 1 deletion quotes-server/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ pub struct QuotesApp {
#[allow(clippy::too_many_arguments)]
impl QuotesApp {
pub async fn run(
pool: sqlx::PgPool,
mut health_check_trigger: HealthCheckTrigger,
health_check_cfg: QuotesServerHealthCheckConfig,
fee_calc_cfg: QuotesFeeCalculatorConfig,
subscriber: memory::Subscriber<PriceStreamPayload>,
price_cache_config: QuotesExchangePriceCacheConfig,
exchange_weights: ExchangeWeights,
config: QuotesConfig,
pool: sqlx::PgPool,
) -> Result<Self, QuotesAppError> {
let health_subscriber = subscriber.resubscribe();
tokio::spawn(async move {
Expand Down
4 changes: 2 additions & 2 deletions quotes-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ pub use server::*;

#[allow(clippy::too_many_arguments)]
pub async fn run(
pool: sqlx::PgPool,
health_check_trigger: HealthCheckTrigger,
health_check_cfg: QuotesServerHealthCheckConfig,
server_config: QuotesServerConfig,
fee_calc_cfg: QuotesFeeCalculatorConfig,
subscriber: memory::Subscriber<PriceStreamPayload>,
price_cache_config: QuotesExchangePriceCacheConfig,
exchange_weights: ExchangeWeights,
pool: sqlx::PgPool,
quotes_config: QuotesConfig,
) -> Result<(), QuotesServerError> {
let app = QuotesApp::run(
pool,
health_check_trigger,
health_check_cfg,
fee_calc_cfg,
subscriber,
price_cache_config,
exchange_weights,
quotes_config,
pool,
)
.await?;

Expand Down

0 comments on commit 07e7609

Please sign in to comment.