-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathmasternodes_home_view.dart
More file actions
420 lines (394 loc) · 13.7 KB
/
masternodes_home_view.dart
File metadata and controls
420 lines (394 loc) · 13.7 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:isar_community/isar.dart';
import '../../providers/global/wallets_provider.dart';
import '../../themes/stack_colors.dart';
import '../../utilities/amount/amount.dart';
import '../../utilities/assets.dart';
import '../../utilities/logger.dart';
import '../../utilities/text_styles.dart';
import '../../utilities/util.dart';
import '../../wallets/wallet/impl/firo_wallet.dart';
import '../../widgets/custom_buttons/app_bar_icon_button.dart';
import '../../widgets/desktop/desktop_app_bar.dart';
import '../../widgets/desktop/desktop_scaffold.dart';
import '../../widgets/desktop/primary_button.dart';
import '../../widgets/dialogs/s_dialog.dart';
import '../../widgets/loading_indicator.dart';
import '../../widgets/stack_dialog.dart';
import 'create_masternode_view.dart';
import 'sub_widgets/masternodes_list.dart';
import 'sub_widgets/masternodes_table_desktop.dart';
class MasternodesHomeView extends ConsumerStatefulWidget {
const MasternodesHomeView({super.key, required this.walletId});
final String walletId;
static const String routeName = "/masternodesHomeView";
@override
ConsumerState<MasternodesHomeView> createState() =>
_MasternodesHomeViewState();
}
class _MasternodesHomeViewState extends ConsumerState<MasternodesHomeView>
with WidgetsBindingObserver {
late Future<List<MasternodeInfo>> _masternodesFuture;
bool _hasPromptedForCollateral = false;
bool _isCheckingForCollateral = false;
Future<({String txid, int vout, String address})?> _findCollateralUtxo()
async {
final wallet =
ref.read(pWallets).getWallet(widget.walletId) as FiroWallet;
final utxos = await wallet.mainDB.getUTXOs(widget.walletId).findAll();
final currentChainHeight = await wallet.chainHeight;
final masternodeRaw = Amount.fromDecimal(
kMasterNodeValue,
fractionDigits: wallet.cryptoCurrency.fractionDigits,
).raw.toInt();
for (final utxo in utxos) {
if (utxo.value == masternodeRaw &&
!utxo.isBlocked &&
utxo.used != true &&
utxo.isConfirmed(
currentChainHeight,
wallet.cryptoCurrency.minConfirms,
wallet.cryptoCurrency.minCoinbaseConfirms,
) &&
utxo.address != null) {
return (txid: utxo.txid, vout: utxo.vout, address: utxo.address!);
}
}
return null;
}
Future<void> _createMasternode() async {
final collateral = await _findCollateralUtxo();
if (!mounted) {
return;
}
if (collateral == null) {
await showDialog<void>(
context: context,
builder: (_) => StackOkDialog(
title: "No collateral found",
message:
"A masternode needs one confirmed, unblocked transparent "
"UTXO of exactly 1000 FIRO.\n\n"
"Total balance above 1000 FIRO is not enough if no single "
"1000 output exists. Also ensure fee is not subtracted from "
"the recipient amount when sending to yourself.",
desktopPopRootNavigator: Util.isDesktop,
maxWidth: Util.isDesktop ? 400 : null,
),
);
return;
}
if (Util.isDesktop) {
final txid = await showDialog<Object>(
context: context,
barrierDismissible: true,
builder: (context) => SDialog(
child: CreateMasternodeView(
firoWalletId: widget.walletId,
collateralTxid: collateral.txid,
collateralVout: collateral.vout,
collateralAddress: collateral.address,
),
),
);
_handleSuccessTxid(txid);
} else {
final txid = await Navigator.of(context).pushNamed(
CreateMasternodeView.routeName,
arguments: {
'walletId': widget.walletId,
'collateralTxid': collateral.txid,
'collateralVout': collateral.vout,
'collateralAddress': collateral.address,
},
);
_handleSuccessTxid(txid);
}
}
Future<void> _maybePromptForExistingCollateral() async {
if (_hasPromptedForCollateral || _isCheckingForCollateral || !mounted) {
return;
}
_isCheckingForCollateral = true;
try {
final collateral = await _findCollateralUtxo();
if (collateral == null || !mounted) {
return;
}
_hasPromptedForCollateral = true;
final wantsMN = await showDialog<bool>(
context: context,
barrierDismissible: true,
builder: (ctx) => StackDialog(
title: "Register Masternode?",
message:
"A 1000 FIRO collateral UTXO was found in your wallet. "
"Would you like to register a masternode now?",
leftButton: TextButton(
style: Theme.of(ctx)
.extension<StackColors>()!
.getSecondaryEnabledButtonStyle(ctx),
child: Text(
"Later",
style: STextStyles.button(
ctx,
).copyWith(
color: Theme.of(ctx).extension<StackColors>()!.accentColorDark,
),
),
onPressed: () => Navigator.of(ctx).pop(false),
),
rightButton: TextButton(
style: Theme.of(ctx)
.extension<StackColors>()!
.getPrimaryEnabledButtonStyle(ctx),
child: Text(
"Register",
style: STextStyles.button(ctx),
),
onPressed: () => Navigator.of(ctx).pop(true),
),
),
);
if (wantsMN != true || !mounted) {
return;
}
if (Util.isDesktop) {
final txid = await showDialog<Object>(
context: context,
barrierDismissible: true,
builder: (context) => SDialog(
child: CreateMasternodeView(
firoWalletId: widget.walletId,
collateralTxid: collateral.txid,
collateralVout: collateral.vout,
collateralAddress: collateral.address,
),
),
);
_handleSuccessTxid(txid);
} else {
final txid = await Navigator.of(context).pushNamed(
CreateMasternodeView.routeName,
arguments: {
'walletId': widget.walletId,
'collateralTxid': collateral.txid,
'collateralVout': collateral.vout,
'collateralAddress': collateral.address,
},
);
_handleSuccessTxid(txid);
}
} finally {
_isCheckingForCollateral = false;
}
}
void _handleSuccessTxid(Object? txid) {
Logging.instance.i(
"$runtimeType _handleSuccessTxid($txid) called where mounted=$mounted",
);
if (mounted && txid is String) {
setState(() {
_masternodesFuture =
(ref.read(pWallets).getWallet(widget.walletId) as FiroWallet)
.getMyMasternodes();
});
showDialog<void>(
context: context,
builder: (_) => StackOkDialog(
title: "Masternode Registration Submitted",
message:
"Masternode registration submitted, your masternode will "
"appear in the list after the tx is confirmed.\n\nTransaction"
" ID: $txid",
desktopPopRootNavigator: Util.isDesktop,
maxWidth: Util.isDesktop ? 400 : null,
),
);
}
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
// TODO polling and update on successful registration
_masternodesFuture =
(ref.read(pWallets).getWallet(widget.walletId) as FiroWallet)
.getMyMasternodes();
WidgetsBinding.instance.addPostFrameCallback((_) {
unawaited(_maybePromptForExistingCollateral());
});
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
unawaited(_maybePromptForExistingCollateral());
}
}
@override
Widget build(BuildContext context) {
final isDesktop = Util.isDesktop;
return MasterScaffold(
isDesktop: isDesktop,
appBar: isDesktop
? DesktopAppBar(
isCompactHeight: true,
background: Theme.of(context).extension<StackColors>()!.popupBG,
leading: Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 24, right: 20),
child: AppBarIconButton(
size: 32,
color: Theme.of(
context,
).extension<StackColors>()!.textFieldDefaultBG,
shadows: const [],
icon: SvgPicture.asset(
Assets.svg.arrowLeft,
width: 18,
height: 18,
colorFilter: ColorFilter.mode(
Theme.of(
context,
).extension<StackColors>()!.topNavIconPrimary,
BlendMode.srcIn,
),
),
onPressed: Navigator.of(context).pop,
),
),
SvgPicture.asset(
Assets.svg.robotHead,
width: 32,
height: 32,
colorFilter: ColorFilter.mode(
Theme.of(context).extension<StackColors>()!.textDark,
BlendMode.srcIn,
),
),
const SizedBox(width: 10),
Text("Masternodes", style: STextStyles.desktopH3(context)),
],
),
trailing: Padding(
padding: const EdgeInsets.only(right: 24),
child: PrimaryButton(
label: "Create Masternode",
buttonHeight: .l,
horizontalContentPadding: 10,
icon: SvgPicture.asset(
Assets.svg.circlePlus,
colorFilter: ColorFilter.mode(
Theme.of(
context,
).extension<StackColors>()!.buttonTextPrimary,
.srcIn,
),
),
onPressed: _createMasternode,
),
),
)
: AppBar(
leading: AppBarBackButton(
onPressed: () => Navigator.of(context).pop(),
),
titleSpacing: 0,
title: Text(
"Masternodes",
style: STextStyles.navBarTitle(context),
overflow: TextOverflow.ellipsis,
),
actions: [
Padding(
padding: const EdgeInsets.only(
top: 10,
bottom: 10,
right: 10,
),
child: AspectRatio(
aspectRatio: 1,
child: AppBarIconButton(
key: const Key("createNewMasterNodeButton"),
size: 36,
shadows: const [],
color: Theme.of(
context,
).extension<StackColors>()!.background,
icon: SvgPicture.asset(
Assets.svg.plus,
colorFilter: ColorFilter.mode(
Theme.of(
context,
).extension<StackColors>()!.accentColorDark,
.srcIn,
),
width: 20,
height: 20,
),
onPressed: _createMasternode,
),
),
),
],
),
body: FutureBuilder<List<MasternodeInfo>>(
future: _masternodesFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: LoadingIndicator(height: 50, width: 50));
}
if (snapshot.hasError) {
return Center(
child: Text(
"Failed to load masternodes",
style: STextStyles.w600_14(context),
),
);
}
final nodes = snapshot.data ?? const <MasternodeInfo>[];
if (nodes.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"No masternodes found",
style: STextStyles.w600_14(context),
),
const SizedBox(height: 24),
Row(
mainAxisSize: .min,
mainAxisAlignment: .center,
children: [
PrimaryButton(
label: "Create Your First Masternode",
horizontalContentPadding: 16,
buttonHeight: Util.isDesktop ? .l : null,
onPressed: _createMasternode,
),
],
),
],
),
);
}
if (Util.isDesktop) {
return MasternodesTableDesktop(nodes: nodes);
} else {
return MasternodesList(nodes: nodes);
}
},
),
);
}
}