mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Make ask
field of Rate
private
This commit is contained in:
parent
bc46d95985
commit
cfc530e8ab
@ -13,8 +13,8 @@ impl FixedRate {
|
|||||||
|
|
||||||
impl Default for FixedRate {
|
impl Default for FixedRate {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self(Rate {
|
let ask = bitcoin::Amount::from_btc(Self::RATE).expect("Static value should never fail");
|
||||||
ask: bitcoin::Amount::from_btc(Self::RATE).expect("Static value should never fail"),
|
|
||||||
})
|
Self(Rate::new(ask))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ use std::fmt::{Debug, Display, Formatter};
|
|||||||
/// sell 1 XMR.
|
/// sell 1 XMR.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct Rate {
|
pub struct Rate {
|
||||||
pub ask: bitcoin::Amount,
|
ask: bitcoin::Amount,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rate {
|
impl Rate {
|
||||||
@ -17,6 +17,14 @@ impl Rate {
|
|||||||
ask: bitcoin::Amount::ZERO,
|
ask: bitcoin::Amount::ZERO,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub fn new(ask: bitcoin::Amount) -> Self {
|
||||||
|
Self { ask }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ask(&self) -> bitcoin::Amount {
|
||||||
|
self.ask
|
||||||
|
}
|
||||||
|
|
||||||
// This function takes the quote amount as it is what Bob sends to Alice in the
|
// This function takes the quote amount as it is what Bob sends to Alice in the
|
||||||
// swap request
|
// swap request
|
||||||
pub fn sell_quote(&self, quote: bitcoin::Amount) -> Result<monero::Amount> {
|
pub fn sell_quote(&self, quote: bitcoin::Amount) -> Result<monero::Amount> {
|
||||||
|
@ -31,7 +31,7 @@ pub fn connect() -> Result<RateUpdateStream> {
|
|||||||
let mut stream = connection::new().await?;
|
let mut stream = connection::new().await?;
|
||||||
|
|
||||||
while let Some(update) = stream.try_next().await.map_err(to_backoff)? {
|
while let Some(update) = stream.try_next().await.map_err(to_backoff)? {
|
||||||
let send_result = rate_update.send(Ok(update));
|
let send_result = rate_update.send(Ok(Rate::new(update.ask)));
|
||||||
|
|
||||||
if send_result.is_err() {
|
if send_result.is_err() {
|
||||||
return Err(backoff::Error::Permanent(anyhow!(
|
return Err(backoff::Error::Permanent(anyhow!(
|
||||||
@ -120,7 +120,7 @@ mod connection {
|
|||||||
use futures::stream::BoxStream;
|
use futures::stream::BoxStream;
|
||||||
use tokio_tungstenite::tungstenite;
|
use tokio_tungstenite::tungstenite;
|
||||||
|
|
||||||
pub async fn new() -> Result<BoxStream<'static, Result<Rate, Error>>> {
|
pub async fn new() -> Result<BoxStream<'static, Result<wire::PriceUpdate, Error>>> {
|
||||||
let (mut rate_stream, _) = tokio_tungstenite::connect_async("wss://ws.kraken.com")
|
let (mut rate_stream, _) = tokio_tungstenite::connect_async("wss://ws.kraken.com")
|
||||||
.await
|
.await
|
||||||
.context("Failed to connect to Kraken websocket API")?;
|
.context("Failed to connect to Kraken websocket API")?;
|
||||||
@ -134,12 +134,12 @@ mod connection {
|
|||||||
Ok(stream)
|
Ok(stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a websocket message into a [`Rate`].
|
/// Parse a websocket message into a [`wire::PriceUpdate`].
|
||||||
///
|
///
|
||||||
/// Messages which are not actually ticker updates are ignored and result in
|
/// Messages which are not actually ticker updates are ignored and result in
|
||||||
/// `None` being returned. In the context of a [`TryStream`], these will
|
/// `None` being returned. In the context of a [`TryStream`], these will
|
||||||
/// simply be filtered out.
|
/// simply be filtered out.
|
||||||
async fn parse_message(msg: tungstenite::Message) -> Result<Option<Rate>, Error> {
|
async fn parse_message(msg: tungstenite::Message) -> Result<Option<wire::PriceUpdate>, Error> {
|
||||||
let msg = match msg {
|
let msg = match msg {
|
||||||
tungstenite::Message::Text(msg) => msg,
|
tungstenite::Message::Text(msg) => msg,
|
||||||
tungstenite::Message::Close(close_frame) => {
|
tungstenite::Message::Close(close_frame) => {
|
||||||
@ -182,7 +182,7 @@ mod connection {
|
|||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
// if the message is not an event, it is a ticker update or an unknown event
|
// if the message is not an event, it is a ticker update or an unknown event
|
||||||
Err(_) => match serde_json::from_str::<wire::TickerUpdate>(&msg) {
|
Err(_) => match serde_json::from_str::<wire::PriceUpdate>(&msg) {
|
||||||
Ok(ticker) => ticker,
|
Ok(ticker) => ticker,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::warn!(%e, "Failed to deserialize message '{}' as ticker update", msg);
|
tracing::warn!(%e, "Failed to deserialize message '{}' as ticker update", msg);
|
||||||
@ -192,8 +192,6 @@ mod connection {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let update = Rate::try_from(update)?;
|
|
||||||
|
|
||||||
Ok(Some(update))
|
Ok(Some(update))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,6 +245,13 @@ mod wire {
|
|||||||
BitcoinParseAmount(#[from] ParseAmountError),
|
BitcoinParseAmount(#[from] ParseAmountError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents an update within the price ticker.
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(try_from = "TickerUpdate")]
|
||||||
|
pub struct PriceUpdate {
|
||||||
|
pub ask: bitcoin::Amount,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
#[serde(transparent)]
|
#[serde(transparent)]
|
||||||
pub struct TickerUpdate(Vec<TickerField>);
|
pub struct TickerUpdate(Vec<TickerField>);
|
||||||
@ -273,7 +278,7 @@ mod wire {
|
|||||||
Number(u64),
|
Number(u64),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<TickerUpdate> for Rate {
|
impl TryFrom<TickerUpdate> for PriceUpdate {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn try_from(value: TickerUpdate) -> Result<Self, Error> {
|
fn try_from(value: TickerUpdate) -> Result<Self, Error> {
|
||||||
@ -293,7 +298,7 @@ mod wire {
|
|||||||
_ => return Err(Error::UnexpectedAskRateElementType),
|
_ => return Err(Error::UnexpectedAskRateElementType),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Self { ask })
|
Ok(PriceUpdate { ask })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,7 +278,7 @@ where
|
|||||||
.context("Failed to get latest rate")?;
|
.context("Failed to get latest rate")?;
|
||||||
|
|
||||||
Ok(BidQuote {
|
Ok(BidQuote {
|
||||||
price: rate.ask,
|
price: rate.ask(),
|
||||||
max_quantity: max_buy,
|
max_quantity: max_buy,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user