Skip to content

Commit

Permalink
enhancement: improve quorum calculation for votes in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
nicole-obrien committed Mar 8, 2024
1 parent 08dfd92 commit 9f0399f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
</script>

<div class="flex flex-col gap-1">
Expand All @@ -24,14 +32,10 @@
<TooltipIcon tooltip={localize('views.governance.details.quorum.tooltip')} />
</div>
<div class="flex gap-1">
<Text align="center" fontWeight="medium" textColor="brand"
>{circulatingSupplyVotedPercentage} / {QUORUM_PERCENTAGE_DECIMAL * 100}%</Text
>
<Text align="center" fontWeight="medium" textColor="brand">
{formatPercentage(circulatingSupplyVotedPercentage)} / {QUORUM_PERCENTAGE_DECIMAL * 100}%
</Text>
</div>
</div>
<Progress
size="sm"
progress={Number(circulatingSupplyVotedPercentage.replace('%', '').replace(',', '.')) /
QUORUM_PERCENTAGE_DECIMAL}
/>
<Progress size="sm" progress={circulatingSupplyVotedPercentage / QUORUM_PERCENTAGE_DECIMAL} />
</div>
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
}

0 comments on commit 9f0399f

Please sign in to comment.