Quote websocket
Instead of just sending a price we properly create a bid quote analogue to the libp2p quote protocol. The service websocket sends `BidQuote`s or an Error. The `BidQuote` sent reuses the struct from the libp2p quote protocol.random-branch
parent
b8f689bda0
commit
dd624b1df9
@ -1,28 +0,0 @@
|
||||
use crate::kraken::PriceUpdates;
|
||||
use anyhow::Context;
|
||||
use futures::{stream, StreamExt, TryStreamExt};
|
||||
use warp::ws::{Message, WebSocket};
|
||||
|
||||
pub async fn latest_rate(ws: WebSocket, subscription: PriceUpdates) {
|
||||
let stream = stream::try_unfold(subscription.inner, |mut receiver| async move {
|
||||
// todo print error message but don't forward it to the user and don't panic
|
||||
receiver
|
||||
.changed()
|
||||
.await
|
||||
.context("failed to receive latest rate update")
|
||||
.expect("Should not fail :)");
|
||||
|
||||
let latest_rate = receiver.borrow().clone().expect("Should work");
|
||||
|
||||
// TODO: Proper definition of what to send over the wire
|
||||
// TODO: Properly calculate the actual rate (using spread) and add min and max
|
||||
// amounts tradeable
|
||||
let msg = Message::text(serde_json::to_string(&latest_rate).expect("to serialize"));
|
||||
|
||||
Ok(Some((msg, receiver)))
|
||||
})
|
||||
.into_stream();
|
||||
|
||||
let (ws_tx, mut _ws_rx) = ws.split();
|
||||
tokio::task::spawn(stream.forward(ws_tx));
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
use crate::asb::Rate;
|
||||
use crate::kraken::PriceUpdates;
|
||||
use crate::network::quote::BidQuote;
|
||||
use anyhow::Context;
|
||||
use futures::{stream, StreamExt, TryStreamExt};
|
||||
use rust_decimal::Decimal;
|
||||
use serde::Serialize;
|
||||
use warp::ws::{Message, WebSocket};
|
||||
use warp::Filter;
|
||||
|
||||
pub async fn setup_quote_websocket(
|
||||
price_updates: PriceUpdates,
|
||||
spread: Decimal,
|
||||
min: bitcoin::Amount,
|
||||
max: bitcoin::Amount,
|
||||
) {
|
||||
let latest_quote = warp::get()
|
||||
.and(warp::path!("api" / "quote" / "xmr-btc"))
|
||||
.and(warp::ws())
|
||||
.map(move |ws: warp::ws::Ws| {
|
||||
let price_updates = price_updates.clone();
|
||||
tracing::info!("New quote websocket connection");
|
||||
ws.on_upgrade(move |socket| {
|
||||
quote_stream(socket, price_updates, spread, min, max)
|
||||
})
|
||||
});
|
||||
tokio::spawn(async move {
|
||||
warp::serve(latest_quote).run(([0, 0, 0, 0], 3030)).await;
|
||||
});
|
||||
}
|
||||
|
||||
async fn quote_stream(
|
||||
ws: WebSocket,
|
||||
subscription: PriceUpdates,
|
||||
spread: Decimal,
|
||||
min: bitcoin::Amount,
|
||||
max: bitcoin::Amount,
|
||||
) {
|
||||
let stream = stream::try_unfold(subscription.inner, move |mut receiver| async move {
|
||||
if let Err(e) = receiver.changed().await {
|
||||
tracing::error!(
|
||||
"Failed to initialize price update stream for quote websocket: {:#}",
|
||||
e
|
||||
);
|
||||
}
|
||||
|
||||
let quote = match receiver.borrow().clone() {
|
||||
Ok(latest_price_update) => match Rate::new(latest_price_update.ask, spread).ask() {
|
||||
Ok(amount) => WebsocketBidQuote::Quote(BidQuote {
|
||||
price: amount,
|
||||
min_quantity: min,
|
||||
max_quantity: max,
|
||||
}),
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to create quote for quote websocket: {:#}", e);
|
||||
WebsocketBidQuote::Error(Error::BidQuoteError)
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
tracing::error!(
|
||||
"Failed to fetch latest price update for quote websocket: {:#}",
|
||||
e
|
||||
);
|
||||
WebsocketBidQuote::Error(Error::PriceUpdateError)
|
||||
}
|
||||
};
|
||||
|
||||
let msg = Message::text(serde_json::to_string("e).expect("quote to serialize"));
|
||||
|
||||
Ok(Some((msg, receiver)))
|
||||
})
|
||||
.into_stream();
|
||||
|
||||
let (ws_tx, mut _ws_rx) = ws.split();
|
||||
tokio::task::spawn(stream.forward(ws_tx));
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
enum WebsocketBidQuote {
|
||||
Quote(BidQuote),
|
||||
Error(Error),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
enum Error {
|
||||
PriceUpdateError,
|
||||
BidQuoteError,
|
||||
}
|
Loading…
Reference in New Issue