Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retrieve unity and quorum #337

Merged
merged 8 commits into from
Oct 8, 2024
Merged
5 changes: 2 additions & 3 deletions lib/core/extension/base_proposal_model_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ extension BaseProposalModelExtension on BaseProposalModel {
double unityToPercent() => unity==null?0:unity!*.01;
double commitmentToPercent() => commitment == null ? 0 : commitment! * .01;
bool isExpired() => expiration!.toLocal().isBefore(DateTime.now());
// TODO(saif): Replace hardcoded values (.2 and .8) with dynamic values fetched from the server.
// These thresholds are relative to each DAO and should be retrieved from the DAO settings.
bool isPassing() => quorumToPercent() >= .2 && unityToPercent() >= .8;

bool isPassing() => (quorum ?? 0) >= (pastQuorum ?? 0) && (unity ?? 0) >= (pastUnity ?? 0);
}
21 changes: 21 additions & 0 deletions lib/core/extension/vote_tally_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:hypha_wallet/core/extension/string_extension.dart';

extension VoteTallyExtension on Map<String, dynamic> {
Map<String, dynamic> calculateUnityAndQuorum() {
if (this['votetally'] != null && this['votetally'].isNotEmpty) {
final double abstain = (this['votetally'][0]['abstain_votePower_a'] as String).quantityAsDouble;
final double pass = (this['votetally'][0]['pass_votePower_a'] as String).quantityAsDouble;
final double fail = (this['votetally'][0]['fail_votePower_a'] as String).quantityAsDouble;

final double unity = (pass + fail > 0) ? pass / (pass + fail) : 0;

final double supply = (this['details_ballotSupply_a'] as String?)?.quantityAsDouble ?? 0;
final double quorum = supply > 0 ? (abstain + pass + fail) / supply : 0;

this['unity'] = unity * 100;
this['quorum'] = quorum * 100;
}

return this;
}
}
9 changes: 4 additions & 5 deletions lib/core/network/api/services/proposal_service.dart

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions lib/core/network/models/base_proposal_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ abstract class BaseProposalModel {
@JsonKey(name: 'details_title_s')
final String? title;

final double? unity;

final double? quorum;

@JsonKey(name: 'details_ballotAlignment_i')
final int? unity;
final int? pastUnity;

@JsonKey(name: 'details_ballotQuorum_i')
final int? quorum;
final int? pastQuorum;

@JsonKey(name: 'details_ballotSupply_a')
final int? supply;

@JsonKey(name: 'ballot_expiration_t')
final DateTime? expiration;
Expand All @@ -37,6 +44,9 @@ abstract class BaseProposalModel {
this.title,
this.unity,
this.quorum,
this.pastUnity,
this.pastQuorum,
this.supply,
this.expiration,
this.creator,
this.votes,
Expand Down
8 changes: 7 additions & 1 deletion lib/core/network/models/proposal_details_model.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:hypha_wallet/core/extension/string_extension.dart'; // Add this import
import 'package:hypha_wallet/core/extension/string_extension.dart';
import 'package:hypha_wallet/core/extension/vote_tally_extension.dart';
import 'package:hypha_wallet/core/network/models/base_proposal_model.dart';
import 'package:hypha_wallet/core/network/models/dao_data_model.dart';
import 'package:hypha_wallet/core/network/models/vote_model.dart';
Expand Down Expand Up @@ -78,6 +79,8 @@ class ProposalDetailsModel extends BaseProposalModel {
super.title,
super.unity,
super.quorum,
super.pastUnity,
super.pastQuorum,
super.expiration,
super.creator,
super.votes,
Expand Down Expand Up @@ -116,6 +119,9 @@ class ProposalDetailsModel extends BaseProposalModel {
}
json['dao'] = null;
json['creator'] = null;

json.calculateUnityAndQuorum();

return _$ProposalDetailsModelFromJson(json);
}

Expand Down
12 changes: 8 additions & 4 deletions lib/core/network/models/proposal_details_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/core/network/models/proposal_model.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hypha_wallet/core/extension/vote_tally_extension.dart';
import 'package:hypha_wallet/core/network/models/base_proposal_model.dart';
import 'package:hypha_wallet/core/network/models/dao_data_model.dart';
import 'package:hypha_wallet/core/network/models/vote_model.dart';
Expand All @@ -15,6 +16,8 @@ class ProposalModel extends BaseProposalModel{
super.title,
super.unity,
super.quorum,
super.pastUnity,
super.pastQuorum,
super.expiration,
super.creator,
super.votes,
Expand All @@ -24,6 +27,9 @@ class ProposalModel extends BaseProposalModel{
if (json.containsKey('original')) {
json['details_title_s'] = json['original'][0]['details_title_s'];
}

json.calculateUnityAndQuorum();

return _$ProposalModelFromJson(json);
}
}
12 changes: 8 additions & 4 deletions lib/core/network/models/proposal_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion lib/ui/proposals/filter/components/hypha_filter_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ class HyphaFilterCard extends StatelessWidget {
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
valueNotifier.value = valueNotifier.value == index ? null : index;
if (valueNotifier.value != index) {
valueNotifier.value = index;
} else if (subTitle != null) {
valueNotifier.value = null;
}
},
child: HyphaCard(
child: Container(
Expand Down
Loading