Make LatestRate::Error require std::error::Error trait bound

This allows us to use .context instead of .map_err when calling
`latest_rate()`. For the static rate module, we simply fill in
`Infallible` which is actually better suited because it describes
that we are never using this error.
This commit is contained in:
Thomas Eizinger 2021-02-19 17:04:42 +11:00 committed by Daniel Karzel
parent b47b06aa23
commit a8bfc1d686
3 changed files with 6 additions and 6 deletions

View File

@ -8,7 +8,7 @@ mod amounts;
pub use amounts::Rate;
pub trait LatestRate {
type Error: std::fmt::Debug;
type Error: std::error::Error + Send + Sync + 'static;
fn latest_rate(&mut self) -> Result<Rate, Self::Error>;
}

View File

@ -1,5 +1,5 @@
use crate::asb::{LatestRate, Rate};
use anyhow::Result;
use std::convert::Infallible;
pub const RATE: f64 = 0.01;
@ -7,9 +7,9 @@ pub const RATE: f64 = 0.01;
pub struct RateService(Rate);
impl LatestRate for RateService {
type Error = anyhow::Error;
type Error = Infallible;
fn latest_rate(&mut self) -> Result<Rate> {
fn latest_rate(&mut self) -> Result<Rate, Infallible> {
Ok(self.0)
}
}

View File

@ -14,7 +14,7 @@ use crate::{
},
seed::Seed,
};
use anyhow::{anyhow, Context, Result};
use anyhow::{Context, Result};
use futures::future::RemoteHandle;
use libp2p::{
core::Multiaddr, futures::FutureExt, request_response::ResponseChannel, PeerId, Swarm,
@ -208,7 +208,7 @@ where
let rate = self
.rate_service
.latest_rate()
.map_err(|e| anyhow!("Failed to get latest rate: {:?}", e))?;
.context("Failed to get latest rate")?;
let btc_amount = quote_request.btc_amount;
let xmr_amount = rate.sell_quote(btc_amount)?;