Skip to content

Commit

Permalink
Add tx flowchart for visual entertainment
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardparis committed Feb 15, 2022
1 parent 1357289 commit 05e7f09
Show file tree
Hide file tree
Showing 13 changed files with 825 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bitcoin = { version = "0.27", features = ["base64", "use-serde"] }
revaultd = { git = "https://github.com/revault/revaultd", branch = "master", default-features = false}
backtrace = "0.3"

iced = { version = "0.3", default-features= false, features = ["tokio", "wgpu", "svg", "qr_code"] }
iced = { version = "0.3", default-features= false, features = ["tokio", "wgpu", "svg", "qr_code", "canvas"] }
revault_ui = { path = "./ui" }
revault_hwi = { path = "./hwi" }
iced_native = "0.4"
Expand Down
6 changes: 5 additions & 1 deletion src/app/message.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use bitcoin::{util::psbt::PartiallySignedTransaction as Psbt, OutPoint};
use revault_hwi::{app::revault::RevaultHWI, HWIError};
use std::sync::Arc;
use tokio::sync::Mutex;

use revault_hwi::{app::revault::RevaultHWI, HWIError};
use revault_ui::chart::FlowChartMessage;

use crate::{
app::{error::Error, menu::Menu},
daemon::{
Expand Down Expand Up @@ -77,6 +79,8 @@ pub enum SpendTxMessage {
#[derive(Debug, Clone)]
pub enum HistoryEventMessage {
OnChainTransactions(Result<Vec<VaultTransactions>, RevaultDError>),
ToggleFlowChart(bool),
FlowChart(FlowChartMessage),
}

#[derive(Debug, Clone)]
Expand Down
105 changes: 95 additions & 10 deletions src/app/state/history.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::convert::TryInto;
use std::time::{SystemTime, UNIX_EPOCH};

use bitcoin::Txid;
use iced::{Command, Element};

use super::State;

use revault_ui::chart::{FlowChart, FlowChartMessage};

use crate::{
app::{
context::Context,
Expand All @@ -13,7 +16,10 @@ use crate::{
view::LoadingDashboard,
view::{HistoryEventListItemView, HistoryEventView, HistoryView},
},
daemon::model::{HistoryEvent, HistoryEventKind, VaultTransactions, ALL_HISTORY_EVENTS},
daemon::model::{
HistoryEvent, HistoryEventKind, HistoryEventTransaction, TransactionKind,
ALL_HISTORY_EVENTS,
},
};

pub const HISTORY_EVENT_PAGE_SIZE: u64 = 20;
Expand Down Expand Up @@ -93,7 +99,7 @@ impl State for HistoryState {
}
Message::HistoryEvent(msg) => {
if let Some(event) = selected_event {
event.update(msg)
event.update(ctx, msg)
}
}
Message::Close => {
Expand Down Expand Up @@ -283,7 +289,9 @@ impl HistoryEventListItemState {
#[derive(Debug)]
pub struct HistoryEventState {
event: HistoryEvent,
txs: Vec<VaultTransactions>,
txs: Vec<HistoryEventTransaction>,
selected_tx: Option<Txid>,
flowchart: Option<FlowChart>,
loading_fail: Option<Error>,
view: HistoryEventView,
}
Expand All @@ -293,22 +301,99 @@ impl HistoryEventState {
Self {
event,
txs: Vec::new(),
flowchart: None,
selected_tx: None,
loading_fail: None,
view: HistoryEventView::new(),
}
}

pub fn update(&mut self, message: HistoryEventMessage) {
let HistoryEventMessage::OnChainTransactions(res) = message;
match res {
Ok(txs) => self.txs = txs,
Err(e) => self.loading_fail = Some(e.into()),
pub fn update(&mut self, ctx: &Context, message: HistoryEventMessage) {
match message {
HistoryEventMessage::ToggleFlowChart(toggle) => {
if toggle {
self.flowchart = Some(FlowChart::new(
ctx.network(),
self.txs
.iter()
.map(|event_tx| event_tx.tx.clone())
.collect(),
));
} else {
self.flowchart = None;
}
}
HistoryEventMessage::FlowChart(FlowChartMessage::TxSelected(txid)) => {
if self.selected_tx.is_none() {
self.selected_tx = txid;
} else {
self.selected_tx = None;
}
}
HistoryEventMessage::OnChainTransactions(res) => match res {
Ok(vault_txs) => {
let mut list: Vec<HistoryEventTransaction> = Vec::new();
for txs in vault_txs {
list.push(HistoryEventTransaction::new(
&txs.deposit,
TransactionKind::Deposit,
));

if let Some(unvault) = txs.unvault {
list.push(HistoryEventTransaction::new(
&unvault,
TransactionKind::Unvault,
));
}
if let Some(cancel) = txs.cancel {
list.push(HistoryEventTransaction::new(
&cancel,
TransactionKind::Cancel,
));
}
if let Some(spend) = txs.spend {
list.push(HistoryEventTransaction::new(&spend, TransactionKind::Spend));
}
if let Some(unvault_emergency) = txs.unvault_emergency {
list.push(HistoryEventTransaction::new(
&unvault_emergency,
TransactionKind::UnvaultEmergency,
));
}
if let Some(emergency) = txs.emergency {
list.push(HistoryEventTransaction::new(
&emergency,
TransactionKind::Emergency,
));
}
}

list.sort_by(|a, b| a.blockheight.cmp(&b.blockheight));
self.txs = list;
}
Err(e) => self.loading_fail = Some(e.into()),
},
}
}

pub fn view(&mut self, ctx: &Context) -> Element<Message> {
self.view
.view(ctx, &self.event, &self.txs, self.loading_fail.as_ref())
let selected = if let Some(txid) = self.selected_tx {
self.txs.iter().find(|vault_tx| vault_tx.tx.txid() == txid)
} else {
None
};
self.view.view(
ctx,
&self.event,
&self.txs,
selected,
self.flowchart.as_mut().map(|chart| {
chart
.view()
.map(|msg| Message::HistoryEvent(HistoryEventMessage::FlowChart(msg)))
}),
self.loading_fail.as_ref(),
)
}

pub fn load(&self, ctx: &Context) -> Command<Message> {
Expand Down
2 changes: 1 addition & 1 deletion src/app/state/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl State for ManagerHomeState {
}
Message::HistoryEvent(msg) => {
if let Some(event) = selected_event {
event.update(msg)
event.update(ctx, msg)
}
}
Message::Close => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/state/stakeholder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl State for StakeholderHomeState {
}
Message::HistoryEvent(msg) => {
if let Some(event) = selected_event {
event.update(msg)
event.update(ctx, msg)
}
}
Message::Close => {
Expand Down
Loading

0 comments on commit 05e7f09

Please sign in to comment.