Skip to content

Commit

Permalink
Info mostro message (#279)
Browse files Browse the repository at this point in the history
* first idea of mostro message on request

* info message schedule every 5 minutes

* cargo.toml fix

* cargo.lock fix

* fix on line 161 od mod.rs - check if ok

* removed import

* Update Cargo.toml

* fix cargo.lock

* Added commit hash and mostro version in info message

* typo on git commit info
  • Loading branch information
arkanoider authored May 4, 2024
1 parent 1092fbc commit d84bb0a
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 10 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
config = "0.14.0"
clap = { version = "4.5.3", features = ["derive"] }
lnurl-rs = "0.5.0"
openssl = { version = "0.10", features = ["vendored"] }
openssl = { version = "0.10", features = ["vendored"] }
11 changes: 11 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::process::Command;
fn main() {
// note: add error checking yourself.
println!("cargo:rerun-if-changed=.git/refs/head/main");
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.unwrap();
let git_hash = String::from_utf8(output.stdout).unwrap();
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
}
4 changes: 1 addition & 3 deletions src/lightning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use anyhow::Result;
use easy_hasher::easy_hasher::*;
use nostr_sdk::nostr::hashes::hex::FromHex;
use nostr_sdk::nostr::secp256k1::rand::{self, RngCore};
use nostr_sdk::secp256k1::hashes::Hash;
use tokio::sync::mpsc::Sender;
use tonic_openssl_lnd::invoicesrpc::{
AddHoldInvoiceRequest, AddHoldInvoiceResp, CancelInvoiceMsg, CancelInvoiceResp,
Expand Down Expand Up @@ -159,8 +158,7 @@ impl LndConnector {
listener: Sender<PaymentMessage>,
) -> Result<(), MostroError> {
let invoice = decode_invoice(payment_request)?;
let payment_hash = invoice.payment_hash();
let payment_hash = payment_hash.to_byte_array();
let payment_hash = invoice.signable_hash();
let hash = bytes_to_string(&payment_hash);
let mostro_settings = Settings::get_mostro();

Expand Down
66 changes: 66 additions & 0 deletions src/nip33.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::Settings;
use chrono::Duration;
use mostro_core::order::Order;
use mostro_core::NOSTR_REPLACEABLE_EVENT_KIND;
Expand Down Expand Up @@ -68,3 +69,68 @@ pub fn order_to_tags(order: &Order) -> Vec<(String, String)> {

tags
}

/// Transform mostro info fields to tags
///
/// # Arguments
///
///
pub fn info_to_tags(mostro_pubkey: &PublicKey) -> Vec<(String, String)> {
let mostro_settings = Settings::get_mostro();
let ln_settings = Settings::get_ln();

let tags = vec![
// max_order_amount
("mostro_pubkey".to_string(), mostro_pubkey.to_string()),
// mostro version
(
"mostro_version".to_string(),
env!("CARGO_PKG_VERSION").to_string(),
),
// mostro commit id
("mostro_commit_id".to_string(), env!("GIT_HASH").to_string()),
// max_order_amount
(
"max_order_amount".to_string(),
mostro_settings.max_order_amount.to_string(),
),
// min_order_amount
(
"min_order_amount".to_string(),
mostro_settings.min_payment_amount.to_string(),
),
// expiration_hours
(
"expiration_hours".to_string(),
mostro_settings.expiration_hours.to_string(),
),
// expiration_seconds
(
"expiration_seconds".to_string(),
mostro_settings.expiration_seconds.to_string(),
),
// fee
("fee".to_string(), mostro_settings.fee.to_string()),
// hold_invoice_expiration_window
(
"hold_invoice_expiration_window".to_string(),
ln_settings.hold_invoice_expiration_window.to_string(),
),
// hold_invoice_cltv_delta
(
"hold_invoice_cltv_delta".to_string(),
ln_settings.hold_invoice_cltv_delta.to_string(),
),
// invoice_expiration_window
(
"invoice_expiration_window".to_string(),
ln_settings.hold_invoice_expiration_window.to_string(),
),
// Label to identify this is a Mostro's infos
("y".to_string(), "mostrop2p".to_string()),
// Table name
("z".to_string(), "info".to_string()),
];

tags
}
22 changes: 22 additions & 0 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,32 @@ pub async fn start_scheduler(rate_list: Arc<Mutex<Vec<Event>>>) {
job_update_rate_events(rate_list).await;
job_cancel_orders().await;
job_retry_failed_payments().await;
job_info_event_send().await;

info!("Scheduler Started");
}

async fn job_info_event_send() {
let mostro_pubkey = match get_keys() {
Ok(keys) => keys,
Err(e) => return error!("{e}"),
};

tokio::spawn(async move {
loop {
info!("Sending info about mostro");

let tags = crate::nip33::info_to_tags(&mostro_pubkey.public_key());
let id = format!("info-{}", mostro_pubkey.public_key());

let info_ev = crate::nip33::new_event(&mostro_pubkey, "", id, tags).unwrap();
let _ = NOSTR_CLIENT.get().unwrap().send_event(info_ev).await;

tokio::time::sleep(tokio::time::Duration::from_secs(300)).await;
}
});
}

async fn job_retry_failed_payments() {
let ln_settings = Settings::get_ln();
let retries_number = ln_settings.payment_attempts as i64;
Expand Down

0 comments on commit d84bb0a

Please sign in to comment.