Skip to content

Commit

Permalink
Merge pull request #14 from blockworks-foundation/max/vote-progress
Browse files Browse the repository at this point in the history
correctly calculate vote progress
  • Loading branch information
saml33 committed Aug 28, 2021
2 parents e264859 + 34b28a6 commit c808929
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
11 changes: 4 additions & 7 deletions components/ProposalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@ import { ChevronRightIcon } from '@heroicons/react/solid'
import StatusBadge from './StatusBadge'
import Link from 'next/link'
import { Proposal, ProposalState } from '../models/accounts'
import { calculatePct, fmtUnixTime } from '../utils/formatting'
import { fmtUnixTime } from '../utils/formatting'
import ApprovalProgress from './ApprovalProgress'
import useRealm from '../hooks/useRealm'
import useProposalVotes from '../hooks/useProposalVotes'

type ProposalCardProps = {
id: string
proposal: Proposal
}

const ProposalCard = ({ id, proposal }: ProposalCardProps) => {
const { symbol, mint } = useRealm()

const yesVotePct = calculatePct(proposal.yesVotesCount, mint.supply)

const yesVoteProgress =
(yesVotePct / proposal.voteThresholdPercentage?.value) * 100
const { symbol } = useRealm()
const { yesVoteProgress } = useProposalVotes(proposal)

return (
<div>
Expand Down
37 changes: 37 additions & 0 deletions hooks/useProposalVotes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Proposal } from '../models/accounts'
import { calculatePct, fmtTokenAmount } from '../utils/formatting'
import useRealm from './useRealm'

export default function useProposalVotes(proposal?: Proposal) {
const { mint, governances } = useRealm()

const governance = governances[proposal?.governance?.toBase58()]?.info

// TODO: optimize using memo
if (!proposal || !governance || !mint)
return {
voteThresholdPct: 100,
yesVotePct: 0,
yesVoteProgress: 0,
yesVoteCount: 'N/A',
noVoteCount: 'N/A',
}

const voteThresholdPct =
(proposal.isVoteFinalized() && proposal.voteThresholdPercentage?.value) ||
governance.config.voteThresholdPercentage.value

const yesVotePct = calculatePct(proposal.yesVotesCount, mint.supply)
const yesVoteProgress = (yesVotePct / voteThresholdPct) * 100

const yesVoteCount = fmtTokenAmount(proposal.yesVotesCount, mint.decimals)
const noVoteCount = fmtTokenAmount(proposal.noVotesCount, mint.decimals)

return {
voteThresholdPct,
yesVotePct,
yesVoteProgress,
yesVoteCount,
noVoteCount,
}
}
28 changes: 7 additions & 21 deletions pages/dao/[symbol]/proposal/[pk].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ import DiscussionPanel from '../../../../components/DiscussionPanel'
import VotePanel from '../../../../components/VotePanel'
import { ProposalState } from '../../../../models/accounts'

import { calculatePct, fmtTokenAmount } from '../../../../utils/formatting'
import ApprovalProgress from '../../../../components/ApprovalProgress'
import useRealm from '../../../../hooks/useRealm'
import useProposalVotes from '../../../../hooks/useProposalVotes'

const Proposal = () => {
const { symbol, mint } = useRealm()
const { symbol } = useRealm()
const { proposal, description, instructions } = useProposal()

const yesVotePct = proposal
? calculatePct(proposal.info.yesVotesCount, mint?.supply)
: null

const yesVoteProgress =
(yesVotePct / proposal?.info.voteThresholdPercentage?.value) * 100
const { yesVoteProgress, yesVoteCount, noVoteCount } = useProposalVotes(
proposal?.info
)

console.log('proposal data', { proposal, instructions })

Expand Down Expand Up @@ -88,21 +84,11 @@ const Proposal = () => {
<>
<div className="bg-bkg-1 px-4 py-2 rounded w-full">
<p className="text-fgd-3 text-xs">Approve</p>
<div className="font-bold">
{fmtTokenAmount(
proposal?.info.yesVotesCount,
mint?.decimals
)}
</div>
<div className="font-bold">{yesVoteCount}</div>
</div>
<div className="bg-bkg-1 px-4 py-2 rounded w-full">
<p className="text-fgd-3 text-xs">Deny</p>
<div className="font-bold">
{fmtTokenAmount(
proposal?.info.noVotesCount,
mint?.decimals
)}
</div>
<div className="font-bold">{noVoteCount}</div>
</div>
</>
) : (
Expand Down

0 comments on commit c808929

Please sign in to comment.