-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhelpers.dart
256 lines (224 loc) · 9.45 KB
/
helpers.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
import 'dart:math';
import 'package:encointer_wallet/config/consts.dart';
import 'package:encointer_wallet/l10n/l10.dart';
import 'package:encointer_wallet/models/communities/community_identifier.dart';
import 'package:encointer_wallet/models/location/location.dart';
import 'package:encointer_wallet/service/service.dart';
import 'package:encointer_wallet/service/substrate_api/encointer/encointer_api.dart';
import 'package:encointer_wallet/store/app.dart';
import 'package:encointer_wallet/utils/format.dart';
import 'package:ew_keyring/ew_keyring.dart';
import 'package:ew_substrate_fixed/substrate_fixed.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:ew_polkadart/encointer_types.dart' as et;
import 'package:ew_polkadart/ew_polkadart.dart'
show
AddLocation,
Approved,
Confirming,
Enacted,
Ongoing,
Petition,
Proposal,
ProposalAction,
Rejected,
RemoveLocation,
SetInactivityTimeout,
SpendNative,
SupersededBy,
Tally,
UpdateDemurrage,
UpdateNominalIncome,
IssueSwapNativeOption;
/// Gets the localized proposal action title.
///
/// Todo: add localization for all variants.
String getProposalActionTitle(BuildContext context, ProposalAction action) {
final l10n = context.l10n;
final store = context.read<AppStore>();
switch (action.runtimeType) {
case UpdateNominalIncome:
final store = context.read<AppStore>();
final cidPolkadart = getCommunityIdentifierFromProposal(action);
final cid = CommunityIdentifier(cidPolkadart!.geohash, cidPolkadart.digest);
return l10n.proposalUpdateNominalIncome(
i64F64Util.toDouble((action as UpdateNominalIncome).value1.bits).toStringAsFixed(2),
store.encointer.communityStores![cid.toFmtString()]?.symbol ?? cid.toFmtString(),
);
case UpdateDemurrage:
final demurrageDouble = i64F64Util.toDouble((action as UpdateDemurrage).value1.bits);
final d = demurragePerMonth(demurrageDouble, BigInt.from(6));
return l10n.proposalUpdateDemurrage(d.toStringAsFixed(2));
case AddLocation:
final cidPolkadart = (action as AddLocation).value0;
final cidStr = cidOrGlobal(cidPolkadart, store);
final location = Location.fromPolkadart(action.value1);
return '${l10n.proposalAddLocation(cidStr)} (${location.latLongFmt()})';
case RemoveLocation:
final cidPolkadart = (action as AddLocation).value0;
final cidStr = cidOrGlobal(cidPolkadart, store);
final location = Location.fromPolkadart(action.value1);
return '${l10n.proposalRemoveLocation(cidStr)} (${location.latLongFmt()})';
case SetInactivityTimeout:
final timeout = (action as SetInactivityTimeout).value0;
return l10n.proposalSetInactivityTimeoutTo(timeout.toString());
case Petition:
final cidPolkadart = getCommunityIdentifierFromProposal(action);
final cidStr = cidOrGlobal(cidPolkadart, store);
final demand = String.fromCharCodes((action as Petition).value1);
return l10n.proposalPetition(cidStr, demand);
case SpendNative:
final cidPolkadart = getCommunityIdentifierFromProposal(action);
final cidStr = cidOrGlobal(cidPolkadart, store);
final beneficiary = Fmt.address(
AddressUtils.pubKeyToAddress((action as SpendNative).value1, prefix: store.settings.currentNetwork.ss58()))!;
final amount = Fmt.token(action.value2, ertDecimals);
return l10n.proposalSpendNative(cidStr, amount, beneficiary);
case IssueSwapNativeOption:
final issueOption = action as IssueSwapNativeOption;
final cidPolkadart = getCommunityIdentifierFromProposal(action);
final cidStr = cidOrGlobal(cidPolkadart, store);
final beneficiary =
Fmt.address(AddressUtils.pubKeyToAddress(issueOption.value1, prefix: store.settings.currentNetwork.ss58()))!;
final swapNativeOption = issueOption.value2;
final allowance = Fmt.token(swapNativeOption.nativeAllowance, ertDecimals);
final rate = swapNativeOption.rate != null ? i64F64Parser.toDouble(swapNativeOption.rate!.bits) : null;
return l10n.proposalIssueSwapNativeOption(cidStr, beneficiary, allowance, rate.toString());
default:
throw Exception('ProposalAction: Invalid Type: "${action.runtimeType}"');
}
}
String cidOrGlobal(et.CommunityIdentifier? cidPolkadart, AppStore store) {
final cidStr = cidPolkadart == null
? 'global'
: (store.encointer.communityStores![CommunityIdentifier(cidPolkadart.geohash, cidPolkadart.digest).toFmtString()]
?.symbol ??
CommunityIdentifier(cidPolkadart.geohash, cidPolkadart.digest).toFmtString());
return cidStr;
}
double demurragePerMonth(double demurrage, BigInt blockProductionTime) {
return (1 - exp(-1 * demurrage * blocksPerMonth(blockProductionTime))) * 100;
}
double monthlyDemurragePercentToDemurrage(double monthly, BigInt blockProductionTime) {
final blocks = blocksPerMonth(blockProductionTime);
return -log(1 - (monthly / 100)) / blocks;
}
double blocksPerMonth(BigInt blockProductionTime) {
return (86400 / blockProductionTime.toDouble()) * (365 / 12);
}
/// Gets the community identifier from a proposal for community proposals.
///
/// Returns null for global proposals.
et.CommunityIdentifier? getCommunityIdentifierFromProposal(ProposalAction action) {
switch (action.runtimeType) {
case AddLocation:
return (action as AddLocation).value0;
case RemoveLocation:
return (action as RemoveLocation).value0;
case UpdateDemurrage:
return (action as UpdateDemurrage).value0;
case UpdateNominalIncome:
return (action as UpdateNominalIncome).value0;
case SetInactivityTimeout:
// This is a global action hence all communities can vote for it.
return null;
case Petition:
// can be global or local
return (action as Petition).value0;
case SpendNative:
// can be global or local
return (action as SpendNative).value0;
case IssueSwapNativeOption:
// can be global or local
return (action as IssueSwapNativeOption).value0;
default:
throw Exception('ProposalAction: Invalid Type: "${action.runtimeType}"');
}
}
bool isInVotingCindexes(
int cIndex,
Proposal proposal,
int reputationLifetime,
DemocracyParams params,
int cycleDuration,
) {
final votingCindexLowerBound =
proposal.startCindex - reputationLifetime + (params.proposalLifetime.toInt() / cycleDuration).ceil();
final votingCindexUpperBound = proposal.startCindex - 2;
Log.d('[Democracy] valid voting cIndexes (inclusive): [$votingCindexLowerBound, $votingCindexUpperBound]');
return cIndex >= votingCindexLowerBound && cIndex <= votingCindexUpperBound;
}
bool isPassing(Tally tally, BigInt electorateSize, DemocracyParams params) {
// minTurnout is in perThousands
if ((tally.turnout < BigInt.from(1)) | (tally.turnout < params.minTurnout * electorateSize ~/ BigInt.from(1000))) {
return false;
}
return positiveTurnoutBias(
electorateSize.toInt(),
tally.turnout.toInt(),
tally.ayes.toInt(),
);
}
bool positiveTurnoutBias(int electorate, int turnout, int ayes) {
return ayes / turnout > approvalThreshold(electorate, turnout);
}
/// Returns the approval threshold in the range of [0,1].
double approvalThreshold(int electorate, int turnout) {
if (electorate == 0 || turnout == 0) return 0;
return 1 / (1 + sqrt(turnout / electorate));
}
/// Function to partition a list into two lists based on a predicate
///
/// The first value of the result are the items for which the predicate
/// returned true.
List<List<T>> partition<T>(Iterable<T> items, bool Function(T) predicate) {
final trueList = <T>[];
final falseList = <T>[];
for (final item in items) {
if (predicate(item)) {
trueList.add(item);
} else {
falseList.add(item);
}
}
return [trueList, falseList];
}
Iterable<MapEntry<BigInt, Proposal>> proposalsForCommunityOrGlobal(
Map<BigInt, Proposal> proposals, CommunityIdentifier cid) {
return proposals.entries.where((e) {
final maybeCid = getCommunityIdentifierFromProposal(e.value.action);
return maybeCid == null || maybeCid == et.CommunityIdentifier(geohash: cid.geohash, digest: cid.digest);
});
}
extension ProposalExt on Proposal {
bool isActive() {
return state.runtimeType == Ongoing || state.runtimeType == Confirming;
}
bool isPast() {
// Approved, Enacted, Superseded, Rejected
return !isActive();
}
bool hasPassed() {
return state.runtimeType == Approved || state.runtimeType == Enacted;
}
bool supersededOrRejected() {
return state.runtimeType == SupersededBy || state.runtimeType == Rejected;
}
/// Returns true if the proposal started after now - `duration`.
bool isMoreRecentThan(Duration duration) {
return DateTime.now().subtract(duration).isBefore(DateTime.fromMillisecondsSinceEpoch(start.toInt()));
}
/// Returns true if the proposal started before now - `duration`.
bool isOlderThan(Duration duration) {
return !isMoreRecentThan(duration);
}
/// Returns true if the proposal has been in `Confirming` for longer than `duration`.
///
/// Returns null if the proposal is not in confirming state at all.
bool? isConfirmingLongerThan(Duration duration) {
if (state.runtimeType != Confirming) return null;
final confirmingSince = (state as Confirming).since;
return DateTime.now().subtract(duration).isAfter(DateTime.fromMillisecondsSinceEpoch(confirmingSince.toInt()));
}
}