Skip to content

Commit f8bb1d5

Browse files
committed
[service/tx] reap voucher uses now the dart way
1 parent d6d3412 commit f8bb1d5

File tree

4 files changed

+57
-30
lines changed

4 files changed

+57
-30
lines changed

app/lib/page/reap_voucher/reap_voucher_page.dart

+23-18
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class ReapVoucherPage extends StatefulWidget {
4646
}
4747

4848
class _ReapVoucherPageState extends State<ReapVoucherPage> {
49+
late KeyringAccount _voucherKeyringAccount;
4950
String? _voucherAddress;
5051
double? _voucherBalance;
5152

@@ -56,8 +57,8 @@ class _ReapVoucherPageState extends State<ReapVoucherPage> {
5657
Log.d('Fetching voucher data...', 'ReapVoucherPage');
5758
final store = context.read<AppStore>();
5859

59-
final voucherPair = await KeyringAccount.fromUri('Voucher', voucherUri);
60-
_voucherAddress = voucherPair.address(prefix: store.settings.endpoint.ss58 ?? 42).encode();
60+
_voucherKeyringAccount = await KeyringAccount.fromUri('Voucher', voucherUri);
61+
_voucherAddress = _voucherKeyringAccount.address(prefix: store.settings.endpoint.ss58 ?? 42).encode();
6162

6263
setState(() {});
6364

@@ -96,12 +97,12 @@ class _ReapVoucherPageState extends State<ReapVoucherPage> {
9697
@override
9798
Widget build(BuildContext context) {
9899
final l10n = context.l10n;
99-
final store = context.watch<AppStore>();
100+
final store = context.read<AppStore>();
100101
final h2Grey = context.titleLarge.copyWith(color: AppColors.encointerGrey);
101102
final h4Grey = context.bodyLarge.copyWith(color: AppColors.encointerGrey);
102103

103104
final voucher = widget.voucher;
104-
final recipient = store.account.currentAddress;
105+
final recipient = Address.decode(store.account.currentAddress);
105106

106107
return Scaffold(
107108
appBar: AppBar(title: Text(l10n.voucher)),
@@ -156,7 +157,7 @@ class _ReapVoucherPageState extends State<ReapVoucherPage> {
156157
SubmitButton(
157158
key: const Key(EWTestKeys.submitVoucher),
158159
onPressed: _isReady
159-
? (context) => _submitReapVoucher(context, voucher.voucherUri, voucher.cid, recipient)
160+
? (context) => _submitReapVoucher(context, _voucherKeyringAccount, voucher.cid, recipient)
160161
: null,
161162
child: Row(
162163
mainAxisAlignment: MainAxisAlignment.center,
@@ -175,24 +176,28 @@ class _ReapVoucherPageState extends State<ReapVoucherPage> {
175176

176177
Future<void> _submitReapVoucher(
177178
BuildContext context,
178-
String voucherUri,
179+
KeyringAccount voucherKeyringAccount,
179180
CommunityIdentifier cid,
180-
String recipientAddress,
181+
Address recipientAddress,
181182
) async {
182183
// Fixme, use proper threshold here: #589
183184
if (_voucherBalance! < 0.04) return showRedeemFailedDialog(context, context.l10n.voucherBalanceTooLow);
184185

185-
final res = await submitReapVoucher(widget.api, voucherUri, recipientAddress, cid);
186-
187-
if (res['hash'] == null) {
188-
Log.d('Error redeeming voucher: ${res['error']}', 'ReapVoucherPage');
189-
await showRedeemFailedDialog(context, res['error'] as String?);
190-
} else {
191-
await VoucherDialogs.showRedeemSuccessDialog(
192-
context: context,
193-
onOK: () => Navigator.of(context).popUntil((route) => route.isFirst),
194-
);
195-
}
186+
await submitEncointerTransferAll(
187+
context, context.read<AppStore>(), widget.api, voucherKeyringAccount, recipientAddress, cid,
188+
// the voucher obviously has tokens in cid.
189+
txPaymentAsset: cid,
190+
onError: (report) async {
191+
Log.d('Error redeeming voucher: ${report.toJson()}', 'ReapVoucherPage');
192+
await showRedeemFailedDialog(context, report.toJson().toString());
193+
},
194+
onFinish: (context, report) async {
195+
await VoucherDialogs.showRedeemSuccessDialog(
196+
context: context,
197+
onOK: () => Navigator.of(context).popUntil((route) => route.isFirst),
198+
);
199+
},
200+
);
196201
}
197202

198203
Future<ChangeResult?> _changeNetworkAndCommunityIfNeeded(

app/lib/service/substrate_api/account_api.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ class AccountApi {
5454

5555
Future<ExtrinsicReport> sendTxAndShowNotification(
5656
OpaqueExtrinsic xt,
57-
TxNotification notification, {
57+
TxNotification? notification, {
5858
String? cid,
5959
}) async {
6060
final report = await EWAuthorApi(provider).submitAndWatchExtrinsicWithReport(xt);
6161

62-
if (report.isExtrinsicSuccess) {
62+
if (report.isExtrinsicSuccess && notification != null) {
6363
final hash = report.blockHash;
6464
unawaited(NotificationPlugin.showNotification(
6565
int.parse(hash.substring(0, 6)),

app/lib/service/tx/lib/src/submit_to_inner.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Future<void> submitTxInner(
2929
AppStore store,
3030
Api api,
3131
OpaqueExtrinsic extrinsic,
32-
TxNotification notification,
32+
TxNotification? notification,
3333
bool showStatusSnackBar, {
3434
void Function(DispatchError error)? onError,
3535
dynamic Function(BuildContext, ExtrinsicReport)? onFinish,

app/lib/service/tx/lib/src/submit_tx_wrappers.dart

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:convert';
2-
31
import 'package:flutter/cupertino.dart';
42
import 'package:provider/provider.dart';
53

@@ -37,7 +35,7 @@ Future<void> submitTx(
3735
AppStore store,
3836
Api api,
3937
OpaqueExtrinsic xt,
40-
TxNotification notification, {
38+
TxNotification? notification, {
4139
dynamic Function(BuildContext txPageContext, ExtrinsicReport report)? onFinish,
4240
void Function(DispatchError report)? onError,
4341
}) async {
@@ -356,13 +354,37 @@ Future<dynamic> submitNextPhaseWithAlice(BuildContext context, AppStore store, A
356354
}
357355
}
358356

359-
Future<Map<String, dynamic>> submitReapVoucher(
357+
Future<void> submitEncointerTransferAll(
358+
BuildContext context,
359+
AppStore store,
360360
Api api,
361-
String voucherUri,
362-
String recipientAddress,
363-
CommunityIdentifier cid,
364-
) async {
365-
return api.js.evalJavascript('encointer.reapVoucher("$voucherUri","$recipientAddress", ${jsonEncode(cid)})');
361+
KeyringAccount signer,
362+
Address recipientAddress,
363+
CommunityIdentifier cid, {
364+
required CommunityIdentifier? txPaymentAsset,
365+
TxNotification? notification,
366+
dynamic Function(BuildContext txPageContext, ExtrinsicReport report)? onFinish,
367+
void Function(DispatchError report)? onError,
368+
}) async {
369+
final call = api.encointer.encointerKusama.tx.encointerBalances.transferAll(
370+
dest: recipientAddress.pubkey,
371+
cid: cid.toPolkadart(),
372+
);
373+
final xt = await TxBuilder(api.provider).createSignedExtrinsic(
374+
signer.pair,
375+
call,
376+
paymentAsset: txPaymentAsset?.toPolkadart(),
377+
);
378+
379+
return submitTx(
380+
context,
381+
context.read<AppStore>(),
382+
api,
383+
OpaqueExtrinsic(xt),
384+
notification,
385+
onFinish: onFinish,
386+
onError: onError,
387+
);
366388
}
367389

368390
void _showEducationalDialog(ParticipantType registrationType, BuildContext context) {

0 commit comments

Comments
 (0)