Skip to content

Commit

Permalink
Add LSP fee ID and pass it back when creating an invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGiorgio committed Nov 3, 2023
1 parent 27d2af2 commit e0e1e34
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
13 changes: 10 additions & 3 deletions mutiny-core/src/lspclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct ProposalRequest {
pub host: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub port: Option<u16>,
pub fee_id: String,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -60,6 +61,7 @@ pub struct FeeRequest {

#[derive(Serialize, Deserialize)]
pub struct FeeResponse {
pub id: String,
pub fee_amount_msat: u64,
}

Expand Down Expand Up @@ -120,11 +122,16 @@ impl LspClient {
})
}

pub(crate) async fn get_lsp_invoice(&self, bolt11: String) -> Result<String, MutinyError> {
pub(crate) async fn get_lsp_invoice(
&self,
bolt11: String,
fee_id: String,
) -> Result<String, MutinyError> {
let payload = ProposalRequest {
bolt11,
host: None,
port: None,
fee_id,
};

let request = self
Expand Down Expand Up @@ -170,7 +177,7 @@ impl LspClient {
pub(crate) async fn get_lsp_fee_msat(
&self,
fee_request: FeeRequest,
) -> Result<u64, MutinyError> {
) -> Result<FeeResponse, MutinyError> {
let request = self
.http_client
.post(format!("{}{}", &self.url, FEE_PATH))
Expand All @@ -185,6 +192,6 @@ impl LspClient {
.await
.map_err(|_| MutinyError::LspGenericError)?;

Ok(fee_response.fee_amount_msat)
Ok(fee_response)
}
}
18 changes: 13 additions & 5 deletions mutiny-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ impl<S: MutinyStorage> Node<S> {
route_hints: Option<Vec<PhantomRouteHints>>,
) -> Result<Bolt11Invoice, MutinyError> {
// the amount to create for the invoice whether or not there is an lsp
let (amount_sat, lsp_fee_msat) = if let Some(lsp) = self.lsp_client.clone() {
let (amount_sat, lsp_fee) = if let Some(lsp) = self.lsp_client.clone() {
// LSP requires an amount:
let amount_sat = amount_sat.ok_or(MutinyError::BadAmountError)?;

Expand All @@ -806,15 +806,15 @@ impl<S: MutinyStorage> Node<S> {
}

// check the fee from the LSP
let lsp_fee_msat = lsp
let lsp_fee = lsp
.get_lsp_fee_msat(FeeRequest {
pubkey: self.pubkey.to_hex(),
amount_msat: amount_sat * 1000,
})
.await?;

// Convert the fee from msat to sat for comparison and subtraction
let lsp_fee_sat = lsp_fee_msat / 1000;
let lsp_fee_sat = lsp_fee.fee_amount_msat / 1000;

// Ensure that the fee is less than the amount being requested.
// If it isn't, we don't subtract it.
Expand All @@ -828,19 +828,27 @@ impl<S: MutinyStorage> Node<S> {
amount_sat
};

(Some(amount_minus_fee), Some(lsp_fee_msat))
(Some(amount_minus_fee), Some(lsp_fee))
} else {
(amount_sat, None)
};

let lsp_fee_msat = lsp_fee.as_ref().map(|l| l.fee_amount_msat);

let invoice = self
.create_internal_invoice(amount_sat, lsp_fee_msat, labels, route_hints)
.await?;

if let Some(lsp) = self.lsp_client.clone() {
self.connect_peer(PubkeyConnectionInfo::new(&lsp.connection_string)?, None)
.await?;
let lsp_invoice = match lsp.get_lsp_invoice(invoice.to_string()).await {
let lsp_invoice = match lsp
.get_lsp_invoice(
invoice.to_string(),
lsp_fee.expect("should have gotten a fee id").id,
)
.await
{
Ok(lsp_invoice_str) => Bolt11Invoice::from_str(&lsp_invoice_str)?,
Err(e) => {
log_error!(self.logger, "Failed to get invoice from LSP: {e}");
Expand Down

0 comments on commit e0e1e34

Please sign in to comment.