Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix network in order event #452

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/app/release.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::cli::settings::Settings;
use crate::db::{self};
use crate::lightning::LndConnector;
use crate::lightning::get_ln_status;
use crate::lnurl::resolv_ln_address;
use crate::util::{
enqueue_order_msg, get_keys, get_nostr_client, get_order, settle_seller_hold_invoice,
Expand Down Expand Up @@ -406,9 +407,13 @@ async fn create_order_event(new_order: &mut Order, my_keys: &Keys) -> Result<Eve
.await
.map_err(|e| MostroInternalErr(ServiceError::DbAccessError(e.to_string())))?;

// Get node status
let ln_status = get_ln_status().await?;

let tags = crate::nip33::order_to_tags(
new_order,
Some((user.total_rating, user.total_reviews, user.created_at)),
&ln_status,
);
let event = crate::nip33::new_event(my_keys, "", new_order.id.to_string(), tags)
.map_err(|e| MostroInternalErr(ServiceError::NostrError(e.to_string())))?;
Expand Down
7 changes: 7 additions & 0 deletions src/lightning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ impl LndConnector {
}
}

pub async fn get_ln_status() -> Result<LnStatus, MostroError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need this function, let's keep using LN_STATUS static

let mut ln_client = LndConnector::new().await?;
let ln_status = ln_client.get_node_info().await?;
let ln_status = LnStatus::from_get_info_response(ln_status);
Ok(ln_status)
}

#[derive(Debug)]
pub struct LnStatus {
pub version: String,
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::lightning::LnStatus;
use anyhow::Result;
use db::find_held_invoices;
use lightning::LndConnector;
use crate::lightning::get_ln_status;
use mostro_core::message::Message;
use nostr_sdk::prelude::*;
use scheduler::start_scheduler;
Expand Down Expand Up @@ -93,9 +94,11 @@ async fn main() -> Result<()> {
// Client subscription
client.subscribe(vec![subscription], None).await?;



let mut ln_client = LndConnector::new().await?;
let ln_status = ln_client.get_node_info().await?;
let ln_status = LnStatus::from_get_info_response(ln_status);
let ln_status = get_ln_status().await?;

if LN_STATUS.set(ln_status).is_err() {
panic!("No connection to LND node - shutting down Mostro!");
};
Expand Down
4 changes: 2 additions & 2 deletions src/nip33.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn create_fiat_amt_array(order: &Order) -> Vec<String> {
///
/// * `order` - The order to transform
///
pub fn order_to_tags(order: &Order, reputation_data: Option<(f64, i64, i64)>) -> Tags {
pub fn order_to_tags(order: &Order, reputation_data: Option<(f64, i64, i64)>, ln_status: &LnStatus) -> Tags {
let mut tags: Vec<Tag> = vec![
Tag::custom(
TagKind::Custom(Cow::Borrowed("k")),
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn order_to_tags(order: &Order, reputation_data: Option<(f64, i64, i64)>) ->
),
Tag::custom(
TagKind::Custom(Cow::Borrowed("network")),
vec!["mainnet".to_string()],
vec![ln_status.networks.join(",")],
),
Tag::custom(
TagKind::Custom(Cow::Borrowed("layer")),
Expand Down
12 changes: 11 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::flow;
use crate::lightning;
use crate::lightning::invoice::is_valid_invoice;
use crate::lightning::LndConnector;
use crate::lightning::get_ln_status;
use crate::messages;
use crate::models::Yadio;
use crate::nip33::{new_event, order_to_tags};
Expand Down Expand Up @@ -203,10 +204,15 @@ pub async fn publish_order(
let user = is_user_present(pool, identity_pubkey.to_string())
.await
.map_err(|e| MostroInternalErr(ServiceError::DbAccessError(e.to_string())))?;

// Get node status
let ln_status = get_ln_status().await?;

// We transform the order fields to tags to use in the event
let tags = order_to_tags(
&new_order_db,
Some((user.total_rating, user.total_reviews, user.created_at)),
&ln_status,
);
// nip33 kind with order fields as tags and order id as identifier
let event = new_event(keys, "", order_id.to_string(), tags)
Expand Down Expand Up @@ -402,8 +408,12 @@ pub async fn update_order_event(
let mut order_updated = order.clone();
// update order.status with new status
order_updated.status = status.to_string();

// Get node status
let ln_status = get_ln_status().await?;

// We transform the order fields to tags to use in the event
let tags = order_to_tags(&order_updated, None);
let tags = order_to_tags(&order_updated, None, &ln_status);
// nip33 kind with order id as identifier and order fields as tags
let event = new_event(keys, "", order.id.to_string(), tags)
.map_err(|e| MostroInternalErr(ServiceError::NostrError(e.to_string())))?;
Expand Down