-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathactivity_transaction_status_screen.dart
More file actions
557 lines (519 loc) · 17.1 KB
/
Copy pathactivity_transaction_status_screen.dart
File metadata and controls
557 lines (519 loc) · 17.1 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../../main.dart' show log;
import '../../../core/formatting/zec_amount.dart';
import '../../../core/config/zcash_explorer.dart';
import '../../../core/layout/app_desktop_shell.dart';
import '../../../core/layout/app_layout.dart';
import '../../../core/layout/app_main_sidebar.dart';
import '../../../core/privacy/privacy_mask.dart';
import '../../../core/storage/wallet_paths.dart';
import '../../../core/theme/app_theme.dart';
import '../../../core/widgets/app_back_link.dart';
import '../../../core/widgets/app_toast.dart';
import '../../../providers/account_provider.dart';
import '../../../providers/privacy_mode_provider.dart';
import '../../../providers/rpc_endpoint_provider.dart';
import '../../../providers/sync_provider.dart';
import '../../../providers/zns_provider.dart';
import '../../../rust/api/sync.dart' as rust_sync;
import '../../send/widgets/transaction_receipt_view.dart';
class ActivityTransactionStatusArgs {
const ActivityTransactionStatusArgs({
required this.txidHex,
this.txKind,
this.initialTransaction,
this.initialDetail,
});
final String txidHex;
final String? txKind;
final rust_sync.TransactionInfo? initialTransaction;
final rust_sync.TransactionDetail? initialDetail;
}
class ActivityTransactionStatusScreen extends ConsumerStatefulWidget {
const ActivityTransactionStatusScreen({super.key, required this.args});
final ActivityTransactionStatusArgs args;
@override
ConsumerState<ActivityTransactionStatusScreen> createState() =>
_ActivityTransactionStatusScreenState();
}
class _ActivityTransactionStatusScreenState
extends ConsumerState<ActivityTransactionStatusScreen> {
rust_sync.TransactionInfo? _transaction;
rust_sync.TransactionDetail? _detail;
bool _isLoading = false;
String? _error;
String? _activeAccountUuid;
bool _messageExpanded = false;
String? _znsName;
int _znsSeq = 0;
@override
void initState() {
super.initState();
_transaction = widget.args.initialTransaction;
_detail = widget.args.initialDetail;
_activeAccountUuid = ref.read(accountProvider).value?.activeAccountUuid;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
ref.read(appLayoutProvider.notifier).setMode(AppLayoutMode.large);
unawaited(_loadTransaction(showLoading: _transaction == null));
_triggerZnsLookup(widget.args.initialDetail?.primaryAddress);
});
}
Future<void> _loadTransaction({bool showLoading = false}) async {
final accountUuid = ref.read(accountProvider).value?.activeAccountUuid;
_activeAccountUuid = accountUuid;
if (showLoading && mounted) {
setState(() {
_isLoading = true;
_error = null;
});
}
if (accountUuid == null) {
if (!mounted) return;
setState(() {
_isLoading = false;
_error = 'No active account.';
});
return;
}
try {
final dbPath = await getWalletDbPath();
final endpoint = ref.read(rpcEndpointProvider);
final txs = await rust_sync.getTransactionHistory(
dbPath: dbPath,
network: endpoint.networkName,
accountUuid: accountUuid,
);
if (!mounted) return;
if (accountUuid != ref.read(accountProvider).value?.activeAccountUuid) {
return;
}
final tx = _findTransaction(
txs,
widget.args.txidHex,
txKind:
_transaction?.txKind ??
widget.args.initialTransaction?.txKind ??
widget.args.txKind,
);
rust_sync.TransactionDetail? detail;
if (tx != null) {
try {
detail = rust_sync.getTransactionDetail(
dbPath: dbPath,
network: endpoint.networkName,
accountUuid: accountUuid,
txidHex: tx.txidHex,
txKind: tx.txKind,
);
} catch (e, st) {
log('ActivityTransactionStatus: detail load failed: $e\n$st');
}
if (!mounted) return;
if (accountUuid != ref.read(accountProvider).value?.activeAccountUuid) {
return;
}
}
setState(() {
if (tx != null) {
_transaction = tx;
_detail = detail;
_error = null;
} else {
_detail = null;
_error = _transaction == null
? 'Transaction could not be loaded.'
: 'Latest transaction status could not be refreshed.';
}
_isLoading = false;
});
if (tx != null) _triggerZnsLookup(detail?.primaryAddress);
} catch (e, st) {
log('ActivityTransactionStatus: transaction load failed: $e\n$st');
if (!mounted) return;
setState(() {
_detail = null;
_error = _transaction == null
? 'Transaction could not be loaded.'
: 'Latest transaction status could not be refreshed.';
_isLoading = false;
});
}
}
rust_sync.TransactionInfo? _findTransaction(
Iterable<rust_sync.TransactionInfo> transactions,
String txidHex, {
String? txKind,
}) {
final normalized = txidHex.toLowerCase();
if (txKind != null) {
for (final tx in transactions) {
if (tx.txidHex.toLowerCase() == normalized &&
_txKindMatches(txKind, tx.txKind)) {
return tx;
}
}
return null;
}
for (final tx in transactions) {
if (tx.txidHex.toLowerCase() == normalized) return tx;
}
return null;
}
String _recentTxSignature(SyncState? sync) {
final txid = widget.args.txidHex.toLowerCase();
final txKind =
_transaction?.txKind ??
widget.args.initialTransaction?.txKind ??
widget.args.txKind;
if (txKind != null) {
for (final tx in sync?.recentTransactions ?? const []) {
if (tx.txidHex.toLowerCase() == txid &&
_txKindMatches(txKind, tx.txKind)) {
return [
tx.txidHex,
tx.minedHeight,
tx.expiredUnmined,
tx.txKind,
tx.displayAmount,
].join(':');
}
}
return '';
}
for (final tx in sync?.recentTransactions ?? const []) {
if (tx.txidHex.toLowerCase() == txid) {
return [
tx.txidHex,
tx.minedHeight,
tx.expiredUnmined,
tx.txKind,
tx.displayAmount,
].join(':');
}
}
return '';
}
bool _txKindMatches(String expected, String actual) {
if (expected == actual) return true;
return (expected == 'receiving' && actual == 'received') ||
(expected == 'received' && actual == 'receiving');
}
void _triggerZnsLookup(String? address) {
if (address == null || address.isEmpty) return;
final seq = ++_znsSeq;
setState(() => _znsName = null);
unawaited(_resolveZnsName(address, seq));
}
Future<void> _resolveZnsName(String address, int seq) async {
try {
final name = await ref.read(znsResolverProvider).reverseResolve(address);
if (!mounted || seq != _znsSeq) return;
setState(() => _znsName = name);
} catch (e) {
log('ActivityTransactionStatus: ZNS reverse lookup failed: $e');
}
}
Future<void> _copyTransactionHash() async {
await _copyText(widget.args.txidHex, 'Transaction Hash Copied');
}
Future<void> _openTransactionExplorer() async {
final endpoint = ref.read(rpcEndpointProvider);
final launched = await launchZcashExplorerTransaction(
networkName: endpoint.networkName,
txidHex: widget.args.txidHex,
txidOrder: ZcashExplorerTxidOrder.protocol,
);
if (launched || !mounted) return;
await _copyText(widget.args.txidHex, 'Transaction Hash Copied');
}
Future<void> _copyText(String text, String message) async {
await Clipboard.setData(ClipboardData(text: text));
if (!mounted) return;
showAppToast(context, message);
}
void _toggleMessageExpanded() {
setState(() {
_messageExpanded = !_messageExpanded;
});
}
TransactionReceiptPhase _phaseFor(rust_sync.TransactionInfo? tx) {
if (tx == null) {
return _isLoading
? TransactionReceiptPhase.loading
: TransactionReceiptPhase.failed;
}
if (tx.expiredUnmined) return TransactionReceiptPhase.failed;
if (tx.minedHeight == BigInt.zero) return TransactionReceiptPhase.pending;
return TransactionReceiptPhase.succeeded;
}
String _amountText(
rust_sync.TransactionInfo? tx, {
required bool privacyModeEnabled,
}) {
if (tx == null) return '--';
if (privacyModeEnabled) {
return hideAmountIfPrivacyMode('', privacyModeEnabled: true);
}
if (tx.displayAmount == BigInt.zero) return '--';
return hideAmountIfPrivacyMode(
ZecAmount.fromZatoshi(tx.displayAmount).activityDetail.toString(),
privacyModeEnabled: privacyModeEnabled,
);
}
String _dateText(rust_sync.TransactionInfo? tx) {
if (tx == null) return '--';
final seconds = tx.blockTime > BigInt.zero ? tx.blockTime : tx.createdTime;
if (seconds <= BigInt.zero) return '--';
return _formatDate(
DateTime.fromMillisecondsSinceEpoch(seconds.toInt() * 1000),
);
}
String _feeText(
rust_sync.TransactionInfo? tx, {
required bool privacyModeEnabled,
}) {
if (tx == null || tx.fee <= BigInt.zero) return '--';
return hideAmountIfPrivacyMode(
ZecAmount.fromZatoshi(tx.fee).fee.toString(),
privacyModeEnabled: privacyModeEnabled,
);
}
String _formatDate(DateTime value) {
const months = <String>[
'',
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
final local = value.toLocal();
final hh = local.hour.toString().padLeft(2, '0');
final mm = local.minute.toString().padLeft(2, '0');
return '${months[local.month]} ${local.day}, ${local.year} $hh:$mm';
}
List<String> _splitTxid(String txid) {
if (txid.length <= 32) return [txid];
return [txid.substring(0, 32), txid.substring(32)];
}
rust_sync.TransactionDetail? _matchingDetailFor(
rust_sync.TransactionInfo? tx,
) {
final detail = _detail;
if (tx == null || detail == null) return null;
if (detail.txidHex.toLowerCase() != tx.txidHex.toLowerCase()) {
return null;
}
if (!_txKindMatches(detail.txKind, tx.txKind)) return null;
return detail;
}
TransactionReceiptBlockData _transactionHashBlock(BuildContext context) {
final colors = context.colors;
final txidLines = _splitTxid(widget.args.txidHex);
return TransactionReceiptBlockData(
title: 'Transaction Hash',
onCopy: () => unawaited(_copyTransactionHash()),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (final line in txidLines)
Text(
line,
style: AppTypography.codeSmall.copyWith(
color: colors.text.accent,
),
),
],
),
);
}
TransactionReceiptBlockData _addressBlock(
BuildContext context, {
required String title,
required String address,
String? znsName,
bool useFailedReceiptLayout = false,
bool compactAddress = false,
}) {
final colors = context.colors;
final trimmedAddress = address.trim();
return TransactionReceiptBlockData(
title: title,
onCopy: () => unawaited(_copyText(trimmedAddress, 'Address Copied')),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (znsName != null) ...[
Text(
znsName,
style: AppTypography.labelMedium.copyWith(
color: colors.text.secondary,
),
),
const SizedBox(height: AppSpacing.xxs),
],
TransactionReceiptAddressText(
address: trimmedAddress,
highlightEdges: _shouldHighlightAddressEdges(trimmedAddress),
compact: compactAddress,
highlightColor: useFailedReceiptLayout
? colors.text.destructive
: colors.text.success,
),
],
),
);
}
bool _shouldHighlightAddressEdges(String address) {
return address.startsWith('u1') || address.startsWith('zs');
}
TransactionReceiptBlockData _primaryBlockFor(
BuildContext context,
rust_sync.TransactionInfo? tx,
rust_sync.TransactionDetail? detail,
) {
final primaryAddress = detail?.primaryAddress?.trim();
final useFailedReceiptLayout = tx?.expiredUnmined == true;
final compactAddress = detail?.memo?.trim().isNotEmpty == true;
if (tx?.txKind == 'sent' &&
primaryAddress != null &&
primaryAddress.isNotEmpty) {
return _addressBlock(
context,
title: 'To',
address: primaryAddress,
znsName: _znsName,
useFailedReceiptLayout: useFailedReceiptLayout,
compactAddress: compactAddress,
);
}
if ((tx?.txKind == 'received' || tx?.txKind == 'receiving') &&
primaryAddress != null &&
primaryAddress.isNotEmpty) {
return _addressBlock(
context,
title: 'From',
address: primaryAddress,
useFailedReceiptLayout: useFailedReceiptLayout,
compactAddress: compactAddress,
);
}
return _transactionHashBlock(context);
}
List<TransactionReceiptBlockData> _extraBlocksFor(
rust_sync.TransactionDetail? detail,
) {
final memo = detail?.memo?.trim();
if (memo == null || memo.isEmpty) return const [];
return [
TransactionReceiptBlockData(
title: 'Message',
titleTrailing: TransactionReceiptMessageToggle(
expanded: _messageExpanded,
onTap: _toggleMessageExpanded,
),
child: TransactionReceiptMessageText(
memo: memo,
expanded: _messageExpanded,
),
),
];
}
@override
Widget build(BuildContext context) {
ref.listen<AsyncValue<AccountState>>(accountProvider, (previous, next) {
final nextUuid = next.value?.activeAccountUuid;
if (nextUuid != _activeAccountUuid) {
unawaited(_loadTransaction(showLoading: _transaction == null));
}
});
ref.listen<AsyncValue<SyncState>>(syncProvider, (previous, next) {
final prevSig = _recentTxSignature(previous?.value);
final nextSig = _recentTxSignature(next.value);
if (prevSig != nextSig) {
unawaited(_loadTransaction());
}
});
final tx = _transaction;
final detail = _matchingDetailFor(tx);
final privacyModeEnabled = ref.watch(privacyModeProvider);
final useFailedReceiptLayout = tx?.expiredUnmined == true;
final error = useFailedReceiptLayout
? 'Transaction expired before it was mined.'
: _error;
return AppDesktopShell(
sidebar: const AppMainSidebar(),
pane: AppDesktopPane(
padding: EdgeInsets.zero,
child: Stack(
children: [
Positioned.fill(
child: IgnorePointer(
child: TransactionReceiptIllustration(
failed: useFailedReceiptLayout,
),
),
),
Positioned.fill(
child: Padding(
padding: const EdgeInsets.fromLTRB(
AppSpacing.md,
AppSpacing.md,
0,
AppSpacing.md,
),
child: Column(
children: [
const Align(
alignment: Alignment.centerLeft,
child: AppRouteBackLink(),
),
const SizedBox(height: AppSpacing.s),
Expanded(
child: Padding(
padding: const EdgeInsets.only(right: 255),
child: Align(
alignment: Alignment.centerLeft,
child: TransactionReceiptView(
phase: _phaseFor(tx),
amountText: _amountText(
tx,
privacyModeEnabled: privacyModeEnabled,
),
primaryBlock: _primaryBlockFor(context, tx, detail),
extraBlocks: _extraBlocksFor(detail),
dateText: _dateText(tx),
feeText: _feeText(
tx,
privacyModeEnabled: privacyModeEnabled,
),
error: error,
useFailedReceiptLayout: useFailedReceiptLayout,
showPrimaryCopyAction: true,
pinActionsToBottom: true,
onTransactionHashPressed: _openTransactionExplorer,
),
),
),
),
],
),
),
),
],
),
),
);
}
}