Skip to content

Commit

Permalink
compiling with new lnd crate
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider committed Oct 7, 2024
1 parent b3f3e60 commit c66c34e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/app/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::util::{
use crate::NOSTR_CLIENT;

use anyhow::{Error, Result};
use fedimint_tonic_lnd::lnrpc::payment::PaymentStatus;
use lnurl::lightning_address::LightningAddress;
use mostro_core::message::{Action, Message};
use mostro_core::order::{Order, Status};
Expand All @@ -19,7 +20,6 @@ use sqlx_crud::Crud;
use std::cmp::Ordering;
use std::str::FromStr;
use tokio::sync::mpsc::channel;
use tonic_lnd::lnrpc::payment::PaymentStatus;
use tracing::{error, info};

pub async fn check_failure_retries(order: &Order) -> Result<Order> {
Expand Down Expand Up @@ -176,7 +176,7 @@ pub async fn do_payment(mut order: Order) -> Result<()> {
// We redeclare vars to use inside this block
// Receiving msgs from send_payment()
while let Some(msg) = rx.recv().await {
if let Some(status) = PaymentStatus::from_i32(msg.payment.status) {
if let Ok(status) = PaymentStatus::try_from(msg.payment.status) {
match status {
PaymentStatus::Succeeded => {
info!(
Expand Down
55 changes: 27 additions & 28 deletions src/lightning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ use crate::util::bytes_to_string;

use anyhow::Result;
use easy_hasher::easy_hasher::*;
use fedimint_tonic_lnd::tonic::Status;
use fedimint_tonic_lnd::invoicesrpc::{
AddHoldInvoiceRequest, AddHoldInvoiceResp, CancelInvoiceMsg, CancelInvoiceResp,
SettleInvoiceMsg, SettleInvoiceResp,
};
use fedimint_tonic_lnd::lnrpc::{invoice::InvoiceState, Payment};
use fedimint_tonic_lnd::routerrpc::{SendPaymentRequest, TrackPaymentRequest};
use fedimint_tonic_lnd::Client;
use nostr_sdk::nostr::hashes::hex::FromHex;
use nostr_sdk::nostr::secp256k1::rand::{self, RngCore};
use tokio::sync::mpsc::Sender;
use fedimint_tonic_lnd::lnrpc::{AddInvoiceResponse, InvoiceSubscription};
use fedimint_tonic_lnd::lnrpc::{invoice::InvoiceState, Payment, Invoice};
use fedimint_tonic_lnd::invoicesrpc::{AddHoldInvoiceRequest, AddHoldInvoiceResp};
use fedimint_tonic_lnd::{ConnectError, LightningClient};
use fedimint_tonic_lnd::{Client,};
use tracing::info;
// use tonic_lnd::lnrpc::


pub struct LndConnector {
client: Client,
}
Expand Down Expand Up @@ -57,13 +57,13 @@ impl LndConnector {
&mut self,
description: &str,
amount: i64,
) -> Result<(AddHoldInvoiceResp, Vec<u8>, Vec<u8>), String > {
) -> Result<(AddHoldInvoiceResp, Vec<u8>, Vec<u8>), MostroError> {
let mut preimage = [0u8; 32];
rand::thread_rng().fill_bytes(&mut preimage);
let hash = raw_sha256(preimage.to_vec());
let ln_settings = Settings::get_ln();
let cltv_expiry = ln_settings.hold_invoice_cltv_delta as u64;

let invoice = AddHoldInvoiceRequest {
hash: hash.to_vec(),
memo: description.to_string(),
Expand All @@ -76,25 +76,27 @@ impl LndConnector {
.invoices()
.add_hold_invoice(invoice)
.await
.map_err(|e| e);
.map_err(|e| MostroError::LnNodeError(e.to_string()));

match holdinvoice {
Ok(holdinvoice) => Ok((holdinvoice.into_inner(), preimage.to_vec(), hash.to_vec())),
Err(e) => Err(e.message().to_string()),
Err(e) => Err(e),
}
}

pub async fn subscribe_invoice(
&mut self,
r_hash: Vec<u8>,
listener: Sender<InvoiceState>,
listener: Sender<InvoiceMessage>,
) -> anyhow::Result<()> {
let invoice_stream = self
.client.lightning()
.subscribe_invoices(fedimint_tonic_lnd::lnrpc::InvoiceSubscription {
add_index: 0,
settle_index: 0,
})
.client
.invoices()
.subscribe_single_invoice(
fedimint_tonic_lnd::invoicesrpc::SubscribeSingleInvoiceRequest {
r_hash: r_hash.clone(),
},
)
.await
.map_err(|e| MostroError::LnNodeError(e.to_string()))?;

Expand All @@ -105,16 +107,15 @@ impl LndConnector {
.await
.map_err(|e| MostroError::LnNodeError(e.to_string()))?
{
if let Some(state) =
fedimint_tonic_lnd::lnrpc::invoice::InvoiceState::try_from(invoice.state)
let state = fedimint_tonic_lnd::lnrpc::invoice::InvoiceState::try_from(invoice.state)?;
{
let msg = InvoiceMessage {
hash: r_hash.clone(),
state,
};
listener
.clone()
.send(state)
.send(msg)
.await
.map_err(|e| MostroError::LnNodeError(e.to_string()))?
}
Expand All @@ -125,18 +126,16 @@ impl LndConnector {
pub async fn settle_hold_invoice(
&mut self,
preimage: &str,
) -> Result<SettleInvoiceResp, ConnectError> {
) -> Result<SettleInvoiceResp, MostroError> {
let preimage = FromHex::from_hex(preimage).expect("Wrong preimage");

let preimage_message = SettleInvoiceMsg { preimage };
let settle = self
.client
.lightning().se

// .invoices()
// .settle_invoice(preimage_message)
.invoices()
.settle_invoice(preimage_message)
.await
.map_err(|e| e.to_string());
.map_err(|e| MostroError::LnNodeError(e.to_string()));

match settle {
Ok(settle) => Ok(settle.into_inner()),
Expand All @@ -147,7 +146,7 @@ impl LndConnector {
pub async fn cancel_hold_invoice(
&mut self,
hash: &str,
) -> Result<CancelInvoiceResp, ConnectError> {
) -> Result<CancelInvoiceResp, MostroError> {
let payment_hash = FromHex::from_hex(hash).expect("Wrong payment hash");

let cancel_message = CancelInvoiceMsg { payment_hash };
Expand All @@ -156,7 +155,7 @@ impl LndConnector {
.invoices()
.cancel_invoice(cancel_message)
.await
.map_err(|e| e.to_string());
.map_err(|e| MostroError::LnNodeError(e.to_string()));

match cancel {
Ok(cancel) => Ok(cancel.into_inner()),
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use std::sync::Arc;
use std::thread;
use tokio::sync::mpsc::channel;
use tokio::sync::Mutex;
use tonic_lnd::Client;
use tonic_lnd::lnrpc::invoice::InvoiceState;
// use fedimint_tonic_lnd::Client;
use fedimint_tonic_lnd::lnrpc::invoice::InvoiceState;
use tracing::error;
use tracing::info;
use uuid::Uuid;
Expand Down

0 comments on commit c66c34e

Please sign in to comment.