From 16528acc01204ecef4cc923df97cdcd83f5b6f9c Mon Sep 17 00:00:00 2001 From: "kody.low" Date: Sun, 17 Sep 2023 22:36:24 -0700 Subject: [PATCH] refactor: helper func and remove unnecessary stack variable --- moksha-mint/src/lightning.rs | 23 +++++++++++++---------- moksha-mint/src/strike.rs | 10 ++-------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/moksha-mint/src/lightning.rs b/moksha-mint/src/lightning.rs index 4bf51008..1f376f0a 100644 --- a/moksha-mint/src/lightning.rs +++ b/moksha-mint/src/lightning.rs @@ -245,16 +245,7 @@ impl Lightning for StrikeLightning { .0; // invoiceId is the first 32 bytes of the description hash - let invoice_id = hex::encode(&description_hash[..16]); - // then convert it to uuid format with hypens - let invoice_id = format!( - "{}-{}-{}-{}-{}", - &invoice_id[..8], - &invoice_id[8..12], - &invoice_id[12..16], - &invoice_id[16..20], - &invoice_id[20..] - ); + let invoice_id = format_as_uuid_string(&description_hash[..16]); Ok(self.client.is_invoice_paid(&invoice_id).await?) } @@ -316,6 +307,18 @@ impl Lightning for StrikeLightning { } } +fn format_as_uuid_string(bytes: &[u8]) -> String { + let byte_str = hex::encode(bytes); + format!( + "{}-{}-{}-{}-{}", + &byte_str[..8], + &byte_str[8..12], + &byte_str[12..16], + &byte_str[16..20], + &byte_str[20..] + ) +} + fn deserialize_url<'de, D>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, diff --git a/moksha-mint/src/strike.rs b/moksha-mint/src/strike.rs index d32df571..d847f3ce 100644 --- a/moksha-mint/src/strike.rs +++ b/moksha-mint/src/strike.rs @@ -196,19 +196,13 @@ impl StrikeClient { .await?; let response: serde_json::Value = serde_json::from_str(&body)?; - let state = response["state"].as_str().unwrap_or(""); - let is_paid = matches!(state, "COMPLETED"); - - Ok(is_paid) + Ok(response["state"].as_str().unwrap_or("") == "COMPLETED") } pub async fn is_invoice_paid(&self, invoice_id: &str) -> Result { let body = self.make_get(&format!("invoices/{invoice_id}")).await?; let response = serde_json::from_str::(&body)?; - let state = response["state"].as_str().unwrap_or(""); - let is_paid = matches!(state, "PAID"); - - Ok(is_paid) + Ok(response["state"].as_str().unwrap_or("") == "PAID") } }