-
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.
feat: adds votes percentage projection toggle
- Loading branch information
1 parent
d7e52e9
commit 7b5ae85
Showing
5 changed files
with
72 additions
and
6 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
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
44 changes: 44 additions & 0 deletions
44
packages/shared/src/lib/contexts/governance/utils/getProjectedPercentages.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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { get } from 'svelte/store' | ||
import { IProposal, IProposalAnswerPercentages, selectedProposal } from '..' | ||
import { AnswerStatus } from '@iota/sdk' | ||
import { networkStatus } from '@core/network/stores/network-status.store' | ||
import { round } from '@core/utils/number' | ||
|
||
export function getProjectedPercentages( | ||
answerStatuses: AnswerStatus[], | ||
proposal: IProposal = get(selectedProposal) | ||
): IProposalAnswerPercentages { | ||
if (!proposal) { | ||
return {} | ||
} | ||
|
||
const answerStatusesWithProjection = answerStatuses.map((answerStatus) => { | ||
return { ...answerStatus, projected: getProjectedVotesFromAnswerStatus(answerStatus, proposal) } | ||
}) | ||
|
||
const totalVotes = answerStatusesWithProjection?.reduce((acc, answerStatus) => acc + answerStatus.projected, 0) ?? 0 | ||
if (totalVotes === 0 || Number.isNaN(totalVotes)) { | ||
return {} | ||
} | ||
|
||
let percentages: IProposalAnswerPercentages = {} | ||
answerStatusesWithProjection.forEach((answerStatus) => { | ||
if (answerStatus.value !== undefined) { | ||
const divisionResult = (answerStatus.projected ?? 0) / totalVotes | ||
percentages = { | ||
...percentages, | ||
[answerStatus.value]: Number.isNaN(divisionResult) ? '0%' : `${round(divisionResult * 100, 1)}%`, | ||
} | ||
} | ||
}) | ||
|
||
return percentages | ||
} | ||
|
||
function getProjectedVotesFromAnswerStatus(answerStatus: AnswerStatus, proposal: IProposal): number { | ||
const { accumulated, current } = answerStatus | ||
const endingMilestone = proposal.milestones?.ended ?? 0 | ||
const currentMilestone = get(networkStatus)?.currentMilestone ?? 0 | ||
|
||
return Math.max(accumulated, accumulated + current * (endingMilestone - currentMilestone)) | ||
} |
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
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