Skip to content

Commit

Permalink
update polkadot-js/api to v1.9.0-beta.0 & refactored referendum vote
Browse files Browse the repository at this point in the history
  • Loading branch information
shawn committed Mar 24, 2020
1 parent 88ca360 commit 77223ef
Show file tree
Hide file tree
Showing 20 changed files with 388 additions and 193 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.6.2
- Update polkadot-js/api to 1.9.0-beta.0

# 0.6.1
- fix nominator list bug
- Refactor pending payouts query
Expand Down
2 changes: 0 additions & 2 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
29E32FB92428BC47006F05B2 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E2E5C930B0B11FFACB230966 /* Pods_Runner.framework */; };
29E32FBA2428BC47006F05B2 /* Pods_Runner.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = E2E5C930B0B11FFACB230966 /* Pods_Runner.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; };
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand All @@ -30,7 +29,6 @@
files = (
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */,
9705A1C71CF904A300538489 /* Flutter.framework in Embed Frameworks */,
29E32FBA2428BC47006F05B2 /* Pods_Runner.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 2 additions & 0 deletions lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:polka_wallet/page/assets/transfer/transferPage.dart';
import 'package:polka_wallet/page/governance/council/candidateDetailPage.dart';
import 'package:polka_wallet/page/governance/council/candidateListPage.dart';
import 'package:polka_wallet/page/governance/council/councilVotePage.dart';
import 'package:polka_wallet/page/governance/democracy/referendumVotePage.dart';
import 'package:polka_wallet/page/profile/aboutPage.dart';
import 'package:polka_wallet/page/profile/account/accountManagePage.dart';
import 'package:polka_wallet/page/profile/account/changeNamePage.dart';
Expand Down Expand Up @@ -164,6 +165,7 @@ class _WalletAppState extends State<WalletApp> {
CandidateDetailPage.route: (_) => CandidateDetailPage(_appStore),
CouncilVotePage.route: (_) => CouncilVotePage(_appStore),
CandidateListPage.route: (_) => CandidateListPage(_appStore),
ReferendumVotePage.route: (_) => ReferendumVotePage(_appStore),
// profile
AccountManagePage.route: (_) => AccountManagePage(_appStore),
ContactsPage.route: (_) => ContactsPage(_appStore.settings),
Expand Down
3 changes: 2 additions & 1 deletion lib/page/account/txConfirmPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ class _TxConfirmPageState extends State<TxConfirmPage> {
}

store.assets.setSubmitting(true);
store.account.setTxStatus('queued');
state.showSnackBar(SnackBar(
backgroundColor: Theme.of(context).cardColor,
content: ListTile(
leading: CupertinoActivityIndicator(),
title: Text(
dic['submit.tx'],
dic['tx.${store.account.txStatus}'] ?? dic['tx.queued'],
style: TextStyle(color: Colors.black54),
),
),
Expand Down
6 changes: 2 additions & 4 deletions lib/page/governance/democracy/democracy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,12 @@ class _DemocracyState extends State<Democracy> {
: ListView.builder(
itemCount: list.length,
itemBuilder: (BuildContext context, int i) {
int voted = store.gov.votedMap[list[i].index];
return ReferendumPanel(
data: list[i],
bestNumber: bestNumber,
votes: store.gov.referendumVotes[list[i].index],
votes: list[i].votes,
symbol: symbol,
onVote: _onVote,
voted: voted,
voted: store.gov.votedMap[list[i].index],
);
},
),
Expand Down
41 changes: 24 additions & 17 deletions lib/page/governance/democracy/referendumPanel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:polka_wallet/common/components/roundedButton.dart';
import 'package:polka_wallet/common/components/roundedCard.dart';
import 'package:polka_wallet/page/governance/democracy/referendumVotePage.dart';
import 'package:polka_wallet/store/governance.dart';
import 'package:polka_wallet/utils/format.dart';
import 'package:polka_wallet/utils/i18n/index.dart';

class ReferendumPanel extends StatelessWidget {
ReferendumPanel(
{this.symbol,
this.data,
this.bestNumber,
this.votes,
this.voted,
this.onVote});
ReferendumPanel({
this.symbol,
this.data,
this.bestNumber,
this.votes,
this.voted,
});

final String symbol;
final ReferendumInfo data;
final int bestNumber;
final ReferendumVotes votes;
final Map votes;
final int voted;
final Function(int, bool) onVote;

@override
Widget build(BuildContext context) {
Expand All @@ -46,7 +46,7 @@ class ReferendumPanel extends StatelessWidget {
child: Image.asset('assets/images/gov/time.png'),
),
Text(
'${data.info['end'] + data.info['delay'] - bestNumber} blocks ${dic['end']}',
'${data.status['end'] + data.status['delay'] - bestNumber} blocks ${dic['end']}',
style: TextStyle(color: Colors.lightGreen),
)
],
Expand Down Expand Up @@ -111,8 +111,11 @@ class ReferendumPanel extends StatelessWidget {

if (votes != null) {
double widthFull = MediaQuery.of(context).size.width - 72;
double yes = votes.votedAye / votes.votedTotal;
double widthYes = votes.votedTotal > 0 ? yes * widthFull : widthFull / 2;
int votedTotal = int.parse(votes['votedTotal'].toString());
int votedAye = int.parse(votes['votedAye'].toString());
int votedNay = int.parse(votes['votedNay'].toString());
double yes = int.parse(votes['votedAye'].toString()) / votedTotal;
double widthYes = votedTotal > 0 ? yes * widthFull : widthFull / 2;
double widthMin = 6;
list.add(Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
Expand Down Expand Up @@ -143,13 +146,13 @@ class ReferendumPanel extends StatelessWidget {
list.add(Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('${Fmt.token(votes.votedNay)} $symbol'),
Text('${Fmt.token(votes.votedAye)} $symbol')
Text('${Fmt.token(votedNay)} $symbol'),
Text('${Fmt.token(votedAye)} $symbol')
],
));
}

bool votedYes = voted ?? 0 > 6;
bool votedYes = (voted ?? 0) > 6;
list.add(Container(
margin: EdgeInsets.only(top: 16),
child: Row(
Expand All @@ -160,7 +163,9 @@ class ReferendumPanel extends StatelessWidget {
text:
'${voted != null && !votedYes ? dic['voted'] : ''} ${dic['no']}',
onPressed: voted == null || votedYes
? () => onVote(data.index, false)
? () => Navigator.of(context).pushNamed(
ReferendumVotePage.route,
arguments: {'referenda': data, 'voteYes': false})
: null,
),
),
Expand All @@ -170,7 +175,9 @@ class ReferendumPanel extends StatelessWidget {
text:
'${voted != null && votedYes ? dic['voted'] : ''} ${dic['yes']}',
onPressed: voted == null || !votedYes
? () => onVote(data.index, true)
? () => Navigator.of(context).pushNamed(
ReferendumVotePage.route,
arguments: {'referenda': data, 'voteYes': true})
: null,
),
)
Expand Down
202 changes: 202 additions & 0 deletions lib/page/governance/democracy/referendumVotePage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import 'dart:convert';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:polka_wallet/common/components/roundedButton.dart';
import 'package:polka_wallet/common/regInputFormatter.dart';
import 'package:polka_wallet/page/account/txConfirmPage.dart';
import 'package:polka_wallet/store/app.dart';
import 'package:polka_wallet/store/governance.dart';
import 'package:polka_wallet/utils/UI.dart';
import 'package:polka_wallet/utils/format.dart';
import 'package:polka_wallet/utils/i18n/index.dart';

class ReferendumVotePage extends StatefulWidget {
ReferendumVotePage(this.store);
static final String route = '/gov/referenda';
final AppStore store;
@override
_ReferendumVoteState createState() => _ReferendumVoteState(store);
}

class _ReferendumVoteState extends State<ReferendumVotePage> {
_ReferendumVoteState(this.store);
final AppStore store;

final _formKey = GlobalKey<FormState>();

final TextEditingController _amountCtrl = new TextEditingController();

final List<int> _voteConvictionOptions = [0, 1, 2, 3, 4, 5, 6];

int _voteConviction = 0;

void _onSubmit(int id, bool voteYes) {
if (_formKey.currentState.validate()) {
var govDic = I18n.of(context).gov;
int decimals = store.settings.networkState.tokenDecimals;
String amt = _amountCtrl.text.trim();
Map vote = {
'balance': (double.parse(amt) * pow(10, decimals)).toInt(),
'vote': {'aye': voteYes, 'conviction': _voteConviction},
};
var args = {
"title": govDic['vote.proposal'],
"txInfo": {
"module": 'democracy',
"call": 'vote',
},
"detail": jsonEncode({
"id": id,
"balance": amt,
"vote": vote['vote'],
}),
"params": [
// "id"
id,
// "options"
{"Standard": vote},
],
'onFinish': (BuildContext txPageContext) {
Navigator.popUntil(txPageContext, ModalRoute.withName('/'));
globalDemocracyRefreshKey.currentState.show();
}
};
Navigator.of(context).pushNamed(TxConfirmPage.route, arguments: args);
}
}

List<num> _calcTimes(int value) {
double amountX = 0.1;
int timeX = 0;
if (value > 0) {
amountX = value * 1.0;
timeX = pow(2, value - 1);
}
return [amountX, timeX];
}

String _getConvictionLabel(int value) {
var dicGov = I18n.of(context).gov;
List times = _calcTimes(value);
return value == 0
? dicGov['locked.no']
: '${dicGov['locked']} ${times[1] * 8} ${dicGov['day']} (${times[1]}x)';
}

void _showConvictionSelect() {
showCupertinoModalPopup(
context: context,
builder: (_) => Container(
height: MediaQuery.of(context).copyWith().size.height / 3,
child: WillPopScope(
child: CupertinoPicker(
backgroundColor: Colors.white,
itemExtent: 58,
scrollController:
FixedExtentScrollController(initialItem: _voteConviction),
children: _voteConvictionOptions.map((i) {
return Padding(
padding: EdgeInsets.all(16),
child: Text(_getConvictionLabel(i)));
}).toList(),
onSelectedItemChanged: (v) {
setState(() {
_voteConviction = v;
});
},
),
),
),
);
}

@override
Widget build(BuildContext context) {
var govDic = I18n.of(context).gov;
return Scaffold(
appBar: AppBar(
title: Text(govDic['vote.proposal']),
centerTitle: true,
),
body: Observer(
builder: (_) {
final Map<String, String> dic = I18n.of(context).assets;
final Map<String, String> dicGov = I18n.of(context).gov;
int decimals = store.settings.networkState.tokenDecimals;

int balance = Fmt.balanceInt(store.assets.balance);

Map args = ModalRoute.of(context).settings.arguments;
ReferendumInfo info = args['referenda'];
bool voteYes = args['voteYes'];
return SafeArea(
child: Column(
children: <Widget>[
Expanded(
child: Form(
key: _formKey,
child: ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.all(16),
child: Text(
dicGov[voteYes ? 'yes.text' : 'no.text'],
style: Theme.of(context).textTheme.display4,
),
),
Padding(
padding:
EdgeInsets.only(left: 16, right: 16, bottom: 16),
child: TextFormField(
decoration: InputDecoration(
hintText: dic['amount'],
labelText:
'${dic['amount']} (${dic['balance']}: ${Fmt.token(balance)})',
),
inputFormatters: [
RegExInputFormatter.withRegex(
'^[0-9]{0,6}(\\.[0-9]{0,$decimals})?\$')
],
controller: _amountCtrl,
keyboardType:
TextInputType.numberWithOptions(decimal: true),
validator: (v) {
if (v.isEmpty) {
return dic['amount.error'];
}
if (double.parse(v.trim()) >=
balance / pow(10, decimals)) {
return dic['amount.low'];
}
return null;
},
),
),
ListTile(
title: Text(dicGov['locked']),
subtitle: Text(_getConvictionLabel(_voteConviction)),
trailing: Icon(Icons.arrow_forward_ios, size: 18),
onTap: _showConvictionSelect,
),
],
),
),
),
Container(
padding: EdgeInsets.all(16),
child: RoundedButton(
text: I18n.of(context).home['submit.tx'],
onPressed: () => _onSubmit(info.index, voteYes),
),
)
],
),
);
},
),
);
}
}
6 changes: 3 additions & 3 deletions lib/page/staking/validators/overview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ class _StakingOverviewPageState extends State<StakingOverviewPage> {
return Container();
}
String symbol = store.settings.networkState.tokenSymbol;
String address = store.account.currentAddress;

String stashAddress = store.staking.ledger['stakingLedger']['stash'];
List nominators = store.staking.ledger['nominators'];

return Container(
Expand All @@ -202,10 +201,11 @@ class _StakingOverviewPageState extends State<StakingOverviewPage> {

int meStaked;
int meIndex =
validator.nominators.indexWhere((i) => i['who'] == address);
validator.nominators.indexWhere((i) => i['who'] == stashAddress);
if (meIndex >= 0) {
meStaked = validator.nominators[meIndex]['value'];
}

Map accInfo = store.account.accountIndexMap[id];
return Expanded(
child: ListTile(
Expand Down
Loading

0 comments on commit 77223ef

Please sign in to comment.