-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhelpers.dart
158 lines (139 loc) · 5.63 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
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/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,
Petition,
Proposal,
ProposalAction,
RemoveLocation,
SetInactivityTimeout,
SpendNative,
Tally,
UpdateDemurrage,
UpdateNominalIncome;
/// 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(
u64F64Util.toDouble((action as UpdateNominalIncome).value1.bits).toStringAsFixed(2),
store.encointer.communityStores![cid.toFmtString()]?.symbol ?? cid.toFmtString(),
);
case UpdateDemurrage:
final blockProductionTime = webApi.encointer.encointerKusama.constant.timestamp.minimumPeriod;
final demurrageDouble = u64F64Util.toDouble((action as UpdateDemurrage).value1.bits);
final d = demurragePerMonth(demurrageDouble, blockProductionTime);
return l10n.proposalUpdateDemurrage(d.toStringAsFixed(2));
case AddLocation:
return 'Add Location (unsupported)';
case RemoveLocation:
return 'Remove Location (unsupported)';
case SetInactivityTimeout:
return 'SetInactivity Timeout (unsupported)';
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);
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 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;
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;
return cIndex > votingCindexLowerBound && cIndex <= votingCindexUpperBound;
}
bool isPassing(Tally tally, BigInt electorateSize, DemocracyParams params) {
if (tally.turnout < params.minTurnout) {
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));
}