Skip to content

Commit

Permalink
Use payment model in the home
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardparis committed Dec 17, 2024
1 parent b1abce5 commit 790beaa
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 104 deletions.
2 changes: 2 additions & 0 deletions liana-gui/src/app/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub enum Message {
HardwareWallets(HardwareWalletMessage),
HistoryTransactionsExtension(Result<Vec<HistoryTransaction>, Error>),
HistoryTransactions(Result<Vec<HistoryTransaction>, Error>),
Payments(Result<Vec<Payment>, Error>),
PaymentsExtension(Result<Vec<Payment>, Error>),
Payment(Result<(HistoryTransaction, usize), Error>),
LabelsUpdated(Result<HashMap<String, Option<String>>, Error>),
BroadcastModal(Result<HashSet<Txid>, Error>),
Expand Down
3 changes: 2 additions & 1 deletion liana-gui/src/app/state/coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use iced::Command;
use liana_ui::widget::Element;
use lianad::commands::CoinStatus;

use crate::daemon::model::LabelsLoader;
use crate::{
app::{
cache::Cache,
Expand Down Expand Up @@ -132,7 +133,7 @@ impl State for CoinsPanel {
match self.labels_edited.update(
daemon,
message,
std::iter::once(&mut self.coins).map(|a| a as &mut dyn Labelled),
std::iter::once(&mut self.coins).map(|a| a as &mut dyn LabelsLoader),
) {
Ok(cmd) => return cmd,
Err(e) => {
Expand Down
4 changes: 2 additions & 2 deletions liana-gui/src/app/state/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{collections::HashMap, iter::IntoIterator, sync::Arc};
use crate::{
app::{error::Error, message::Message, view},
daemon::{
model::{LabelItem, Labelled, LabelsLoader},
model::{LabelItem, LabelsLoader},
Daemon,
},
};
Expand All @@ -19,7 +19,7 @@ impl LabelsEdited {
pub fn cache(&self) -> &HashMap<String, form::Value<String>> {
&self.0
}
pub fn update<'a, T: IntoIterator<Item = &'a mut dyn Labelled>>(
pub fn update<'a, T: IntoIterator<Item = &'a mut dyn LabelsLoader>>(
&mut self,
daemon: Arc<dyn Daemon + Sync + Send>,
message: Message,
Expand Down
49 changes: 26 additions & 23 deletions liana-gui/src/app/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ use super::{

pub const HISTORY_EVENT_PAGE_SIZE: u64 = 20;

use crate::daemon::model::LabelsLoader;
use crate::daemon::{
model::{remaining_sequence, Coin, HistoryTransaction, Labelled},
model::{remaining_sequence, Coin, HistoryTransaction, Payment},
Daemon,
};
pub use coins::CoinsPanel;
Expand Down Expand Up @@ -127,7 +128,7 @@ pub struct Home {
unconfirmed_balance: Amount,
remaining_sequence: Option<u32>,
expiring_coins: Vec<OutPoint>,
events: Vec<HistoryTransaction>,
events: Vec<Payment>,
is_last_page: bool,
processing: bool,
selected_event: Option<(HistoryTransaction, usize)>,
Expand Down Expand Up @@ -170,7 +171,7 @@ impl State for Home {
if let Some((tx, output_index)) = &self.selected_event {
view::home::payment_view(
cache,
&tx,
tx,
*output_index,
self.labels_edited.cache(),
self.warning.as_ref(),
Expand Down Expand Up @@ -217,15 +218,15 @@ impl State for Home {
);
}
},
Message::HistoryTransactions(res) => match res {
Message::Payments(res) => match res {
Err(e) => self.warning = Some(e),
Ok(events) => {
self.warning = None;
self.events = events;
self.is_last_page = (self.events.len() as u64) < HISTORY_EVENT_PAGE_SIZE;
}
},
Message::HistoryTransactionsExtension(res) => match res {
Message::PaymentsExtension(res) => match res {
Err(e) => self.warning = Some(e),
Ok(events) => {
self.processing = false;
Expand All @@ -235,13 +236,13 @@ impl State for Home {
if let Some(position) = self
.events
.iter()
.position(|event2| event2.txid == event.txid)
.position(|event2| event2.outpoint == event.outpoint)
{
let len = self.events.len();
for event in events {
if !self.events[position..len]
.iter()
.any(|event2| event2.txid == event.txid)
.any(|event2| event2.outpoint == event.outpoint)
{
self.events.push(event);
}
Expand Down Expand Up @@ -274,12 +275,11 @@ impl State for Home {
self.warning = Some(e);
}
},
Message::View(view::Message::SelectSub(i, j)) => {
let txid = self.events[i].txid;
Message::View(view::Message::SelectPayment(outpoint)) => {
return Command::perform(
async move {
let tx = daemon.get_history_txs(&[txid]).await?.remove(0);
Ok((tx, j))
let tx = daemon.get_history_txs(&[outpoint.txid]).await?.remove(0);
Ok((tx, outpoint.vout as usize))
},
Message::Payment,
);
Expand All @@ -290,11 +290,11 @@ impl State for Home {
message,
self.events
.iter_mut()
.map(|tx| tx as &mut dyn Labelled)
.map(|tx| tx as &mut dyn LabelsLoader)
.chain(
self.selected_event
.iter_mut()
.map(|(tx, _)| tx as &mut dyn Labelled),
.map(|(tx, _)| tx as &mut dyn LabelsLoader),
),
) {
Ok(cmd) => {
Expand All @@ -319,9 +319,10 @@ impl State for Home {
self.processing = true;
return Command::perform(
async move {
let last_event_date = last_event_date.timestamp() as u32;
let mut limit = HISTORY_EVENT_PAGE_SIZE;
let mut events = daemon
.list_history_txs(0_u32, last_event_date, limit)
.list_confirmed_payments(0_u32, last_event_date, limit)
.await?;

// because gethistory cursor is inclusive and use blocktime
Expand All @@ -344,12 +345,14 @@ impl State for Home {
{
// increments of the equivalent of one page more.
limit += HISTORY_EVENT_PAGE_SIZE;
events = daemon.list_history_txs(0, last_event_date, limit).await?;
events = daemon
.list_confirmed_payments(0, last_event_date, limit)
.await?;
}
events.sort_by(|a, b| a.compare(b));
Ok(events)
},
Message::HistoryTransactionsExtension,
Message::PaymentsExtension,
);
}
}
Expand Down Expand Up @@ -382,16 +385,16 @@ impl State for Home {
Command::batch(vec![
Command::perform(
async move {
let mut txs = daemon
.list_history_txs(0, now, HISTORY_EVENT_PAGE_SIZE)
let mut payments = daemon
.list_confirmed_payments(0, now, HISTORY_EVENT_PAGE_SIZE)
.await?;
txs.sort_by(|a, b| a.compare(b));
payments.sort_by(|a, b| a.compare(b));

let mut pending_txs = daemon.list_pending_txs().await?;
pending_txs.extend(txs);
Ok(pending_txs)
let mut pending_payments = daemon.list_pending_payments().await?;
pending_payments.extend(payments);
Ok(pending_payments)
},
Message::HistoryTransactions,
Message::Payments,
),
Command::perform(
async move {
Expand Down
3 changes: 2 additions & 1 deletion liana-gui/src/app/state/psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use liana_ui::{
widget::Element,
};

use crate::daemon::model::LabelsLoader;
use crate::{
app::{
cache::Cache,
Expand Down Expand Up @@ -193,7 +194,7 @@ impl PsbtState {
match self.labels_edited.update(
daemon,
message,
std::iter::once(&mut self.tx).map(|tx| tx as &mut dyn Labelled),
std::iter::once(&mut self.tx).map(|tx| tx as &mut dyn LabelsLoader),
) {
Ok(cmd) => {
return cmd;
Expand Down
3 changes: 2 additions & 1 deletion liana-gui/src/app/state/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use liana::miniscript::bitcoin::{
};
use liana_ui::{component::modal, widget::*};

use crate::daemon::model::LabelsLoader;
use crate::{
app::{
cache::Cache,
Expand Down Expand Up @@ -117,7 +118,7 @@ impl State for ReceivePanel {
match self.labels_edited.update(
daemon,
message,
std::iter::once(&mut self.addresses).map(|a| a as &mut dyn Labelled),
std::iter::once(&mut self.addresses).map(|a| a as &mut dyn LabelsLoader),
) {
Ok(cmd) => cmd,
Err(e) => {
Expand Down
15 changes: 9 additions & 6 deletions liana-gui/src/app/state/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
view,
wallet::Wallet,
},
daemon::model,
daemon::model::{self, LabelsLoader},
};

use crate::daemon::{
Expand Down Expand Up @@ -192,11 +192,14 @@ impl State for TransactionsPanel {
match self.labels_edited.update(
daemon,
message,
self.txs.iter_mut().map(|tx| tx as &mut dyn Labelled).chain(
self.selected_tx
.iter_mut()
.map(|tx| tx as &mut dyn Labelled),
),
self.txs
.iter_mut()
.map(|tx| tx as &mut dyn LabelsLoader)
.chain(
self.selected_tx
.iter_mut()
.map(|tx| tx as &mut dyn LabelsLoader),
),
) {
Ok(cmd) => {
return cmd;
Expand Down
115 changes: 47 additions & 68 deletions liana-gui/src/app/view/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
view::{coins, dashboard, label, message::Message},
wallet::SyncStatus,
},
daemon::model::{HistoryTransaction, TransactionKind},
daemon::model::{HistoryTransaction, Payment, PaymentKind, TransactionKind},
};

#[allow(clippy::too_many_arguments)]
Expand All @@ -32,7 +32,7 @@ pub fn home_view<'a>(
unconfirmed_balance: &'a bitcoin::Amount,
remaining_sequence: &Option<u32>,
expiring_coins: &[bitcoin::OutPoint],
events: &'a [HistoryTransaction],
events: &'a [Payment],
is_last_page: bool,
processing: bool,
sync_status: &SyncStatus,
Expand Down Expand Up @@ -147,16 +147,9 @@ pub fn home_view<'a>(
Column::new()
.spacing(10)
.push(h4_bold("Last payments"))
.push(events.iter().enumerate().fold(
Column::new().spacing(10),
|col, (i, event)| {
if !event.is_send_to_self() {
col.push(event_list_view(i, event))
} else {
col
}
},
))
.push(events.iter().fold(Column::new().spacing(10), |col, event| {
col.push(event_list_view(event))
}))
.push_maybe(if !is_last_page && !events.is_empty() {
Some(
Container::new(
Expand Down Expand Up @@ -189,62 +182,48 @@ pub fn home_view<'a>(
.into()
}

fn event_list_view(i: usize, event: &HistoryTransaction) -> Column<'_, Message> {
event.tx.output.iter().enumerate().fold(
Column::new().spacing(10),
|col, (output_index, output)| {
let label = if let Some(label) = event.labels.get(
&bitcoin::OutPoint {
txid: event.tx.txid(),
vout: output_index as u32,
}
.to_string(),
) {
Some(p1_regular(label))
} else if let Ok(addr) =
bitcoin::Address::from_script(&output.script_pubkey, event.network)
{
event.labels.get(&addr.to_string()).map(|label| {
p1_regular(format!("address label: {}", label)).style(color::GREY_3)
})
} else {
None
};
if event.is_external() {
if !event.change_indexes.contains(&output_index) {
col
} else if let Some(t) = event.time {
col.push(event::confirmed_incoming_event(
label,
DateTime::<Utc>::from_timestamp(t as i64, 0).unwrap(),
&output.value,
Message::SelectSub(i, output_index),
))
} else {
col.push(event::unconfirmed_incoming_event(
label,
&output.value,
Message::SelectSub(i, output_index),
))
}
} else if event.change_indexes.contains(&output_index) {
col
} else if let Some(t) = event.time {
col.push(event::confirmed_outgoing_event(
label,
DateTime::<Utc>::from_timestamp(t as i64, 0).unwrap(),
&output.value,
Message::SelectSub(i, output_index),
))
} else {
col.push(event::unconfirmed_outgoing_event(
label,
&output.value,
Message::SelectSub(i, output_index),
))
}
},
)
fn event_list_view(event: &Payment) -> Element<'_, Message> {
let label = if let Some(label) = &event.label {
Some(p1_regular(label))
} else {
event
.address_label
.as_ref()
.map(|label| p1_regular(format!("address label: {}", label)).style(color::GREY_3))
};
if event.kind == PaymentKind::Incoming {
if let Some(t) = event.time {
event::confirmed_incoming_event(
label,
t,
&event.amount,
Message::SelectPayment(event.outpoint),
)
.into()
} else {
event::unconfirmed_incoming_event(
label,
&event.amount,
Message::SelectPayment(event.outpoint),
)
.into()
}
} else if let Some(t) = event.time {
event::confirmed_outgoing_event(
label,
t,
&event.amount,
Message::SelectPayment(event.outpoint),
)
.into()
} else {
event::unconfirmed_outgoing_event(
label,
&event.amount,
Message::SelectPayment(event.outpoint),
)
.into()
}
}

pub fn payment_view<'a>(
Expand Down
Loading

0 comments on commit 790beaa

Please sign in to comment.