Skip to content

Commit

Permalink
feat: model lightning payment fees
Browse files Browse the repository at this point in the history
  • Loading branch information
Restioson committed Sep 4, 2023
1 parent 6a9bb51 commit 10243c5
Show file tree
Hide file tree
Showing 21 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE
payments DROP COLUMN fee_msat;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE
payments
ADD
COLUMN fee_msat BIGINT DEFAULT null;
16 changes: 16 additions & 0 deletions coordinator/src/db/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Payment {
pub updated_at: OffsetDateTime,
pub description: String,
pub invoice: Option<String>,
pub fee_msat: Option<i64>,
}

pub fn get(
Expand Down Expand Up @@ -114,6 +115,7 @@ impl TryFrom<Payment> for (lightning::ln::PaymentHash, ln_dlc_node::PaymentInfo)

let amt_msat =
ln_dlc_node::MillisatAmount::new(value.amount_msat.map(|amount| amount as u64));
let fee_msat = ln_dlc_node::MillisatAmount::new(value.fee_msat.map(|amount| amount as u64));

Ok((
payment_hash,
Expand All @@ -122,6 +124,7 @@ impl TryFrom<Payment> for (lightning::ln::PaymentHash, ln_dlc_node::PaymentInfo)
secret,
status: value.htlc_status.into(),
amt_msat,
fee_msat,
flow: value.flow.into(),
timestamp: value.payment_timestamp,
description: value.description,
Expand Down Expand Up @@ -240,6 +243,7 @@ pub fn update(
payment_hash: lightning::ln::PaymentHash,
htlc_status: ln_dlc_node::HTLCStatus,
amount_msat: ln_dlc_node::MillisatAmount,
fee_msat: ln_dlc_node::MillisatAmount,
preimage: Option<lightning::ln::PaymentPreimage>,
secret: Option<lightning::ln::PaymentSecret>,
conn: &mut PgConnection,
Expand All @@ -252,6 +256,7 @@ pub fn update(
let payment_hash = payment_hash.0.to_hex();
let htlc_status: HtlcStatus = htlc_status.into();
let amount_msat = amount_msat.to_inner().map(|amt| amt as i64);
let fee_msat = fee_msat.to_inner().map(|amt| amt as i64);

conn.transaction::<(), _, _>(|conn| {
let affected_rows = diesel::update(payments::table)
Expand All @@ -274,6 +279,17 @@ pub fn update(
}
}

if let Some(fee_msat) = fee_msat {
let affected_rows = diesel::update(payments::table)
.filter(schema::payments::payment_hash.eq(&payment_hash))
.set(schema::payments::fee_msat.eq(fee_msat))
.execute(conn)?;

if affected_rows == 0 {
bail!("Could not update payment fee amount")
}
}

if let Some(preimage) = preimage {
let affected_rows = diesel::update(payments::table)
.filter(schema::payments::payment_hash.eq(&payment_hash))
Expand Down
3 changes: 3 additions & 0 deletions coordinator/src/node/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl node::Storage for NodeStorage {
payment_hash: &PaymentHash,
flow: PaymentFlow,
amt_msat: MillisatAmount,
fee_msat: MillisatAmount,
htlc_status: HTLCStatus,
preimage: Option<PaymentPreimage>,
secret: Option<PaymentSecret>,
Expand All @@ -56,6 +57,7 @@ impl node::Storage for NodeStorage {
*payment_hash,
htlc_status,
amt_msat,
fee_msat,
preimage,
secret,
&mut conn,
Expand All @@ -70,6 +72,7 @@ impl node::Storage for NodeStorage {
secret,
status: htlc_status,
amt_msat,
fee_msat,
flow,
timestamp: OffsetDateTime::now_utc(),
description: "".to_string(),
Expand Down
1 change: 1 addition & 0 deletions coordinator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ diesel::table! {
updated_at -> Timestamptz,
description -> Text,
invoice -> Nullable<Text>,
fee_msat -> Nullable<Int8>,
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/ln-dlc-node/src/ldk_node_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ pub mod tests {
_payment_hash: &lightning::ln::PaymentHash,
_flow: crate::PaymentFlow,
_amt_msat: crate::MillisatAmount,
_fee_msat: crate::MillisatAmount,
_htlc_status: crate::HTLCStatus,
_preimage: Option<lightning::ln::PaymentPreimage>,
_secret: Option<lightning::ln::PaymentSecret>,
Expand Down
2 changes: 2 additions & 0 deletions crates/ln-dlc-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub struct PaymentInfo {
pub secret: Option<PaymentSecret>,
pub status: HTLCStatus,
pub amt_msat: MillisatAmount,
pub fee_msat: MillisatAmount,
pub flow: PaymentFlow,
pub timestamp: OffsetDateTime,
pub description: String,
Expand Down Expand Up @@ -137,6 +138,7 @@ impl From<Invoice> for PaymentInfo {
secret: Some(*value.payment_secret()),
status: HTLCStatus::Pending,
amt_msat: MillisatAmount(value.amount_milli_satoshis()),
fee_msat: MillisatAmount(None),
flow: PaymentFlow::Inbound,
timestamp: OffsetDateTime::from(value.timestamp()),
description: match value.description() {
Expand Down
4 changes: 4 additions & 0 deletions crates/ln-dlc-node/src/ln/common_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ where
&payment_hash,
PaymentFlow::Outbound,
amount_msat,
MillisatAmount(fee_paid_msat),
HTLCStatus::Succeeded,
Some(payment_preimage),
None,
Expand All @@ -186,6 +187,7 @@ where
secret: None,
status: HTLCStatus::Succeeded,
amt_msat,
fee_msat: MillisatAmount(fee_paid_msat),
flow: PaymentFlow::Outbound,
timestamp: OffsetDateTime::now_utc(),
description: "".to_string(),
Expand Down Expand Up @@ -334,6 +336,7 @@ pub fn handle_payment_claimed<S>(
&payment_hash,
PaymentFlow::Inbound,
amount_msat,
MillisatAmount(None),
HTLCStatus::Succeeded,
payment_preimage,
payment_secret,
Expand All @@ -359,6 +362,7 @@ where
&payment_hash,
PaymentFlow::Outbound,
amount_msat,
MillisatAmount(None),
HTLCStatus::Failed,
None,
None,
Expand Down
1 change: 1 addition & 0 deletions crates/ln-dlc-node/src/node/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ where
secret: None,
status,
amt_msat: MillisatAmount(invoice.amount_milli_satoshis()),
fee_msat: MillisatAmount(None),
flow: PaymentFlow::Outbound,
timestamp: OffsetDateTime::now_utc(),
description,
Expand Down
8 changes: 8 additions & 0 deletions crates/ln-dlc-node/src/node/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ pub trait Storage {
/// Add a new payment.
fn insert_payment(&self, payment_hash: PaymentHash, info: PaymentInfo) -> Result<()>;
/// Add a new payment or update an existing one.
#[allow(clippy::too_many_arguments)]
fn merge_payment(
&self,
payment_hash: &PaymentHash,
flow: PaymentFlow,
amt_msat: MillisatAmount,
fee_msat: MillisatAmount,
htlc_status: HTLCStatus,
preimage: Option<PaymentPreimage>,
secret: Option<PaymentSecret>,
Expand Down Expand Up @@ -116,6 +118,7 @@ impl Storage for InMemoryStore {
payment_hash: &PaymentHash,
flow: PaymentFlow,
amt_msat: MillisatAmount,
fee_msat: MillisatAmount,
htlc_status: HTLCStatus,
preimage: Option<PaymentPreimage>,
secret: Option<PaymentSecret>,
Expand All @@ -129,6 +132,10 @@ impl Storage for InMemoryStore {
payment.amt_msat = amt_msat
}

if let fee_msat @ MillisatAmount(Some(_)) = fee_msat {
payment.fee_msat = fee_msat
}

if let Some(preimage) = preimage {
payment.preimage = Some(preimage);
}
Expand All @@ -145,6 +152,7 @@ impl Storage for InMemoryStore {
secret,
status: htlc_status,
amt_msat: MillisatAmount(None),
fee_msat,
flow,
timestamp: OffsetDateTime::now_utc(),
description: "".to_string(),
Expand Down
7 changes: 5 additions & 2 deletions crates/ln-dlc-node/src/node/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ where
status: info.status,
flow: info.flow,
amount_msat: info.amt_msat.0,
fee_msat: info.fee_msat.0,
timestamp: info.timestamp,
description: info.description.clone(),
preimage: info.preimage.map(|preimage| preimage.0.to_hex()),
Expand All @@ -170,6 +171,7 @@ pub struct PaymentDetails {
pub status: HTLCStatus,
pub flow: PaymentFlow,
pub amount_msat: Option<u64>,
pub fee_msat: Option<u64>,
pub timestamp: OffsetDateTime,
pub description: String,
pub preimage: Option<String>,
Expand All @@ -182,14 +184,15 @@ impl fmt::Display for PaymentDetails {
let status = self.status.to_string();
let flow = self.flow;
let amount_msat = self.amount_msat.unwrap_or_default();
let fee_msat = self.fee_msat.unwrap_or_default();
let timestamp = self.timestamp.to_string();
let description = self.description.clone();
let invoice = self.invoice.clone();

write!(
f,
"payment_hash {}, status {}, flow {}, amount_msat {}, timestamp {}, description {} invoice {:?}",
payment_hash, status, flow, amount_msat, timestamp, description, invoice
"payment_hash {}, status {}, flow {}, amount_msat {}, fee_msat {}, timestamp {}, description {}, invoice {:?}",
payment_hash, status, flow, amount_msat, fee_msat, timestamp, description, invoice
)
}
}
3 changes: 3 additions & 0 deletions mobile/lib/features/wallet/domain/wallet_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ abstract class WalletHistoryItemData {
preimage: type.paymentPreimage,
description: type.description,
paymentHash: type.paymentHash,
feeMsats: type.feeMsat,
expiry: expiry,
invoice: type.invoice);
}
Expand All @@ -103,6 +104,7 @@ class LightningPaymentData extends WalletHistoryItemData {
final String description;
final String? invoice;
final DateTime? expiry;
final int? feeMsats;

LightningPaymentData(
{required super.flow,
Expand All @@ -113,6 +115,7 @@ class LightningPaymentData extends WalletHistoryItemData {
required this.description,
required this.invoice,
required this.expiry,
required this.feeMsats,
required this.paymentHash});

@override
Expand Down
4 changes: 4 additions & 0 deletions mobile/lib/features/wallet/wallet_history_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class LightningPaymentHistoryItem extends WalletHistoryItem {
@override
List<Widget> getDetails() {
return [
Visibility(
visible: data.feeMsats != null,
child: HistoryDetail(label: "Fee", value: "${(data.feeMsats ?? 0) / 1000} sats"),
),
Visibility(
visible: data.expiry != null,
child: HistoryDetail(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE
payments DROP COLUMN fee_msat;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE
payments
ADD
COLUMN fee_msat BIGINT DEFAULT null;
1 change: 1 addition & 0 deletions mobile/native/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub enum WalletHistoryItemType {
description: String,
payment_preimage: Option<String>,
invoice: Option<String>,
fee_msat: Option<u64>,
expiry_timestamp: Option<u64>,
},
Trade {
Expand Down
2 changes: 2 additions & 0 deletions mobile/native/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ pub fn update_payment(
payment_hash: lightning::ln::PaymentHash,
htlc_status: ln_dlc_node::HTLCStatus,
amt_msat: ln_dlc_node::MillisatAmount,
fee_msat: ln_dlc_node::MillisatAmount,
preimage: Option<lightning::ln::PaymentPreimage>,
secret: Option<lightning::ln::PaymentSecret>,
) -> Result<()> {
Expand All @@ -287,6 +288,7 @@ pub fn update_payment(
base64.encode(payment_hash.0),
htlc_status.into(),
amt_msat.to_inner().map(|amt| amt as i64),
fee_msat.to_inner().map(|amt| amt as i64),
preimage,
secret,
&mut db,
Expand Down
Loading

0 comments on commit 10243c5

Please sign in to comment.