-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhancement: improve quorum calculation for votes in progress
- Loading branch information
1 parent
08dfd92
commit 9f0399f
Showing
2 changed files
with
31 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 14 additions & 9 deletions
23
packages/shared/src/lib/contexts/governance/utils/getCirculatingSupplyVotedPercentage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,36 @@ | ||
import { IProfile } from '@core/profile/interfaces' | ||
import { activeProfile } from '@core/profile/stores' | ||
import { get } from 'svelte/store' | ||
import { calculatePercentageOfBigInt } from '@core/utils/number' | ||
import { ParticipationEventStatus } from '@iota/sdk' | ||
import { get } from 'svelte/store' | ||
import { IProposal } from '../interfaces' | ||
import { calculatePercentageOfBigInt, getSignificantDigitsAndRound } from '@core/utils/number' | ||
import { getDecimalSeparator } from '@core/i18n' | ||
|
||
export function getCirculatingSupplyVotedPercentage( | ||
participationEventStatus: ParticipationEventStatus, | ||
proposal: IProposal, | ||
currentMilestone: number, | ||
profile: IProfile = get(activeProfile) | ||
): string { | ||
): number { | ||
const circulatingSupply = profile.network.protocol?.circulatingSupply | ||
if (!circulatingSupply || !participationEventStatus?.questions || !proposal?.milestones) { | ||
return '0%' | ||
return 0 | ||
} | ||
|
||
let milestoneCount: number | ||
if (currentMilestone < proposal.milestones.holding) { | ||
milestoneCount = 1 | ||
} else if (currentMilestone > proposal.milestones.ended) { | ||
milestoneCount = proposal.milestones.ended - proposal.milestones.holding | ||
} else { | ||
milestoneCount = currentMilestone - proposal.milestones.holding | ||
} | ||
const totalEventVotes = participationEventStatus.questions[0].answers.reduce( | ||
(total, answer) => (total += BigInt(answer.accumulated)), | ||
BigInt(0) | ||
) | ||
const milestoneCount = proposal.milestones.ended - proposal.milestones.holding | ||
const maximumVotes = BigInt(circulatingSupply) * BigInt(milestoneCount) | ||
|
||
const percentage = calculatePercentageOfBigInt(totalEventVotes, maximumVotes, 6) | ||
const percentageWithSignificantDigits = getSignificantDigitsAndRound(Number(percentage)) | ||
const percentageString = String(percentageWithSignificantDigits).replace(/[,.]/g, getDecimalSeparator()) + '%' | ||
|
||
return percentageString | ||
return percentage | ||
} |