Skip to content

Commit

Permalink
refactor: Change the logic of token amount calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
nbetsaif committed Sep 28, 2024
1 parent ca0d395 commit e2863a7
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 323 deletions.
57 changes: 34 additions & 23 deletions lib/core/extension/proposal_details_model_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,42 +43,53 @@ extension ProposalDetailsModelExtension on ProposalDetailsModel {
return null;
}

String? tokenValue(int index, bool isOneCycleRewardsShown) {
String? input;
String? perPeriodInput;

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;
perPeriodInput = utilityAmountPerPeriod;
tokenAmount = utilityAmountDouble;
tokenAmountPerPeriod = utilityAmountPerPeriodDouble;
break;
case 1:
input = voiceAmount;
perPeriodInput = voiceAmountPerPeriod;
tokenAmount = voiceAmountDouble;
tokenAmountPerPeriod = voiceAmountPerPeriodDouble;
break;
case 2:
input = cashAmount;
perPeriodInput = cashAmountPerPeriod;
tokenAmount = cashAmountDouble;
tokenAmountPerPeriod = cashAmountPerPeriodDouble;

break;
default:
return null;
}
final RegExp regExp = RegExp(r'(\d+(\.\d+)?)');
if (input != null) {
final match = regExp.firstMatch(input);
if (isOneCycleRewardsShown || cycleCount == 1) return match?.group(1);
return (double.parse((match?.group(1))!) * cycleCount!)
.toStringAsFixed(3);
if (tokenAmount != null) {
if (isOneCycleRewardsShown) return tokenAmount;
return tokenAmount / periodsOnCycle;
}
if (perPeriodInput != null) {
final match = regExp.firstMatch(perPeriodInput);
if (isOneCycleRewardsShown && cycleCount != 1) {
return (double.parse((match?.group(1))!) / cycleCount!)
.toStringAsFixed(3);
if (tokenAmountPerPeriod != null) {
if (isOneCycleRewardsShown) {
return tokenAmountPerPeriod * periodsOnCycle;
}
return match?.group(1);
return tokenAmountPerPeriod;
}
return tokenAmount;
}
}

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 '';
}
return input;
}
}
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);
}


}
57 changes: 45 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,43 @@ 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() ??
[];

void printParentDao() {
print(super.dao); // Access the dao from the parent class
}
}
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

0 comments on commit e2863a7

Please sign in to comment.