forked from wizardsardine/liana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
158 lines (140 loc) · 4.65 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
mod step;
use std::collections::HashSet;
use std::sync::Arc;
use iced::Command;
use liana::{
commands::CoinStatus,
miniscript::bitcoin::{Network, OutPoint},
};
use liana_ui::widget::Element;
use super::{redirect, State};
use crate::{
app::{cache::Cache, error::Error, menu::Menu, message::Message, view, wallet::Wallet},
daemon::{
model::{Coin, LabelItem},
Daemon,
},
};
pub struct CreateSpendPanel {
draft: step::TransactionDraft,
current: usize,
steps: Vec<Box<dyn step::Step>>,
}
impl CreateSpendPanel {
pub fn new(wallet: Arc<Wallet>, coins: &[Coin], blockheight: u32, network: Network) -> Self {
let descriptor = wallet.main_descriptor.clone();
let timelock = descriptor.first_timelock_value();
Self {
draft: step::TransactionDraft::new(network),
current: 0,
steps: vec![
Box::new(
step::DefineSpend::new(network, descriptor, coins, timelock)
.with_coins_sorted(blockheight),
),
Box::new(step::SaveSpend::new(wallet)),
],
}
}
pub fn new_self_send(
wallet: Arc<Wallet>,
coins: &[Coin],
blockheight: u32,
preselected_coins: &[OutPoint],
network: Network,
) -> Self {
let descriptor = wallet.main_descriptor.clone();
let timelock = descriptor.first_timelock_value();
Self {
draft: step::TransactionDraft::new(network),
current: 0,
steps: vec![
Box::new(
step::DefineSpend::new(network, descriptor, coins, timelock)
.with_preselected_coins(preselected_coins)
.with_coins_sorted(blockheight)
.self_send(),
),
Box::new(step::SaveSpend::new(wallet)),
],
}
}
pub fn is_first_step(&self) -> bool {
self.current == 0
}
}
impl State for CreateSpendPanel {
fn view<'a>(&'a self, cache: &'a Cache) -> Element<'a, view::Message> {
self.steps.get(self.current).unwrap().view(cache)
}
fn subscription(&self) -> iced::Subscription<Message> {
self.steps.get(self.current).unwrap().subscription()
}
fn update(
&mut self,
daemon: Arc<dyn Daemon + Sync + Send>,
cache: &Cache,
message: Message,
) -> Command<Message> {
if matches!(message, Message::View(view::Message::Close)) {
return redirect(Menu::PSBTs);
}
if matches!(message, Message::View(view::Message::Next)) {
if let Some(step) = self.steps.get(self.current) {
step.apply(&mut self.draft);
}
if let Some(step) = self.steps.get_mut(self.current + 1) {
self.current += 1;
step.load(&self.draft);
}
}
if matches!(message, Message::View(view::Message::Previous))
&& self.steps.get(self.current - 1).is_some()
{
self.current -= 1;
}
if let Some(step) = self.steps.get_mut(self.current) {
return step.update(daemon, cache, message);
}
Command::none()
}
fn reload(
&mut self,
daemon: Arc<dyn Daemon + Sync + Send>,
_wallet: Arc<Wallet>,
) -> Command<Message> {
let daemon1 = daemon.clone();
let daemon2 = daemon.clone();
Command::batch(vec![
Command::perform(
async move {
daemon1
.list_coins(&[CoinStatus::Unconfirmed, CoinStatus::Confirmed], &[])
.map(|res| res.coins)
.map_err(|e| e.into())
},
Message::Coins,
),
Command::perform(
async move {
let coins = daemon
.list_coins(&[CoinStatus::Unconfirmed, CoinStatus::Confirmed], &[])
.map(|res| res.coins)
.map_err(Error::from)?;
let mut targets = HashSet::<LabelItem>::new();
for coin in coins {
targets.insert(LabelItem::OutPoint(coin.outpoint));
targets.insert(LabelItem::Txid(coin.outpoint.txid));
}
daemon2.get_labels(&targets).map_err(|e| e.into())
},
Message::Labels,
),
])
}
}
impl From<CreateSpendPanel> for Box<dyn State> {
fn from(s: CreateSpendPanel) -> Box<dyn State> {
Box::new(s)
}
}