-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathreap_voucher_page.dart
244 lines (216 loc) · 8.21 KB
/
reap_voucher_page.dart
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
import 'package:ew_keyring/ew_keyring.dart';
import 'package:ew_test_keys/ew_test_keys.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:iconsax/iconsax.dart';
import 'package:provider/provider.dart';
import 'package:encointer_wallet/common/components/address_icon.dart';
import 'package:encointer_wallet/common/components/gradient_elements.dart';
import 'package:encointer_wallet/common/components/secondary_button_wide.dart';
import 'package:encointer_wallet/common/components/submit_button.dart';
import 'package:encointer_wallet/theme/theme.dart';
import 'package:encointer_wallet/models/communities/community_identifier.dart';
import 'package:encointer_wallet/page/assets/transfer/transfer_page.dart';
import 'package:encointer_wallet/page/qr_scan/qr_codes/index.dart';
import 'package:encointer_wallet/page/reap_voucher/dialogs.dart';
import 'package:encointer_wallet/page/reap_voucher/utils.dart';
import 'package:encointer_wallet/service/log/log_service.dart';
import 'package:encointer_wallet/service/substrate_api/api.dart';
import 'package:encointer_wallet/service/tx/lib/tx.dart';
import 'package:encointer_wallet/store/app.dart';
import 'package:encointer_wallet/utils/format.dart';
import 'package:encointer_wallet/l10n/l10.dart';
class ReapVoucherParams {
ReapVoucherParams({
required this.voucher,
this.showFundVoucher = false,
});
final VoucherData voucher;
final bool showFundVoucher;
}
class ReapVoucherPage extends StatefulWidget {
const ReapVoucherPage(this.api, this.voucher, this.showFundVoucher, {super.key});
static const String route = '/qrcode/voucher';
final Api api;
final VoucherData voucher;
final bool showFundVoucher;
@override
State<ReapVoucherPage> createState() => _ReapVoucherPageState();
}
class _ReapVoucherPageState extends State<ReapVoucherPage> {
late KeyringAccount _voucherKeyringAccount;
String? _voucherAddress;
double? _voucherBalance;
/// Is true when all the data has been fetched.
bool _isReady = false;
Future<void> fetchVoucherData(Api api, String voucherUri, CommunityIdentifier cid) async {
Log.d('Fetching voucher data...', 'ReapVoucherPage');
final store = context.read<AppStore>();
_voucherKeyringAccount = await KeyringAccount.fromUri('Voucher', voucherUri);
_voucherAddress = _voucherKeyringAccount.address(prefix: store.settings.endpoint.ss58 ?? 42).encode();
setState(() {});
final voucherBalanceEntry = await api.encointer.getEncointerBalance(_voucherAddress!, cid);
_voucherBalance = voucherBalanceEntry.applyDemurrage(
store.chain.latestHeaderNumber!,
store.encointer.community!.demurrage!,
);
_isReady = true;
setState(() {});
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback(
(_) async {
final l10n = context.l10n;
final voucher = widget.voucher;
final result = await _changeNetworkAndCommunityIfNeeded(context, voucher.network, voucher.cid);
if (result == ChangeResult.ok) {
await fetchVoucherData(widget.api, voucher.voucherUri, voucher.cid);
} else if (result == ChangeResult.invalidNetwork) {
await showErrorDialog(context, l10n.invalidNetwork);
} else if (result == ChangeResult.invalidCommunity) {
await showErrorDialog(context, l10n.invalidCommunity);
}
},
);
}
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
final store = context.read<AppStore>();
final h2Grey = context.titleLarge.copyWith(color: AppColors.encointerGrey);
final h4Grey = context.bodyLarge.copyWith(color: AppColors.encointerGrey);
final voucher = widget.voucher;
final recipient = Address.decode(store.account.currentAddress);
return Scaffold(
appBar: AppBar(title: Text(l10n.voucher)),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
child: Column(
children: [
SizedBox(
height: 96,
child: _voucherAddress != null
? AddressIcon(_voucherAddress!, _voucherAddress!)
: const CupertinoActivityIndicator(),
),
const SizedBox(height: 8),
Text(voucher.issuer, style: h2Grey),
SizedBox(
height: 80,
child: _voucherBalance != null
? TextGradient(
text: '${Fmt.doubleFormat(_voucherBalance)} ⵐ',
style: const TextStyle(fontSize: 60),
)
: const CupertinoActivityIndicator(),
),
Text('${l10n.voucherBalance}, ${store.encointer.community?.symbol}', style: h4Grey),
Expanded(
// fit: FlexFit.tight,
child: Center(
child: Text(
l10n.doYouWantToRedeemThisVoucher(store.account.currentAccount.name),
style: h2Grey,
textAlign: TextAlign.center,
),
),
),
if (widget.showFundVoucher)
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: SecondaryButtonWide(
key: const Key(EWTestKeys.voucherToTransferPage),
onPressed: _isReady ? () => _pushTransferPage(context, voucher, _voucherAddress!) : null,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Iconsax.login_1),
const SizedBox(width: 6),
Text(l10n.fundVoucher),
],
),
),
),
SubmitButton(
key: const Key(EWTestKeys.submitVoucher),
onPressed: _isReady
? (context) => _submitReapVoucher(context, _voucherKeyringAccount, voucher.cid, recipient)
: null,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Iconsax.login_1),
const SizedBox(width: 6),
Text(l10n.redeemVoucher),
],
),
),
],
),
),
);
}
Future<void> _submitReapVoucher(
BuildContext context,
KeyringAccount voucherKeyringAccount,
CommunityIdentifier cid,
Address recipientAddress,
) async {
// Fixme, use proper threshold here: #589
if (_voucherBalance! < 0.04) return showRedeemFailedDialog(context, context.l10n.voucherBalanceTooLow);
await submitEncointerTransferAll(
context, context.read<AppStore>(), widget.api, voucherKeyringAccount, recipientAddress, cid,
// the voucher obviously has tokens in cid.
txPaymentAsset: cid,
onError: (report) async {
Log.d('Error redeeming voucher: ${report.toJson()}', 'ReapVoucherPage');
await showRedeemFailedDialog(context, report.toJson().toString());
},
onFinish: (context, report) async {
await VoucherDialogs.showRedeemSuccessDialog(
context: context,
onOK: () => Navigator.of(context).popUntil((route) => route.isFirst),
);
},
);
}
Future<ChangeResult?> _changeNetworkAndCommunityIfNeeded(
BuildContext context,
String networkInfo,
CommunityIdentifier cid,
) async {
final store = context.read<AppStore>();
if (store.settings.endpoint.info != networkInfo) {
return showChangeNetworkAndCommunityDialog(
context,
store,
widget.api,
networkInfo,
cid,
);
}
if (store.encointer.chosenCid != cid) {
return showChangeCommunityDialog(
context,
store,
widget.api,
networkInfo,
cid,
);
}
// We are already at the correct network and cid
return ChangeResult.ok;
}
}
void _pushTransferPage(BuildContext context, VoucherData data, String voucherAddress) {
Navigator.of(context).popAndPushNamed(
TransferPage.route,
arguments: TransferPageParams(
cid: data.cid,
recipientAddress: voucherAddress,
label: data.issuer,
),
);
}