Skip to content

Commit

Permalink
Fix backwards compatibility for lightning payout fees (#864)
Browse files Browse the repository at this point in the history
(cherry picked from commit ba28d2a)
  • Loading branch information
dleutenegger authored and danielgranhao committed Jan 10, 2024
1 parent daa59cf commit bb1a770
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/data_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ fn offer_kind_from_row(row: &Row) -> rusqlite::Result<Option<OfferKind>> {
let topup_value_minor_units: u64 = row.get(9)?;
let exchange_fee_minor_units: u64 = row.get(10)?;
let exchange_fee_rate_permyriad: u16 = row.get(11)?;
let topup_value_sats: u64 = row.get(13)?;
let topup_value_sats: Option<u64> = row.get(13)?;

let exchange_rate = ExchangeRate {
currency_code: fiat_currency,
Expand Down Expand Up @@ -572,7 +572,7 @@ mod tests {
id: "id".to_string(),
exchange_rate: exchange_rate.clone(),
topup_value_minor_units: 51245,
topup_value_sats: 2625281,
topup_value_sats: Some(2625281),
exchange_fee_minor_units: 123,
exchange_fee_rate_permyriad: 50,
lightning_payout_fee: None,
Expand All @@ -590,7 +590,7 @@ mod tests {
id: "id".to_string(),
exchange_rate: exchange_rate.clone(),
topup_value_minor_units: 51245,
topup_value_sats: 2625281,
topup_value_sats: Some(2625281),
exchange_fee_minor_units: 123,
exchange_fee_rate_permyriad: 50,
lightning_payout_fee: None,
Expand Down Expand Up @@ -804,7 +804,7 @@ mod tests {
id: "id".to_string(),
exchange_rate: exchange_rate.clone(),
topup_value_minor_units: 51245,
topup_value_sats: 2625281,
topup_value_sats: Some(2625281),
exchange_fee_minor_units: 123,
exchange_fee_rate_permyriad: 50,
lightning_payout_fee: None,
Expand Down
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ impl LightningNode {
.amount_msat
.add(payment.fee_msat)
.as_msats()
.to_amount_down(&exchange_rate)
.to_amount_down(exchange_rate)
}

fn payment_from_created_invoice(&self, created_invoice: &CreatedInvoice) -> Result<Payment> {
Expand Down Expand Up @@ -1717,9 +1717,12 @@ fn fill_payout_fee(
lightning_payout_fee: _,
error,
} => {
let lightning_payout_fee =
(topup_value_sats.as_sats().msats - requested_amount.msats).as_msats();
let lightning_payout_fee = Some(lightning_payout_fee.to_amount_up(rate));
let lightning_payout_fee = topup_value_sats.map(|v| {
(v.as_sats().msats - requested_amount.msats)
.as_msats()
.to_amount_up(rate)
});

OfferKind::Pocket {
id,
exchange_rate,
Expand Down Expand Up @@ -1912,7 +1915,7 @@ mod tests {
updated_at: SystemTime::now(),
},
topup_value_minor_units: 0,
topup_value_sats: 0,
topup_value_sats: Some(0),
exchange_fee_minor_units: 0,
exchange_fee_rate_permyriad: 0,
lightning_payout_fee: None,
Expand Down Expand Up @@ -1961,7 +1964,7 @@ mod tests {
updated_at: SystemTime::now(),
},
topup_value_minor_units: 0,
topup_value_sats: 0,
topup_value_sats: Some(0),
exchange_fee_minor_units: 0,
exchange_fee_rate_permyriad: 0,
lightning_payout_fee: None,
Expand Down
2 changes: 1 addition & 1 deletion src/lipalightninglib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ interface OfferKind {
string id,
ExchangeRate exchange_rate,
u64 topup_value_minor_units,
u64 topup_value_sats,
u64? topup_value_sats,
u64 exchange_fee_minor_units,
u16 exchange_fee_rate_permyriad,
Amount? lightning_payout_fee,
Expand Down
6 changes: 3 additions & 3 deletions src/offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub enum OfferKind {
exchange_rate: ExchangeRate,
/// The original fiat amount sent to the exchange.
topup_value_minor_units: u64,
/// The sat amount after the exchange.
topup_value_sats: u64,
/// The sat amount after the exchange. Isn't available for topups collected before version v0.29.4-beta.
topup_value_sats: Option<u64>,
/// The fee paid to perform the exchange from fiat to sats.
exchange_fee_minor_units: u64,
/// The rate of the fee expressed in permyriad (e.g. 1.5% would be 150).
Expand Down Expand Up @@ -79,7 +79,7 @@ impl OfferInfo {
id: topup_info.id,
exchange_rate,
topup_value_minor_units: topup_info.topup_value_minor_units,
topup_value_sats: topup_info.amount_sat,
topup_value_sats: Some(topup_info.amount_sat),
exchange_fee_minor_units: topup_info.exchange_fee_minor_units,
exchange_fee_rate_permyriad: topup_info.exchange_fee_rate_permyriad,
lightning_payout_fee: None,
Expand Down

0 comments on commit bb1a770

Please sign in to comment.