-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsubmit_tx_wrappers.dart
505 lines (463 loc) · 16 KB
/
submit_tx_wrappers.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
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
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import 'package:encointer_wallet/config/consts.dart';
import 'package:encointer_wallet/models/communities/community_identifier.dart';
import 'package:encointer_wallet/models/index.dart';
import 'package:encointer_wallet/service/launch/app_launch.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/src/tx_notification.dart';
import 'package:encointer_wallet/service/tx/lib/src/submit_to_inner.dart';
import 'package:encointer_wallet/store/app.dart';
import 'package:encointer_wallet/l10n/l10.dart';
import 'package:encointer_wallet/config/networks/networks.dart';
import 'package:encointer_wallet/service/notification/lib/notification.dart';
import 'package:encointer_wallet/modules/login/logic/login_store.dart';
import 'package:encointer_wallet/models/proof_of_attendance/proof_of_attendance.dart';
import 'package:encointer_wallet/service/tx/lib/src/send_tx_dart.dart';
import 'package:encointer_wallet/service/tx/lib/src/tx_builder.dart';
import 'package:ew_keyring/ew_keyring.dart';
import 'package:ew_polkadart/generated/encointer_kusama/types/sp_runtime/dispatch_error.dart';
import 'package:ew_polkadart/generated/encointer_kusama/types/tuples.dart';
import 'package:ew_test_keys/ew_test_keys.dart';
import 'package:ew_polkadart/generated/encointer_kusama/types/substrate_fixed/fixed_u128.dart';
import 'package:ew_substrate_fixed/substrate_fixed.dart';
import 'package:ew_polkadart/ew_polkadart.dart' show Vote;
import 'package:ew_polkadart/ew_polkadart.dart' as pd;
/// Helpers to submit transactions.
/// Submit tx to the chain.
///
/// Asks for the pin input if it's not cached and submits the tx via the JS interface.
///
/// This function is intended to be the universal interface for sending transactions.
Future<void> submitTx(
BuildContext context,
AppStore store,
Api api,
OpaqueExtrinsic xt,
TxNotification? notification, {
dynamic Function(BuildContext txPageContext, ExtrinsicReport report)? onFinish,
void Function(DispatchError report)? onError,
}) async {
final pin = await context.read<LoginStore>().getPin(context);
if (pin != null) {
return submitTxInner(
context,
store,
api,
xt,
notification,
onError: onError,
onFinish: onFinish,
);
}
}
Future<void> submitClaimRewards(
BuildContext context,
AppStore store,
Api api,
KeyringAccount signer,
CommunityIdentifier chosenCid, {
required CommunityIdentifier? txPaymentAsset,
}) async {
// meetupIndex = null; the chain will figure out the meetup index.
final call = api.encointer.encointerKusama.tx.encointerCeremonies.claimRewards(cid: chosenCid.toPolkadart());
final xt = await TxBuilder(api.provider).createSignedExtrinsic(
signer.pair,
call,
paymentAsset: txPaymentAsset?.toPolkadart(),
);
return submitTx(
context,
store,
api,
OpaqueExtrinsic(xt),
TxNotification.claimRewards(context.l10n),
onFinish: (BuildContext txPageContext, ExtrinsicReport report) {
// Claiming the rewards creates a new reputation if successful.
// Hence, we should update the state afterwards.
store.encointer.getEncointerBalance();
webApi.encointer.getReputations();
return report;
},
);
}
Future<void> submitRemark(
BuildContext context,
AppStore store,
Api api,
KeyringAccount signer,
String remark, {
required CommunityIdentifier? txPaymentAsset,
}) async {
final List<int> remarkList = remark.codeUnits;
final call = api.encointer.encointerKusama.tx.system.remarkWithEvent(remark: remarkList);
final xt = await TxBuilder(api.provider).createSignedExtrinsic(
signer.pair,
call,
paymentAsset: txPaymentAsset?.toPolkadart(),
);
return submitTx(
context,
store,
api,
OpaqueExtrinsic(xt),
TxNotification.claimRewards(context.l10n),
onFinish: (BuildContext txPageContext, ExtrinsicReport report) {
// Claiming the rewards creates a new reputation if successful.
// Hence, we should update the state afterwards.
store.encointer.getEncointerBalance();
webApi.encointer.getReputations();
return report;
},
);
}
Future<void> submitEndorseNewcomer(
BuildContext context,
AppStore store,
Api api,
KeyringAccount signer,
CommunityIdentifier chosenCid,
Address newbie, {
required CommunityIdentifier? txPaymentAsset,
}) async {
final call = api.encointer.encointerKusama.tx.encointerCeremonies.endorseNewcomer(
cid: chosenCid.toPolkadart(),
newbie: newbie.pubkey.toList(),
);
final xt = await TxBuilder(api.provider).createSignedExtrinsic(
signer.pair,
call,
paymentAsset: txPaymentAsset?.toPolkadart(),
);
return submitTx(
context,
store,
api,
OpaqueExtrinsic(xt),
TxNotification.endorseNewcomer(context.l10n),
onFinish: (BuildContext txPageContext, ExtrinsicReport report) {
store.encointer.account!.getNumberOfNewbieTicketsForReputable(at: report.blockHashBytes);
store.encointer.communityAccount!.getNumberOfNewbieTicketsForBootstrapper(at: report.blockHashBytes);
},
);
}
Future<void> submitUnRegisterParticipant(
BuildContext context,
AppStore store,
Api api,
KeyringAccount signer,
CommunityIdentifier chosenCid, {
required ProofOfAttendance? lastProofOfAttendance,
required CommunityIdentifier? txPaymentAsset,
}) async {
final maybeReputationCommunityCeremony = (lastProofOfAttendance != null)
? Tuple2(
lastProofOfAttendance.communityIdentifier.toPolkadart(),
lastProofOfAttendance.ceremonyIndex,
)
: null;
final call = api.encointer.encointerKusama.tx.encointerCeremonies.unregisterParticipant(
cid: chosenCid.toPolkadart(),
maybeReputationCommunityCeremony: maybeReputationCommunityCeremony,
);
final xt = await TxBuilder(api.provider).createSignedExtrinsic(
signer.pair,
call,
paymentAsset: txPaymentAsset?.toPolkadart(),
);
return submitTx(
context,
store,
webApi,
OpaqueExtrinsic(xt),
TxNotification.unregisterParticipant(context.l10n),
onFinish: (BuildContext txPageContext, ExtrinsicReport report) async {
await Future.wait([
webApi.encointer.getReputations(),
webApi.encointer
.getAggregatedAccountData(
store.encointer.chosenCid!, store.account.currentAccountPubKey!,
// Get data at included block to prevent race conditions with `store.chain.latestHead`.
at: report.blockHashBytes,
)
.then((data) => store.encointer
.setAggregatedAccountData(store.encointer.chosenCid!, store.account.currentAddress, data)),
]);
},
);
}
Future<void> submitRegisterParticipant(
BuildContext context,
AppStore store,
Api api,
KeyringAccount signer,
CommunityIdentifier chosenCid, {
required CommunityIdentifier? txPaymentAsset,
}) async {
final proof = api.encointer.getProofOfAttendance();
final call = api.encointer.encointerKusama.tx.encointerCeremonies.registerParticipant(
cid: chosenCid.toPolkadart(),
proof: proof,
);
final xt = await TxBuilder(api.provider).createSignedExtrinsic(
signer.pair,
call,
paymentAsset: txPaymentAsset?.toPolkadart(),
);
return submitTx(
context,
store,
api,
OpaqueExtrinsic(xt),
TxNotification.registerParticipant(context.l10n),
onFinish: (BuildContext txPageContext, ExtrinsicReport report) async {
store.encointer.account!.lastProofOfAttendance = (proof != null) ? ProofOfAttendance.fromPolkadart(proof) : null;
final data = await webApi.encointer
.getAggregatedAccountData(store.encointer.chosenCid!, store.account.currentAccountPubKey!,
// Get data at included block to prevent race conditions with `store.chain.latestHead`.
at: report.blockHashBytes);
store.encointer.setAggregatedAccountData(store.encointer.chosenCid!, store.account.currentAddress, data);
Log.d('$data', 'AggregatedAccountData from register participant');
final registrationType = data.personal?.participantType;
if (registrationType != null) {
_showEducationalDialog(registrationType, context);
if (store.settings.currentNetwork == Network.encointerKusama) {
await CeremonyNotifications.scheduleMeetupReminders(
ceremonyIndex: data.global.ceremonyIndex,
meetupTime: store.encointer.community!.meetupTime!,
l10n: context.l10n,
cid: store.encointer.community?.cid.toFmtString(),
);
}
}
await webApi.encointer.getReputations();
},
);
}
Future<void> submitAttestAttendees(
BuildContext context,
AppStore store,
Api api,
KeyringAccount signer,
CommunityIdentifier chosenCid, {
required CommunityIdentifier? txPaymentAsset,
}) async {
final attestations =
store.encointer.communityAccount!.attendees!.map((address) => Address.decode(address).pubkey).toList();
final call = api.encointer.encointerKusama.tx.encointerCeremonies.attestAttendees(
cid: chosenCid.toPolkadart(),
numberOfParticipantsVote: store.encointer.communityAccount!.participantCountVote!,
attestations: attestations,
);
final xt = await TxBuilder(api.provider).createSignedExtrinsic(
signer.pair,
call,
paymentAsset: txPaymentAsset?.toPolkadart(),
);
return submitTx(
context,
store,
api,
OpaqueExtrinsic(xt),
TxNotification.attestAttendees(context.l10n),
onFinish: (BuildContext txPageContext, ExtrinsicReport report) {
store.encointer.communityAccount!.setMeetupCompleted();
Navigator.popUntil(txPageContext, (route) => route.isFirst);
},
);
}
Future<void> submitFaucetDrip(
BuildContext context,
AppStore store,
Api api,
KeyringAccount signer,
String faucetPubKey,
CommunityIdentifier cid,
int cIndex, {
required CommunityIdentifier? txPaymentAsset,
}) async {
final call = api.encointer.encointerKusama.tx.encointerFaucet.drip(
faucetAccount: AddressUtils.pubKeyHexToPubKey(faucetPubKey).toList(),
cid: cid.toPolkadart(),
cindex: cIndex,
);
final xt = await TxBuilder(api.provider).createSignedExtrinsic(
signer.pair,
call,
paymentAsset: txPaymentAsset?.toPolkadart(),
);
return submitTx(
context,
store,
api,
OpaqueExtrinsic(xt),
TxNotification.faucetDrip(context.l10n),
);
}
Future<void> submitEncointerTransfer(
BuildContext context,
AppStore store,
Api api,
KeyringAccount signer,
CommunityIdentifier cid,
Address recipientAddress,
double amount, {
required CommunityIdentifier? txPaymentAsset,
dynamic Function(BuildContext txPageContext, ExtrinsicReport report)? onFinish,
void Function(DispatchError report)? onError,
}) async {
final call = api.encointer.encointerKusama.tx.encointerBalances.transfer(
dest: recipientAddress.pubkey,
communityId: cid.toPolkadart(),
amount: FixedU128(bits: u64F64Util.toFixed(amount)),
);
final xt = await TxBuilder(api.provider).createSignedExtrinsic(
signer.pair,
call,
paymentAsset: txPaymentAsset?.toPolkadart(),
);
await submitTx(
context,
context.read<AppStore>(),
api,
OpaqueExtrinsic(xt),
TxNotification.encointerBalanceTransfer(context.l10n),
onFinish: (BuildContext txPageContext, ExtrinsicReport report) {
if (onFinish != null) onFinish(txPageContext, report);
store.encointer.getEncointerBalance();
return report;
},
onError: onError,
);
}
/// Calls `encointerScheduler.nextPhase()` with Alice.
///
/// This will only work on the local dev-setup.
Future<dynamic> submitNextPhaseWithAlice(BuildContext context, AppStore store, Api api) async {
// This is valid for the encointer-node dev chain config.
// We currently don't have access to the dev nodes metadata
// so we hardcode the call.
const sudoNextPhaseCall = '0x05003c00';
final alice = await KeyringAccount.fromUri('Alice', '//Alice');
final xt = await TxBuilder(api.provider).createSignedExtrinsicWithEncodedCall(
alice.pair,
sudoNextPhaseCall,
);
try {
await EWAuthorApi(api.provider).submitAndWatchExtrinsicWithReport(OpaqueExtrinsic(xt));
// this is actually unexpected, see exception case below.
return 'successfully called next phase';
} catch (e) {
// this will always throw an exception with the current implementation
// because we use the kusama metadata, which does not know the sudo
// pallet.
Log.p('sudo.nextPhase() threw an exception, decoding error is expected though: $e');
return "called next phase, but can't evaluate result";
}
}
Future<void> submitEncointerTransferAll(
BuildContext context,
AppStore store,
Api api,
KeyringAccount signer,
Address recipientAddress,
CommunityIdentifier cid, {
required CommunityIdentifier? txPaymentAsset,
TxNotification? notification,
dynamic Function(BuildContext txPageContext, ExtrinsicReport report)? onFinish,
void Function(DispatchError report)? onError,
}) async {
final call = api.encointer.encointerKusama.tx.encointerBalances.transferAll(
dest: recipientAddress.pubkey,
cid: cid.toPolkadart(),
);
final xt = await TxBuilder(api.provider).createSignedExtrinsic(
signer.pair,
call,
paymentAsset: txPaymentAsset?.toPolkadart(),
);
return submitTx(
context,
context.read<AppStore>(),
api,
OpaqueExtrinsic(xt),
notification,
onFinish: onFinish,
onError: onError,
);
}
typedef ReputationTuple = Tuple2<pd.CommunityIdentifier, int>;
typedef Reputations = List<ReputationTuple>;
Future<void> submitDemocracyVote(
BuildContext context,
AppStore store,
Api api,
KeyringAccount signer,
BigInt proposalId,
Vote vote,
Reputations reputations, {
required CommunityIdentifier? txPaymentAsset,
}) async {
final call = api.encointer.encointerKusama.tx.encointerDemocracy.vote(
proposalId: proposalId,
vote: vote,
reputations: reputations,
);
final xt = await TxBuilder(api.provider).createSignedExtrinsic(
signer.pair,
call,
paymentAsset: txPaymentAsset?.toPolkadart(),
);
return submitTx(
context,
store,
api,
OpaqueExtrinsic(xt),
TxNotification.democracyVote(context.l10n),
);
}
void _showEducationalDialog(ParticipantType registrationType, BuildContext context) {
final l10n = context.l10n;
final texts = _getEducationalDialogTexts(registrationType, context);
final languageCode = Localizations.localeOf(context).languageCode;
showCupertinoDialog<void>(
barrierDismissible: true,
context: context,
builder: (context) {
return CupertinoAlertDialog(
key: Key(EWTestKeys.educateDialogRegistrationType(registrationType.name)),
title: Text('${texts['title']}'),
content: Text(
'${texts['content']}',
textAlign: TextAlign.center,
),
actions: <Widget>[
if (registrationType == ParticipantType.Newbie) const SizedBox(),
CupertinoButton(
key: const Key(EWTestKeys.closeEducateDialog),
child: Text(l10n.ok),
onPressed: () => Navigator.of(context).pop(),
),
if (registrationType == ParticipantType.Newbie)
CupertinoButton(
child: Text(
l10n.leuZurichFAQ,
textAlign: TextAlign.center,
),
onPressed: () => AppLaunch.launchURL(leuZurichCycleAssignmentFAQLink(languageCode)),
),
],
);
},
);
}
Map<String, String> _getEducationalDialogTexts(ParticipantType type, BuildContext context) {
final l10n = context.l10n;
return switch (type) {
ParticipantType.Newbie => {'title': l10n.newbieTitle, 'content': l10n.newbieContent},
ParticipantType.Endorsee => {'title': l10n.endorseeTitle, 'content': l10n.endorseeContent},
ParticipantType.Reputable => {'title': l10n.reputableTitle, 'content': l10n.reputableContent},
ParticipantType.Bootstrapper => {'title': l10n.bootstrapperTitle, 'content': l10n.bootstrapperContent},
};
}