diff --git a/swap/src/api/request.rs b/swap/src/api/request.rs index bb7efa70..2279475f 100644 --- a/swap/src/api/request.rs +++ b/swap/src/api/request.rs @@ -655,9 +655,13 @@ impl Request { let peer_id = context.db.get_peer_id(swap_id).await?; let btc_amount = state3.tx_lock.lock_amount(); let xmr_amount = state3.xmr; - let exchange_rate = (Decimal::from_f64(btc_amount.to_btc()).unwrap() - / xmr_amount.as_xmr()) - .round_dp(8); + let exchange_rate = Decimal::from_f64(btc_amount.to_btc()) + .ok_or_else(|| { + anyhow::anyhow!("Failed to convert BTC amount to Decimal") + })? + .checked_div(xmr_amount.as_xmr()) + .ok_or_else(|| anyhow::anyhow!("Division by zero or overflow"))? + .round_dp(8); let exchange_rate_str = format!("{} XMR/BTC", exchange_rate); let swap_data = json!({ diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index 6a8cc829..131fe113 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -267,8 +267,11 @@ async fn main() -> Result<()> { let swap_start_date = db.get_swap_start_date(swap_id).await?; let peer_id = db.get_peer_id(swap_id).await?; - let exchange_rate = - Decimal::from_f64(state3.btc.to_btc()).unwrap() / state3.xmr.as_xmr(); + let exchange_rate = Decimal::from_f64(btc_amount.to_btc()) + .ok_or_else(|| anyhow::anyhow!("Failed to convert BTC amount to Decimal"))? + .checked_div(xmr_amount.as_xmr()) + .ok_or_else(|| anyhow::anyhow!("Division by zero or overflow"))? + .round_dp(8); let exchange_rate = format!("{} XMR/BTC", exchange_rate.round_dp(8)); if json {