Skip to content

Commit

Permalink
Revert "feat: weighted price from order book cache (#181)" (#196)
Browse files Browse the repository at this point in the history
This reverts commit 952850c.
  • Loading branch information
enigbe authored Nov 21, 2022
1 parent c954afd commit d9a2ead
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 474 deletions.
2 changes: 1 addition & 1 deletion cli/src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async fn health_check_error(
*n_errors += 1;
let span = tracing::Span::current();
span.record("component_name", name);
span.record("n_errors", *n_errors);
span.record("n_errors", &*n_errors);
span.record("error.message", tracing::field::display(&err));
if *n_errors > 4 {
span.record(
Expand Down
8 changes: 3 additions & 5 deletions price-server/src/app/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use thiserror::Error;

use crate::{currency::CurrencyError, order_book_cache::OrderBookCacheError};
use crate::{currency::CurrencyError, exchange_price_cache::ExchangePriceCacheError};
use shared::pubsub::SubscriberError;

#[allow(clippy::large_enum_variant)]
Expand All @@ -10,8 +10,6 @@ pub enum PriceAppError {
CurrencyError(#[from] CurrencyError),
#[error("PriceAppError - SubscriberError: {0}")]
SubscriberError(#[from] SubscriberError),
#[error("PriceAppError - SnapshotCacheError: {0}")]
OrderBookCacheError(#[from] OrderBookCacheError),
#[error("PriceAppError - FloatingPointConversion: {0}")]
FloatingPointConversion(#[from] rust_decimal::Error),
#[error("PriceAppError - ExchangePriceCacheError: {0}")]
ExchangePriceCacheError(#[from] ExchangePriceCacheError),
}
55 changes: 25 additions & 30 deletions price-server/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use chrono::Duration;
use futures::stream::StreamExt;
use tracing::{info_span, instrument, Instrument};

use shared::{health::HealthCheckTrigger, payload::OkexBtcUsdSwapOrderBookPayload, pubsub::*};
use shared::{health::HealthCheckTrigger, payload::OkexBtcUsdSwapPricePayload, pubsub::*};

use super::exchange_price_cache::ExchangePriceCache;

use crate::OrderBookCache;
pub use crate::{currency::*, fee_calculator::*};
pub use error::*;

pub struct PriceApp {
snapshot_cache: OrderBookCache,
price_cache: ExchangePriceCache,
fee_calculator: FeeCalculator,
}

Expand All @@ -22,9 +23,7 @@ impl PriceApp {
pubsub_cfg: PubSubConfig,
) -> Result<Self, PriceAppError> {
let subscriber = Subscriber::new(pubsub_cfg).await?;
let mut stream = subscriber
.subscribe::<OkexBtcUsdSwapOrderBookPayload>()
.await?;
let mut stream = subscriber.subscribe::<OkexBtcUsdSwapPricePayload>().await?;
tokio::spawn(async move {
while let Some(check) = health_check_trigger.next().await {
check
Expand All @@ -33,10 +32,10 @@ impl PriceApp {
}
});

let order_book_cache = OrderBookCache::new(Duration::seconds(30));
let price_cache = ExchangePriceCache::new(Duration::seconds(30));
let fee_calculator = FeeCalculator::new(fee_calc_cfg);
let app = Self {
snapshot_cache: order_book_cache.clone(),
price_cache: price_cache.clone(),
fee_calculator,
};

Expand All @@ -50,7 +49,7 @@ impl PriceApp {
shared::tracing::inject_tracing_data(&span, &msg.meta.tracing_data);

async {
order_book_cache.apply_update(msg).await;
price_cache.apply_update(msg).await;
}
.instrument(span)
.await;
Expand All @@ -65,8 +64,8 @@ impl PriceApp {
sats: Sats,
) -> Result<UsdCents, PriceAppError> {
let cents = self
.snapshot_cache
.latest_snapshot()
.price_cache
.latest_tick()
.await?
.buy_usd()
.cents_from_sats(sats);
Expand All @@ -79,8 +78,8 @@ impl PriceApp {
sats: Sats,
) -> Result<UsdCents, PriceAppError> {
let cents = self
.snapshot_cache
.latest_snapshot()
.price_cache
.latest_tick()
.await?
.sell_usd()
.cents_from_sats(sats);
Expand All @@ -93,8 +92,8 @@ impl PriceApp {
sats: Sats,
) -> Result<UsdCents, PriceAppError> {
let cents = self
.snapshot_cache
.latest_snapshot()
.price_cache
.latest_tick()
.await?
.buy_usd()
.cents_from_sats(sats);
Expand All @@ -107,8 +106,8 @@ impl PriceApp {
sats: Sats,
) -> Result<UsdCents, PriceAppError> {
let cents = self
.snapshot_cache
.latest_snapshot()
.price_cache
.latest_tick()
.await?
.sell_usd()
.cents_from_sats(sats);
Expand All @@ -121,8 +120,8 @@ impl PriceApp {
cents: UsdCents,
) -> Result<Sats, PriceAppError> {
let sats = self
.snapshot_cache
.latest_snapshot()
.price_cache
.latest_tick()
.await?
.buy_usd()
.sats_from_cents(cents);
Expand All @@ -135,8 +134,8 @@ impl PriceApp {
cents: UsdCents,
) -> Result<Sats, PriceAppError> {
let sats = self
.snapshot_cache
.latest_snapshot()
.price_cache
.latest_tick()
.await?
.sell_usd()
.sats_from_cents(cents);
Expand All @@ -149,8 +148,8 @@ impl PriceApp {
cents: UsdCents,
) -> Result<Sats, PriceAppError> {
let sats = self
.snapshot_cache
.latest_snapshot()
.price_cache
.latest_tick()
.await?
.buy_usd()
.sats_from_cents(cents);
Expand All @@ -163,8 +162,8 @@ impl PriceApp {
cents: UsdCents,
) -> Result<Sats, PriceAppError> {
let sats = self
.snapshot_cache
.latest_snapshot()
.price_cache
.latest_tick()
.await?
.sell_usd()
.sats_from_cents(cents);
Expand All @@ -173,11 +172,7 @@ impl PriceApp {

#[instrument(skip_all, fields(correlation_id), ret, err)]
pub async fn get_cents_per_sat_exchange_mid_rate(&self) -> Result<f64, PriceAppError> {
let cents_per_sat = self
.snapshot_cache
.latest_snapshot()
.await?
.mid_price_of_one_sat()?;
let cents_per_sat = self.price_cache.latest_tick().await?.mid_price_of_one_sat();
Ok(f64::try_from(cents_per_sat)?)
}
}
2 changes: 1 addition & 1 deletion price-server/src/currency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum CurrencyError {

macro_rules! currency {
($name:ident, $code:ident) => {
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct $name {
inner: Money<'static, inner::stablesats::Currency>,
}
Expand Down
4 changes: 0 additions & 4 deletions price-server/src/exchange_price_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ pub enum ExchangePriceCacheError {
}

#[derive(Clone)]
#[allow(dead_code)]
pub struct ExchangePriceCache {
inner: Arc<RwLock<ExchangePriceCacheInner>>,
}

#[allow(dead_code)]
impl ExchangePriceCache {
pub fn new(stale_after: Duration) -> Self {
Self {
Expand All @@ -53,7 +51,6 @@ impl ExchangePriceCache {
}

#[derive(Clone)]
#[allow(dead_code)]
pub struct BtcSatTick {
timestamp: TimeStamp,
correlation_id: CorrelationId,
Expand All @@ -62,7 +59,6 @@ pub struct BtcSatTick {
bid_price_of_one_sat: UsdCents,
}

#[allow(dead_code)]
impl BtcSatTick {
pub fn mid_price_of_one_sat(&self) -> UsdCents {
(&self.bid_price_of_one_sat + &self.ask_price_of_one_sat) / 2
Expand Down
4 changes: 0 additions & 4 deletions price-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ pub mod app;
pub mod currency;
mod exchange_price_cache;
mod fee_calculator;
mod order_book_cache;
mod price_converter;
mod server;

use shared::{health::HealthCheckTrigger, pubsub::PubSubConfig};

use app::PriceApp;
pub use exchange_price_cache::ExchangePriceCacheError;
pub use fee_calculator::FeeCalculatorConfig;
pub use order_book_cache::*;
pub use price_converter::*;
pub use server::*;

pub async fn run(
Expand Down
Loading

0 comments on commit d9a2ead

Please sign in to comment.