mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 05:45:40 +00:00
Make kraken
module emit PriceUpdate
s instead of Rate
s
This commit is contained in:
parent
cfc530e8ab
commit
654cfff2a8
@ -92,7 +92,7 @@ async fn main() -> Result<()> {
|
|||||||
info!("Monero balance: {}", monero_balance);
|
info!("Monero balance: {}", monero_balance);
|
||||||
}
|
}
|
||||||
|
|
||||||
let kraken_rate_updates = kraken::connect()?;
|
let kraken_price_updates = kraken::connect()?;
|
||||||
|
|
||||||
let mut swarm = swarm::new::<Behaviour>(&seed)?;
|
let mut swarm = swarm::new::<Behaviour>(&seed)?;
|
||||||
Swarm::listen_on(&mut swarm, config.network.listen)
|
Swarm::listen_on(&mut swarm, config.network.listen)
|
||||||
@ -104,7 +104,7 @@ async fn main() -> Result<()> {
|
|||||||
Arc::new(bitcoin_wallet),
|
Arc::new(bitcoin_wallet),
|
||||||
Arc::new(monero_wallet),
|
Arc::new(monero_wallet),
|
||||||
Arc::new(db),
|
Arc::new(db),
|
||||||
kraken_rate_updates,
|
kraken_price_updates,
|
||||||
max_buy,
|
max_buy,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -9,8 +9,8 @@ async fn main() -> Result<()> {
|
|||||||
let mut ticker = swap::kraken::connect().context("Failed to connect to kraken")?;
|
let mut ticker = swap::kraken::connect().context("Failed to connect to kraken")?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match ticker.wait_for_update().await? {
|
match ticker.wait_for_next_update().await? {
|
||||||
Ok(rate) => println!("Rate update: {}", rate),
|
Ok(update) => println!("Price update: {}", update.ask),
|
||||||
Err(e) => println!("Error: {:#}", e),
|
Err(e) => println!("Error: {:#}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use crate::asb::Rate;
|
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use futures::{SinkExt, StreamExt, TryStreamExt};
|
use futures::{SinkExt, StreamExt, TryStreamExt};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -10,9 +9,9 @@ use tokio::sync::watch;
|
|||||||
/// Connect to Kraken websocket API for a constant stream of rate updates.
|
/// Connect to Kraken websocket API for a constant stream of rate updates.
|
||||||
///
|
///
|
||||||
/// If the connection fails, it will automatically be re-established.
|
/// If the connection fails, it will automatically be re-established.
|
||||||
pub fn connect() -> Result<RateUpdateStream> {
|
pub fn connect() -> Result<PriceUpdates> {
|
||||||
let (rate_update, rate_update_receiver) = watch::channel(Err(Error::NotYetAvailable));
|
let (price_update, price_update_receiver) = watch::channel(Err(Error::NotYetAvailable));
|
||||||
let rate_update = Arc::new(rate_update);
|
let price_update = Arc::new(price_update);
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
// The default backoff config is fine for us apart from one thing:
|
// The default backoff config is fine for us apart from one thing:
|
||||||
@ -26,12 +25,12 @@ pub fn connect() -> Result<RateUpdateStream> {
|
|||||||
let result = backoff::future::retry_notify::<Infallible, _, _, _, _, _>(
|
let result = backoff::future::retry_notify::<Infallible, _, _, _, _, _>(
|
||||||
backoff,
|
backoff,
|
||||||
|| {
|
|| {
|
||||||
let rate_update = rate_update.clone();
|
let price_update = price_update.clone();
|
||||||
async move {
|
async move {
|
||||||
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(Rate::new(update.ask)));
|
let send_result = price_update.send(Ok(update));
|
||||||
|
|
||||||
if send_result.is_err() {
|
if send_result.is_err() {
|
||||||
return Err(backoff::Error::Permanent(anyhow!(
|
return Err(backoff::Error::Permanent(anyhow!(
|
||||||
@ -54,30 +53,30 @@ pub fn connect() -> Result<RateUpdateStream> {
|
|||||||
tracing::warn!("Rate updates incurred an unrecoverable error: {:#}", e);
|
tracing::warn!("Rate updates incurred an unrecoverable error: {:#}", e);
|
||||||
|
|
||||||
// in case the retries fail permanently, let the subscribers know
|
// in case the retries fail permanently, let the subscribers know
|
||||||
rate_update.send(Err(Error::PermanentFailure))
|
price_update.send(Err(Error::PermanentFailure))
|
||||||
}
|
}
|
||||||
Ok(never) => match never {},
|
Ok(never) => match never {},
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(RateUpdateStream {
|
Ok(PriceUpdates {
|
||||||
inner: rate_update_receiver,
|
inner: price_update_receiver,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct RateUpdateStream {
|
pub struct PriceUpdates {
|
||||||
inner: watch::Receiver<RateUpdate>,
|
inner: watch::Receiver<PriceUpdate>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RateUpdateStream {
|
impl PriceUpdates {
|
||||||
pub async fn wait_for_update(&mut self) -> Result<RateUpdate> {
|
pub async fn wait_for_next_update(&mut self) -> Result<PriceUpdate> {
|
||||||
self.inner.changed().await?;
|
self.inner.changed().await?;
|
||||||
|
|
||||||
Ok(self.inner.borrow().clone())
|
Ok(self.inner.borrow().clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn latest_update(&mut self) -> RateUpdate {
|
pub fn latest_update(&mut self) -> PriceUpdate {
|
||||||
self.inner.borrow().clone()
|
self.inner.borrow().clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,7 +89,7 @@ pub enum Error {
|
|||||||
PermanentFailure,
|
PermanentFailure,
|
||||||
}
|
}
|
||||||
|
|
||||||
type RateUpdate = Result<Rate, Error>;
|
type PriceUpdate = Result<wire::PriceUpdate, Error>;
|
||||||
|
|
||||||
/// Maps a [`connection::Error`] to a backoff error, effectively defining our
|
/// Maps a [`connection::Error`] to a backoff error, effectively defining our
|
||||||
/// retry strategy.
|
/// retry strategy.
|
||||||
@ -246,7 +245,7 @@ mod wire {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Represents an update within the price ticker.
|
/// Represents an update within the price ticker.
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
#[serde(try_from = "TickerUpdate")]
|
#[serde(try_from = "TickerUpdate")]
|
||||||
pub struct PriceUpdate {
|
pub struct PriceUpdate {
|
||||||
pub ask: bitcoin::Amount,
|
pub ask: bitcoin::Amount,
|
||||||
|
@ -354,11 +354,14 @@ impl LatestRate for FixedRate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LatestRate for kraken::RateUpdateStream {
|
impl LatestRate for kraken::PriceUpdates {
|
||||||
type Error = kraken::Error;
|
type Error = kraken::Error;
|
||||||
|
|
||||||
fn latest_rate(&mut self) -> Result<Rate, Self::Error> {
|
fn latest_rate(&mut self) -> Result<Rate, Self::Error> {
|
||||||
self.latest_update()
|
let update = self.latest_update()?;
|
||||||
|
let rate = Rate::new(update.ask);
|
||||||
|
|
||||||
|
Ok(rate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user