Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit e09db90

Browse files
Merge pull request #823 from MutinyWallet/dynamic-fee
Dynamic fee
2 parents 385cacc + 39ab87f commit e09db90

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

mutiny-core/src/lspclient.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub struct ProposalRequest {
4545
pub host: Option<String>,
4646
#[serde(skip_serializing_if = "Option::is_none")]
4747
pub port: Option<u16>,
48+
pub fee_id: String,
4849
}
4950

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

6162
#[derive(Serialize, Deserialize)]
6263
pub struct FeeResponse {
64+
pub id: String,
6365
pub fee_amount_msat: u64,
6466
}
6567

@@ -120,11 +122,16 @@ impl LspClient {
120122
})
121123
}
122124

123-
pub(crate) async fn get_lsp_invoice(&self, bolt11: String) -> Result<String, MutinyError> {
125+
pub(crate) async fn get_lsp_invoice(
126+
&self,
127+
bolt11: String,
128+
fee_id: String,
129+
) -> Result<String, MutinyError> {
124130
let payload = ProposalRequest {
125131
bolt11,
126132
host: None,
127133
port: None,
134+
fee_id,
128135
};
129136

130137
let request = self
@@ -170,7 +177,7 @@ impl LspClient {
170177
pub(crate) async fn get_lsp_fee_msat(
171178
&self,
172179
fee_request: FeeRequest,
173-
) -> Result<u64, MutinyError> {
180+
) -> Result<FeeResponse, MutinyError> {
174181
let request = self
175182
.http_client
176183
.post(format!("{}{}", &self.url, FEE_PATH))
@@ -185,6 +192,6 @@ impl LspClient {
185192
.await
186193
.map_err(|_| MutinyError::LspGenericError)?;
187194

188-
Ok(fee_response.fee_amount_msat)
195+
Ok(fee_response)
189196
}
190197
}

mutiny-core/src/node.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ impl<S: MutinyStorage> Node<S> {
793793
route_hints: Option<Vec<PhantomRouteHints>>,
794794
) -> Result<Bolt11Invoice, MutinyError> {
795795
// the amount to create for the invoice whether or not there is an lsp
796-
let (amount_sat, lsp_fee_msat) = if let Some(lsp) = self.lsp_client.as_ref() {
796+
let (amount_sat, lsp_fee) = if let Some(lsp) = self.lsp_client.as_ref() {
797797
// LSP requires an amount:
798798
let amount_sat = amount_sat.ok_or(MutinyError::BadAmountError)?;
799799

@@ -815,15 +815,15 @@ impl<S: MutinyStorage> Node<S> {
815815
}
816816

817817
// check the fee from the LSP
818-
let lsp_fee_msat = lsp
818+
let lsp_fee = lsp
819819
.get_lsp_fee_msat(FeeRequest {
820820
pubkey: self.pubkey.to_hex(),
821821
amount_msat: amount_sat * 1000,
822822
})
823823
.await?;
824824

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

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

840-
(Some(amount_minus_fee), Some(lsp_fee_msat))
840+
(Some(amount_minus_fee), Some(lsp_fee))
841841
} else {
842842
(amount_sat, None)
843843
};
844844

845+
let lsp_fee_msat = lsp_fee.as_ref().map(|l| l.fee_amount_msat);
846+
845847
let invoice = self
846848
.create_internal_invoice(amount_sat, lsp_fee_msat, labels, route_hints)
847849
.await?;
848850

849851
if let Some(lsp) = self.lsp_client.as_ref() {
850852
self.connect_peer(PubkeyConnectionInfo::new(&lsp.connection_string)?, None)
851853
.await?;
852-
let lsp_invoice = match lsp.get_lsp_invoice(invoice.to_string()).await {
854+
let lsp_invoice = match lsp
855+
.get_lsp_invoice(
856+
invoice.to_string(),
857+
lsp_fee.expect("should have gotten a fee id").id,
858+
)
859+
.await
860+
{
853861
Ok(lsp_invoice_str) => Bolt11Invoice::from_str(&lsp_invoice_str)?,
854862
Err(e) => {
855863
log_error!(self.logger, "Failed to get invoice from LSP: {e}");

0 commit comments

Comments
 (0)