Skip to content

Commit

Permalink
Add new function to handle bytes to string
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch committed Oct 26, 2023
1 parent ed4992c commit e3cc16d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/app/rate_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,10 @@ pub async fn update_user_reputation_action(
let min_rate = 1;
let max_rate = 5;

if rep.is_none() {
reputation = Rating::new(1, rating as f64, min_rate, max_rate, rating);
} else {
if let Some(r) = rep {
// Update user reputation
// Going on with calculation
reputation = rep.unwrap();
reputation = r;
let old_rating = reputation.total_rating;
let last_rating = reputation.last_rating;
let new_rating =
Expand All @@ -142,6 +140,8 @@ 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);
}

if buyer_rating || seller_rating {
Expand Down
7 changes: 2 additions & 5 deletions src/lightning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::cmp::Ordering;

use crate::cli::settings::Settings;
use crate::lightning::invoice::decode_invoice;
use crate::util::bytes_to_string;

use anyhow::Result;
use easy_hasher::easy_hasher::*;
Expand Down Expand Up @@ -158,11 +159,7 @@ impl LndConnector {
let invoice = decode_invoice(payment_request).unwrap();
let payment_hash = invoice.payment_hash();
let payment_hash = payment_hash.to_vec();
let hash: String = payment_hash
.clone()
.iter()
.map(|b| format!("{:02x}", b))
.collect();
let hash = bytes_to_string(&payment_hash);
let mostro_settings = Settings::get_mostro();

// We need to set a max fee amount
Expand Down
15 changes: 11 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use nostr_sdk::prelude::*;
use sqlx::types::chrono::Utc;
use sqlx::SqlitePool;
use sqlx::{Pool, Sqlite};
use std::fmt::Write;
use std::str::FromStr;
use std::sync::Arc;
use std::thread;
Expand Down Expand Up @@ -295,9 +296,8 @@ pub async fn show_hold_invoice(
if let Some(invoice) = payment_request {
db::edit_buyer_invoice_order(pool, order.id, &invoice).await?;
};
let preimage: String = preimage.iter().map(|b| format!("{:02x}", b)).collect();
let hash_str: String = hash.iter().map(|b| format!("{:02x}", b)).collect();

let preimage = bytes_to_string(&preimage);
let hash_str = bytes_to_string(&hash);
db::edit_order(
pool,
&Status::WaitingPayment,
Expand Down Expand Up @@ -345,7 +345,7 @@ pub async fn show_hold_invoice(
async move {
// Receiving msgs from the invoice subscription.
while let Some(msg) = rx.recv().await {
let hash: String = msg.hash.iter().map(|b| format!("{:02x}", b)).collect();
let hash = bytes_to_string(msg.hash.as_ref());
// If this invoice was paid by the seller
if msg.state == InvoiceState::Accepted {
flow::hold_invoice_paid(&hash).await;
Expand Down Expand Up @@ -488,3 +488,10 @@ pub async fn settle_seller_hold_invoice(

Ok(())
}

pub fn bytes_to_string(bytes: &[u8]) -> String {
bytes.iter().fold(String::new(), |mut output, b| {
let _ = write!(output, "{:02x}", b);
output
})
}

0 comments on commit e3cc16d

Please sign in to comment.