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

feat: Implement the calculation of tokens amounts #331

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 44 additions & 14 deletions lib/core/extension/proposal_details_model_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import 'package:hypha_wallet/core/network/models/proposal_details_model.dart';
import 'package:intl/intl.dart';

extension ProposalDetailsModelExtension on ProposalDetailsModel {
double tokenMixToPercent() => tokenMixPercentage == null ? 0 : tokenMixPercentage! * .01;
double tokenMixToPercent() =>
tokenMixPercentage == null ? 0 : tokenMixPercentage! * .01;

String formatCycleStartDate() => cycleStartDate != null ? DateFormat('EEEE, MMMM yyyy').format(cycleStartDate!) : '';
String formatCycleStartDate() => cycleStartDate != null
? DateFormat('EEEE, MMMM yyyy').format(cycleStartDate!)
: '';

String cycleEndDate() => DateFormat('EEEE, MMMM yyyy').format(cycleStartDate!.add(Duration(days: cycleCount! * 7)));
String cycleEndDate() => DateFormat('EEEE, MMMM yyyy')
.format(cycleStartDate!.add(Duration(days: cycleCount! * 7)));

String? tokenTitle(int index) {
String input;
Expand Down Expand Up @@ -39,27 +43,53 @@ extension ProposalDetailsModelExtension on ProposalDetailsModel {
return null;
}

// TODO(Saif): adjust this function
String? tokenValue(int index, bool isOneCycleRewardsShown) {
String input;
nbetsaif marked this conversation as resolved.
Show resolved Hide resolved

double? tokenValue(int index, bool isOneCycleRewardsShown) {
double? tokenAmount;
double? tokenAmountPerPeriod;
final int cycleDurationSec = 2629800;
final double periodsOnCycle = cycleDurationSec / periodDurationSec!;
switch (index) {
case 0:
input = utilityAmount ?? utilityAmountPerPeriod!;
tokenAmount = utilityAmountDouble;
tokenAmountPerPeriod = utilityAmountPerPeriodDouble;
break;
case 1:
input = voiceAmount ?? voiceAmountPerPeriod!;
tokenAmount = voiceAmountDouble;
tokenAmountPerPeriod = voiceAmountPerPeriodDouble;
break;
case 2:
input = cashAmount ?? cashAmountPerPeriod!;
tokenAmount = cashAmountDouble;
tokenAmountPerPeriod = cashAmountPerPeriodDouble;

break;
default:
return null;
}
if (tokenAmount != null) {
if (isOneCycleRewardsShown) return tokenAmount;
return tokenAmount / periodsOnCycle;
}
if (tokenAmountPerPeriod != null) {
if (isOneCycleRewardsShown) {
return tokenAmountPerPeriod * periodsOnCycle;
}
return tokenAmountPerPeriod;
}
return tokenAmount;
}
}

final RegExp regExp = RegExp(r'(\S+)\s');
final match = regExp.firstMatch(input);

return match?.group(1);
extension TokenTypeExtension on TokenType {
String get name {
switch (this) {
case TokenType.utility:
return 'Utility Token';
case TokenType.voice:
return 'Voice Token';
case TokenType.cash:
return 'Cash Token';
default:
return '';
}
}
}
3 changes: 2 additions & 1 deletion lib/core/network/api/services/proposal_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class ProposalService {
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}} 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}} 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}} 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}} 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}} 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}} details_ballotAlignment_i details_ballotQuorum_i} ... on Poll {details_title_s details_description_s ballot_expiration_t dao {settings {settings_daoTitle_s}} 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}} 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}} 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 {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"}}';
return _graphQLService.graphQLQuery(network: user.network, query: query);
}


}
53 changes: 41 additions & 12 deletions lib/core/network/models/proposal_details_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,35 @@ import 'package:json_annotation/json_annotation.dart';

part 'proposal_details_model.g.dart';

enum TokenType {
utility,
voice,
cash,
}

@JsonSerializable()
class ProposalDetailsModel extends BaseProposalModel {
double? get utilityAmountDouble => utilityAmount?.quantityAsDouble;

double? get voiceAmountDouble => voiceAmount?.quantityAsDouble;

double? get cashAmountDouble => cashAmount?.quantityAsDouble;
double? get utilityAmountPerPeriodDouble => utilityAmountPerPeriod?.quantityAsDouble;
double? get voiceAmountPerPeriodDouble => voiceAmountPerPeriod?.quantityAsDouble;
double? get cashAmountPerPeriodDouble => cashAmountPerPeriod?.quantityAsDouble;

double? get utilityAmountPerPeriodDouble =>
utilityAmountPerPeriod?.quantityAsDouble;

double? get voiceAmountPerPeriodDouble =>
voiceAmountPerPeriod?.quantityAsDouble;

double? get cashAmountPerPeriodDouble =>
cashAmountPerPeriod?.quantityAsDouble;

@JsonKey(name: '__typename')
final String type;

@JsonKey(name: 'settings_periodDurationSec_i')
final int? periodDurationSec;

@JsonKey(name: 'createdDate')
final DateTime creationDate;

Expand Down Expand Up @@ -73,27 +90,39 @@ class ProposalDetailsModel extends BaseProposalModel {
this.utilityAmountPerPeriod,
this.voiceAmountPerPeriod,
this.cashAmountPerPeriod,
this.description});
this.description,
this.periodDurationSec});

factory ProposalDetailsModel.fromJson(Map<String, dynamic> json) {
// Handle the 'start' field if it's a list
if (json['start'] is List) {
if ((json['start'] as List).isNotEmpty) {
json['start'] = json['start'][0]['details_startTime_t'];
} else {
json['start'] = null;
}
}
// TODO(Saif): check this
if (json['dao'] is List) {
final daoList = json['dao'];
if (daoList.isNotEmpty &&
daoList[0] is Map &&
daoList[0]['settings'] is List) {
final settingsList = daoList[0]['settings'] as List;
if (settingsList.isNotEmpty && settingsList[0] is Map) {
json['settings_periodDurationSec_i'] =
settingsList[0]['settings_periodDurationSec_i'];
}
}
}
json['dao'] = null;
json['creator'] = null;
/*if(json['dao'] != null) {
json['dao'] = json['dao'][0]['settings'][0]['settings_daoTitle_s'];
}*/
return _$ProposalDetailsModelFromJson(json);
}

List<VoteModel> fetchVotersByStatus(VoteStatus voteStatus) => votes
?.where((vote) => vote.voteStatus == voteStatus)
.map((vote) => vote)
.toList() ?? [];
List<VoteModel> fetchVotersByStatus(VoteStatus voteStatus) =>
votes
?.where((vote) => vote.voteStatus == voteStatus)
.map((vote) => vote)
.toList() ??
[];
}
3 changes: 3 additions & 0 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.

Loading
Loading