-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfaucet_list_tile.dart
155 lines (141 loc) · 4.95 KB
/
faucet_list_tile.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
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:encointer_wallet/common/components/submit_button.dart';
import 'package:encointer_wallet/config/consts.dart';
import 'package:encointer_wallet/models/communities/community_identifier.dart';
import 'package:encointer_wallet/models/faucet/faucet.dart';
import 'package:encointer_wallet/service/tx/lib/src/submit_tx_wrappers.dart';
import 'package:encointer_wallet/theme/theme.dart';
import 'package:encointer_wallet/gen/assets.gen.dart';
import 'package:encointer_wallet/service/substrate_api/api.dart';
import 'package:encointer_wallet/store/app.dart';
import 'package:encointer_wallet/utils/format.dart';
class FaucetListTile extends StatefulWidget {
const FaucetListTile(
this.store, {
super.key,
required this.userAddress,
required this.faucetPubKey,
required this.faucet,
});
final AppStore store;
final String userAddress;
final String faucetPubKey;
final Faucet faucet;
@override
State<FaucetListTile> createState() => _FaucetListTileState();
}
class _FaucetListTileState extends State<FaucetListTile> {
late Future<Map<int, CommunityIdentifier>> future;
late Future<BigInt> nativeBalance;
@override
void initState() {
super.initState();
future = _getUncommittedReputationIds(widget.userAddress);
nativeBalance = getNativeFreeBalance(widget.userAddress);
}
@override
Widget build(BuildContext context) {
return ListTile(
contentPadding: const EdgeInsets.symmetric(),
leading: SizedBox(
width: 50,
height: 50,
child: CircleAvatar(
backgroundColor: Colors.transparent,
child: Assets.kusama.svg(fit: BoxFit.fitHeight),
),
),
title: Text(
widget.faucet.name,
style: context.titleMedium.copyWith(color: context.colorScheme.primary),
),
subtitle: Row(
children: [
const Text('KSM: '),
FutureBuilder(
future: nativeBalance,
builder: (BuildContext context, AsyncSnapshot<BigInt> snapshot) {
if (snapshot.hasData) {
return Text(Fmt.token(snapshot.data!, ertDecimals));
} else {
return const CupertinoActivityIndicator();
}
})
],
),
trailing: FutureBuilder(
future: future,
builder: (BuildContext context, AsyncSnapshot<Map<int, CommunityIdentifier>> snapshot) {
if (snapshot.hasData) {
if (snapshot.data!.isNotEmpty) {
return SubmitButtonSmall(
onPressed: (context) async {
await _submitFaucetDripTxs(
context,
snapshot.data!,
widget.faucetPubKey,
);
future = _getUncommittedReputationIds(widget.userAddress);
nativeBalance = getNativeFreeBalance(widget.userAddress);
setState(() {});
},
child: const Text('Claim'),
);
} else {
return const SubmitButtonSmall(child: Text('No Claim'));
}
} else {
return const CupertinoActivityIndicator();
}
},
),
);
}
/// Returns all reputation ids, which haven't been committed for this faucet's
/// purpose id yet, i.e., can be used to drip the faucet currently.
Future<Map<int, CommunityIdentifier>> _getUncommittedReputationIds(String address) async {
final reputations = widget.store.encointer.accountStores![address]!.verifiedReputations;
final ids = Map<int, CommunityIdentifier>.of({});
// Create a set of futures to await in parallel.
final futures = reputations.entries.map(
(e) async {
final cid = e.value.communityIdentifier;
// Only check if the reputations community id is allowed to drip the faucet.
if (widget.faucet.whitelist == null || widget.faucet.whitelist!.contains(cid)) {
final hasCommitted = await webApi.encointer.hasCommittedFor(
cid,
e.key,
widget.faucet.purposeId,
address,
);
if (!hasCommitted) ids[e.key] = e.value.communityIdentifier;
}
},
);
await Future.wait(futures);
return ids;
}
Future<void> _submitFaucetDripTxs(
BuildContext context,
Map<int, CommunityIdentifier> ids,
String faucetAccount,
) async {
final store = widget.store;
final e = ids.entries.first;
return submitFaucetDrip(
context,
store,
webApi,
store.account.getKeyringAccount(store.account.currentAccountPubKey!),
faucetAccount,
e.value,
e.key,
txPaymentAsset: store.encointer.getTxPaymentAsset(store.encointer.chosenCid),
);
}
Future<BigInt> getNativeFreeBalance(String address) async {
final balance = await webApi.assets.getBalanceOf(address);
return balance.free;
}
}