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);
}
30 changes: 30 additions & 0 deletions lib/core/extension/vote_tally_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extension VoteTallyExtension on Map<String, dynamic> {
Map<String, dynamic> calculateUnityAndQuorum() {
if (this['votetally'] != null && this['votetally'].isNotEmpty) {
double supply = 0;
final double abstain = _extractVotePower(this['votetally'][0]['abstain_votePower_a'] ?? '0.00');
final double pass = _extractVotePower(this['votetally'][0]['pass_votePower_a'] ?? '0.00');
final double fail = _extractVotePower(this['votetally'][0]['fail_votePower_a'] ?? '0.00');
final double unity = (pass + fail > 0) ? pass / (pass + fail) : 0;

if (this['details_ballotSupply_a'] != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more...

double supply = this['details_ballotSupply_a']?.quantityAsDouble() ?? 0;

final List<String> supplyParts = this['details_ballotSupply_a'].split(' ');
if (supplyParts.isNotEmpty) {
supply = double.parse(supplyParts[0]);
}
}

final double quorum = supply > 0 ? (abstain + pass + fail) / supply : 0;

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

return this;
}

double _extractVotePower(String votePower) {
n13 marked this conversation as resolved.
Show resolved Hide resolved
final List<String> parts = votePower.split(' ');
return parts.isNotEmpty ? double.parse(parts[0]) : 0.0;
}
}
7 changes: 3 additions & 4 deletions lib/core/network/api/services/proposal_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ class ProposalService {
const ProposalService(this._graphQLService);

Future<Result<Map<String, dynamic>, HyphaError>> getActiveProposals(UserProfileData user, int daoId) async {
final String query = '{"query":"query ActiveProposals(\$docId: String!) { queryDao(filter: { docId: { eq: \$docId } }) { proposal { docId ... on Poll { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Budget { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Queststart { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Questcomplet { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Policy { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Circle { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Payout { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Assignment { details_timeShareX100_i details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Assignbadge { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Role { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Badge { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Suspend { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Edit { details_timeShareX100_i original { ... on Assignbadge { details_title_s } ... on Assignment { details_title_s } } details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Votable { vote { ... on Vote { vote_voter_n vote_vote_s } } } } } }","variables":{"docId":"$daoId"}}';
final String query = '{"query":"query ActiveProposals(\$docId: String!) { queryDao(filter: { docId: { eq: \$docId } }) { proposal { docId ... on Poll { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Budget { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Queststart { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Questcomplet { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Policy { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Circle { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Payout { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Assignment { details_timeShareX100_i details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Assignbadge { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Role { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Badge { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Suspend { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Edit { details_timeShareX100_i original { ... on Assignbadge { details_title_s } ... on Assignment { details_title_s } } details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Votable { vote { ... on Vote { vote_voter_n vote_vote_s } } votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } } } }","variables":{"docId":"$daoId"}}';

return _graphQLService.graphQLQuery(network: user.network, query: query);
}

Future<Result<Map<String, dynamic>, HyphaError>> getPastProposals(UserProfileData user, int daoId) async {
final String query = '{"query":"query PastProposals(\$docId: String!) { queryDao(filter: { docId: { eq: \$docId } }) { votable { docId ... on Poll { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Budget { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Queststart { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Questcomplet { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Policy { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Circle { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Payout { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Assignment { details_timeShareX100_i details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Assignbadge { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Role { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Badge { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Suspend { details_title_s details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Edit { details_timeShareX100_i original { ... on Assignbadge { details_title_s } ... on Assignment { details_title_s } } details_ballotAlignment_i details_ballotQuorum_i ballot_expiration_t creator } ... on Votable { vote { ... on Vote { vote_voter_n vote_vote_s } } } } } }","variables":{"docId":"$daoId"}}';
final String query = '{"query":"query PastProposals(\$docId: String!) { queryDao(filter: { docId: { eq: \$docId } }) { votable { docId ... on Poll { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Budget { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Queststart { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Questcomplet { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Policy { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Circle { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Payout { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Assignment { details_timeShareX100_i details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Assignbadge { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Role { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Badge { details_title_s details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } } ... on Edit { details_timeShareX100_i votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } original { ... on Assignbadge { details_title_s } ... on Assignment { details_title_s } } details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i ballot_expiration_t creator } ... on Votable { vote { ... on Vote { vote_voter_n vote_vote_s } } } } } }","variables":{"docId":"$daoId"}}';

return _graphQLService.graphQLQuery(network: user.network, query: query);
}

Future<Result<Map<String, dynamic>, HyphaError>> getProposalDetails(
String proposalId, UserProfileData user) async {
final String query =
'{"query":"query proposalDetails(\$docId: String!) { getDocument(docId: \$docId) {__typename docId creator createdDate ... on Votable {pass: voteAggregate(filter: { vote_vote_s: { regexp: \\"/.*pass*./\\" } }) {count} fail: voteAggregate(filter: { vote_vote_s: { regexp: \\"/.*fail*./\\" } }) {count} vote { ... on Vote { vote_voter_n vote_vote_s } } } ... on Edit {details_title_s details_description_s details_periodCount_i details_deferredPercX100_i ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotQuorum_i} ... on Queststart {details_title_s details_description_s details_periodCount_i details_deferredPercX100_i start {details_startTime_t} ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotQuorum_i details_pegAmount_a details_voiceAmount_a details_rewardAmount_a} ... on Questcomple {details_title_s details_description_s ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_pegAmount_a details_voiceAmount_a details_rewardAmount_a} ... on Policy {details_title_s details_description_s details_periodCount_i details_deferredPercX100_i ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotQuorum_i details_pegAmount_a details_voiceAmount_a details_rewardAmount_a} ... on Payout {details_title_s details_description_s details_periodCount_i details_deferredPercX100_i start {details_startTime_t} ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotQuorum_i details_pegAmount_a details_voiceAmount_a details_rewardAmount_a} ... on Badge {details_title_s details_description_s details_periodCount_i ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotQuorum_i} ... on Poll {details_title_s details_description_s ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotQuorum_i} ... on Budget {details_title_s details_description_s details_deferredPercX100_i ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotQuorum_i details_pegAmount_a details_voiceAmount_a details_rewardAmount_a} ... on Assignment {details_title_s details_description_s details_periodCount_i details_deferredPercX100_i start {details_startTime_t} details_rewardSalaryPerPeriod_a details_voiceSalaryPerPeriod_a details_pegSalaryPerPeriod_a ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_timeShareX100_i details_ballotAlignment_i details_ballotQuorum_i} } }", "variables":{"docId":"$proposalId"}}';
'{"query":"query proposalDetails(\$docId: String!) { getDocument(docId: \$docId) {__typename docId creator createdDate ... on Votable { votetally { ... on VoteTally { pass_votePower_a fail_votePower_a abstain_votePower_a } } pass: voteAggregate(filter: { vote_vote_s: { regexp: \\"/.*pass*./\\" } }) {count} fail: voteAggregate(filter: { vote_vote_s: { regexp: \\"/.*fail*./\\" } }) {count} vote { ... on Vote { vote_voter_n vote_vote_s } } } ... on Edit {details_title_s details_description_s details_periodCount_i details_deferredPercX100_i ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotQuorum_i details_ballotSupply_a } ... on Queststart {details_title_s details_description_s details_periodCount_i details_deferredPercX100_i start {details_startTime_t} ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i details_pegAmount_a details_voiceAmount_a details_rewardAmount_a} ... on Questcomple {details_title_s details_description_s ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_pegAmount_a details_voiceAmount_a details_rewardAmount_a} ... on Policy {details_title_s details_description_s details_periodCount_i details_deferredPercX100_i ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i details_pegAmount_a details_voiceAmount_a details_rewardAmount_a} ... on Payout {details_title_s details_description_s details_periodCount_i details_deferredPercX100_i start {details_startTime_t} ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i details_pegAmount_a details_voiceAmount_a details_rewardAmount_a} ... on Badge {details_title_s details_description_s details_periodCount_i ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i} ... on Poll {details_title_s details_description_s ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i} ... on Budget {details_title_s details_description_s details_deferredPercX100_i ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i details_pegAmount_a details_voiceAmount_a details_rewardAmount_a} ... on Assignment {details_title_s details_description_s details_periodCount_i details_deferredPercX100_i start {details_startTime_t} details_rewardSalaryPerPeriod_a details_voiceSalaryPerPeriod_a details_pegSalaryPerPeriod_a ballot_expiration_t dao {settings {settings_daoTitle_s settings_periodDurationSec_i}} details_timeShareX100_i details_ballotAlignment_i details_ballotSupply_a details_ballotQuorum_i} } }", "variables":{"docId":"$proposalId"}}';
return _graphQLService.graphQLQuery(network: user.network, query: query);
}

Expand Down
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
Loading
Loading