From 57e7e0f93b1a1c2911ba367f3ec639eaf11ca518 Mon Sep 17 00:00:00 2001 From: arkanoider <113362043+arkanoider@users.noreply.github.com> Date: Sun, 21 Apr 2024 14:33:40 +0200 Subject: [PATCH] removed other unwraps and minor cosmetics (#272) --- src/app/admin_cancel.rs | 6 +++++- src/app/admin_settle.rs | 9 +++++++-- src/app/admin_take_dispute.rs | 30 +++++++++++++++++++++++++----- src/app/cancel.rs | 30 ++++++++++++++++-------------- src/app/dispute.rs | 16 ++++++++++++---- src/app/fiat_sent.rs | 8 ++++++-- src/app/rate_user.rs | 30 ++++++++++++++++++++---------- src/app/release.rs | 23 +++++++++++++++++++---- src/app/take_buy.rs | 9 +++++++-- src/app/take_sell.rs | 6 +++++- 10 files changed, 122 insertions(+), 45 deletions(-) diff --git a/src/app/admin_cancel.rs b/src/app/admin_cancel.rs index bd0065f..dffedc5 100644 --- a/src/app/admin_cancel.rs +++ b/src/app/admin_cancel.rs @@ -30,7 +30,11 @@ pub async fn admin_cancel_action( return Ok(()); } - let order_id = msg.get_inner_message_kind().id.unwrap(); + let order_id = if let Some(order_id) = msg.get_inner_message_kind().id { + order_id + } else { + return Err(Error::msg("No order id")); + }; match is_assigned_solver(pool, &event.pubkey.to_string(), order_id).await { Ok(false) => { diff --git a/src/app/admin_settle.rs b/src/app/admin_settle.rs index a40e0ae..5d9ec2b 100644 --- a/src/app/admin_settle.rs +++ b/src/app/admin_settle.rs @@ -4,7 +4,7 @@ use crate::nip33::new_event; use crate::util::{send_cant_do_msg, send_dm, settle_seller_hold_invoice, update_order_event}; use crate::NOSTR_CLIENT; -use anyhow::Result; +use anyhow::{Error, Result}; use mostro_core::dispute::Status as DisputeStatus; use mostro_core::message::{Action, Message}; use mostro_core::order::{Order, Status}; @@ -30,7 +30,12 @@ pub async fn admin_settle_action( return Ok(()); } - let order_id = msg.get_inner_message_kind().id.unwrap(); + + let order_id = if let Some(order_id) = msg.get_inner_message_kind().id { + order_id + } else { + return Err(Error::msg("No order id")); + }; match is_assigned_solver(pool, &event.pubkey.to_string(), order_id).await { Ok(false) => { diff --git a/src/app/admin_take_dispute.rs b/src/app/admin_take_dispute.rs index 5569c9d..5feb9e6 100644 --- a/src/app/admin_take_dispute.rs +++ b/src/app/admin_take_dispute.rs @@ -3,13 +3,14 @@ use crate::nip33::new_event; use crate::util::{send_cant_do_msg, send_dm}; use crate::NOSTR_CLIENT; -use anyhow::Result; +use anyhow::{Error, Result}; use mostro_core::dispute::{Dispute, Status}; use mostro_core::message::{Action, Content, Message, Peer}; use mostro_core::order::Order; use nostr_sdk::prelude::*; use sqlx::{Pool, Sqlite}; use sqlx_crud::Crud; +use std::str::FromStr; use tracing::info; pub async fn pubkey_event_can_solve(pool: &Pool, ev_pubkey: &PublicKey) -> bool { @@ -41,7 +42,13 @@ pub async fn admin_take_dispute_action( send_cant_do_msg(None, Some("Not allowed".to_string()), &event.pubkey).await; return Ok(()); } - let dispute_id = msg.get_inner_message_kind().id.unwrap(); + + let dispute_id = if let Some(dispute_id) = msg.get_inner_message_kind().id { + dispute_id + } else { + return Err(Error::msg("No order id")); + }; + let mut dispute = match Dispute::by_id(pool, dispute_id).await? { Some(dispute) => dispute, None => { @@ -55,7 +62,12 @@ pub async fn admin_take_dispute_action( return Ok(()); } }; - let order = Order::by_id(pool, dispute.order_id).await?.unwrap(); + + let order = match Order::by_id(pool, dispute.order_id).await? { + Some(o) => o, + None => return Err(Error::msg("No order id")), + }; + let mut new_order = order.as_new_order(); new_order.master_buyer_pubkey = order.master_buyer_pubkey.clone(); new_order.master_seller_pubkey = order.master_seller_pubkey.clone(); @@ -85,8 +97,16 @@ pub async fn admin_take_dispute_action( Action::AdminTookDispute, Some(Content::Peer(solver_pubkey)), ); - let buyer_pubkey = PublicKey::from_hex(order.buyer_pubkey.clone().unwrap())?; - let seller_pubkey = PublicKey::from_hex(order.seller_pubkey.clone().unwrap())?; + + let (seller_pubkey, buyer_pubkey) = match (&order.seller_pubkey, &order.buyer_pubkey) { + (Some(seller), Some(buyer)) => ( + PublicKey::from_str(seller.as_str())?, + PublicKey::from_str(buyer.as_str())?, + ), + (None, _) => return Err(Error::msg("Missing seller pubkey")), + (_, None) => return Err(Error::msg("Missing buyer pubkey")), + }; + let message = message.as_json()?; send_dm(&buyer_pubkey, message.clone()).await?; send_dm(&seller_pubkey, message).await?; diff --git a/src/app/cancel.rs b/src/app/cancel.rs index 24b2f91..714ec25 100644 --- a/src/app/cancel.rs +++ b/src/app/cancel.rs @@ -17,7 +17,11 @@ pub async fn cancel_action( pool: &Pool, ln_client: &mut LndConnector, ) -> Result<()> { - let order_id = msg.get_inner_message_kind().id.unwrap(); + let order_id = if let Some(order_id) = msg.get_inner_message_kind().id { + order_id + } else { + return Err(Error::msg("No order id")); + }; let mut order = match Order::by_id(pool, order_id).await? { Some(order) => order, None => { @@ -89,9 +93,8 @@ pub async fn cancel_action( send_cant_do_msg(Some(order_id), None, &event.pubkey).await; return Ok(()); } else { - if order.hash.is_some() { + if let Some(hash) = &order.hash { // We return funds to seller - let hash = order.hash.as_ref().unwrap(); ln_client.cancel_hold_invoice(hash).await?; info!( "Cooperative cancel: Order Id {}: Funds returned to seller", @@ -154,12 +157,13 @@ pub async fn cancel_add_invoice( pool: &Pool, my_keys: &Keys, ) -> Result<()> { - if order.hash.is_some() { - // We return funds to seller - let hash = order.hash.as_ref().unwrap(); + if let Some(hash) = &order.hash { ln_client.cancel_hold_invoice(hash).await?; info!("Order Id {}: Funds returned to seller", &order.id); + } else { + return Err(Error::msg("No hash present")); } + let user_pubkey = event.pubkey.to_string(); let (seller_pubkey, buyer_pubkey) = match (&order.seller_pubkey, &order.buyer_pubkey) { @@ -214,15 +218,13 @@ pub async fn cancel_pay_hold_invoice( info!("Order Id {}: Funds returned to seller", &order.id); } let user_pubkey = event.pubkey.to_string(); - let buyer_pubkey = order.buyer_pubkey.as_ref().unwrap(); - let seller_pubkey = order.seller_pubkey.as_ref().unwrap(); - let seller_pubkey = match PublicKey::from_str(seller_pubkey) { - Ok(pk) => pk, - Err(e) => { - error!("Error parsing seller pubkey: {:#?}", e); - return Ok(()); - } + + let (seller_pubkey, buyer_pubkey) = match (&order.seller_pubkey, &order.buyer_pubkey) { + (Some(seller), Some(buyer)) => (PublicKey::from_str(seller.as_str())?, buyer), + (None, _) => return Err(Error::msg("Missing seller pubkey")), + (_, None) => return Err(Error::msg("Missing buyer pubkey")), }; + if seller_pubkey.to_string() != user_pubkey { // We create a Message send_cant_do_msg(Some(order.id), None, &event.pubkey).await; diff --git a/src/app/dispute.rs b/src/app/dispute.rs index b70d74c..e11a90b 100644 --- a/src/app/dispute.rs +++ b/src/app/dispute.rs @@ -5,7 +5,7 @@ use crate::nip33::new_event; use crate::util::{send_cant_do_msg, send_new_order_msg}; use crate::NOSTR_CLIENT; -use anyhow::Result; +use anyhow::{Error, Result}; use mostro_core::dispute::Dispute; use mostro_core::message::{Action, Message}; use mostro_core::order::{Order, Status}; @@ -20,7 +20,11 @@ pub async fn dispute_action( my_keys: &Keys, pool: &Pool, ) -> Result<()> { - let order_id = msg.get_inner_message_kind().id.unwrap(); + let order_id = if let Some(order_id) = msg.get_inner_message_kind().id { + order_id + } else { + return Err(Error::msg("No order id")); + }; // Check dispute for this order id is yet present. if find_dispute_by_order_id(pool, order_id).await.is_ok() { @@ -52,8 +56,12 @@ pub async fn dispute_action( } }; - let buyer = order.buyer_pubkey.clone().unwrap(); - let seller = order.seller_pubkey.clone().unwrap(); + let (seller, buyer) = match (&order.seller_pubkey, &order.buyer_pubkey) { + (Some(seller), Some(buyer)) => (seller.to_owned(), buyer.to_owned()), + (None, _) => return Err(Error::msg("Missing seller pubkey")), + (_, None) => return Err(Error::msg("Missing buyer pubkey")), + }; + let message_sender = event.pubkey.to_string(); // Get counterpart pubkey let mut counterpart: String = String::new(); diff --git a/src/app/fiat_sent.rs b/src/app/fiat_sent.rs index 736f97a..4e225d9 100644 --- a/src/app/fiat_sent.rs +++ b/src/app/fiat_sent.rs @@ -1,6 +1,6 @@ use crate::util::{send_cant_do_msg, send_new_order_msg, update_order_event}; -use anyhow::Result; +use anyhow::{Error, Result}; use mostro_core::message::{Action, Content, Message, Peer}; use mostro_core::order::{Order, Status}; use nostr_sdk::prelude::*; @@ -15,7 +15,11 @@ pub async fn fiat_sent_action( my_keys: &Keys, pool: &Pool, ) -> Result<()> { - let order_id = msg.get_inner_message_kind().id.unwrap(); + let order_id = if let Some(order_id) = msg.get_inner_message_kind().id { + order_id + } else { + return Err(Error::msg("No order id")); + }; let order = match Order::by_id(pool, order_id).await? { Some(order) => order, None => { diff --git a/src/app/rate_user.rs b/src/app/rate_user.rs index c871fef..fa60c96 100644 --- a/src/app/rate_user.rs +++ b/src/app/rate_user.rs @@ -3,7 +3,7 @@ use crate::util::{ }; use crate::NOSTR_CLIENT; -use anyhow::Result; +use anyhow::{Error, Result}; use mostro_core::message::{Action, Content, Message}; use mostro_core::order::{Order, Status}; use mostro_core::rating::Rating; @@ -16,6 +16,9 @@ use std::time::Duration; use tokio::sync::Mutex; use tracing::error; +const MAX_RATING: u8 = 5; +const MIN_RATING: u8 = 1; + pub async fn get_counterpart_reputation(user: &str, my_keys: &Keys) -> Result> { // Request NIP33 of the counterparts let filters = Filter::new() @@ -50,7 +53,11 @@ pub async fn update_user_reputation_action( pool: &Pool, rate_list: Arc>>, ) -> Result<()> { - let order_id = msg.get_inner_message_kind().id.unwrap(); + let order_id = if let Some(order_id) = msg.get_inner_message_kind().id { + order_id + } else { + return Err(Error::msg("No order id")); + }; let order = match Order::by_id(pool, order_id).await? { Some(order) => order, None => { @@ -60,8 +67,12 @@ pub async fn update_user_reputation_action( }; // Get needed info about users - let buyer = order.buyer_pubkey.unwrap(); - let seller = order.seller_pubkey.unwrap(); + let (seller, buyer) = match (&order.seller_pubkey, &order.buyer_pubkey) { + (Some(seller), Some(buyer)) => (seller.to_owned(), buyer.to_owned()), + (None, _) => return Err(Error::msg("Missing seller pubkey")), + (_, None) => return Err(Error::msg("Missing buyer pubkey")), + }; + let message_sender = event.pubkey.to_string(); if order.status != Status::Success.to_string() { @@ -104,19 +115,18 @@ pub async fn update_user_reputation_action( }; // Check if content of Peer is the same of counterpart - let mut rating = 0_u8; + let rating; - if let Content::RatingUser(v) = msg.get_inner_message_kind().content.to_owned().unwrap() { + if let Some(Content::RatingUser(v)) = msg.get_inner_message_kind().content.to_owned() { rating = v; + } else { + return Err(Error::msg("No rating present")); } // Ask counterpart reputation let rep = get_counterpart_reputation(&counterpart, my_keys).await?; // Here we have to update values of the review of the counterpart let mut reputation; - // min_rate is 1 and max_rate is 5 - let min_rate = 1; - let max_rate = 5; if let Some(r) = rep { // Update user reputation @@ -135,7 +145,7 @@ pub async fn update_user_reputation_action( // Assing new total rating to review reputation.total_rating = new_rating; } else { - reputation = Rating::new(1, rating as f64, min_rate, max_rate, rating); + reputation = Rating::new(1, rating as f64, MIN_RATING, MAX_RATING, rating); } let reputation = reputation.to_tags()?; diff --git a/src/app/release.rs b/src/app/release.rs index a7b1b94..12865f1 100644 --- a/src/app/release.rs +++ b/src/app/release.rs @@ -38,9 +38,13 @@ pub async fn check_failure_retries(order: &Order) -> Result { order.payment_attempts += 1; } let msg = format!("I tried to send you the sats but the payment of your invoice failed, I will try {} more times in {} minutes window, please check your node/wallet is online",retries_number,time_window); - let buyer_key = PublicKey::from_str(order.buyer_pubkey.as_ref().unwrap()).unwrap(); - send_cant_do_msg(Some(order.id), Some(msg), &buyer_key).await; + let buyer_pubkey = match &order.buyer_pubkey { + Some(buyer) => PublicKey::from_str(buyer.as_str())?, + None => return Err(Error::msg("Missing buyer pubkey")), + }; + + send_cant_do_msg(Some(order.id), Some(msg), &buyer_pubkey).await; // Update order let result = order.update(&pool).await?; @@ -54,7 +58,13 @@ pub async fn release_action( pool: &Pool, ln_client: &mut LndConnector, ) -> Result<()> { - let order_id = msg.get_inner_message_kind().id.unwrap(); + // Check if order id is ok + let order_id = if let Some(order_id) = msg.get_inner_message_kind().id { + order_id + } else { + return Err(Error::msg("No order id")); + }; + let order = match Order::by_id(pool, order_id).await? { Some(order) => order, None => { @@ -71,7 +81,12 @@ pub async fn release_action( }; let seller_pubkey = event.pubkey; - let current_status = Status::from_str(&order.status).unwrap(); + let current_status = if let Ok(current_status) = Status::from_str(&order.status) { + current_status + } else { + return Err(Error::msg("Wrong order status")); + }; + if current_status != Status::Active && current_status != Status::FiatSent && current_status != Status::Dispute diff --git a/src/app/take_buy.rs b/src/app/take_buy.rs index d073046..42d0dc7 100644 --- a/src/app/take_buy.rs +++ b/src/app/take_buy.rs @@ -2,7 +2,7 @@ use crate::util::{ get_market_amount_and_fee, send_cant_do_msg, send_new_order_msg, show_hold_invoice, }; -use anyhow::Result; +use anyhow::{Error, Result}; use mostro_core::message::{Action, Content, Message}; use mostro_core::order::{Kind, Order, Status}; use nostr_sdk::prelude::*; @@ -18,7 +18,12 @@ pub async fn take_buy_action( pool: &Pool, ) -> Result<()> { // Safe unwrap as we verified the message - let order_id = msg.get_inner_message_kind().id.unwrap(); + let order_id = if let Some(order_id) = msg.get_inner_message_kind().id { + order_id + } else { + return Err(Error::msg("No order id")); + }; + let mut order = match Order::by_id(pool, order_id).await? { Some(order) => order, None => { diff --git a/src/app/take_sell.rs b/src/app/take_sell.rs index 66c223b..9d99d51 100644 --- a/src/app/take_sell.rs +++ b/src/app/take_sell.rs @@ -20,7 +20,11 @@ pub async fn take_sell_action( pool: &Pool, ) -> Result<()> { // Safe unwrap as we verified the message - let order_id = msg.get_inner_message_kind().id.unwrap(); + let order_id = if let Some(order_id) = msg.get_inner_message_kind().id { + order_id + } else { + return Err(Error::msg("No order id")); + }; let mut order = match Order::by_id(pool, order_id).await? { Some(order) => order,