From 9f0399fb6fd2c00695d1039ecf74d80e74b4177a Mon Sep 17 00:00:00 2001 From: Nicole O'Brien Date: Fri, 8 Mar 2024 11:24:17 +0000 Subject: [PATCH] enhancement: improve quorum calculation for votes in progress --- .../proposal-details/QuorumProgress.svelte | 30 +++++++++++-------- .../getCirculatingSupplyVotedPercentage.ts | 23 ++++++++------ 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/packages/desktop/views/dashboard/governance/components/proposal-details/QuorumProgress.svelte b/packages/desktop/views/dashboard/governance/components/proposal-details/QuorumProgress.svelte index d9b2d0ae14..fa35223285 100644 --- a/packages/desktop/views/dashboard/governance/components/proposal-details/QuorumProgress.svelte +++ b/packages/desktop/views/dashboard/governance/components/proposal-details/QuorumProgress.svelte @@ -5,16 +5,24 @@ selectedParticipationEventStatus, } from '@contexts/governance' import { Text, Progress, TooltipIcon } from '@bloomwalletio/ui' - import { localize } from '@core/i18n' + import { getDecimalSeparator, localize } from '@core/i18n' + import { networkStatus } from '@core/network' + import { getSignificantDigitsAndRound } from '@core/utils' export let proposal: IProposal const QUORUM_PERCENTAGE_DECIMAL = 0.05 - $: circulatingSupplyVotedPercentage = getCirculatingSupplyVotedPercentage( - $selectedParticipationEventStatus, - proposal - ) + const currentMilestone = $networkStatus.currentMilestone + + $: circulatingSupplyVotedPercentage = + getCirculatingSupplyVotedPercentage($selectedParticipationEventStatus, proposal, currentMilestone) ?? 0 + + function formatPercentage(percentage: number): string { + const percentageWithSignificantDigits = getSignificantDigitsAndRound(Number(percentage)) + const percentageString = String(percentageWithSignificantDigits).replace(/[,.]/g, getDecimalSeparator()) + '%' + return percentageString + }
@@ -24,14 +32,10 @@
- {circulatingSupplyVotedPercentage} / {QUORUM_PERCENTAGE_DECIMAL * 100}% + + {formatPercentage(circulatingSupplyVotedPercentage)} / {QUORUM_PERCENTAGE_DECIMAL * 100}% +
- + diff --git a/packages/shared/src/lib/contexts/governance/utils/getCirculatingSupplyVotedPercentage.ts b/packages/shared/src/lib/contexts/governance/utils/getCirculatingSupplyVotedPercentage.ts index a3a9a13088..c2aeebf4ed 100644 --- a/packages/shared/src/lib/contexts/governance/utils/getCirculatingSupplyVotedPercentage.ts +++ b/packages/shared/src/lib/contexts/governance/utils/getCirculatingSupplyVotedPercentage.ts @@ -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 }