Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model more transaction history details #1111

Merged
merged 13 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE
payments DROP COLUMN invoice;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE
payments
ADD
COLUMN invoice TEXT DEFAULT null;
5 changes: 5 additions & 0 deletions coordinator/src/db/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct Payment {
pub created_at: OffsetDateTime,
pub updated_at: OffsetDateTime,
pub description: String,
pub invoice: Option<String>,
}

pub fn get(
Expand Down Expand Up @@ -81,6 +82,7 @@ impl From<(lightning::ln::PaymentHash, ln_dlc_node::PaymentInfo)> for NewPayment
flow: info.flow.into(),
payment_timestamp: info.timestamp,
description: info.description,
invoice: info.invoice,
}
}
}
Expand Down Expand Up @@ -123,6 +125,7 @@ impl TryFrom<Payment> for (lightning::ln::PaymentHash, ln_dlc_node::PaymentInfo)
flow: value.flow.into(),
timestamp: value.payment_timestamp,
description: value.description,
invoice: value.invoice,
},
))
}
Expand Down Expand Up @@ -172,6 +175,8 @@ pub(crate) struct NewPayment {
pub payment_timestamp: OffsetDateTime,
#[diesel(sql_type = Text)]
pub description: String,
#[diesel(sql_type = Nullable<Text>)]
pub invoice: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, FromSqlRow, AsExpression)]
Expand Down
1 change: 1 addition & 0 deletions coordinator/src/node/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl node::Storage for NodeStorage {
flow,
timestamp: OffsetDateTime::now_utc(),
description: "".to_string(),
invoice: None,
},
),
&mut conn,
Expand Down
1 change: 1 addition & 0 deletions coordinator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ diesel::table! {
created_at -> Timestamptz,
updated_at -> Timestamptz,
description -> Text,
invoice -> Nullable<Text>,
}
}

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 @@ -99,6 +99,7 @@ pub struct PaymentInfo {
pub flow: PaymentFlow,
pub timestamp: OffsetDateTime,
pub description: String,
pub invoice: Option<String>,
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -142,6 +143,7 @@ impl From<Invoice> for PaymentInfo {
InvoiceDescription::Direct(direct) => direct.to_string(),
InvoiceDescription::Hash(hash) => hash.0.to_hex(),
},
invoice: Some(value.to_string()),
}
}
}
1 change: 1 addition & 0 deletions crates/ln-dlc-node/src/ln/common_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ where
flow: PaymentFlow::Outbound,
timestamp: OffsetDateTime::now_utc(),
description: "".to_string(),
invoice: None,
},
) {
tracing::error!(
Expand Down
12 changes: 10 additions & 2 deletions crates/ln-dlc-node/src/node/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,21 @@ where
pub fn create_interceptable_invoice(
&self,
amount_in_sats: Option<u64>,
invoice_expiry: u32,
invoice_expiry: Option<Duration>,
description: String,
final_route_hint_hop: RouteHintHop,
) -> Result<Invoice> {
let invoice_expiry = invoice_expiry
.unwrap_or_else(|| Duration::from_secs(lightning_invoice::DEFAULT_EXPIRY_TIME));
let amount_msat = amount_in_sats.map(|x| x * 1000);
let (payment_hash, payment_secret) = self
.channel_manager
.create_inbound_payment(amount_msat, invoice_expiry, None)
.create_inbound_payment(amount_msat, invoice_expiry.as_secs() as u32, None)
.map_err(|_| anyhow!("Failed to create inbound payment"))?;
let invoice_builder = InvoiceBuilder::new(self.get_currency())
.payee_pub_key(self.info.pubkey)
.description(description)
.expiry_time(invoice_expiry)
.payment_hash(sha256::Hash::from_slice(&payment_hash.0)?)
.payment_secret(payment_secret)
.timestamp(SystemTime::now())
Expand All @@ -101,6 +104,10 @@ where
})
.map_err(|_| anyhow!("Failed to sign invoice"))?;
let invoice = Invoice::from_signed(signed_invoice)?;

self.storage
.insert_payment(payment_hash, invoice.clone().into())?;

Ok(invoice)
}

Expand Down Expand Up @@ -221,6 +228,7 @@ where
flow: PaymentFlow::Outbound,
timestamp: OffsetDateTime::now_utc(),
description,
invoice: Some(format!("{invoice}")),
},
)?;

Expand Down
1 change: 1 addition & 0 deletions crates/ln-dlc-node/src/node/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl Storage for InMemoryStore {
flow,
timestamp: OffsetDateTime::now_utc(),
description: "".to_string(),
invoice: None,
},
);
}
Expand Down
18 changes: 16 additions & 2 deletions crates/ln-dlc-node/src/node/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use crate::node::HTLCStatus;
use crate::node::Node;
use crate::node::Storage;
use crate::PaymentFlow;
use crate::ToHex;
use anyhow::Context;
use anyhow::Result;
use bdk::blockchain::EsploraBlockchain;
use bdk::sled;
use bitcoin::secp256k1::SecretKey;
use bitcoin::Address;
use dlc_manager::Blockchain;
use lightning::ln::PaymentHash;
use std::fmt;
use std::sync::Arc;
Expand Down Expand Up @@ -60,6 +62,12 @@ where
self.wallet.unused_address()
}

