Skip to content

Commit

Permalink
Merge pull request #849 from threefoldtech/development_fix_dao_results
Browse files Browse the repository at this point in the history
Fix the calculations of the dao results
  • Loading branch information
AhmedHanafy725 authored Jan 16, 2025
2 parents b57734c + 5d7da4f commit 908f1c5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
5 changes: 4 additions & 1 deletion app/lib/screens/dao_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ class _DaoPageState extends State<DaoPage> with SingleTickerProviderStateMixin {
children: [
RefreshIndicator(
onRefresh: loadProposals,
child: ProposalsWidget(proposals: activeList)),
child: ProposalsWidget(
proposals: activeList,
active: true,
)),
RefreshIndicator(
onRefresh: loadProposals,
child: ProposalsWidget(
Expand Down
16 changes: 16 additions & 0 deletions app/lib/services/tfchain_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:threebotlogin/services/shared_preference_service.dart';
import 'package:tfchain_client/tfchain_client.dart' as TFChain;
import 'package:tfchain_client/models/dao.dart';
import 'package:tfchain_client/generated/dev/types/pallet_dao/proposal/dao_votes.dart';
import 'package:tfchain_client/generated/dev/types/pallet_dao/proposal/vote_weight.dart';
import 'package:crypto/crypto.dart';
import 'package:http/http.dart' as http;
import 'package:hashlib/hashlib.dart' as hashlib;
Expand Down Expand Up @@ -107,6 +108,21 @@ Future<DaoVotes> getProposalVotes(String hash) async {
}
}

Future<int> getProposalProgress(
List<VoteWeight> ayes, List<VoteWeight> nayes, bool typeAye) async {
final chainUrl = Globals().chainUrl;
final client = TFChain.QueryClient(chainUrl);
try {
await client.connect();
final progress = client.dao.getProgress(ayes, nayes, typeAye);
return progress;
} catch (e) {
throw Exception('Failed to get dao proposals progress due to $e');
} finally {
await client.disconnect();
}
}

Future<DaoVotes> vote(bool vote, String hash, int farmId, String seed) async {
final chainUrl = Globals().chainUrl;
final client = TFChain.Client(chainUrl, seed, 'sr25519');
Expand Down
31 changes: 16 additions & 15 deletions app/lib/widgets/dao/show_result_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class _ShowResultDialogState extends State<ShowResultDialog>

bool loading = true;
int totalVotes = 0;
int noVotes = 0;
int yesVotes = 0;
int noVotesPercentage = 0;
int yesVotesPercentage = 0;
int threshold = 1;
void getVotes() async {
setState(() {
Expand All @@ -41,13 +41,20 @@ class _ShowResultDialogState extends State<ShowResultDialog>
late dynamic votes;
if (widget.type == ProposalType.DAO) {
votes = await getProposalVotes(widget.proposalHash);
totalVotes = votes.ayes.length + votes.nays.length;
noVotesPercentage =
await getProposalProgress(votes.ayes, votes.nays, false);
yesVotesPercentage =
await getProposalProgress(votes.ayes, votes.nays, true);
} else {
votes =
await getCouncilProposalVotes(widget.chainUrl, widget.proposalHash);
totalVotes = votes.ayes.length + votes.nays.length;
final noVotes = votes.nays.length;
final yesVotes = votes.ayes.length;
noVotesPercentage = (noVotes / totalVotes * 100).round();
yesVotesPercentage = (yesVotes / totalVotes * 100).round();
}
totalVotes = votes.ayes.length + votes.nays.length;
noVotes = votes.nays.length;
yesVotes = votes.ayes.length;
threshold = votes.threshold;
setState(() {
loading = false;
Expand All @@ -59,12 +66,12 @@ class _ShowResultDialogState extends State<ShowResultDialog>

_noAnimation = Tween<double>(
begin: 0.0,
end: totalVotes != 0 ? (noVotes / totalVotes * 1.0) : 0,
end: totalVotes != 0 ? (noVotesPercentage / 100 * 1.0) : 0,
).animate(_animationController);

_yesAnimation = Tween<double>(
begin: 0.0,
end: totalVotes != 0 ? (yesVotes / totalVotes * 1.0) : 0,
end: totalVotes != 0 ? (yesVotesPercentage / 100 * 1.0) : 0,
).animate(_animationController);

_animation = Tween<double>(
Expand Down Expand Up @@ -145,10 +152,7 @@ class _ShowResultDialogState extends State<ShowResultDialog>
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
)),
Text(
totalVotes == 0
? '0%'
: '${((yesVotes / totalVotes) * 100).toStringAsFixed(0)}%',
Text(totalVotes == 0 ? '0%' : '$yesVotesPercentage%',
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
)),
Expand All @@ -172,10 +176,7 @@ class _ShowResultDialogState extends State<ShowResultDialog>
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
)),
Text(
totalVotes == 0
? '0%'
: '${(noVotes / totalVotes * 100).toStringAsFixed(0)}%',
Text(totalVotes == 0 ? '0%' : '$noVotesPercentage%',
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
)),
Expand Down

0 comments on commit 908f1c5

Please sign in to comment.