pub fn get_blockchain_height(&self) -> Result<u64> {
self.wallet
.get_blockchain_height()
.context("Failed to get blockchain height")
}

pub fn get_on_chain_balance(&self) -> Result<bdk::Balance> {
self.wallet
.inner()
Expand Down Expand Up @@ -145,6 +153,8 @@ where
amount_msat: info.amt_msat.0,
timestamp: info.timestamp,
description: info.description.clone(),
preimage: info.preimage.map(|preimage| preimage.0.to_hex()),
invoice: info.invoice.clone(),
})
.collect::<Vec<_>>();

Expand All @@ -162,6 +172,8 @@ pub struct PaymentDetails {
pub amount_msat: Option<u64>,
pub timestamp: OffsetDateTime,
pub description: String,
pub preimage: Option<String>,
pub invoice: Option<String>,
}

impl fmt::Display for PaymentDetails {
Expand All @@ -172,10 +184,12 @@ impl fmt::Display for PaymentDetails {
let amount_msat = self.amount_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 {}",
payment_hash, status, flow, amount_msat, timestamp, description,
"payment_hash {}, status {}, flow {}, amount_msat {}, timestamp {}, description {} invoice {:?}",
payment_hash, status, flow, amount_msat, timestamp, description, invoice
)
}
}
7 changes: 3 additions & 4 deletions crates/ln-dlc-node/src/tests/just_in_time_channel/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async fn fail_to_open_jit_channel_with_fee_rate_over_max() {
let invoice = payee
.create_interceptable_invoice(
Some(payer_to_payee_invoice_amount),
0,
None,
"interceptable-invoice".to_string(),
final_route_hint_hop,
)
Expand Down Expand Up @@ -181,7 +181,7 @@ async fn open_jit_channel_with_disconnected_payee() {
let invoice = payee
.create_interceptable_invoice(
Some(payer_to_payee_invoice_amount),
0,
None,
"interceptable-invoice".to_string(),
final_route_hint_hop,
)
Expand Down Expand Up @@ -238,10 +238,9 @@ pub(crate) async fn send_interceptable_payment(
let interceptable_route_hint_hop =
coordinator.prepare_interceptable_payment(payee.info.pubkey)?;

let invoice_expiry = 0; // an expiry of 0 means the invoice never expires
let invoice = payee.create_interceptable_invoice(
Some(invoice_amount_sat),
invoice_expiry,
None,
"interceptable-invoice".to_string(),
interceptable_route_hint_hop,
)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn fail_intercepted_htlc_if_coordinator_cannot_reconnect_to_payee() {
let invoice = payee
.create_interceptable_invoice(
Some(invoice_amount),
0,
None,
"interceptable-invoice".to_string(),
interceptable_route_hint_hop,
)
Expand Down Expand Up @@ -100,7 +100,7 @@ async fn fail_intercepted_htlc_if_connection_lost_after_funding_tx_generated() {
let invoice = payee
.create_interceptable_invoice(
Some(invoice_amount),
0,
None,
"interceptable-invoice".to_string(),
interceptable_route_hint_hop,
)
Expand Down Expand Up @@ -169,7 +169,7 @@ async fn fail_intercepted_htlc_if_coordinator_cannot_pay_to_open_jit_channel() {
let invoice = payee
.create_interceptable_invoice(
Some(invoice_amount),
0,
None,
"interceptable-invoice".to_string(),
interceptable_route_hint_hop,
)
Expand Down
4 changes: 2 additions & 2 deletions crates/tests-e2e/tests/open_position.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use native::api;
use native::api::ContractSymbol;
use native::api::WalletType;
use native::api::WalletHistoryItemType;
use native::health::Service;
use native::health::ServiceStatus;
use native::trade::order::api::NewOrder;
Expand Down Expand Up @@ -72,5 +72,5 @@ async fn can_open_position() {
.expect("to retrieve wallet info")
.history
.iter()
.any(|item| matches!(item.wallet_type, WalletType::OrderMatchingFee { ref order_id } if order_id == &order_id_original)));
.any(|item| matches!(item.wallet_type, WalletHistoryItemType::OrderMatchingFee { ref order_id, .. } if order_id == &order_id_original)));
}
21 changes: 8 additions & 13 deletions mobile/lib/features/trade/order_list_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@ class OrderListItem extends StatelessWidget {
formatter.maximumFractionDigits = 2;

const double iconSize = 18;
Icon statusIcon = () {
switch (order.state) {
case OrderState.open:
return const Icon(
Icons.pending,
size: iconSize,
);
case OrderState.filled:
return const Icon(Icons.check_circle, color: Colors.green, size: iconSize);
case OrderState.failed:
return const Icon(Icons.error, color: Colors.red, size: iconSize);
}
}();
Icon statusIcon = switch (order.state) {
OrderState.open => const Icon(
Icons.pending,
size: iconSize,
),
OrderState.filled => const Icon(Icons.check_circle, color: Colors.green, size: iconSize),
OrderState.failed => const Icon(Icons.error, color: Colors.red, size: iconSize),
};

return Card(
child: ListTile(
Expand Down
Loading